summaryrefslogtreecommitdiffstats
path: root/nikola/plugins/command/console.py
diff options
context:
space:
mode:
Diffstat (limited to 'nikola/plugins/command/console.py')
-rw-r--r--nikola/plugins/command/console.py116
1 files changed, 66 insertions, 50 deletions
diff --git a/nikola/plugins/command/console.py b/nikola/plugins/command/console.py
index b0a8958..9dfc975 100644
--- a/nikola/plugins/command/console.py
+++ b/nikola/plugins/command/console.py
@@ -30,7 +30,7 @@ import os
from nikola import __version__
from nikola.plugin_categories import Command
-from nikola.utils import get_logger, STDERR_HANDLER
+from nikola.utils import get_logger, STDERR_HANDLER, req_missing
LOGGER = get_logger('console', STDERR_HANDLER)
@@ -41,86 +41,102 @@ class CommandConsole(Command):
shells = ['ipython', 'bpython', 'plain']
doc_purpose = "start an interactive Python console with access to your site"
doc_description = """\
-Order of resolution: IPython → bpython [deprecated] → plain Python interpreter
-The site engine is accessible as `SITE`, and the config as `conf`."""
- header = "Nikola v" + __version__ + " -- {0} Console (conf = configuration, SITE = site engine)"
+The site engine is accessible as `site`, the config file as `conf`, and commands are available as `commands`.
+If there is no console to use specified (as -b, -i, -p) it tries IPython, then falls back to bpython, and finally falls back to the plain Python console."""
+ header = "Nikola v" + __version__ + " -- {0} Console (conf = configuration file, site = site engine, commands = nikola commands)"
cmd_options = [
{
+ 'name': 'bpython',
+ 'short': 'b',
+ 'long': 'bpython',
+ 'type': bool,
+ 'default': False,
+ 'help': 'Use bpython',
+ },
+ {
+ 'name': 'ipython',
+ 'short': 'i',
+ 'long': 'plain',
+ 'type': bool,
+ 'default': False,
+ 'help': 'Use IPython',
+ },
+ {
'name': 'plain',
'short': 'p',
'long': 'plain',
'type': bool,
'default': False,
- 'help': 'Force the plain Python console',
- }
+ 'help': 'Use the plain Python interpreter',
+ },
]
- def ipython(self):
+ def ipython(self, willful=True):
"""IPython shell."""
- from nikola import Nikola
try:
- import conf
- except ImportError:
- LOGGER.error("No configuration found, cannot run the console.")
- else:
import IPython
- SITE = Nikola(**conf.__dict__)
- SITE.scan_posts()
+ except ImportError as e:
+ if willful:
+ req_missing(['IPython'], 'use the IPython console')
+ raise e # That’s how _execute knows whether to try something else.
+ else:
+ site = self.context['site'] # NOQA
+ conf = self.context['conf'] # NOQA
+ commands = self.context['commands'] # NOQA
IPython.embed(header=self.header.format('IPython'))
- def bpython(self):
+ def bpython(self, willful=True):
"""bpython shell."""
- from nikola import Nikola
try:
- import conf
- except ImportError:
- LOGGER.error("No configuration found, cannot run the console.")
- else:
import bpython
- SITE = Nikola(**conf.__dict__)
- SITE.scan_posts()
- gl = {'conf': conf, 'SITE': SITE, 'Nikola': Nikola}
- bpython.embed(banner=self.header.format(
- 'bpython (Slightly Deprecated)'), locals_=gl)
+ except ImportError as e:
+ if willful:
+ req_missing(['bpython'], 'use the bpython console')
+ raise e # That’s how _execute knows whether to try something else.
+ else:
+ bpython.embed(banner=self.header.format('bpython'), locals_=self.context)
- def plain(self):
+ def plain(self, willful=True):
"""Plain Python shell."""
- from nikola import Nikola
+ import code
try:
- import conf
- SITE = Nikola(**conf.__dict__)
- SITE.scan_posts()
- gl = {'conf': conf, 'SITE': SITE, 'Nikola': Nikola}
+ import readline
except ImportError:
- LOGGER.error("No configuration found, cannot run the console.")
+ pass
else:
- import code
+ import rlcompleter
+ readline.set_completer(rlcompleter.Completer(self.context).complete)
+ readline.parse_and_bind("tab:complete")
+
+ pythonrc = os.environ.get("PYTHONSTARTUP")
+ if pythonrc and os.path.isfile(pythonrc):
try:
- import readline
- except ImportError:
+ execfile(pythonrc) # NOQA
+ except NameError:
pass
- else:
- import rlcompleter
- readline.set_completer(rlcompleter.Completer(gl).complete)
- readline.parse_and_bind("tab:complete")
- pythonrc = os.environ.get("PYTHONSTARTUP")
- if pythonrc and os.path.isfile(pythonrc):
- try:
- execfile(pythonrc) # NOQA
- except NameError:
- pass
-
- code.interact(local=gl, banner=self.header.format('Python'))
+ code.interact(local=self.context, banner=self.header.format('Python'))
def _execute(self, options, args):
"""Start the console."""
- if options['plain']:
- self.plain()
+ self.site.scan_posts()
+ # Create nice object with all commands:
+
+ self.context = {
+ 'conf': self.site.config,
+ 'site': self.site,
+ 'commands': self.site.commands,
+ }
+ if options['bpython']:
+ self.bpython(True)
+ elif options['ipython']:
+ self.ipython(True)
+ elif options['plain']:
+ self.plain(True)
else:
for shell in self.shells:
try:
- return getattr(self, shell)()
+ return getattr(self, shell)(False)
except ImportError:
pass
raise ImportError