From 568c935a5573560af437356c5c5aadbc8f11cb5b Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 21 Nov 2023 22:32:14 +0100 Subject: [PATCH] La til git og fargelegging --- otime/cli.py | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/otime/cli.py b/otime/cli.py index d63ab37..bef3ef2 100644 --- a/otime/cli.py +++ b/otime/cli.py @@ -2,13 +2,19 @@ import argparse import file_io import iof_xml import serial +import subprocess +import os import otime +import iof_xml +import pdf +from pdf import format_m_s +from rich import print # Main entrypoint for now. Cli with two options; init will make the files needed and run will start the program from the specified directory def main(): parser = argparse.ArgumentParser(description='Otime very alpha version 😁') subparsers = parser.add_subparsers(dest='command') - parser_init = subparsers.add_parser('init', help='INIT') + parser_init = subparsers.add_parser('init', help='Create project files') parser_init.add_argument('--entries', required=True, dest='entries_file', action='store', default='./', help='The xml file with entries') parser_init.add_argument('--courses', required=True, dest='courses_file', action='store', default='./', @@ -19,14 +25,18 @@ def main(): parser_init = subparsers.add_parser('run', help='run otime') parser_init.add_argument('--dir', required=False, dest='dir', action='store', default='./', help='specify a directory') parser_init.add_argument('--port', required=False, dest='port', action='store', help='specify a serial port') - + parser_init.add_argument('--xml', required=False, dest='xml_path', action='store', default=None, help='Where the xml result file should be saved') args = parser.parse_args() + + if not args.xml_path: + args.xml_path = args.dir + '/output' + match args.command: case 'init': init_dir(args.dir, args.entries_file, args.courses_file) case 'run': - run(args.port, args.dir) + run(args.port, args.dir, args.xml_path) def init_dir(project_dir, entries_xml_file, courses_xml_file): # Lager mappe med en config fil, en csv fil med løpere og en fil med mtr data @@ -36,16 +46,24 @@ def init_dir(project_dir, entries_xml_file, courses_xml_file): event = iof_xml.event_from_xml_entries(entries_xml_file) event.courses = iof_xml.courses_from_xml(courses_xml_file) + + print(f'Read {len(event.runners)} runners, {len(event.card_dumps)} ecards, {len(event.courses)} courses and {len(event.o_classes)} classes') + print('Remember to manually link the courses and classes in the config file') file_io.write_runners_csv(event, csv_db_path) file_io.write_config(event, config_path) file_io.write_card_dumps(event, mtr_path) + os.makedirs(project_dir+'/output') -def run(port='/dev/ttyUSB0', dir='./'): + subprocess.run(['git', 'init', project_dir]) + subprocess.run(['git', 'add', './*'], cwd=project_dir) + subprocess.run(['git', 'commit', '-m', f'Project initiated by otime'], cwd=project_dir) + +def run(port='/dev/ttyUSB0', project_dir='./', xml_path='./output/'): mtr = serial.Serial(port=port, baudrate=9600, timeout=40) - config_path = dir + '/config.yaml' - mtr_path = dir + '/mtr.yaml' - csv_path = dir + '/runners.csv' + config_path = project_dir + '/config.yaml' + mtr_path = project_dir + '/mtr.yaml' + csv_path = project_dir + '/runners.csv' while True: if mtr.in_waiting > 0: mtr.read_until(expected=b'\xFF\xFF\xFF\xFF') @@ -58,16 +76,30 @@ def run(port='/dev/ttyUSB0', dir='./'): event.card_dumps.append(card_dump) file_io.write_card_dumps(event, mtr_path) print(runner_info(event, card_dump)) + subprocess.run(['git', 'add', './*'], cwd=project_dir, stdout=subprocess.DEVNULL) + subprocess.run(['git', 'commit', '-m', f'Added {card_dump.card}'], cwd=project_dir, stdout=subprocess.DEVNULL) + + iof_xml.create_result_file(event, xml_path + '/results.xml') + pdf.create_result_list(event, project_dir + '/output/results.pdf') + elif size == b'\x37': meat = mtr.read(55) inspect(status) def runner_info(event, card_dump): runner = next((i for i in event.runners if str(i.card_id) == str(card_dump.card)), None) - if runner: - return f'{runner.fullname()}, {runner.o_class}, {runner.club}, {event.get_runner_status(runner.id)}, {event.get_runner_time(runner.id)}' - else: + if runner is None: return card_dump + result = event.get_runner_result(runner.id) + if result is None: + return '🧐 Dette skal ikke skje...' + if result.status == 'OK': + result_text = f'[green]Place: {result.place}. Time: [bold][yellow]{format_m_s(result.total_time)}[/yellow][/bold][/green]' + elif result.status == 'MissingPunch': + result_text = f'[red]Missed Control! Time: [bold][yellow]{format_m_s(result.total_time)}[/yellow][/bold][/red]' + else: + result_text = f'[blue]{result.status}, Time: [bold][yellow]{result.total_time}[/yellow][/bold][/blue]' + return result_text + f' [blue]{result.fullname()}[/blue], {result.o_class}, {result.club}, [italic]Card: {result.card_id}[/italic]' if __name__ == '__main__': main() \ No newline at end of file