Fix parsinf for some ESV mails

This commit is contained in:
Christian Wolf 2025-04-27 12:23:15 +02:00
parent 6ccd4fa0c8
commit ff88ca056f
Signed by: christian
GPG Key ID: AB6DF7467D2738F2
2 changed files with 33 additions and 19 deletions

View File

@ -4,7 +4,7 @@ import re
import os
import jinja2
class ParsingFailedEception(Exception):
class ParsingFailedException(Exception):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -30,9 +30,16 @@ class CompetitionParser:
self._reNumber = re.compile('Turnier: ([0-9]+)')
self._rePhone = re.compile('Telefon: (\\+?[0-9 /-]+)')
self._rePlace = re.compile('Ort: (.*), (.*)')
self._reCompetition = re.compile('(.*) ([A-ES]) ((?:Std)|(?:Lat)|(?:Kombi))')
self._reCompetition = re.compile('(.*) ([A-ES]) ((?:Std)|(?:Lat)|(?:Kombi)|(?:Kmb))')
self._reCompetitionSectionMap = {
'Std': 'Std',
'Lat': 'Lat',
'Kombi': 'Kombi',
'Kmb': 'Kombi',
}
self._reWDSFCompetition = re.compile('WDSF Open ([a-zA-Z0-9 ]*) ((?:Standard)|(?:Latin))(?: *-.*)?')
self._reWDSFCompetitionReversed = re.compile('WDSF Open ((?:Standard)|(?:Latin)) ([a-zA-Z0-9 ]*)(?: *-.*)?')
self._reGOCCompetition = re.compile('GOC ([a-zA-Z0-9 ]*) ((?:Standard)|(?:Latin))(?: (?:Rising Stars))?')
self._reCleaningString = re.compile('[^a-z0-9-]')
self._reDashes = re.compile('-+')
@ -46,7 +53,7 @@ class CompetitionParser:
matcher = self._reName.match(h2.string)
if matcher is None:
self._l.error('Parsing of header "%s" failed.', h2)
raise ParsingFailedEception('Header could not be successfully parsed')
raise ParsingFailedException('Header could not be successfully parsed')
self._partner = matcher.group(1)
self._partnerin = matcher.group(2)
@ -54,38 +61,43 @@ class CompetitionParser:
def parseDate(date):
match = self._reDate.fullmatch(date)
if match is None:
raise ParsingFailedEception('Cannot parse date %s in mail' % date)
raise ParsingFailedException('Cannot parse date %s in mail' % date)
self._date = f'{match.group(3)}-{match.group(2)}-{match.group(1)}'
def parseNumber(content):
match = self._reNumber.fullmatch(content)
if match is None:
raise ParsingFailedEception(f'Cannot parse the turnier number in field {content}')
raise ParsingFailedException(f'Cannot parse the turnier number in field {content}')
self._number = match.group(1)
def parseCompetition(competition):
def parseDTVCompetition():
match = self._reCompetition.fullmatch(competition)
if match is None:
raise ParsingFailedEception(f'Cannot parse the competition line {competition}')
raise ParsingFailedException(f'Cannot parse the competition line {competition}')
self._group = match.group(1)
self._class = match.group(2)
self._section = match.group(3)
self._section = self._reCompetitionSectionMap[match.group(3)]
def parseWDSFCompetition():
def checkMatch(match):
if match is None:
raise ParsingFailedEception(f'Cannot parse WDSF competition line')
raise ParsingFailedException(f'Cannot parse WDSF competition line')
def parseForward():
match = self._reWDSFCompetition.fullmatch(competition.strip())
checkMatch(match)
return match.group(2), match.group(1)
return match.group(2), match.group(1), 'WDSF Open'
def parseReverse():
match = self._reWDSFCompetitionReversed.fullmatch(competition.strip())
checkMatch(match)
return match.group(1), match.group(2).strip()
return match.group(1), match.group(2).strip(), 'WDSF Open'
def parseGOC():
match = self._reGOCCompetition.fullmatch(competition.strip())
checkMatch(match)
return match.group(2), match.group(1), 'GOC Rising Stars'
groupMap = {
'juvenile i': 'Kin',
@ -104,39 +116,39 @@ class CompetitionParser:
'standard': 'Std',
'latin': 'Lat',
}
funs = [parseForward, parseReverse]
funs = [parseForward, parseReverse, parseGOC]
for fun in funs:
try:
sec, grp = fun()
sec, grp, cls_ = fun()
self._group = groupMap.get(grp.lower(), grp)
self._class = 'WDSF Open'
self._class = cls_
self._section = sectionMap.get(sec.lower(), sec)
return
except ParsingFailedEception:
except ParsingFailedException:
pass
raise ParsingFailedEception('Neither forward not reversed parsing worked')
raise ParsingFailedException('Neither forward not reversed parsing worked')
functions = [parseDTVCompetition, parseWDSFCompetition]
for fun in functions:
try:
fun()
return
except ParsingFailedEception:
except ParsingFailedException:
pass
raise ParsingFailedEception(f'No more matchers for the competition line "{competition}" were left.')
raise ParsingFailedException(f'No more matchers for the competition line "{competition}" were left.')
def parsePlace(place):
match = self._rePlace.fullmatch(place)
if match is None:
raise ParsingFailedEception(f'Cannot parse the place entry {place}')
raise ParsingFailedException(f'Cannot parse the place entry {place}')
self._verein = match.group(1)
self._ort = match.group(2)
def parsePhone(phone):
match = self._rePhone.fullmatch(phone)
if match is None:
raise ParsingFailedEception(f'Cannot parse the phone line {phone}')
raise ParsingFailedException(f'Cannot parse the phone line {phone}')
self._telefon = match.group(1)
tds = table('td')

View File

@ -1,5 +1,7 @@
#!/bin/bash
source "$(dirname "$0")/venv/bin/activate"
export PYTHONPATH="$(dirname "$0"):$PYTHONPATH"
python -m competitionNotificationReader "$@"