many pep8 improvements
This commit is contained in:
+84
-75
@@ -51,7 +51,7 @@ from globals import current
|
||||
# The two are identical unless web2py_path is changed via the web2py.py -f folder option
|
||||
# main.web2py_path is the same as applications_parent (for backward compatibility)
|
||||
|
||||
web2py_path = global_settings.applications_parent # backward compatibility
|
||||
web2py_path = global_settings.applications_parent # backward compatibility
|
||||
|
||||
create_missing_folders()
|
||||
|
||||
@@ -107,7 +107,7 @@ try:
|
||||
version_info.close()
|
||||
global_settings.web2py_version = parse_version(raw_version_string)
|
||||
except:
|
||||
raise RuntimeError, "Cannot determine web2py version"
|
||||
raise RuntimeError("Cannot determine web2py version")
|
||||
|
||||
web2py_version = global_settings.web2py_version
|
||||
|
||||
@@ -119,7 +119,8 @@ except:
|
||||
|
||||
load()
|
||||
|
||||
HTTPS_SCHEMES = set(('https','HTTPS'))
|
||||
HTTPS_SCHEMES = set(('https', 'HTTPS'))
|
||||
|
||||
|
||||
def get_client(env):
|
||||
"""
|
||||
@@ -138,10 +139,11 @@ def get_client(env):
|
||||
else:
|
||||
client = '127.0.0.1'
|
||||
if not is_valid_ip_address(client):
|
||||
raise HTTP(400,"Bad Request (request.client=%s)" % client)
|
||||
raise HTTP(400, "Bad Request (request.client=%s)" % client)
|
||||
return client
|
||||
|
||||
def copystream_progress(request, chunk_size= 10**5):
|
||||
|
||||
def copystream_progress(request, chunk_size=10 ** 5):
|
||||
"""
|
||||
copies request.env.wsgi_input into request.body
|
||||
and stores progress upload status in cache_ram
|
||||
@@ -154,22 +156,22 @@ def copystream_progress(request, chunk_size= 10**5):
|
||||
try:
|
||||
size = int(env.content_length)
|
||||
except ValueError:
|
||||
raise HTTP(400,"Invalid Content-Length header")
|
||||
raise HTTP(400, "Invalid Content-Length header")
|
||||
dest = tempfile.TemporaryFile()
|
||||
if not 'X-Progress-ID' in request.vars:
|
||||
copystream(source, dest, size, chunk_size)
|
||||
return dest
|
||||
cache_key = 'X-Progress-ID:'+request.vars['X-Progress-ID']
|
||||
cache_key = 'X-Progress-ID:' + request.vars['X-Progress-ID']
|
||||
cache_ram = CacheInRam(request) # same as cache.ram because meta_storage
|
||||
cache_ram(cache_key+':length', lambda: size, 0)
|
||||
cache_ram(cache_key+':uploaded', lambda: 0, 0)
|
||||
cache_ram(cache_key + ':length', lambda: size, 0)
|
||||
cache_ram(cache_key + ':uploaded', lambda: 0, 0)
|
||||
while size > 0:
|
||||
if size < chunk_size:
|
||||
data = source.read(size)
|
||||
cache_ram.increment(cache_key+':uploaded', size)
|
||||
cache_ram.increment(cache_key + ':uploaded', size)
|
||||
else:
|
||||
data = source.read(chunk_size)
|
||||
cache_ram.increment(cache_key+':uploaded', chunk_size)
|
||||
cache_ram.increment(cache_key + ':uploaded', chunk_size)
|
||||
length = len(data)
|
||||
if length > size:
|
||||
(data, length) = (data[:size], size)
|
||||
@@ -180,8 +182,8 @@ def copystream_progress(request, chunk_size= 10**5):
|
||||
if length < chunk_size:
|
||||
break
|
||||
dest.seek(0)
|
||||
cache_ram(cache_key+':length', None)
|
||||
cache_ram(cache_key+':uploaded', None)
|
||||
cache_ram(cache_key + ':length', None)
|
||||
cache_ram(cache_key + ':uploaded', None)
|
||||
return dest
|
||||
|
||||
|
||||
@@ -224,8 +226,9 @@ def serve_controller(request, response, session):
|
||||
page = response.body.getvalue()
|
||||
# logic to garbage collect after exec, not always, once every 100 requests
|
||||
global requests
|
||||
requests = ('requests' in globals()) and (requests+1) % 100 or 0
|
||||
if not requests: gc.collect()
|
||||
requests = ('requests' in globals()) and (requests + 1) % 100 or 0
|
||||
if not requests:
|
||||
gc.collect()
|
||||
# end garbage collection logic
|
||||
|
||||
# ##################################################
|
||||
@@ -233,13 +236,14 @@ def serve_controller(request, response, session):
|
||||
# ##################################################
|
||||
|
||||
default_headers = [
|
||||
('Content-Type', contenttype('.'+request.extension)),
|
||||
('Cache-Control','no-store, no-cache, must-revalidate, post-check=0, pre-check=0'),
|
||||
('Content-Type', contenttype('.' + request.extension)),
|
||||
('Cache-Control',
|
||||
'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'),
|
||||
('Expires', time.strftime('%a, %d %b %Y %H:%M:%S GMT',
|
||||
time.gmtime())),
|
||||
('Pragma', 'no-cache')]
|
||||
for key,value in default_headers:
|
||||
response.headers.setdefault(key,value)
|
||||
for key, value in default_headers:
|
||||
response.headers.setdefault(key, value)
|
||||
|
||||
raise HTTP(response.status, page, **response.headers)
|
||||
|
||||
@@ -253,9 +257,9 @@ def start_response_aux(status, headers, exc_info, response=None):
|
||||
|
||||
to call third party WSGI applications
|
||||
"""
|
||||
response.status = str(status).split(' ',1)[0]
|
||||
response.status = str(status).split(' ', 1)[0]
|
||||
response.headers = dict(headers)
|
||||
return lambda *args, **kargs: response.write(escape=False,*args,**kargs)
|
||||
return lambda *args, **kargs: response.write(escape=False, *args, **kargs)
|
||||
|
||||
|
||||
def middleware_aux(request, response, *middleware_apps):
|
||||
@@ -270,24 +274,27 @@ def middleware_aux(request, response, *middleware_apps):
|
||||
def middleware(f):
|
||||
def app(environ, start_response):
|
||||
data = f()
|
||||
start_response(response.status,response.headers.items())
|
||||
if isinstance(data,list):
|
||||
start_response(response.status, response.headers.items())
|
||||
if isinstance(data, list):
|
||||
return data
|
||||
return [data]
|
||||
for item in middleware_apps:
|
||||
app=item(app)
|
||||
app = item(app)
|
||||
|
||||
def caller(app):
|
||||
wsgi = request.wsgi
|
||||
return app(wsgi.environ, wsgi.start_response)
|
||||
return lambda caller=caller, app=app: caller(app)
|
||||
return middleware
|
||||
|
||||
def environ_aux(environ,request):
|
||||
|
||||
def environ_aux(environ, request):
|
||||
new_environ = copy.copy(environ)
|
||||
new_environ['wsgi.input'] = request.body
|
||||
new_environ['wsgi.version'] = 1
|
||||
return new_environ
|
||||
|
||||
|
||||
def parse_get_post_vars(request, environ):
|
||||
|
||||
# always parse variables in URL for GET, POST, PUT, DELETE, etc. in get_vars
|
||||
@@ -307,22 +314,23 @@ def parse_get_post_vars(request, environ):
|
||||
try:
|
||||
request.body = body = copystream_progress(request)
|
||||
except IOError:
|
||||
raise HTTP(400,"Bad Request - HTTP body is incomplete")
|
||||
raise HTTP(400, "Bad Request - HTTP body is incomplete")
|
||||
if (body and env.request_method in ('POST', 'PUT', 'BOTH')):
|
||||
dpost = cgi.FieldStorage(fp=body,environ=environ,keep_blank_values=1)
|
||||
dpost = cgi.FieldStorage(fp=body, environ=environ, keep_blank_values=1)
|
||||
# The same detection used by FieldStorage to detect multipart POSTs
|
||||
is_multipart = dpost.type[:10] == 'multipart/'
|
||||
body.seek(0)
|
||||
isle25 = sys.version_info[1] <= 5
|
||||
|
||||
def listify(a):
|
||||
return (not isinstance(a,list) and [a]) or a
|
||||
return (not isinstance(a, list) and [a]) or a
|
||||
try:
|
||||
keys = sorted(dpost)
|
||||
except TypeError:
|
||||
keys = []
|
||||
for key in keys:
|
||||
if key is None: continue # not sure why cgi.FieldStorage returns None key
|
||||
if key is None:
|
||||
continue # not sure why cgi.FieldStorage returns None key
|
||||
dpk = dpost[key]
|
||||
# if en element is not a file replace it with its value else leave it alone
|
||||
if isinstance(dpk, list):
|
||||
@@ -347,7 +355,8 @@ def parse_get_post_vars(request, environ):
|
||||
pvalue = pvalue[:-len(gvalue)]
|
||||
request.vars[key] = value
|
||||
if len(pvalue):
|
||||
request.post_vars[key] = (len(pvalue)>1 and pvalue) or pvalue[0]
|
||||
request.post_vars[key] = (len(pvalue) >
|
||||
1 and pvalue) or pvalue[0]
|
||||
|
||||
|
||||
def wsgibase(environ, responder):
|
||||
@@ -407,22 +416,23 @@ def wsgibase(environ, responder):
|
||||
response.status = env.web2py_status_code or response.status
|
||||
|
||||
if static_file:
|
||||
if environ.get('QUERY_STRING','').startswith(
|
||||
'attachment'):
|
||||
if environ.get('QUERY_STRING', '').startswith(
|
||||
'attachment'):
|
||||
response.headers['Content-Disposition'] \
|
||||
= 'attachment'
|
||||
if version:
|
||||
response.headers['Cache-Control'] = 'max-age=315360000'
|
||||
response.headers['Expires'] = 'Thu, 31 Dec 2037 23:59:59 GMT'
|
||||
response.headers[
|
||||
'Expires'] = 'Thu, 31 Dec 2037 23:59:59 GMT'
|
||||
response.stream(static_file, request=request)
|
||||
|
||||
# ##################################################
|
||||
# fill in request items
|
||||
# ##################################################
|
||||
app = request.application ## must go after url_in!
|
||||
app = request.application # must go after url_in!
|
||||
|
||||
if not global_settings.local_hosts:
|
||||
local_hosts = ['127.0.0.1','::ffff:127.0.0.1']
|
||||
local_hosts = ['127.0.0.1', '::ffff:127.0.0.1']
|
||||
if not global_settings.web2py_runtime_gae:
|
||||
try:
|
||||
local_hosts.append(socket.gethostname())
|
||||
@@ -433,7 +443,7 @@ def wsgibase(environ, responder):
|
||||
local_hosts += [
|
||||
env.server_name,
|
||||
socket.gethostbyname(env.server_name)]
|
||||
except (socket.gaierror,TypeError):
|
||||
except (socket.gaierror, TypeError):
|
||||
pass
|
||||
global_settings.local_hosts = local_hosts
|
||||
else:
|
||||
@@ -442,15 +452,15 @@ def wsgibase(environ, responder):
|
||||
x_req_with = str(env.http_x_requested_with).lower()
|
||||
|
||||
request.update(
|
||||
client = client,
|
||||
folder = abspath('applications',app) + os.sep,
|
||||
ajax = x_req_with == 'xmlhttprequest',
|
||||
cid = env.http_web2py_component_element,
|
||||
is_local = env.remote_addr in local_hosts,
|
||||
is_https = env.wsgi_url_scheme in HTTPS_SCHEMES \
|
||||
or request.env.http_x_forwarded_proto in HTTPS_SCHEMES \
|
||||
or env.https=='on')
|
||||
request.uuid = request.compute_uuid() # requires client
|
||||
client=client,
|
||||
folder=abspath('applications', app) + os.sep,
|
||||
ajax=x_req_with == 'xmlhttprequest',
|
||||
cid=env.http_web2py_component_element,
|
||||
is_local=env.remote_addr in local_hosts,
|
||||
is_https=env.wsgi_url_scheme in HTTPS_SCHEMES
|
||||
or request.env.http_x_forwarded_proto in HTTPS_SCHEMES
|
||||
or env.https == 'on')
|
||||
request.uuid = request.compute_uuid() # requires client
|
||||
request.url = environ['PATH_INFO']
|
||||
|
||||
# ##################################################
|
||||
@@ -460,7 +470,7 @@ def wsgibase(environ, responder):
|
||||
if not exists(request.folder):
|
||||
if app == rwthread.routes.default_application \
|
||||
and app != 'welcome':
|
||||
redirect(URL('welcome','default','index'))
|
||||
redirect(URL('welcome', 'default', 'index'))
|
||||
elif rwthread.routes.error_handler:
|
||||
_handler = rwthread.routes.error_handler
|
||||
redirect(URL(_handler['application'],
|
||||
@@ -468,11 +478,11 @@ def wsgibase(environ, responder):
|
||||
_handler['function'],
|
||||
args=app))
|
||||
else:
|
||||
raise HTTP(404, rwthread.routes.error_message \
|
||||
% 'invalid request',
|
||||
raise HTTP(404, rwthread.routes.error_message
|
||||
% 'invalid request',
|
||||
web2py_error='invalid application')
|
||||
elif not request.is_local and \
|
||||
exists(pjoin(request.folder,'DISABLED')):
|
||||
exists(pjoin(request.folder, 'DISABLED')):
|
||||
raise HTTP(503, "<html><body><h1>Temporarily down for maintenance</h1></body></html>")
|
||||
|
||||
# ##################################################
|
||||
@@ -491,13 +501,13 @@ def wsgibase(environ, responder):
|
||||
# expose wsgi hooks for convenience
|
||||
# ##################################################
|
||||
|
||||
request.wsgi.environ = environ_aux(environ,request)
|
||||
request.wsgi.environ = environ_aux(environ, request)
|
||||
request.wsgi.start_response = \
|
||||
lambda status='200', headers=[], \
|
||||
exec_info=None, response=response: \
|
||||
start_response_aux(status, headers, exec_info, response)
|
||||
request.wsgi.middleware = \
|
||||
lambda *a: middleware_aux(request,response,*a)
|
||||
lambda *a: middleware_aux(request, response, *a)
|
||||
|
||||
# ##################################################
|
||||
# load cookies
|
||||
@@ -507,7 +517,7 @@ def wsgibase(environ, responder):
|
||||
try:
|
||||
request.cookies.load(env.http_cookie)
|
||||
except Cookie.CookieError, e:
|
||||
pass # invalid cookies
|
||||
pass # invalid cookies
|
||||
|
||||
# ##################################################
|
||||
# try load session or create new session file
|
||||
@@ -530,7 +540,7 @@ def wsgibase(environ, responder):
|
||||
except HTTP, http_response:
|
||||
|
||||
if static_file:
|
||||
return http_response.to(responder,env=env)
|
||||
return http_response.to(responder, env=env)
|
||||
|
||||
if request.body:
|
||||
request.body.close()
|
||||
@@ -562,9 +572,9 @@ def wsgibase(environ, responder):
|
||||
|
||||
if request.cid:
|
||||
if response.flash:
|
||||
http_response.headers['web2py-component-flash'] = urllib2.quote(xmlescape(response.flash).replace('\n',''))
|
||||
http_response.headers['web2py-component-flash'] = urllib2.quote(xmlescape(response.flash).replace('\n', ''))
|
||||
if response.js:
|
||||
http_response.headers['web2py-component-command'] = response.js.replace('\n','')
|
||||
http_response.headers['web2py-component-command'] = response.js.replace('\n', '')
|
||||
|
||||
# ##################################################
|
||||
# store cookies in headers
|
||||
@@ -576,7 +586,7 @@ def wsgibase(environ, responder):
|
||||
elif session._secure:
|
||||
rcookies[response.session_id_name]['secure'] = True
|
||||
http_response.cookies2headers(rcookies)
|
||||
ticket=None
|
||||
ticket = None
|
||||
|
||||
except RestrictedError, e:
|
||||
|
||||
@@ -594,8 +604,8 @@ def wsgibase(environ, responder):
|
||||
BaseAdapter.close_all_instances('rollback')
|
||||
|
||||
http_response = \
|
||||
HTTP(500, rwthread.routes.error_message_ticket % \
|
||||
dict(ticket=ticket),
|
||||
HTTP(500, rwthread.routes.error_message_ticket %
|
||||
dict(ticket=ticket),
|
||||
web2py_error='ticket %s' % ticket)
|
||||
|
||||
except:
|
||||
@@ -617,8 +627,8 @@ def wsgibase(environ, responder):
|
||||
e = RestrictedError('Framework', '', '', locals())
|
||||
ticket = e.log(request) or 'unrecoverable'
|
||||
http_response = \
|
||||
HTTP(500, rwthread.routes.error_message_ticket \
|
||||
% dict(ticket=ticket),
|
||||
HTTP(500, rwthread.routes.error_message_ticket
|
||||
% dict(ticket=ticket),
|
||||
web2py_error='ticket %s' % ticket)
|
||||
|
||||
finally:
|
||||
@@ -630,10 +640,10 @@ def wsgibase(environ, responder):
|
||||
http_response, new_environ = try_rewrite_on_error(
|
||||
http_response, request, environ, ticket)
|
||||
if not http_response:
|
||||
return wsgibase(new_environ,responder)
|
||||
return wsgibase(new_environ, responder)
|
||||
if global_settings.web2py_crontype == 'soft':
|
||||
newcron.softcron(global_settings.applications_parent).start()
|
||||
return http_response.to(responder,env=env)
|
||||
return http_response.to(responder, env=env)
|
||||
|
||||
|
||||
def save_password(password, port):
|
||||
@@ -712,14 +722,14 @@ def appfactory(wsgiapp=wsgibase,
|
||||
|
||||
locker.acquire()
|
||||
cProfile.runctx('ret[0] = wsgiapp(environ, responder2)',
|
||||
globals(), locals(), profilerfilename+'.tmp')
|
||||
stat = pstats.Stats(profilerfilename+'.tmp')
|
||||
globals(), locals(), profilerfilename + '.tmp')
|
||||
stat = pstats.Stats(profilerfilename + '.tmp')
|
||||
stat.stream = cStringIO.StringIO()
|
||||
stat.strip_dirs().sort_stats("time").print_stats(80)
|
||||
profile_out = stat.stream.getvalue()
|
||||
profile_file = open(profilerfilename, 'a')
|
||||
profile_file.write('%s\n%s\n%s\n%s\n\n' % \
|
||||
('='*60, environ['PATH_INFO'], '='*60, profile_out))
|
||||
profile_file.write('%s\n%s\n%s\n%s\n\n' %
|
||||
('=' * 60, environ['PATH_INFO'], '=' * 60, profile_out))
|
||||
profile_file.close()
|
||||
locker.release()
|
||||
try:
|
||||
@@ -731,7 +741,7 @@ def appfactory(wsgiapp=wsgibase,
|
||||
environ['SERVER_PROTOCOL'],
|
||||
(status_headers[0])[:3],
|
||||
time.time() - time_in,
|
||||
)
|
||||
)
|
||||
if not logfilename:
|
||||
sys.stdout.write(line)
|
||||
elif isinstance(logfilename, str):
|
||||
@@ -766,11 +776,11 @@ class HttpServer(object):
|
||||
server_name=None,
|
||||
request_queue_size=5,
|
||||
timeout=10,
|
||||
socket_timeout = 1,
|
||||
shutdown_timeout=None, # Rocket does not use a shutdown timeout
|
||||
socket_timeout=1,
|
||||
shutdown_timeout=None, # Rocket does not use a shutdown timeout
|
||||
path=None,
|
||||
interfaces=None # Rocket is able to use several interfaces - must be list of socket-tuples as string
|
||||
):
|
||||
interfaces=None # Rocket is able to use several interfaces - must be list of socket-tuples as string
|
||||
):
|
||||
"""
|
||||
starts the web server.
|
||||
"""
|
||||
@@ -779,9 +789,9 @@ class HttpServer(object):
|
||||
# if interfaces is specified, it must be tested for rocket parameter correctness
|
||||
# not necessarily completely tested (e.g. content of tuples or ip-format)
|
||||
import types
|
||||
if isinstance(interfaces,types.ListType):
|
||||
if isinstance(interfaces, types.ListType):
|
||||
for i in interfaces:
|
||||
if not isinstance(i,types.TupleType):
|
||||
if not isinstance(i, types.TupleType):
|
||||
raise "Wrong format for rocket interfaces parameter - see http://packages.python.org/rocket/"
|
||||
else:
|
||||
raise "Wrong format for rocket interfaces parameter - see http://packages.python.org/rocket/"
|
||||
@@ -822,7 +832,7 @@ class HttpServer(object):
|
||||
logger.info('SSL is ON')
|
||||
app_info = {'wsgi_app': appfactory(wsgibase,
|
||||
log_filename,
|
||||
profiler_filename) }
|
||||
profiler_filename)}
|
||||
|
||||
self.server = rocket.Rocket(interfaces or tuple(sock_list),
|
||||
method='wsgi',
|
||||
@@ -834,7 +844,6 @@ class HttpServer(object):
|
||||
handle_signals=False,
|
||||
)
|
||||
|
||||
|
||||
def start(self):
|
||||
"""
|
||||
start the web server
|
||||
|
||||
Reference in New Issue
Block a user