Skip to content

Commit

Permalink
Feature: add datetime to rules context (#484)
Browse files Browse the repository at this point in the history
This PR adds date information to rules context allowing to condition a
profile deployment based on current year, month or day.

In the following example, the profile is installed only if QDT runs on a
Linux operating system and if the current year >= 2023:

```json
{
  "$schema": "https://raw.githubusercontent.com/Guts/qgis-deployment-cli/main/docs/schemas/profile/qgis_profile.json",
  "name": "QDT Only Linux",
  "folder_name": "qdt_only_linux",
  "description": "Demonstrating a QDT profile that's deployed only on Linux.",
  "author": "Julien Moura",
  "email": "infos+qdt@oslandia.com",
  "qgisMinimumVersion": "3.34.0",
  "qgisMaximumVersion": "3.99.10",
  "version": "1.0.0",
  "rules": [
    {
      "name": "Environment",
      "description": "Profile is configured to run only on Linux.",
      "conditions": {
        "all": [
          {
            "path": "$.environment.operating_system_code",
            "value": "linux",
            "operator": "equal"
          },
          {
            "path": "$.date.current_year",
            "value": 2023,
            "operator": "greater_than_inclusive"
          }
        ]
      }
    }
  ]
}
```


Depends on #481
  • Loading branch information
Guts authored Apr 19, 2024
1 parent 1d2b672 commit 7804ac6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/profiles/only_linux/profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"path": "$.environment.operating_system_code",
"value": "linux",
"operator": "equal"
},
{
"path": "$.date.current_year",
"value": 2023,
"operator": "greater_than_inclusive"
}
]
}
Expand Down
7 changes: 5 additions & 2 deletions qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
# package
from qgis_deployment_toolbelt.jobs.generic_job import GenericJob
from qgis_deployment_toolbelt.profiles.qdt_profile import QdtProfile
from qgis_deployment_toolbelt.utils.computer_environment import environment_dict
from qgis_deployment_toolbelt.utils.computer_environment import (
date_dict,
environment_dict,
)

# #############################################################################
# ########## Globals ###############
Expand Down Expand Up @@ -129,7 +132,7 @@ def filter_profiles_on_rules(
li_profiles_matched = []
li_profiles_unmatched = []

context_object = {"environment": environment_dict()}
context_object = {"date": date_dict(), "environment": environment_dict()}
for profile in li_downloaded_profiles:
if profile.rules is None:
logger.debug(f"No rules to apply to {profile.name}")
Expand Down
20 changes: 19 additions & 1 deletion qgis_deployment_toolbelt/utils/computer_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
# ########## Libraries #############
# ##################################

# Standard library
import logging
import platform

# Standard library
from datetime import date
from sys import platform as opersys

# #############################################################################
Expand All @@ -29,6 +31,22 @@
# ##################################


def date_dict() -> dict:
"""Returns a context dictionary with date informations that can be used in QDT
various places: rules...
Returns:
dict: dict with current date informations
"""
today = date.today()
return {
"current_day": today.day,
"current_weekday": today.weekday(), # monday = 0, sunday = 6
"current_month": today.month,
"current_year": today.year,
}


def environment_dict() -> dict:
"""Returns a dictionary containing some environment information (computer, network,
platform) that can be used in QDT various places: rules...
Expand Down

0 comments on commit 7804ac6

Please sign in to comment.