Skip to content

Commit 6b5d531

Browse files
committedJan 18, 2015
Split tasks into stages, with three (four) default stages: EarlyTask, Task and LateTask.
Scanning posts is done in additional stage between EarlyTask and Task.
1 parent 2b34513 commit 6b5d531

12 files changed

+119
-24
lines changed
 

‎nikola/__main__.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,24 @@ def __init__(self, nikola, quiet=False):
237237
self.nikola = nikola
238238
self.quiet = quiet
239239

240+
def _get_stage_info(self, level):
241+
if level == -1:
242+
name = 'pre_scanning'
243+
doc = 'Group of tasks to be run before scanning for posts'
244+
elif level == 0:
245+
name = 'scanning'
246+
doc = 'Task scanning for posts'
247+
elif level == 10:
248+
name = 'render_site'
249+
doc = 'Group of tasks to render the site.'
250+
elif level == 100:
251+
name = 'post_render'
252+
doc = 'Group of tasks to be executed after site is rendered.'
253+
else:
254+
name = 'level_{0}_wait'.format(level)
255+
doc = 'Level {0} tasks'.format(level)
256+
return name, doc
257+
240258
def load_tasks(self, cmd, opt_values, pos_args):
241259
if self.quiet:
242260
DOIT_CONFIG = {
@@ -248,15 +266,16 @@ def load_tasks(self, cmd, opt_values, pos_args):
248266
'reporter': ExecutedOnlyReporter,
249267
'outfile': sys.stderr,
250268
}
251-
DOIT_CONFIG['default_tasks'] = ['render_site', 'post_render']
252-
tasks = generate_tasks(
253-
'render_site',
254-
self.nikola.gen_tasks('render_site', "Task", 'Group of tasks to render the site.'))
255-
latetasks = generate_tasks(
256-
'post_render',
257-
self.nikola.gen_tasks('post_render', "LateTask", 'Group of tasks to be executed after site is rendered.'))
269+
DOIT_CONFIG['default_tasks'] = []
270+
tasks = []
271+
for level in self.nikola.get_task_stages():
272+
name, doc = self._get_stage_info(level)
273+
DOIT_CONFIG['default_tasks'].append(name)
274+
tasks.extend(generate_tasks(
275+
name,
276+
self.nikola.gen_tasks(name, level, doc)))
258277
signal('initialized').send(self.nikola)
259-
return tasks + latetasks, DOIT_CONFIG
278+
return tasks, DOIT_CONFIG
260279

261280

262281
class DoitNikola(DoitMain):

