From 7804ac6ea3e9b7daca765f34b7fabb4dd0597975 Mon Sep 17 00:00:00 2001 From: Julien Date: Fri, 19 Apr 2024 15:39:54 +0200 Subject: [PATCH] Feature: add datetime to rules context (#484) 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 --- examples/profiles/only_linux/profile.json | 5 +++++ .../jobs/job_profiles_synchronizer.py | 7 +++++-- .../utils/computer_environment.py | 20 ++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/examples/profiles/only_linux/profile.json b/examples/profiles/only_linux/profile.json index 4a8c5957..157a3dc9 100644 --- a/examples/profiles/only_linux/profile.json +++ b/examples/profiles/only_linux/profile.json @@ -18,6 +18,11 @@ "path": "$.environment.operating_system_code", "value": "linux", "operator": "equal" + }, + { + "path": "$.date.current_year", + "value": 2023, + "operator": "greater_than_inclusive" } ] } diff --git a/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py b/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py index 6f340369..b71ea49b 100644 --- a/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py +++ b/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py @@ -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 ############### @@ -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}") diff --git a/qgis_deployment_toolbelt/utils/computer_environment.py b/qgis_deployment_toolbelt/utils/computer_environment.py index 6f6e755b..a0921c46 100644 --- a/qgis_deployment_toolbelt/utils/computer_environment.py +++ b/qgis_deployment_toolbelt/utils/computer_environment.py @@ -11,9 +11,11 @@ # ########## Libraries ############# # ################################## -# Standard library import logging import platform + +# Standard library +from datetime import date from sys import platform as opersys # ############################################################################# @@ -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...