Startet på en pyscript side for ttime xml eksport

This commit is contained in:
Trygve 2022-06-30 12:19:16 +02:00
parent 38cc7c7672
commit 74ee5cad56
3 changed files with 146 additions and 10 deletions

View File

@ -3,8 +3,9 @@ import datetime
import csv import csv
import re import re
import json import json
import io
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from fpdf import FPDF # from fpdf import FPDF
# The event object stores all the event data. # The event object stores all the event data.
@ -70,13 +71,17 @@ class Event:
def import_ttime_cnf(self, ttime_file): def import_ttime_cnf(self, ttime_file):
self.add_course(*courses_from_ttime_conf(ttime_file)) self.add_course(*courses_from_ttime_conf(ttime_file))
ttime_file.seek(0) if isinstance(ttime_file, io.TextIOBase):
ttime_file.seek(0)
self.add_o_class(*classes_from_ttime_conf(ttime_file, self.courses)) self.add_o_class(*classes_from_ttime_conf(ttime_file, self.courses))
def import_ttime_db(self, ttime_file): def import_ttime_db(self, ttime_file):
csvreader = csv.reader(ttime_file, delimiter=';',) if type(ttime_file) == str:
f_list = ttime_file.splitlines()
elif isinstance(ttime_file, io.TextIOBase):
csvreader = csv.reader(ttime_file, delimiter=';',)
runnerarray = [] runnerarray = []
for row in csvreader: for row in f_list:
if len(row) == 0 or row[1] == '': if len(row) == 0 or row[1] == '':
continue continue
runnerarray.append(Runner.from_string(row, self.o_classes)) runnerarray.append(Runner.from_string(row, self.o_classes))
@ -128,6 +133,7 @@ class Event:
xml_child(event, 'Id', self.id) xml_child(event, 'Id', self.id)
xml_child(event, 'Name', self.name) xml_child(event, 'Name', self.name)
for i in self.o_classes: for i in self.o_classes:
print('Hmmmmmm')
# <ClassResult> # <ClassResult>
class_result = ET.SubElement(root, 'ClassResult') class_result = ET.SubElement(root, 'ClassResult')
# <Class> # <Class>
@ -456,7 +462,7 @@ class Event:
pdf.output(file_name) pdf.output(file_name)
"""
class PDF(FPDF): class PDF(FPDF):
def footer(self): def footer(self):
self.set_y(-15) self.set_y(-15)
@ -467,7 +473,7 @@ class PDF(FPDF):
self.cell(col_width, 7, 'oTime', border=1, align='L', fill=True) self.cell(col_width, 7, 'oTime', border=1, align='L', fill=True)
self.cell(col_width, 7, datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S'), border=1, align='C', fill=True) self.cell(col_width, 7, datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S'), border=1, align='C', fill=True)
self.cell(col_width, 7, f"Side {self.page_no()} av {{nb}}", border=1, align='R', fill=True) self.cell(col_width, 7, f"Side {self.page_no()} av {{nb}}", border=1, align='R', fill=True)
"""
# The runner object stores all the data specific to a runner. # The runner object stores all the data specific to a runner.
class Runner: class Runner:
def __init__(self, runner_id, first, last, club=None, club_id=None, def __init__(self, runner_id, first, last, club=None, club_id=None,
@ -667,11 +673,13 @@ class CardDump:
return(CardDump(card, controls, splits, read_time, s_time, f_time)) return(CardDump(card, controls, splits, read_time, s_time, f_time))
def list_from_mtr_f(mtr_f): def list_from_mtr_f(mtr_f):
csvreader = csv.reader(mtr_f) if type(mtr_f) == str:
f_list = mtr_f.splitlines()
rows = [] rows = []
cards = [] cards = []
# hver rad er brikkenummer med tilhørende info # hver rad er brikkenummer med tilhørende info
csvreader = csv.reader(f_list)
for row in csvreader: for row in csvreader:
if len(row) == 0: if len(row) == 0:
continue continue
@ -779,8 +787,12 @@ class Fee:
# TODO: Take string instead of file. # TODO: Take string instead of file.
def courses_from_ttime_conf(ttime_file): def courses_from_ttime_conf(ttime_file):
if type(ttime_file) == str:
conf = ttime_file.splitlines()
elif isinstance(ttime_file, io.TextIOBase):
conf = ttime_file.readlines()
courses = [] courses = []
conf = ttime_file.readlines()
for line in conf: for line in conf:
if '-codes' in line: if '-codes' in line:
code_list = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';') code_list = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
@ -793,8 +805,11 @@ def courses_from_ttime_conf(ttime_file):
return courses return courses
def classes_from_ttime_conf(ttime_file, courses): def classes_from_ttime_conf(ttime_file, courses):
if type(ttime_file) == str:
conf = ttime_file.splitlines()
elif isinstance(ttime_file, io.TextIOBase):
conf = ttime_file.readlines()
o_classes = [] o_classes = []
conf = ttime_file.readlines()
for line in conf: for line in conf:
if '-courses' in line: if '-courses' in line:
raw_courselist = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';') raw_courselist = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')

