Skip to content

Commit

Permalink
fix site.commands (works in console only)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Apr 25, 2015
1 parent 9a9d7c2 commit 8cb66c2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
4 changes: 0 additions & 4 deletions nikola/nikola.py
Expand Up @@ -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 = []
Expand Down
4 changes: 3 additions & 1 deletion nikola/plugins/command/console.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
33 changes: 25 additions & 8 deletions nikola/utils.py
Expand Up @@ -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)
Expand Down

0 comments on commit 8cb66c2

Please sign in to comment.