From bf9ff6498de7a46009dfefa24ceccca35a7af829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 2 Oct 2024 11:40:48 +0200 Subject: [PATCH] feat(search): lazily import dateparser It can be expensive to import due to building thousands of regexps at import time (see https://github.com/scrapinghub/dateparser/pull/1181). --- weblate/utils/search.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/weblate/utils/search.py b/weblate/utils/search.py index 55ace21e34bb..6cf665a6d8d2 100644 --- a/weblate/utils/search.py +++ b/weblate/utils/search.py @@ -12,7 +12,6 @@ from operator import and_, or_ from typing import Any, cast, overload -from dateparser import parse as dateparser_parse from dateutil.parser import ParserError from dateutil.parser import parse as dateutil_parse from django.db import transaction @@ -240,6 +239,21 @@ def convert_datetime(self, text, hour=5, minute=55, second=55, microsecond=0): ), ) + return self.human_date_parse(text, hour, minute, second, microsecond) + + def human_date_parse( + self, + text: str, + hour: int = 5, + minute: int = 55, + second: int = 55, + microsecond: int = 0, + ) -> datetime | tuple[datetime, datetime]: + # Lazily import as this can be expensive + from dateparser import parse as dateparser_parse + + tzinfo = timezone.get_current_timezone() + # Attempts to parse the text using dateparser # If the text is unparsable it will return None result = dateparser_parse(text)