otime-testing/event_mgr.py

121 lines
3.9 KiB
Python

#!/usr/bin/env python
import otime
#import mtr_log_extractor as mle
import json
import pickle
import asyncio
import argparse
from difflib import Differ
from watchgod import awatch
from config import config
from rich import print
from rich import inspect
from rich.console import Console
from rich.columns import Columns
from rich.table import Table
def save(obj):
f = open(config['otime_file'], "wb")
pickle.dump(obj,f)
f.close()
def start_parse():
parser = argparse.ArgumentParser(description='Event manager')
subparsers = parser.add_subparsers(dest='command')
open_event = subparsers.add_parser('continue', help='Open pickle')
open_event.add_argument('--file', help='.otime file')
args = parser.parse_args()
if args.command == 'continue':
print('Later ' + args.file)
else:
print('Starter fra scratch')
start_event()
def start_event():
global event
event = otime.event(0, config['event_name'])
event.import_ttime_cnf('/home/trygve/Documents/sprintcup040330/sprintcup040330.cnf')
event.import_ttime_db(config['db_file'])
event.import_mtr_file('/home/trygve/Documents/sprintcup040330/sprintcup040330.log')
event.match_runners_cards()
save(event)
global db_file
global db_file_u
db_file = open(config['db_file'], 'r', encoding='latin_1').read().splitlines()
def load_event(ot_file):
global event
f = open(ot_file, "r")
event = pickle.load(f)
f.close()
global db_file
global db_file_u
db_file = open(config['db_file'], 'r', encoding='latin_1').read().splitlines()
async def main():
async for changes in awatch(config['db_file']):
global event
global db_file
global db_file_u
db_file_u = open(config['db_file'], 'r', encoding='latin_1').read().splitlines()
d = Differ()
result = list(d.compare(db_file, db_file_u))
db_file = db_file_u
added_raw = []
removed_raw = []
added = []
removed = []
changed = []
for line in result:
if line[:1] == '+':
#print(line)
clean = str(line).replace('+ ', '').split(';')
runner = otime.runner.from_string(clean, event.o_classes)
inspect(runner)
if runner.firstname:
added_raw.append(runner)
elif line[:1] == '-':
#print(line)
clean = str(line).replace('- ', '').split(';')
runner = otime.runner.from_string(clean, event.o_classes)
if runner.firstname:
removed_raw.append(runner)
for plus in added_raw:
for index, minus in enumerate(removed_raw):
if plus.id == minus.id:
changed.append(plus)
removed_raw[index] = plus
break
added = [x for x in added_raw if x not in changed]
removed = [x for x in removed_raw if x not in changed]
#print(len(added_raw),len(removed_raw))
#print(len(added),len(removed),len(changed))
event.runners.extend(added)
for n in added:
print(f'[green]Added: {n.fullname()}, {n.o_class.name}, {n.card}[green]')
for r in removed:
for n in event.runners:
if n.id == r.id:
print(f'[red]Removed: {n.fullname()}, {n.o_class.name}, {n.card}')
event.runners.remove(n)
for change in changed:
for i, n in enumerate(event.runners):
if n.id == runner.id:
event.runners[i] = runner
print(f'[blue]Changed: {runner.fullname()}, {runner.o_class.name}, {runner.card}')
event.match_runners_cards()
event.get_xml_res().write(config['xml_res_file'])
db_file_u = None
if __name__ == "__main__":
start_parse()
asyncio.run(main())