Skip to content

Commit

Permalink
Convert all datetime.date to datetime.datetime in config
Browse files Browse the repository at this point in the history
  • Loading branch information
George V. Kouryachy (Fr. Br. George) committed Oct 18, 2023
1 parent 2d69a9f commit 04470d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
20 changes: 14 additions & 6 deletions hworker/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
default_name: Final = "default_hworker"
extension: Final = ".toml"
profile: list = []
DAY_START = 6 # Additinal 6 hours for day to start after 6:00AM ☺


def read_from_path(path: Path) -> dict:
Expand Down Expand Up @@ -58,18 +59,25 @@ def process_configs(user_config: str, *extras: str) -> Path:
return finalpath


def expand_macro(content: dict, parent=str) -> None:
"""Expand "`f-string`" fields
def expand_macro(content: dict, parent: str = "") -> None:
"""Expand "`f-string`" fields and unify dates
:param content: currnet section content
:param parent: parnet section name
:return:
"""
for key, value in content.items():
if isinstance(value, dict):
expand_macro(value, key)
if isinstance(value, str) and value[0] == value[-1] == "`":
content[key] = value[1:-1].format(**content | {"SELF": key, "PARENT": parent})
match value:
case dict():
expand_macro(value, key)
case datetime.datetime():
pass
case datetime.date():
content[key] = datetime.datetime.combine(value, datetime.time(DAY_START))
case str() if "`" in value:
res = value.split("`")
res[1::2] = [r.format(**content | {"SELF": key, "PARENT": parent}) for r in res[1::2]]
content[key] = "".join(res)


def config() -> dict:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
get_task_info,
create_config,
no_merge_processing,
DAY_START,
)
from .user_config import user_config

Expand Down Expand Up @@ -65,10 +66,10 @@ def test_task_info(self, user_config):
"resource_limit": 3145728,
"time_limit": 2,
"deliver_ID": "20230101/01",
"hard_deadline": datetime(2023, 1, 14, 0, 0),
"hard_deadline": datetime(2023, 1, 14, DAY_START, 0),
"hard_deadline_delta": "open_date+13d",
"open_date": datetime(2023, 1, 1, 0, 0),
"soft_deadline": datetime(2023, 1, 7, 0, 0),
"open_date": datetime(2023, 1, 1, DAY_START, 0),
"soft_deadline": datetime(2023, 1, 7, DAY_START, 0),
"soft_deadline_delta": "open_date+6d",
}

Expand Down

0 comments on commit 04470d2

Please sign in to comment.