‎nikola/nikola.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
from .post import Post
7272
from . import utils
7373
from .plugin_categories import (
74+
BaseTask,
7475
Command,
76+
EarlyTask,
7577
LateTask,
7678
PageCompiler,
7779
RestExtension,
@@ -614,8 +616,7 @@ def __init__(self, **config):
614616

615617
self.plugin_manager = PluginManager(categories_filter={
616618
"Command": Command,
617-
"Task": Task,
618-
"LateTask": LateTask,
619+
"BaseTask": BaseTask,
619620
"TemplateSystem": TemplateSystem,
620621
"PageCompiler": PageCompiler,
621622
"TaskMultiplier": TaskMultiplier,
@@ -654,8 +655,14 @@ def __init__(self, **config):
654655
plugin_info.plugin_object.short_help = plugin_info.description
655656
self._commands[plugin_info.name] = plugin_info.plugin_object
656657

657-
self._activate_plugins_of_category("Task")
658-
self._activate_plugins_of_category("LateTask")
658+
task_plugins = self._activate_plugins_of_category("BaseTask")
659+
self.task_stages = defaultdict(list)
660+
for plugin_info in task_plugins:
661+
if type(plugin_info.plugin_object) == EarlyTask: continue
662+
if type(plugin_info.plugin_object) == Task: continue
663+
if type(plugin_info.plugin_object) == LateTask: continue
664+
stage = plugin_info.plugin_object.get_stage()
665+
self.task_stages[stage].append(plugin_info.plugin_object)
659666
self._activate_plugins_of_category("TaskMultiplier")
660667

661668
compilers = defaultdict(set)
@@ -1239,7 +1246,10 @@ def clean_task_paths(self, task):
12391246
task['targets'] = [os.path.normpath(t) for t in targets]
12401247
return task
12411248

1242-
def gen_tasks(self, name, plugin_category, doc=''):
1249+
def get_task_stages(self):
1250+
return sorted(list(self.task_stages.keys()))
1251+
1252+
def gen_tasks(self, name, stage, doc=''):
12431253

12441254
def flatten(task):
12451255
if isinstance(task, dict):
@@ -1250,8 +1260,8 @@ def flatten(task):
12501260
yield ft
12511261

12521262
task_dep = []
1253-
for pluginInfo in self.plugin_manager.getPluginsOfCategory(plugin_category):
1254-
for task in flatten(pluginInfo.plugin_object.gen_tasks()):
1263+
for plugin_object in self.task_stages[stage]:
1264+
for task in flatten(plugin_object.gen_tasks()):
12551265
assert 'basename' in task
12561266
task = self.clean_task_paths(task)
12571267
yield task
@@ -1262,8 +1272,8 @@ def flatten(task):
12621272
yield self.clean_task_paths(task)
12631273
if flag:
12641274
task_dep.append('{0}_{1}'.format(name, multi.plugin_object.name))
1265-
if pluginInfo.plugin_object.is_default:
1266-
task_dep.append(pluginInfo.plugin_object.name)
1275+
if plugin_object.is_default:
1276+
task_dep.append(plugin_object.name)
12671277
yield {
12681278
'basename': name,
12691279
'doc': doc,

‎nikola/plugin_categories.py

+19
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ class BaseTask(BasePlugin):
141141
# the others have to be specifie in the command line.
142142
is_default = True
143143

144+
def get_stage(self):
145+
"""At which level to run."""
146+
raise NotImplementedError()
147+
144148
def gen_tasks(self):
145149
"""Task generator."""
146150
raise NotImplementedError()
@@ -154,17 +158,32 @@ def group_task(self):
154158
}
155159

156160

161+
class EarlyTask(BaseTask):
162+
"""Plugins of this type are task generators which are executed before tasks are generated."""
163+
164+
name = "dummy_task"
165+
166+
def get_stage(self):
167+
return -1
168+
169+
157170
class Task(BaseTask):
158171
"""Plugins of this type are task generators."""
159172

160173
name = "dummy_task"
161174

175+
def get_stage(self):
176+
return 10
177+
162178

163179
class LateTask(BaseTask):
164180
"""Plugins of this type are executed after all plugins of type Task."""
165181

166182
name = "dummy_latetask"
167183

184+
def get_stage(self):
185+
return 100
186+
168187

169188
class TemplateSystem(BasePlugin):
170189
"""Plugins of this type wrap templating systems."""

‎nikola/plugins/task/archive.py

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def gen_tasks(self):
114114
"strip_indexes": self.site.config['STRIP_INDEXES'],
115115
"index_file": self.site.config['INDEX_FILE'],
116116
}
117-
self.site.scan_posts()
118117
yield self.group_task()
119118
# TODO add next/prev links for years
120119
if (kw['create_monthly_archive'] and kw['create_single_archive']) and not kw['create_full_archives']:

‎nikola/plugins/task/indexes.py

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def set_site(self, site):
4242
return super(Indexes, self).set_site(site)
4343

4444
def gen_tasks(self):
45-
self.site.scan_posts()
4645
yield self.group_task()
4746

4847
kw = {

‎nikola/plugins/task/pages.py

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def gen_tasks(self):
4343
"show_untranslated_posts": self.site.config['SHOW_UNTRANSLATED_POSTS'],
4444
"demote_headers": self.site.config['DEMOTE_HEADERS'],
4545
}
46-
self.site.scan_posts()
4746
yield self.group_task()
4847
for lang in kw["translations"]:
4948
for post in self.site.timeline:

‎nikola/plugins/task/posts.py

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class RenderPosts(Task):
4747

4848
def gen_tasks(self):
4949
"""Build HTML fragments from metadata and text."""
50-
self.site.scan_posts()
5150
kw = {
5251
"translations": self.site.config["TRANSLATIONS"],
5352
"timeline": self.site.timeline,

‎nikola/plugins/task/rss.py

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def gen_tasks(self):
6262
"rss_read_more_link": self.site.config["RSS_READ_MORE_LINK"],
6363
"rss_links_append_query": self.site.config["RSS_LINKS_APPEND_QUERY"],
6464
}
65-
self.site.scan_posts()
6665
# Check for any changes in the state of use_in_feeds for any post.
6766
# Issue #934
6867
kw['use_in_feeds_status'] = ''.join(

‎nikola/plugins/task/scan_posts.plugin

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Core]
2+
Name = scan_posts
3+
Module = scan_posts
4+
5+
[Documentation]
6+
Author = Roberto Alsina and others
7+
Version = 1.0
8+
Website = http://getnikola.com
9+
Description = Scans posts.
10+

‎nikola/plugins/task/scan_posts.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright © 2012-2015 Roberto Alsina and others.
4+
5+
# Permission is hereby granted, free of charge, to any
6+
# person obtaining a copy of this software and associated
7+
# documentation files (the "Software"), to deal in the
8+
# Software without restriction, including without limitation
9+
# the rights to use, copy, modify, merge, publish,
10+
# distribute, sublicense, and/or sell copies of the
11+
# Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice
15+
# shall be included in all copies or substantial portions of
16+
# the Software.
17+
#
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19+
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20+
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21+
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
22+
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24+
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
from copy import copy
28+
29+
from nikola.plugin_categories import BaseTask
30+
31+
32+
class ScanPosts(BaseTask):
33+
"""Just load tasks."""
34+
35+
name = "scan_posts"
36+
37+
def get_stage(self):
38+
return 0
39+
40+
def gen_tasks(self):
41+
"""Scan for posts."""
42+
self.site.scan_posts(True)
43+
44+
yield self.group_task()

‎nikola/plugins/task/sources.py

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def gen_tasks(self):
5252
"show_untranslated_posts": self.site.config['SHOW_UNTRANSLATED_POSTS'],
5353
}
5454

55-
self.site.scan_posts()
5655
yield self.group_task()
5756
if self.site.config['COPY_SOURCES']:
5857
for lang in kw["translations"]:

‎nikola/plugins/task/tags.py

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def gen_tasks(self):
8181
"index_file": self.site.config['INDEX_FILE'],
8282
}
8383

84-
self.site.scan_posts()
8584
yield self.group_task()
8685

8786
yield self.list_tags_page(kw)

0 commit comments

Comments
 (0)