Skip to content

Commit

Permalink
Merge pull request #114 from adammcdonagh/feat/config-deltas
Browse files Browse the repository at this point in the history
Add additional delta_hours helper function to config loader
  • Loading branch information
iwilson-oa authored Nov 6, 2024
2 parents fe71f99 + ec6a945 commit fef5413
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# v24.45.1

- Add additional delta_hours helper function to config loader

# v24.45.0

- Add further exception handling for deleting .gnupg directories after handling file decryption
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "opentaskpy"
version = "v24.45.0"
version = "v24.45.1"
authors = [{ name = "Adam McDonagh", email = "[email protected]" }]
license = { text = "GPLv3" }
classifiers = [
Expand Down Expand Up @@ -71,7 +71,7 @@ otf-batch-validator = "opentaskpy.cli.batch_validator:main"
profile = 'black'

[tool.bumpver]
current_version = "v24.45.0"
current_version = "v24.45.1"
version_pattern = "vYY.WW.PATCH[-TAG]"
commit_message = "bump version {old_version} -> {new_version}"
commit = true
Expand Down
13 changes: 13 additions & 0 deletions src/opentaskpy/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, config_dir: str) -> None:
jinja2.make_logging_undefined(logger=self.logger, base=jinja2.Undefined)
self.template_env = jinja2.Environment(undefined=jinja2.StrictUndefined)
self.template_env.filters["delta_days"] = self.delta_days
self.template_env.filters["delta_hours"] = self.delta_hours

self._load_global_variables()

Expand Down Expand Up @@ -80,6 +81,18 @@ def delta_days(self, value: datetime.datetime, days: int) -> datetime.datetime:
"""
return value + datetime.timedelta(days)

def delta_hours(self, value: datetime.datetime, hours: int) -> datetime.datetime:
"""Returns a new datetime object + or - the number of delta hours.
Args:
value (datetime.datetime): Starting datetime object
hours (int): Hours to increment or decrement the value
Returns:
datetime.datetime: New datetime object with the delta applied
"""
return value + datetime.timedelta(hours=hours)

def template_lookup(self, plugin: str, **kwargs) -> str: # type: ignore[no-untyped-def]
"""Lookup function used by Jinja.
Expand Down
7 changes: 7 additions & 0 deletions tests/test_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def test_default_date_variable_resolution(tmpdir):
"PREV_UTC_DD": "{{ (utc_now()|delta_days(-1)).strftime('%d') }}",
"PREV_MM": "{{ (now()|delta_days(-1)).strftime('%m') }}",
"PREV_YYYY": "{{ (now()|delta_days(-1)).strftime('%Y') }}",
"PREV_1H_HH": "{{ (now()|delta_hours(-1)).strftime('%H') }}",
}

# Create a JSON file with some test variables in it
Expand Down Expand Up @@ -535,6 +536,12 @@ def test_default_date_variable_resolution(tmpdir):
assert config_loader.get_global_variables()["PREV_MM"] == yesterday.strftime("%m")
assert config_loader.get_global_variables()["PREV_DD"] == yesterday.strftime("%d")

# Get datetime for 1 hour ago
previous_hour = datetime.now() - timedelta(hours=1)
assert config_loader.get_global_variables()["PREV_1H_HH"] == previous_hour.strftime(
"%H"
)

# Play around with the current time. Set it to a GMT time before the clocks change
# Change the time to before BST starts
initial_datetime = datetime(
Expand Down

0 comments on commit fef5413

Please sign in to comment.