UPDATE Enable parsing of yaml datetime value without error

- ADD parse_datetime helper function to parse datetime or isoformat string to datetime
This commit is contained in:
Jeppy
2022-11-05 18:36:10 +01:00
committed by Sebastian Thomschke
parent 5363fdb087
commit 01803c525c
2 changed files with 20 additions and 3 deletions

View File

@@ -19,7 +19,7 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from . import utils, resources, extract # pylint: disable=W0406 from . import utils, resources, extract # pylint: disable=W0406
from .utils import abspath, apply_defaults, ensure, is_frozen, pause, pluralize, safe_get from .utils import abspath, apply_defaults, ensure, is_frozen, pause, pluralize, safe_get, parse_datetime
from .selenium_mixin import SeleniumMixin from .selenium_mixin import SeleniumMixin
# W0406: possibly a bug, see https://github.com/PyCQA/pylint/issues/3933 # W0406: possibly a bug, see https://github.com/PyCQA/pylint/issues/3933
@@ -256,9 +256,9 @@ class KleinanzeigenBot(SeleniumMixin):
if self.ads_selector == "due": if self.ads_selector == "due":
if ad_cfg["updated_on"]: if ad_cfg["updated_on"]:
last_updated_on = datetime.fromisoformat(ad_cfg["updated_on"]) last_updated_on = parse_datetime(ad_cfg["updated_on"])
elif ad_cfg["created_on"]: elif ad_cfg["created_on"]:
last_updated_on = datetime.fromisoformat(ad_cfg["created_on"]) last_updated_on = parse_datetime(ad_cfg["created_on"])
else: else:
last_updated_on = None last_updated_on = None

View File

@@ -5,6 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later
import copy, decimal, json, logging, os, re, secrets, sys, traceback, time import copy, decimal, json, logging, os, re, secrets, sys, traceback, time
from importlib.resources import read_text as get_resource_as_string from importlib.resources import read_text as get_resource_as_string
from collections.abc import Callable, Sized from collections.abc import Callable, Sized
from datetime import datetime
from types import FrameType, ModuleType, TracebackType from types import FrameType, ModuleType, TracebackType
from typing import Any, Final, TypeVar from typing import Any, Final, TypeVar
@@ -253,3 +254,19 @@ def parse_decimal(number:float | int | str) -> decimal.Decimal:
return decimal.Decimal("".join(parts[:-1]) + "." + parts[-1]) return decimal.Decimal("".join(parts[:-1]) + "." + parts[-1])
except decimal.InvalidOperation: except decimal.InvalidOperation:
raise decimal.DecimalException(f"Invalid number format: {number}") from ex raise decimal.DecimalException(f"Invalid number format: {number}") from ex
def parse_datetime(date:datetime | str | None) -> datetime | None:
"""
>>> parse_datetime(datetime(2020, 1, 1, 0, 0))
datetime.datetime(2020, 1, 1, 0, 0)
>>> parse_datetime("2020-01-01T00:00:00")
datetime.datetime(2020, 1, 1, 0, 0)
>>> parse_datetime(None)
"""
if date is None:
return None
if isinstance(date, datetime):
return date
return datetime.fromisoformat(date)