Skip to content
Permalink
Browse files
fix site.commands (works in console only)
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Apr 25, 2015
1 parent 9a9d7c2 commit 8cb66c2457ad1de5d689df9c5b5118ea69aa00ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
@@ -1315,10 +1315,6 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):
if self._scanned and not really:
return

try:
self.commands = utils.Commands(self.doit)
except AttributeError:
self.commands = None
self.global_data = {}
self.posts = []
self.all_posts = []
@@ -30,7 +30,7 @@

from nikola import __version__
from nikola.plugin_categories import Command
from nikola.utils import get_logger, STDERR_HANDLER, req_missing
from nikola.utils import get_logger, STDERR_HANDLER, req_missing, Commands

LOGGER = get_logger('console', STDERR_HANDLER)

@@ -122,6 +122,8 @@ def _execute(self, options, args):
self.site.scan_posts()
# Create nice object with all commands:

self.site.commands = Commands(self.site.doit, self.config, self._doitargs)

self.context = {
'conf': self.site.config,
'site': self.site,
@@ -1305,34 +1305,51 @@ class Commands(object):
>>> commands.check(list=True) # doctest: +SKIP
"""

def __init__(self, main):
def __init__(self, main, config, doitargs):
"""Takes a main instance, works as wrapper for commands."""
self._cmdnames = []
for k, v in {}.items(): # main.get_commands().items():
self._cmdnames.append(k)
self._main = main
self._config = config
self._doitargs = doitargs
for k, v in self._main.get_cmds().items():
# cleanup: run is doit-only, init is useless in an existing site
if k in ['run', 'init']:
continue
if sys.version_info[0] == 2:
k2 = bytes(k)
else:
k2 = k

self._cmdnames.append(k)

try:
# nikola command: already instantiated (singleton)
opt = v.get_options()
except TypeError:
# doit command: needs some help
opt = v(config=self._config, **self._doitargs).get_options()
nc = type(
k2,
(CommandWrapper,),
{
'__doc__': options2docstring(k, main.sub_cmds[k].options)
'__doc__': options2docstring(k, opt)
})
setattr(self, k, nc(k, self))
self.main = main

def _run(self, cmd_args):
self.main.run(cmd_args)
self._main.run(cmd_args)

def _run_with_kw(self, cmd, *a, **kw):
# cyclic import hack
from nikola.plugin_categories import Command
cmd = self.main.sub_cmds[cmd]
options, _ = CmdParse(cmd.options).parse([])
cmd = self._main.get_cmds()[cmd]
try:
opt = cmd.get_options()
except TypeError:
cmd = cmd(config=self._config, **self._doitargs)
opt = cmd.get_options()

options, _ = CmdParse(opt).parse([])
options.update(kw)
if isinstance(cmd, Command):
cmd.execute(options=options, args=a)

0 comments on commit 8cb66c2

Please sign in to comment.