55
web/index.html Executable file
View File

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>File System Examples</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- paths:
- ./otime.py
- ./ps_ttime.py
</py-env>
</head>
<label for="tt-cnf">tTime konfigurasjons-fil:</label>
<input type="file" id="tt-cnf" name="tt-cnf">
<div style="border:2px inset #AAA;cursor:text;height:50px;overflow:auto;width:600px; resize:both">
<div id="tt-cnf-cnt">
</div>
</div>
<br>
<label for="tt-db">tTime database:</label>
<input type="file" id="tt-db" name="tt-db">
<div style="border:2px inset #AAA;cursor:text;height:50px;overflow:auto;width:600px; resize:both">
<div id="tt-db-cnt">
</div>
</div>
<br>
<label for="mtr">MTR logfil:</label>
<input type="file" id="mtr" name="mtr">
<div style="border:2px inset #AAA;cursor:text;height:50px;overflow:auto;width:600px; resize:both">
<div id="mtr-cnt">
</div>
</div>
<button type="button" id="xml-btn">Lag xml resultater!</button>
<p>File Content:</p>
<div style="border:2px inset #AAA;cursor:text;height:50px;overflow:auto;width:600px; resize:both">
<div id="xml-cnt">
</div>
</div>
<py-script>
from ps_ttime import main
main()
</py-script>
</body>
</html>

66
web/ps_ttime.py Normal file
View File

@ -0,0 +1,66 @@
import otime
import asyncio
from js import document, FileReader
from pyodide import create_proxy
import xml.etree.ElementTree as ET
global event
event = otime.Event(0, 'PlaceHolder')
async def process_conf(f_event):
global event
file_list = f_event.target.files.to_py()
for f in file_list:
data = await f.text()
document.getElementById('tt-cnf-cnt').innerHTML = data
event.import_ttime_cnf(data)
for c in event.o_classes:
print(c.name, c.course.name)
async def process_db(f_event):
global event
file_list = f_event.target.files.to_py()
for f in file_list:
data = await f.text()
document.getElementById('tt-db-cnt').innerHTML = data
event.import_ttime_db(data)
async def process_mtr(f_event):
global event
file_list = f_event.target.files.to_py()
for f in file_list:
data = await f.text()
document.getElementById('mtr-cnt').innerHTML = data
event.import_mtr_file(data)
async def create_xml(f_event):
global event
event.match_all()
xml_res = ET.tostring(event.get_xml_res().getroot(), encoding='unicode', method='xml', xml_declaration=True)
#xml_res = dir(event.get_xml_res())
document.getElementById('xml-cnt').innerHTML = xml_res.replace('&', '&amp').replace('<', '&lt').replace('>', '&gt')
def main():
global event
# Create a Python proxy for the callback function
# process_file() is your function to process events from FileReader
cnf_f_event = create_proxy(process_conf)
ec = document.getElementById("tt-cnf")
ec.addEventListener("change", cnf_f_event, False)
db_f_event = create_proxy(process_db)
ed = document.getElementById("tt-db")
ed.addEventListener("change", db_f_event, False)
mtr_f_event = create_proxy(process_mtr)
em = document.getElementById("mtr")
em.addEventListener("change", mtr_f_event, False)
xml_btn_call = create_proxy(create_xml)
eb = document.getElementById("xml-btn")
eb.addEventListener("click", xml_btn_call, False)