Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def import_apps():
"""Import or reimport modules and exposed static files"""
folder = os.environ["PY4WEB_APPS_FOLDER"]
app = bottle.default_app()
app.routes.clear()
new_apps = []
# if first time reload dummy top module
if not Reloader.MODULES:
path = os.path.join(folder, "__init__.py")
module = importlib.machinery.SourceFileLoader("apps", path).load_module()
# Then load all the apps as submodules
for app_name in os.listdir(folder):
action.app_name = app_name
new_apps += Reloader.import_app(app_name)
# Expose static files with support for static asset management
for path in new_apps:
static_folder = os.path.join(path, "static")
if os.path.exists(static_folder):
app_name = path.split(os.path.sep)[-1]
prefix = "" if app_name == "_default" else ("/%s" % app_name)
@bottle.route(prefix + "/static/")
@bottle.route(
prefix + "/static/_/"
)
def server_static(filename, static_folder=static_folder, version=None):
return bottle.static_file(filename, root=static_folder)
# Register routes list
routes = []
def api(path):
# this is not final, requires pydal 19.5
args = path.split("/")
app_name = args[0]
from py4web.core import Reloader, DAL
from pydal.restapi import RestAPI, ALLOW_ALL_POLICY, DENY_ALL_POLICY
if MODE == "full":
policy = ALLOW_ALL_POLICY
else:
policy = DENY_ALL_POLICY
module = Reloader.MODULES[app_name]
def url(*args):
return request.url + "/" + "/".join(args)
databases = [
name for name in dir(module) if isinstance(getattr(module, name), DAL)
]
if len(args) == 1:
def tables(name):
db = getattr(module, name)
return [
{
"name": t._tablename,
"fields": t.fields,
"link": url(name, t._tablename) + "?model=true",
name
for name in sys.modules
if (name + ".").startswith(module_name + ".")
]
for name in names:
try:
importlib.reload(sys.modules[name])
except ModuleNotFoundError:
pass
print("\x1b[A[X] reloaded %s " % app_name)
Reloader.MODULES[app_name] = module
Reloader.ERRORS[app_name] = None
except:
tb = traceback.format_exc()
print("\x1b[A[FAILED] loading %s \n%s\n" % (app_name, tb))
Reloader.ERRORS[app_name] = tb
return new_apps
new_apps.append(path)
print("\x1b[A[X] loaded %s " % app_name)
else:
print("[ ] reloading %s ..." % app_name)
names = [
name
for name in sys.modules
if (name + ".").startswith(module_name + ".")
]
for name in names:
try:
importlib.reload(sys.modules[name])
except ModuleNotFoundError:
pass
print("\x1b[A[X] reloaded %s " % app_name)
Reloader.MODULES[app_name] = module
Reloader.ERRORS[app_name] = None
except:
tb = traceback.format_exc()
print("\x1b[A[FAILED] loading %s \n%s\n" % (app_name, tb))
Reloader.ERRORS[app_name] = tb
return new_apps
if not filename.count(os.sep)
else filename + ".py"
)
return filename
for route in app.routes:
func = route.callback
routes.append(
{
"rule": route.rule,
"method": route.method,
"filename": to_filename(func.__module__),
"action": func.__name__,
}
)
Reloader.ROUTES = sorted(routes, key=lambda item: item["rule"])
ICECUBE.update(threadsafevariable.ThreadSafeVariable.freeze())
def routes():
"""Returns current registered routes"""
return {"payload": Reloader.ROUTES, "status": "success"}