forked from jeff1evesque/machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.py
87 lines (74 loc) · 3.11 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
'''@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': ''}):
# define configuration
with open(args['prefix'] + 'hiera/settings.yaml', 'r') as stream:
try:
# 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)
# 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'],
PORT_REDIS=settings['redis']['port'],
ROOT=settings['general']['root'],
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']
)
except yaml.YAMLError as error:
logger = Logger('error', 'yaml')
logger.log(error)
# 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