-
Notifications
You must be signed in to change notification settings - Fork 19
/
silpa.py
112 lines (90 loc) · 3.75 KB
/
silpa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from flask import Flask
from logging import handlers, Formatter
from webbridge import WebBridge
from core.modulehelper import enabled_modules, BASEURL, modules
from jinja2 import PackageLoader, ChoiceLoader
import loadconfig
import logging
import os
def register_url():
'''
Function all form of URL which will be handled by SILPA service
This function actually make this flask front end of SILPA independent
of any modules. It doesn't know what modules are present nor doesn't
know how to handle modules all the request will be promptly handed over
to WebBridge
'''
# / or /baseurl for index page
baseurl = '/' if BASEURL == '/' else BASEURL
app.logger.debug("Registering the URL:{0}".format(baseurl))
app.add_url_rule(baseurl, view_func=WebBridge.as_view(baseurl))
# License page
app.logger.debug("Registering the URL:{0}".format(baseurl+"License"))
app.add_url_rule(baseurl+"License", view_func=WebBridge.as_view("license"))
# Credits Page
app.logger.debug("Registering the URL:{0}".format(baseurl+"Credits"))
app.add_url_rule(baseurl+"Credits", view_func=WebBridge.as_view("credits"))
# Contacts Page
app.logger.debug("Registering the URL:{0}".format(baseurl+"Contact"))
app.add_url_rule(baseurl+"Contact", view_func=WebBridge.as_view("contact"))
# Register all enabled modules
# baseurl/modulenames['module']
for module in enabled_modules:
module_url = baseurl + "/" + module if not baseurl == "/" \
else baseurl + module
app.logger.debug("Registering the URL:{0}".format(module_url))
app.add_url_rule(module_url, view_func=WebBridge.as_view(module_url))
# JSONRPC url
jsonrpc_url = (baseurl + "/JSONRPC" if not baseurl == "/"
else baseurl + "JSONRPC")
app.logger.debug("Registering the URL:{0}".format(baseurl))
app.add_url_rule(jsonrpc_url, view_func=WebBridge.as_view(jsonrpc_url))
def add_templates():
templates = [app.jinja_loader]
for key in modules.keys():
if modules.get(key) == 'yes':
templates.append(PackageLoader(key))
app.jinja_loader = ChoiceLoader(templates)
def configure_logging():
'''
This function configures logging for the SILPA applications using Flask's
internal logger.
For now log file will be rotated 7 days once and 4 backups will be kept.
This can't be modified using configuration file as of now.
Default logging level will be ERROR and can be modified from
configuration file. Log folder and file name can also be configured using
configuration file but make sure the path you give is writable for
Webserver user, otherwise this will lead to an error.
'''
log_level = loadconfig.get('log_level')
log_folder = loadconfig.get('log_folder')
log_name = loadconfig.get('log_name')
filename = os.path.join(log_folder, log_name)
handler = handlers.TimedRotatingFileHandler(filename, when='D',
interval=7, backupCount=4)
level = logging.ERROR
if log_level == "debug":
level = logging.DEBUG
elif log_level == "info":
level = logging.INFO
elif log_level == "warn":
level = logging.WARNING
elif log_level == "error":
level = logging.ERROR
handler.setLevel(level)
handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s \
[in %(pathname)s %(lineno)d]'))
app.logger.setLevel(level)
app.logger.addHandler(handler)
DEBUG = False
# Basics
app = Flask(__name__)
app.config.from_object(__name__)
# Logging
configure_logging()
# Register URL's
register_url()
# adds templates from imported modules
add_templates()
if __name__ == '__main__':
app.run('0.0.0.0')