[python-env] PEP8 Fixes for server.py (#1034)
Fixes PEP8 errors for environments/python/server.py
This commit is contained in:
committed by
Ta-Ching Chen
parent
8e6f6ffda9
commit
3f4eb49c2f
@@ -8,8 +8,9 @@ import bjoern
|
|||||||
from gevent.pywsgi import WSGIServer
|
from gevent.pywsgi import WSGIServer
|
||||||
from flask import Flask, request, abort, g
|
from flask import Flask, request, abort, g
|
||||||
|
|
||||||
|
|
||||||
class FuncApp(Flask):
|
class FuncApp(Flask):
|
||||||
def __init__(self, name, loglevel = logging.DEBUG):
|
def __init__(self, name, loglevel=logging.DEBUG):
|
||||||
super(FuncApp, self).__init__(name)
|
super(FuncApp, self).__init__(name)
|
||||||
|
|
||||||
# init the class members
|
# init the class members
|
||||||
@@ -24,7 +25,8 @@ class FuncApp(Flask):
|
|||||||
#
|
#
|
||||||
self.root.setLevel(loglevel)
|
self.root.setLevel(loglevel)
|
||||||
self.ch.setLevel(loglevel)
|
self.ch.setLevel(loglevel)
|
||||||
self.ch.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
self.ch.setFormatter(logging.Formatter(
|
||||||
|
'%(asctime)s - %(levelname)s - %(message)s'))
|
||||||
self.logger.addHandler(self.ch)
|
self.logger.addHandler(self.ch)
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -42,28 +44,30 @@ class FuncApp(Flask):
|
|||||||
body = request.get_json()
|
body = request.get_json()
|
||||||
filepath = body['filepath']
|
filepath = body['filepath']
|
||||||
handler = body['functionName']
|
handler = body['functionName']
|
||||||
|
|
||||||
# The value of "functionName" is consist of `<module-name>.<function-name>`.
|
# The value of "functionName" is consist of
|
||||||
|
# `<module-name>.<function-name>`.
|
||||||
moduleName, funcName = handler.split(".")
|
moduleName, funcName = handler.split(".")
|
||||||
|
|
||||||
# check whether the destination is a directory or a file
|
# check whether the destination is a directory or a file
|
||||||
if os.path.isdir(filepath):
|
if os.path.isdir(filepath):
|
||||||
# add package directory path into module search path
|
# add package directory path into module search path
|
||||||
sys.path.append(filepath)
|
sys.path.append(filepath)
|
||||||
|
|
||||||
# find module from package path we append previously.
|
# find module from package path we append previously.
|
||||||
# Python will try to find module from the same name file under
|
# Python will try to find module from the same name
|
||||||
# the package directory. If search is successful, the return
|
# file under the package directory. If search is
|
||||||
# value is a 3-element tuple; otherwise, an exception "ImportError"
|
# successful, the return value is a 3-element tuple;
|
||||||
# is raised.
|
# otherwise, an exception "ImportError" is raised.
|
||||||
# Second parameter of find_module enforces python to find same
|
# Second parameter of find_module enforces python to
|
||||||
# name module from the given list of directories to prevent name
|
# find same name module from the given list of
|
||||||
# confliction with built-in modules.
|
# directories to prevent name confliction with
|
||||||
|
# built-in modules.
|
||||||
f, path, desc = imp.find_module(moduleName, [filepath])
|
f, path, desc = imp.find_module(moduleName, [filepath])
|
||||||
|
|
||||||
# load module
|
# load module
|
||||||
# Return module object is the load is successful; otherwise,
|
# Return module object is the load is successful;
|
||||||
# an exception is raised.
|
# otherwise, an exception is raised.
|
||||||
try:
|
try:
|
||||||
mod = imp.load_module(moduleName, f, path, desc)
|
mod = imp.load_module(moduleName, f, path, desc)
|
||||||
finally:
|
finally:
|
||||||
@@ -72,30 +76,35 @@ class FuncApp(Flask):
|
|||||||
else:
|
else:
|
||||||
# load source from destination python file
|
# load source from destination python file
|
||||||
mod = imp.load_source(moduleName, filepath)
|
mod = imp.load_source(moduleName, filepath)
|
||||||
|
|
||||||
# load user function from module
|
# load user function from module
|
||||||
self.userfunc = getattr(mod, funcName)
|
self.userfunc = getattr(mod, funcName)
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@self.route('/healthz', methods=['GET'])
|
@self.route('/healthz', methods=['GET'])
|
||||||
def healthz():
|
def healthz():
|
||||||
return "", 200
|
return "", 200
|
||||||
|
|
||||||
@self.route('/', methods=['GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'DELETE'])
|
@self.route('/', methods=['GET', 'POST', 'PUT', 'HEAD', 'OPTIONS',
|
||||||
|
'DELETE'])
|
||||||
def f():
|
def f():
|
||||||
if self.userfunc == None:
|
if self.userfunc is None:
|
||||||
print("Generic container: no requests supported")
|
print("Generic container: no requests supported")
|
||||||
abort(500)
|
abort(500)
|
||||||
#
|
#
|
||||||
# Customizing the request context
|
# Customizing the request context
|
||||||
#
|
#
|
||||||
# If you want to pass something to the function, you can add it to 'g':
|
# If you want to pass something to the function, you can
|
||||||
|
# add it to 'g':
|
||||||
# g.myKey = myValue
|
# g.myKey = myValue
|
||||||
# And the user func can then access that (after doing a "from flask import g").
|
|
||||||
#
|
#
|
||||||
|
# And the user func can then access that
|
||||||
|
# (after doing a"from flask import g").
|
||||||
|
|
||||||
return self.userfunc()
|
return self.userfunc()
|
||||||
|
|
||||||
|
|
||||||
app = FuncApp(__name__, logging.DEBUG)
|
app = FuncApp(__name__, logging.DEBUG)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|||||||
Reference in New Issue
Block a user