Skip to content

Commit

Permalink
Allow decimal values in timedelta
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 15, 2020
1 parent 148b1a5 commit 1308e64
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
2 changes: 0 additions & 2 deletions reminder/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def get_config_class(cls) -> Type[BaseProxyConfig]:
return Config

async def start(self) -> None:
await super().start()
self.on_external_config_update()
self.db = ReminderDatabase(self.database)
self.reminder_loop_task = asyncio.ensure_future(self.reminder_loop(), loop=self.loop)
Expand All @@ -54,7 +53,6 @@ def on_external_config_update(self) -> None:
self.base_aliases = tuple(bc) if isinstance(bc, list) else (bc,)

async def stop(self) -> None:
await super().stop()
self.reminder_loop_task.cancel()

async def reminder_loop(self) -> None:
Expand Down
10 changes: 9 additions & 1 deletion reminder/locale_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ def match(self, val: str) -> Optional[MatcherReturn]:
pass


def int_or_float(val: str) -> Union[int, float]:
if "," in val:
return float(val.replace(",", "."))
elif "." in val:
return float(val)
return int(val)


class RegexMatcher(Matcher):
regex: Pattern
value_type: Type

def __init__(self, pattern: str, value_type: Type = int) -> None:
def __init__(self, pattern: str, value_type: Type = int_or_float) -> None:
self.regex = re.compile(pattern, re.IGNORECASE)
self.value_type = value_type

Expand Down
43 changes: 22 additions & 21 deletions reminder/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
locales: Locales = {}

td_sep_en = r"(?:[\s,]{1,3}(?:and\s)?)?"
number = r"[+-]?\d+(?:[.,]\d+)?"
locales["en_iso"] = Locale(
name="English (ISO)",
timedelta=RegexMatcher(r"(?:(?:in|after)\s)?"
rf"(?:(?P<years>[-+]?\d+)\s?y(?:r|ears?)?{td_sep_en})?"
rf"(?:(?P<months>[-+]?\d+)\s?mo(?:nths?)?{td_sep_en})?"
rf"(?:(?P<weeks>[-+]?\d+)\s?w(?:k|eeks?)?{td_sep_en})?"
rf"(?:(?P<days>[-+]?\d+)\s?d(?:ays?)?{td_sep_en})?"
rf"(?:(?P<hours>[-+]?\d+)\s?h(?:(?:r|our)?s?){td_sep_en})?"
rf"(?:(?P<minutes>[-+]?\d+)\s?m(?:in(?:ute)?s?)?{td_sep_en})?"
r"(?:(?P<seconds>[-+]?\d+)\s?s(?:ec(?:ond)?s?)?)?"
rf"(?:(?P<years>{number})\s?y(?:r|ear)?s?{td_sep_en})?"
rf"(?:(?P<months>{number})\s?mo(?:nth)?s?{td_sep_en})?"
rf"(?:(?P<weeks>{number})\s?w(?:k|eek)?s?{td_sep_en})?"
rf"(?:(?P<days>{number})\s?d(?:ays?)?{td_sep_en})?"
rf"(?:(?P<hours>{number})\s?h(?:(?:r|our)?s?){td_sep_en})?"
rf"(?:(?P<minutes>{number})\s?m(?:in(?:ute)?s?)?{td_sep_en})?"
rf"(?:(?P<seconds>{number})\s?s(?:ec(?:ond)?s?)?)?"
r"(?:\s|$)"),
date=RegexMatcher(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})\s"),
weekday=WeekdayMatcher(pattern=r"(?:today"
Expand Down Expand Up @@ -72,13 +73,13 @@
td_sep_fi = r"(?:[\s,]{1,3}(?:ja\s)?)?"
locales["fi_fi"] = Locale(
name="Finnish",
timedelta=RegexMatcher(rf"(?:(?P<years>[-+]?\d+)\s?v(?:uo(?:tta|den))?{td_sep_fi})?"
rf"(?:(?P<months>[-+]?\d+)\s?k(?:k|uukau(?:si|tta|den))?{td_sep_fi})?"
rf"(?:(?P<weeks>[-+]?\d+)\s?v(?:k|iikk?o[an]?){td_sep_fi})?"
rf"(?:(?P<days>[-+]?\d+)\s?p(?:v|äivä[än]?){td_sep_fi})?"
rf"(?:(?P<hours>[-+]?\d+)\s?t(?:un(?:nin?|tia))?{td_sep_fi})?"
rf"(?:(?P<minutes>[-+]?\d+)\s?m(?:in(?:uut(?:in?|tia))?)?{td_sep_fi})?"
r"(?:(?P<seconds>[-+]?\d+)\s?s(?:ek(?:un(?:nin?|tia))?)?)?"
timedelta=RegexMatcher(rf"(?:(?P<years>{number})\s?v(?:uo(?:tta|den))?{td_sep_fi})?"
rf"(?:(?P<months>{number})\s?k(?:k|uukau(?:si|tta|den))?{td_sep_fi})?"
rf"(?:(?P<weeks>{number})\s?v(?:k|iikk?o[an]?){td_sep_fi})?"
rf"(?:(?P<days>{number})\s?p(?:v|äivä[än]?){td_sep_fi})?"
rf"(?:(?P<hours>{number})\s?t(?:un(?:nin?|tia))?{td_sep_fi})?"
rf"(?:(?P<minutes>{number})\s?m(?:in(?:uut(?:in?|tia))?)?{td_sep_fi})?"
rf"(?:(?P<seconds>{number})\s?s(?:ek(?:un(?:nin?|tia))?)?)?"
r"(?:\s(?:kuluttua|päästä?))?"
r"(?:\s|$)"),
date=ShortYearMatcher(r"(?P<day>\d{1,2})\.(?P<month>\d{1,2})\.(?P<year>\d{2}(?:\d{2})?)\s"),
Expand Down Expand Up @@ -108,13 +109,13 @@
locales["de_de"] = Locale(
name="German",
timedelta=RegexMatcher(rf"(?:in\s)?"
rf"(?:(?P<years>[-+]?\d+)\s?jahr(?:en)?{td_sep_de})?"
rf"(?:(?P<months>[-+]?\d+)\s?monat(?:en)?{td_sep_de})?"
rf"(?:(?P<weeks>[-+]?\d+)\s?wochen?{td_sep_de})?"
rf"(?:(?P<days>[-+]?\d+)\s?tag(?:en)?{td_sep_de})?"
rf"(?:(?P<hours>[-+]?\d+)\s?stunden?{td_sep_de})?"
rf"(?:(?P<minutes>[-+]?\d+)\s?minuten?{td_sep_de})?"
r"(?:(?P<seconds>[-+]?\d+)\s?sekunden?)?"),
rf"(?:(?P<years>{number})\s?jahr(?:en)?{td_sep_de})?"
rf"(?:(?P<months>{number})\s?monat(?:en)?{td_sep_de})?"
rf"(?:(?P<weeks>{number})\s?wochen?{td_sep_de})?"
rf"(?:(?P<days>{number})\s?tag(?:en)?{td_sep_de})?"
rf"(?:(?P<hours>{number})\s?stunden?{td_sep_de})?"
rf"(?:(?P<minutes>{number})\s?minuten?{td_sep_de})?"
rf"(?:(?P<seconds>{number})\s?sekunden?)?"),
date=ShortYearMatcher(
r"(?P<day>\d{1,2})\.(?P<month>\d{1,2})\.(?P<year>\d{2}(?:\d{2})?)(?:\s|$)"),
weekday=WeekdayMatcher(pattern=r"(?:heute"
Expand Down

0 comments on commit 1308e64

Please sign in to comment.