diff options
Diffstat (limited to 'pecan/commands')
-rw-r--r-- | pecan/commands/__init__.py | 8 | ||||
-rw-r--r-- | pecan/commands/base.py | 40 | ||||
-rw-r--r-- | pecan/commands/create.py | 2 | ||||
-rw-r--r-- | pecan/commands/serve.py | 11 | ||||
-rw-r--r-- | pecan/commands/shell.py | 11 |
5 files changed, 46 insertions, 26 deletions
diff --git a/pecan/commands/__init__.py b/pecan/commands/__init__.py index db5aafe..da02464 100644 --- a/pecan/commands/__init__.py +++ b/pecan/commands/__init__.py @@ -1,4 +1,4 @@ -from base import CommandRunner, BaseCommand # noqa -from serve import ServeCommand # noqa -from shell import ShellCommand # noqa -from create import CreateCommand # noqa +from .base import CommandRunner, BaseCommand # noqa +from .serve import ServeCommand # noqa +from .shell import ShellCommand # noqa +from .create import CreateCommand # noqa diff --git a/pecan/commands/base.py b/pecan/commands/base.py index 68b5c0e..2a79ca5 100644 --- a/pecan/commands/base.py +++ b/pecan/commands/base.py @@ -1,10 +1,11 @@ import pkg_resources -import os.path import argparse import logging import sys from warnings import warn +import six + log = logging.getLogger(__name__) @@ -46,7 +47,7 @@ class CommandManager(object): try: cmd = ep.load() assert hasattr(cmd, 'run') - except Exception, e: # pragma: nocover + except Exception as e: # pragma: nocover warn("Unable to load plugin %s: %s" % (ep, e), RuntimeWarning) continue self.add({ep.name: cmd}) @@ -60,9 +61,11 @@ class CommandRunner(object): def __init__(self): self.manager = CommandManager() - self.parser = HelpfulArgumentParser( - version='Pecan %s' % self.version, - add_help=True + self.parser = HelpfulArgumentParser(add_help=True) + self.parser.add_argument( + '--version', + action='version', + version='Pecan %s' % self.version ) self.parse_sub_commands() @@ -78,7 +81,7 @@ class CommandRunner(object): ) for arg in getattr(cmd, 'arguments', tuple()): arg = arg.copy() - if isinstance(arg.get('name'), basestring): + if isinstance(arg.get('name'), six.string_types): sub.add_argument(arg.pop('name'), **arg) elif isinstance(arg.get('name'), list): sub.add_argument(*arg.pop('name'), **arg) @@ -101,7 +104,20 @@ class CommandRunner(object): return self.manager.commands -class BaseCommand(object): +class BaseCommandMeta(type): + + @property + def summary(cls): + """ + This is used to populate the --help argument on the command line. + + This provides a default behavior which takes the first sentence of the + command's docstring and uses it. + """ + return cls.__doc__.strip().splitlines()[0].rstrip('.') + + +class BaseCommandParent(object): """ A base interface for Pecan commands. @@ -126,14 +142,10 @@ class BaseCommand(object): def run(self, args): super(SomeCommand, self).run(args) - print args.extra_arg + if args.extra_arg: + pass """ - class __metaclass__(type): - @property - def summary(cls): - return cls.__doc__.strip().splitlines()[0].rstrip('.') - arguments = ({ 'name': 'config_file', 'help': 'a Pecan configuration file', @@ -147,3 +159,5 @@ class BaseCommand(object): def load_app(self): from pecan import load_app return load_app(self.args.config_file) + +BaseCommand = BaseCommandMeta('BaseCommand', (BaseCommandParent,), {}) diff --git a/pecan/commands/create.py b/pecan/commands/create.py index f332065..b6eec2a 100644 --- a/pecan/commands/create.py +++ b/pecan/commands/create.py @@ -23,7 +23,7 @@ class ScaffoldManager(object): try: cmd = ep.load() assert hasattr(cmd, 'copy_to') - except Exception, e: # pragma: nocover + except Exception as e: # pragma: nocover warn( "Unable to load scaffold %s: %s" % (ep, e), RuntimeWarning ) diff --git a/pecan/commands/serve.py b/pecan/commands/serve.py index 9dd431e..0d1b14a 100644 --- a/pecan/commands/serve.py +++ b/pecan/commands/serve.py @@ -1,6 +1,7 @@ """ Serve command for Pecan. """ +from __future__ import print_function import os import sys import time @@ -42,7 +43,7 @@ class ServeCommand(BaseCommand): DirModifiedEvent ) - print 'Monitoring for changes...' + print('Monitoring for changes...') self.create_subprocess() parent = self @@ -101,13 +102,15 @@ class ServeCommand(BaseCommand): host, port = conf.server.host, int(conf.server.port) srv = make_server(host, port, app) - print 'Starting server in PID %s' % os.getpid() + print('Starting server in PID %s' % os.getpid()) if host == '0.0.0.0': - print 'serving on 0.0.0.0:%s, view at http://127.0.0.1:%s' % \ + print( + 'serving on 0.0.0.0:%s, view at http://127.0.0.1:%s' % (port, port) + ) else: - print "serving on http://%s:%s" % (host, port) + print("serving on http://%s:%s" % (host, port)) try: srv.serve_forever() diff --git a/pecan/commands/shell.py b/pecan/commands/shell.py index 8f7a38a..968ded0 100644 --- a/pecan/commands/shell.py +++ b/pecan/commands/shell.py @@ -125,9 +125,12 @@ class ShellCommand(BaseCommand): locs['model'] = model # insert the pecan locals - exec( - 'from pecan import abort, conf, redirect, request, response' - ) in locs + from pecan import abort, conf, redirect, request, response + locs['abort'] = abort + locs['conf'] = conf + locs['redirect'] = redirect + locs['request'] = request + locs['response'] = response # prepare the banner banner = ' The following objects are available:\n' @@ -153,7 +156,7 @@ class ShellCommand(BaseCommand): shell = self.SHELLS[self.args.shell] try: shell().invoke(locs, banner) - except ImportError, e: + except ImportError as e: warn(( "%s is not installed, `%s`, " "falling back to native shell") % (self.args.shell, e), |