def re_variable(x): r""" >>> v('{word}') '(?P<word>[a-zA-Z][a-zA-Z_0-9]*)' >>> v('{word:int}') '(?P<word>\\d+)' """ R = Re('{([^:}]+)(:[^:}]+)?}', re.I) assert R.fullmatch(x), f'{x!r} does not match {R.pattern!r}' a, b = R.fullmatch(x).groups() r = ('[a-zA-Z][a-zA-Z_0-9]*' if not b or b.lstrip(':') == 'str' else '\d+' if b.lstrip(':') == 'int' else '[a-zA-Z/][a-zA-Z_0-9/]*' if b.lstrip(':') == 'url' else throw(Exception)) return f'(?P<{a}>{r})'
def parse_dot_directory(filename): try: with open(filename) as dot_directory: lines = [line.strip('\n') for line in dot_directory] Reg = Re('^\\[(.*)\\]$') stops = [ i for i, line in enumerate(lines) if Reg.fullmatch(line) ] + [len(lines)] for i in range(len(stops) - 1): a,b = stops[i], stops[i+1] name = Reg.fullmatch(lines[a]).group(1) if name == 'rename_today.py': for l in lines[a+1:b]: a,b = l.split('=', maxsplit=1) yield a,b except FileNotFoundError: return
''') a = args = parser.parse_args() x = a.fuzzy_time from datetime import date, time, datetime, timedelta C = datetime.combine N = datetime.now() from re import compile as Re R = Re('(\d+)[h:](\d*)') match = R.fullmatch(x) if not match: import sys print("Wrong match, must be", R.pattern, file=sys.stderr) else: h, m = match.groups() h, m = int(h), int(m or '0') d = N.replace(hour=h, minute=m, second=0, microsecond=0) if d < N: d += timedelta(days=1) delta = d - N print(*str(delta).split(':')[:2], sep=':')
DAYS = 'lun|mar|mer|jeu|ven|sam|dim'.split('|') DAY = Re('|'.join(DAYS)) HOUR = Re('(\d+)[h:](\d*)') HOUR_FROM = Re('(de|from)(\d+)[h:](\d*)') HOUR_TO = Re('(à|to)(\d+)[h:](\d*)') DATE = Re('(\d+)/(\d+)') WEEK_SHIFT = Re('([+-]\d*)w') GROUP = Re('(' + '|'.join(map(re.escape, map(str, GROUPS))) + ')(\d*)', re.I) SEANCE = Re('S({})'.format({'num': '\d+', 'letters':'\w+', 'mixed':'[\d\w]+'}[SEANCES]), re.I) if SEANCES else Re('') ASSISTANT = Re('|'.join(map(re.escape, ASSISTANTS)), re.I) DURATION_RE = Re('(\d+)h(\d\d)?(min)?|(\d+)min') assert DURATION_RE.fullmatch(ALL_DURATION), '"{ALL_DURATION}" is not like "6h" or "6h30" or "5min"' def convert_duration(string): hour, minute, _, single_minute = DURATION_RE.fullmatch(string).groups() if single_minute: return timedelta(minutes=int(single_minute)) else: return timedelta(hours=int(hour), minutes=int(minute or 0)) DURATION = convert_duration(ALL_DURATION) try: import search_for_ics_gehol except ImportError: print('[Warning] no module search_for_ics_gehol, locations will be empty') search_for_ics_gehol = None