many pep8 improvements

This commit is contained in:
mdipierro
2012-10-19 12:33:53 -05:00
parent b716df1a05
commit 4b2ba185ae
117 changed files with 7160 additions and 5670 deletions
+47 -35
View File
@@ -30,12 +30,13 @@ from dal import BaseAdapter
logger = logging.getLogger("web2py")
def exec_environment(
pyfile='',
request=None,
response=None,
session=None,
):
):
"""
.. function:: gluon.shell.exec_environment([pyfile=''[, request=Request()
[, response=Response[, session=Session()]]]])
@@ -50,9 +51,12 @@ def exec_environment(
"""
if request is None: request = Request()
if response is None: response = Response()
if session is None: session = Session()
if request is None:
request = Request()
if response is None:
response = Response()
if session is None:
session = Session()
if request.folder is None:
mo = re.match(r'(|.*/)applications/(?P<appname>[^/]+)', pyfile)
@@ -78,7 +82,7 @@ def env(
f=None,
dir='',
extra_request={},
):
):
"""
Return web2py execution environment for application (a), controller (c),
function (f).
@@ -113,7 +117,7 @@ def env(
request.env.remote_addr = '127.0.0.1'
request.env.web2py_runtime_gae = global_settings.web2py_runtime_gae
for k,v in extra_request.items():
for k, v in extra_request.items():
request[k] = v
# Monkey patch so credentials checks pass.
@@ -129,7 +133,7 @@ def env(
try:
run_models_in(environment)
except RestrictedError, e:
sys.stderr.write(e.traceback+'\n')
sys.stderr.write(e.traceback + '\n')
sys.exit(1)
environment['__name__'] = '__main__'
@@ -156,7 +160,7 @@ def run(
startfile=None,
bpython=False,
python_code=False
):
):
"""
Start interactive shell or run Python script (startfile) in web2py
controller environment. appname is formatted like:
@@ -172,7 +176,8 @@ def run(
adir = os.path.join('applications', a)
if not os.path.exists(adir):
if sys.stdin and not sys.stdin.name == '/dev/null':
confirm = raw_input('application %s does not exist, create (y/n)?' % a)
confirm = raw_input(
'application %s does not exist, create (y/n)?' % a)
else:
logging.warn('application does not exist and will not be created')
return
@@ -180,16 +185,17 @@ def run(
os.mkdir(adir)
w2p_unpack('welcome.w2p', adir)
for subfolder in ['models','views','controllers', 'databases',
'modules','cron','errors','sessions',
'languages','static','private','uploads']:
subpath = os.path.join(adir,subfolder)
for subfolder in ['models', 'views', 'controllers', 'databases',
'modules', 'cron', 'errors', 'sessions',
'languages', 'static', 'private', 'uploads']:
subpath = os.path.join(adir, subfolder)
if not os.path.exists(subpath):
os.mkdir(subpath)
db = os.path.join(adir,'models/db.py')
db = os.path.join(adir, 'models/db.py')
if os.path.exists(db):
data = fileutils.read_file(db)
data = data.replace('<your secret key>','sha512:'+web2py_uuid())
data = data.replace(
'<your secret key>', 'sha512:' + web2py_uuid())
fileutils.write_file(db, data)
if c:
@@ -198,7 +204,8 @@ def run(
if c:
cfile = os.path.join('applications', a, 'controllers', c + '.py')
if not os.path.isfile(cfile):
cfile = os.path.join('applications', a, 'compiled', "controllers_%s_%s.pyc" % (c,f))
cfile = os.path.join('applications', a, 'compiled',
"controllers_%s_%s.pyc" % (c, f))
if not os.path.isfile(cfile):
die(errmsg)
else:
@@ -214,17 +221,21 @@ def run(
if startfile:
try:
execfile(startfile, _env)
if import_models: BaseAdapter.close_all_instances('commit')
if import_models:
BaseAdapter.close_all_instances('commit')
except Exception, e:
print traceback.format_exc()
if import_models: BaseAdapter.close_all_instances('rollback')
if import_models:
BaseAdapter.close_all_instances('rollback')
elif python_code:
try:
exec(python_code, _env)
if import_models: BaseAdapter.close_all_instances('commit')
if import_models:
BaseAdapter.close_all_instances('commit')
except Exception, e:
print traceback.format_exc()
if import_models: BaseAdapter.close_all_instances('rollback')
if import_models:
BaseAdapter.close_all_instances('rollback')
else:
if not plain:
if bpython:
@@ -248,7 +259,7 @@ def run(
# IPython; thanks Michael Toomim
if '__builtins__' in _env:
del _env['__builtins__']
shell = IPython.Shell.IPShell(argv=[],user_ns=_env)
shell = IPython.Shell.IPShell(argv=[], user_ns=_env)
shell.mainloop()
return
except:
@@ -304,7 +315,7 @@ def test(testpath, import_models=True, verbose=False):
mo = re.match(r'(|.*/)applications/(?P<a>[^/]+)', testpath)
if not mo:
die('test file is not in application directory: %s'
% testpath)
% testpath)
a = mo.group('a')
c = f = None
files = [testpath]
@@ -340,8 +351,8 @@ def test(testpath, import_models=True, verbose=False):
globs = env(a, c=c, f=f, import_models=import_models)
execfile(testfile, globs)
doctest.run_docstring_examples(obj, globs=globs,
name='%s: %s' % (os.path.basename(testfile),
name), verbose=verbose)
name='%s: %s' % (os.path.basename(testfile),
name), verbose=verbose)
if type(obj) in (types.TypeType, types.ClassType):
for attr_name in dir(obj):
@@ -369,8 +380,8 @@ def execute_from_command_line(argv=None):
parser = optparse.OptionParser(usage=get_usage())
parser.add_option('-S', '--shell', dest='shell', metavar='APPNAME',
help='run web2py in interactive shell or IPython(if installed) ' + \
'with specified appname')
help='run web2py in interactive shell or IPython(if installed) ' +
'with specified appname')
msg = 'run web2py in interactive shell or bpython (if installed) with'
msg += ' specified appname (if app does not exist it will be created).'
msg += '\n Use combined with --shell'
@@ -381,7 +392,7 @@ def execute_from_command_line(argv=None):
default=False,
dest='bpython',
help=msg,
)
)
parser.add_option(
'-P',
'--plain',
@@ -389,25 +400,25 @@ def execute_from_command_line(argv=None):
default=False,
dest='plain',
help='only use plain python shell, should be used with --shell option',
)
)
parser.add_option(
'-M',
'--import_models',
action='store_true',
default=False,
dest='import_models',
help='auto import model files, default is False, ' + \
' should be used with --shell option',
)
help='auto import model files, default is False, ' +
' should be used with --shell option',
)
parser.add_option(
'-R',
'--run',
dest='run',
metavar='PYTHON_FILE',
default='',
help='run PYTHON_FILE in web2py environment, ' + \
'should be used with --shell option',
)
help='run PYTHON_FILE in web2py environment, ' +
'should be used with --shell option',
)
(options, args) = parser.parse_args(argv[1:])
@@ -419,7 +430,8 @@ def execute_from_command_line(argv=None):
startfile = args[0]
else:
startfile = ''
run(options.shell, options.plain, startfile=startfile, bpython=options.bpython)
run(options.shell, options.plain, startfile=startfile,
bpython=options.bpython)
if __name__ == '__main__':