forked from jeff1evesque/machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.py
114 lines (97 loc) · 4 KB
/
factory.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
113
114
'''@factory
This file is the acting web server.
@debug, enables debugging, and tracebacks
@host, tells the OS (guest VM) to accept connections from all public IP
addresses.
Note: both the handler, and logger has levels. If the level of the logger is
higher than that of the handler, no messages will be handled by the
corresponding handler.
'''
import yaml
import logging
from log.logger import Logger
from logging.handlers import RotatingFileHandler
from flask import Flask
from interface.views import blueprint
# application factory
def create_app(args={'prefix': '', 'settings': ''}):
# path to hiera
if args['prefix']:
path = 'hiera/' + args['prefix'] + '/hiera/settings.yaml'
else:
path = 'hiera/settings.yaml'
try:
# define configuration
with open(path, 'r') as stream:
# local variables
if args['settings']:
app = Flask(
__name__,
args['settings'],
template_folder='interface/templates',
static_folder='interface/static',
)
else:
app = Flask(
__name__,
template_folder='interface/templates',
static_folder='interface/static',
)
settings = yaml.load(stream)
# secret key: used for maintaining flask sessions
app.secret_key = settings['application']['security_key']
# register blueprint
app.register_blueprint(blueprint)
# local logger: used for this module
root = settings['general']['root']
LOG_PATH = root + settings['webserver']['flask']['log_path']
HANDLER_LEVEL = settings['application']['log_level']
# flask attributes: accessible across application
app.config.update(
HOST=settings['general']['host'],
REDIS_HOST=settings['redis']['host'],
REDIS_PORT=settings['redis']['port'],
ROOT=settings['general']['root'],
DB_HOST=settings['database']['host'],
DB_LOG_PATH=settings['database']['log_path'],
DB_ML=settings['database']['name'],
DB_USERNAME=settings['database']['username'],
DB_PASSWORD=settings['database']['password'],
LOG_LEVEL=HANDLER_LEVEL,
FLASK_LOG_PATH=settings['webserver']['flask']['log_path'],
ERROR_LOG_PATH=settings['application']['error_log_path'],
WARNING_LOG_PATH=settings['application']['warning_log_path'],
INFO_LOG_PATH=settings['application']['info_log_path'],
DEBUG_LOG_PATH=settings['application']['debug_log_path'],
MODEL_TYPE=settings['application']['model_type'],
SALT_LENGTH=settings['crypto']['salt_length'],
SCRYPT_N=settings['crypto']['scrypt_n'],
SCRYPT_R=settings['crypto']['scrypt_r'],
SCRYPT_P=settings['crypto']['scrypt_p'],
PASSWORD_MIN_C=settings['validate_password']['password_min_c'],
PASSWORD_MAX_C=settings['validate_password']['password_max_c'],
USER_ID=0
)
# log handler: requires the below logger
formatter = logging.Formatter(
"[%(asctime)s] {%(pathname)s:%(lineno)d} "
"%(levelname)s - %(message)s"
)
handler = RotatingFileHandler(
LOG_PATH,
maxBytes=10000000,
backupCount=5
)
handler.setLevel(HANDLER_LEVEL)
handler.setFormatter(formatter)
app.logger.addHandler(handler)
# logger: complements the log handler
log = logging.getLogger('werkzeug')
log.setLevel(logging.DEBUG)
log.addHandler(handler)
# return
return app
except Exception as error:
logger = Logger('error', 'yaml')
logger.log(error)
raise