otime/cli.py

203 lines
8.3 KiB
Python
Executable File

#!/usr/bin/env python
import otime
import argparse
import datetime
from rich import print
from rich import inspect
from rich.console import Console
from rich.columns import Columns
from rich.table import Table
def print_runners(Runners):
table = Table(title="Runners")
table.add_column("Name", justify="right", style="cyan", no_wrap=True)
table.add_column("club", style="magenta")
table.add_column("card", style="red")
table.add_column("class", justify="right", style="green")
table.add_column("start_time", justify="right", style="green")
table.add_column("fork", justify="right", style="green")
table.add_column("Status", justify="right", style="blue")
for i in Runners:
try:
o_class = i.o_class.name
except:
o_class = ''
table.add_row(i.fullname(), i.club ,str(i.card), o_class, i.start_time, str(i.fork), i.status())
console = Console()
console.print(table)
def print_time(Runners):
table = Table(title="Time")
table.add_column("Name", justify="right", style="cyan", no_wrap=True)
table.add_column("club", style="magenta")
table.add_column("splits", style="red")
table.add_column("Check", style="green")
table.add_column("Time", justify="right", style="blue")
for i in Runners:
try:
tottime = datetime.timedelta(seconds = i.totaltime())
except:
tottime = 0
table.add_row(i.fullname(), i.o_class.name, str(i.splits), str(i.check_codes()), str(tottime))
console = Console()
console.print(table)
def print_class_result(runners, o_class):
table = Table(title=o_class.name)
table.add_column("Pos", style="red", no_wrap=True)
table.add_column("Name", justify="right", style="cyan", no_wrap=True)
table.add_column("Club", style="magenta")
table.add_column("Time", justify="right", style="blue")
pos = 0
if o_class:
runners = otime.get_runners_in_class(runners, o_class)
runners_ok = []
runners_dsq = []
for n in runners:
if n.status() == 'OK':
runners_ok.append(n)
elif n.status() == 'Disqualified':
runners_dsq.append(n)
runners_ok.sort(key=lambda x: x.totaltime())
for i in runners_ok:
#if i.status() == 'OK':
pos += 1
table.add_row(str(pos)+'.',i.fullname(), i.club, str(datetime.timedelta(seconds=i.totaltime())))
for i in runners_dsq:
table.add_row('Dsq',i.fullname(), i.club, '')
console = Console()
console.print(table)
def print_class_splits(runners, o_class):
table = Table(title=o_class.name)
table.add_column("Pos", style="red", no_wrap=True)
table.add_column("Name", justify="right", style="cyan", no_wrap=True)
table.add_column("club", style="magenta")
if o_class:
for i in o_class.course.codes:
table.add_column(str(i))
runners = otime.get_runners_in_class(runners, o_class)
runners_ok = []
runners_dsq = []
for n in runners:
if n.status() == 'OK':
runners_ok.append(n)
elif n.status() == 'Disqualified':
runners_dsq.append(n)
runners_ok.sort(key=lambda x: x.totaltime())
pos = 0
for i in runners_ok:
splits = i.res_splits()
pos += 1
list_string = map(lambda x:str(datetime.timedelta(seconds=x)), splits)
table.add_row(str(pos)+'.', i.fullname(), i.club, *list_string)
for i in runners_dsq:
splits = i.res_splits()
list_string = map(lambda x:str(datetime.timedelta(seconds=x)), splits)
table.add_row('Dsq', i.fullname(), i.club, *list_string)
console = Console()
console.print(table)
def print_o_classes(class_list):
table = Table(title="Classes")
table.add_column("Class", justify="right", style="cyan", no_wrap=True)
table.add_column("Course", style="magenta")
table.add_column("Controls", justify="right", style="green")
for i in class_list:
table.add_row(i.name, i.course.name , str(i.course.codes))
console = Console()
console.print(table)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--debug',
action='store_true',
help='Print debug info'
)
subparsers = parser.add_subparsers(dest='command')
show_runners = subparsers.add_parser('show_runners', help='show a table of runners')
show_runners.add_argument('--ttcnf', required=True, help='ttime configuration file')
show_runners.add_argument('--ttdb', required=True, help='ttime database file')
show_runners.add_argument('--mtr', required=True, help='mtr csv file')
show_runner = subparsers.add_parser('show_runner', help='show a table of runners')
show_runner.add_argument('--ttcnf', required=True, help='ttime configuration file')
show_runner.add_argument('--ttdb', required=True, help='ttime database file')
show_runner.add_argument('--mtr', required=True, help='mtr csv file')
show_runner.add_argument('--runner', required=True, help='Which runner to print')
show_runners = subparsers.add_parser('show_result', help='show a table of runners')
show_runners.add_argument('--ttcnf', required=True, help='ttime configuration file')
show_runners.add_argument('--ttdb', required=True, help='ttime database file')
show_runners.add_argument('--mtr', required=True, help='mtr csv file')
show_runners.add_argument('--class', dest='o_class_str', help='Which class to show. Prints all classes if not set')
show_runners.add_argument('--splits', action='store_true', help='Shows split times')
show_classes = subparsers.add_parser('show_classes', help='show a table of classes')
show_classes.add_argument('--ttcnf', required=True, help='ttime configuration file')
create_xml = subparsers.add_parser('create_xml', help='Create xml result file')
create_xml.add_argument('--ttcnf', required=True, help='ttime configuration file')
create_xml.add_argument('--ttdb', required=True, help='ttime database file')
create_xml.add_argument('--mtr', required=True, help='mtr csv file')
create_xml.add_argument('--file', required=True, help='Filename for result file')
args = parser.parse_args()
if args.command == 'show_runners':
event = otime.Event(0, 'NoName')
event.import_ttime_cnf(open(args.ttcnf, encoding='latin-1'))
event.import_ttime_db(open(args.ttdb, encoding='latin-1'))
event.import_mtr_file(open(args.mtr, encoding='latin-1'))
event.match_runners_cards()
print_runners(event.runners)
elif args.command == 'show_runner':
event = otime.Event(0, 'NoName')
event.import_ttime_cnf(open(args.ttcnf, encoding='latin-1'))
event.import_ttime_db(open(args.ttdb, encoding='latin-1'))
event.import_mtr_file(open(args.mtr, encoding='latin-1'))
event.match_runners_cards()
for n in event.runners:
if args.runner == n.fullname():
inspect(n)
break
else:
print('Runner not found. Use full name.')
elif args.command == 'show_result':
event = otime.Event(0, 'NoName')
event.import_ttime_cnf(open(args.ttcnf, encoding='latin-1'))
event.import_ttime_db(open(args.ttdb, encoding='latin-1'))
event.import_mtr_file(open(args.mtr, encoding='latin-1'))
event.match_runners_cards()
for n in event.o_classes:
if args.o_class_str == n.name:
sel_classes = [n]
break
else:
sel_classes = event.o_classes
for o_class in sel_classes:
if args.splits:
print_class_splits(event.runners, o_class)
else:
print_class_result(event.runners, o_class)
elif args.command == 'show_classes':
event = otime.Event(0, 'NoName')
event.import_ttime_cnf(open(args.ttcnf, encoding='latin-1'))
print_o_classes(event.o_classes)
elif args.command == 'create_xml':
event = otime.Event(0, 'NoName')
event.import_ttime_cnf(open(args.ttcnf, encoding='latin-1'))
event.import_ttime_db(open(args.ttdb, encoding='latin-1'))
event.import_mtr_file(open(args.mtr, encoding='latin-1'))
event.match_runners_cards()
event.get_xml_res().write(args.file)
if __name__ == "__main__":
main()