-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib_app.py
99 lines (82 loc) · 3.07 KB
/
lib_app.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
""" PTC-Sim's library of app-level classes.
Author: Dustin Fast, 2018
"""
import logging
import logging.handlers
from subprocess import check_output
from ConfigParser import RawConfigParser
# Import conf data
config = RawConfigParser()
config.read('app_config.dat')
APP_NAME = config.get('application', 'app_name')
REFRESH_TIME = int(config.get('application', 'refresh_time'))
WEB_EXPIRE = int(config.get('application', 'web_expire'))
LOG_LEVEL = int(config.get('logging', 'level'))
LOG_SIZE = int(config.get('logging', 'max_file_size'))
LOG_FILES = config.get('logging', 'num_files')
# Module level loggers, Declared here and defined at the end of this file.
track_log = None
broker_log = None
bos_log = None
class Logger(logging.Logger):
""" An extension of Python's logging.Logger. Implements log file rotation
and optional console output.
"""
def __init__(self,
name,
console_output=False,
level=LOG_LEVEL,
num_files=LOG_FILES,
max_filesize=LOG_SIZE):
"""
"""
# Try setting up the logger -
# If it fails, disable logging (necessary in hosted envs with no fs)
try:
logging.Logger.__init__(self, name, level)
# Define output formats
log_fmt = '%(asctime)s - %(levelname)s @ %(module)s: %(message)s'
log_fmt = logging.Formatter(log_fmt + '')
# Init log file rotation
fname = 'logs/' + name + '.log'
rotate_handler = logging.handlers.RotatingFileHandler(
fname, max_filesize, num_files)
rotate_handler.setLevel(level)
rotate_handler.setFormatter(log_fmt)
self.addHandler(rotate_handler)
if console_output:
console_fmt = '%(asctime)s - %(levelname)s @ %(module)s:'
console_fmt += '\n%(message)s'
console_fmt = logging.Formatter(console_fmt)
console_handler = logging.StreamHandler()
console_handler.setLevel(level + 10)
console_handler.setFormatter(console_fmt)
self.addHandler(console_handler)
except:
self.level = 0
self.parent = None
self.name = ''
self.disabled = True
def log(self, s):
if not self.disabled:
self.log(s)
else:
print(s)
def dep_install(module_name):
""" Prompts user to install the given module. Application quits on deny.
"""
install_str = 'pip install ' + module_name
prompt = module_name + ' is required. '
prompt += 'Install with "' + install_str + '"? (Y/n): '
do_install = raw_input(prompt)
if do_install == 'Y':
print('Installing... Please wait.')
check_output(install_str)
print('Success!\n')
else:
print('Exiting.')
exit()
# Module level loggers, Defined here and declared at the top of this file
track_log = Logger('log_track', False)
broker_log = Logger('log_broker', False)
bos_log = Logger('log_bos', False)