After a full-scale scan with pep8.py and pyflakes, identified and

resolved most of our PEP8 compliance issues.
This commit is contained in:
Jonathan LaCour
2012-03-11 09:52:25 -07:00
parent 20a996b58d
commit bed5cbfa14
34 changed files with 1942 additions and 1591 deletions

View File

@@ -1,7 +1,8 @@
"""
PasteScript commands for Pecan.
"""
from runner import CommandRunner
from create import CreateCommand
from shell import ShellCommand
from serve import ServeCommand
from runner import CommandRunner # noqa
from create import CreateCommand # noqa
from shell import ShellCommand # noqa
from serve import ServeCommand # noqa

View File

@@ -2,30 +2,28 @@
PasteScript base command for Pecan.
"""
from pecan import load_app
from pecan.configuration import _runtime_conf, set_config
from paste.script import command as paste_command
import os.path
import sys
class Command(paste_command.Command):
"""
Base class for Pecan commands.
This provides some standard functionality for interacting with Pecan
This provides some standard functionality for interacting with Pecan
applications and handles some of the basic PasteScript command cruft.
See ``paste.script.command.Command`` for more information.
"""
# command information
group_name = 'Pecan'
summary = ''
# command parser
parser = paste_command.Command.standard_parser()
def run(self, args):
try:
return paste_command.Command.run(self, args)
@@ -35,15 +33,17 @@ class Command(paste_command.Command):
def load_app(self):
return load_app(self.validate_file(self.args))
def logging_file_config(self, config_file):
if os.path.splitext(config_file)[1].lower() == '.ini':
paste_command.Command.logging_file_config(self, config_file)
def validate_file(self, argv):
if not argv or not os.path.isfile(argv[0]):
raise paste_command.BadCommand('This command needs a valid config file.')
raise paste_command.BadCommand(
'This command needs a valid config file.'
)
return argv[0]
def command(self):
pass

View File

@@ -13,16 +13,16 @@ import sys
class CreateCommand(CreateDistroCommand, Command):
"""
Creates the file layout for a new Pecan distribution.
For a template to show up when using this command, its name must begin
with "pecan-". Although not required, it should also include the "Pecan"
For a template to show up when using this command, its name must begin
with "pecan-". Although not required, it should also include the "Pecan"
egg plugin for user convenience.
"""
# command information
summary = __doc__.strip().splitlines()[0].rstrip('.')
description = None
def command(self):
if not self.options.list_templates:
if not self.options.templates:
@@ -33,7 +33,7 @@ class CreateCommand(CreateDistroCommand, Command):
sys.stderr.write('%s\n\n' % ex)
CreateDistroCommand.list_templates(self)
return 2
def all_entry_points(self):
entry_points = []
for entry in CreateDistroCommand.all_entry_points(self):

View File

@@ -13,50 +13,52 @@ import warnings
class CommandRunner(object):
"""
Dispatches command execution requests.
This is a custom PasteScript command runner that is specific to Pecan
commands. For a command to show up, its name must begin with "pecan-".
It is also recommended that its group name be set to "Pecan" so that it
This is a custom PasteScript command runner that is specific to Pecan
commands. For a command to show up, its name must begin with "pecan-".
It is also recommended that its group name be set to "Pecan" so that it
shows up under that group when using ``paster`` directly.
"""
def __init__(self):
# set up the parser
self.parser = optparse.OptionParser(add_help_option=False,
version='Pecan %s' % self.get_version(),
usage='%prog [options] COMMAND [command_options]')
self.parser = optparse.OptionParser(
add_help_option=False,
version='Pecan %s' % self.get_version(),
usage='%prog [options] COMMAND [command_options]'
)
self.parser.disable_interspersed_args()
self.parser.add_option('-h', '--help',
action='store_true',
dest='show_help',
help='show detailed help message')
# suppress BaseException.message warnings for BadCommand
if sys.version_info < (2, 7):
warnings.filterwarnings(
'ignore',
'BaseException\.message has been deprecated as of Python 2\.6',
DeprecationWarning,
'ignore',
'BaseException\.message has been deprecated as of Python 2\.6',
DeprecationWarning,
paste_command.__name__.replace('.', '\\.'))
# register Pecan as a system plugin when using the custom runner
paste_command.system_plugins.append('Pecan')
def get_command_template(self, command_names):
if not command_names:
max_length = 10
else:
max_length = max([len(name) for name in command_names])
return ' %%-%ds %%s\n' % max_length
def get_commands(self):
commands = {}
for name, command in paste_command.get_commands().iteritems():
if name.startswith('pecan-'):
commands[name[6:]] = command.load()
return commands
def get_version(self):
try:
dist = pkg_resources.get_distribution('Pecan')
@@ -66,7 +68,7 @@ class CommandRunner(object):
return '(development)'
except:
return '(development)'
def print_usage(self, file=sys.stdout):
self.parser.print_help(file=file)
file.write('\n')
@@ -88,7 +90,7 @@ class CommandRunner(object):
file.write(command_template % (name, command.summary))
if i + 1 < len(command_groups):
file.write('\n')
def print_known_commands(self, file=sys.stderr):
commands = self.get_commands()
command_names = sorted(commands.keys())
@@ -99,7 +101,7 @@ class CommandRunner(object):
command_template = self.get_command_template(command_names)
for name in command_names:
file.write(command_template % (name, commands[name].summary))
def run(self, args):
options, args = self.parser.parse_args(args)
if not args:
@@ -117,7 +119,7 @@ class CommandRunner(object):
return command.run(['-h'])
else:
return command.run(args)
@classmethod
def handle_command_line(cls):
try:

View File

@@ -1,45 +1,45 @@
"""
PasteScript serve command for Pecan.
"""
from paste import httpserver
from paste.script import command as paste_command
from paste import httpserver
from paste.script.serve import ServeCommand as _ServeCommand
from base import Command
import os
import re
class ServeCommand(_ServeCommand, Command):
"""
Serves a Pecan web application.
This command serves a Pecan web application using the provided
This command serves a Pecan web application using the provided
configuration file for the server and application.
If start/stop/restart is given, then --daemon is implied, and it will
If start/stop/restart is given, then --daemon is implied, and it will
start (normal operation), stop (--stop-daemon), or do both.
"""
# command information
usage = 'CONFIG_FILE [start|stop|restart|status]'
summary = __doc__.strip().splitlines()[0].rstrip('.')
description = '\n'.join(map(lambda s: s.rstrip(), __doc__.strip().splitlines()[2:]))
description = '\n'.join(
map(lambda s: s.rstrip(), __doc__.strip().splitlines()[2:])
)
# command options/arguments
max_args = 2
# command parser
parser = _ServeCommand.parser
parser.remove_option('-n')
parser.remove_option('-s')
parser.remove_option('--server-name')
# configure scheme regex
_scheme_re = re.compile(r'.*')
def command(self):
# set defaults for removed options
setattr(self.options, 'app_name', None)
setattr(self.options, 'server', None)
@@ -47,9 +47,11 @@ class ServeCommand(_ServeCommand, Command):
# run the base command
_ServeCommand.command(self)
def loadserver(self, server_spec, name, relative_to, **kw):
return (lambda app: httpserver.serve(app, app.config.server.host, app.config.server.port))
return (lambda app: httpserver.serve(
app, app.config.server.host, app.config.server.port
))
def loadapp(self, app_spec, name, relative_to, **kw):
return self.load_app()

View File

@@ -12,41 +12,47 @@ class ShellCommand(Command):
"""
Open an interactive shell with the Pecan app loaded.
"""
# command information
usage = 'CONFIG_NAME'
summary = __doc__.strip().splitlines()[0].rstrip('.')
# command options/arguments
min_args = 1
max_args = 1
def command(self):
# load the application
app = self.load_app()
# prepare the locals
locs = dict(__name__='pecan-admin')
locs['wsgiapp'] = app
locs['app'] = TestApp(app)
model = self.load_model(app.config)
if model:
locs['model'] = model
# insert the pecan locals
exec('from pecan import abort, conf, redirect, request, response') in locs
exec(
'from pecan import abort, conf, redirect, request, response'
) in locs
# prepare the banner
banner = ' The following objects are available:\n'
banner += ' %-10s - This project\'s WSGI App instance\n' % 'wsgiapp'
banner += ' %-10s - The current configuration\n' % 'conf'
banner += ' %-10s - webtest.TestApp wrapped around wsgiapp\n' % 'app'
if model:
model_name = getattr(model, '__module__', getattr(model, '__name__', 'model'))
model_name = getattr(
model,
'__module__',
getattr(model, '__name__', 'model')
)
banner += ' %-10s - Models from %s\n' % ('model', model_name)
# launch the shell, using IPython if available
try:
from IPython.Shell import IPShellEmbed
@@ -60,7 +66,7 @@ class ShellCommand(Command):
(py_prefix, sys.version)
shell = code.InteractiveConsole(locals=locs)
try:
import readline
import readline # noqa
except ImportError:
pass
shell.interact(shell_banner + banner)