bicycle-statistics/bicycle_statistics/__main__.py
Thomas Klaehn 629794d5f2 bicycle-statistics: Add logger
Signed-off-by: Thomas Klaehn <thomas.klaehn@u-blox.com>
2018-07-30 13:56:03 +02:00

98 lines
2.5 KiB
Python

import argparse
import logging
import sys
import threading
import time
from watchdog.observers import Observer
from gpx2html import Gpx2Html
from input_observer import InputObserver
log_level = logging.INFO
LOG_FILE = "/var/log/bicycle-statistics.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE)
log = logging.getLogger('bicycle-statistics')
def parse_args():
'''Shell argument parser.'''
parser = argparse.ArgumentParser()
parser.add_argument('infolder', help='Specify the in folder.')
parser.add_argument('outfolder', help='Specify the out folder.')
parser.add_argument('-l', '--loglevel', help='Set log level to one of \
"CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET". Default is \
"INFO".')
return parser.parse_args()
class myLoop(threading.Thread):
def __init__(self, infolder, outfolder):
super(myLoop, self).__init__()
self.run_condition = True
self.infolder = infolder
self.outfolder = outfolder
self.observer = Observer()
self.in_obs = InputObserver(patterns=["*.gpx"])
def run(self):
gpx2html = Gpx2Html(self.infolder, self.outfolder)
gpx2html.process()
self.observer.schedule(self.in_obs, self.infolder)
self.observer.start()
while self.run_condition is True:
new_file = self.in_obs.get_new_file()
log.info("new file triggered: {}".format(new_file))
if new_file:
gpx2html.process()
log.info("new file processed: {}".format(new_file))
def stop(self):
self.run_condition = False
self.in_obs.stop()
self.observer.stop()
self.observer.join()
def set_log_level(level):
global log_level
if level == 'CRITICAL':
log_level = logging.CRITICAL
if level == 'ERROR':
log_level = logging.ERROR
if level == 'WARNING':
log_level = logging.WARNING
if level == 'INFO':
log_level = logging.INFO
if level == 'DEBUG':
log_level = logging.DEBUG
if level == 'NOTSET':
log_level = logging.NOTSET
log.setLevel(level=log_level)
def main():
args = parse_args()
if args.loglevel:
set_log_level(args.loglevel)
my_loop = myLoop(args.infolder, args.outfolder)
my_loop.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_loop.stop()
my_loop.join()
return 0
if __name__ == '__main__':
sys.exit(main())