bicycle-statistics/bicycle_statistics/__main__.py
2019-08-15 13:00:44 +02:00

89 lines
2.4 KiB
Python

import argparse
import datetime
import logging
import sys
import threading
import time
from gpx2html import Gpx2Html
log_level = logging.INFO
LOG_FILE = "/var/log/bicycle-statistics.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
UPDATE_INTERVAL = 60
# logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE)
logging.basicConfig(format=LOG_FORMAT, level=log_level)
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
def run(self):
gpx2html = Gpx2Html(self.infolder, self.outfolder, log)
gpx2html.update()
next_update = datetime.datetime.now() + datetime.timedelta(seconds=UPDATE_INTERVAL)
while self.run_condition is True:
now = datetime.datetime.now()
if now >= next_update:
gpx2html.update()
next_update = now + datetime.timedelta(seconds=UPDATE_INTERVAL)
else:
time.sleep(1)
def stop(self):
self.run_condition = False
self.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()
return 0
if __name__ == '__main__':
sys.exit(main())