Skip to content

Commit

Permalink
feat(search): lazily import dateparser
Browse files Browse the repository at this point in the history
It can be expensive to import due to building thousands of regexps at
import time (see scrapinghub/dateparser#1181).
  • Loading branch information
nijel committed Oct 2, 2024
1 parent e23f390 commit 404a3c1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion weblate/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 404a3c1

Please sign in to comment.