From 2c58571d621bec5a938674559990339b3d6ed293 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Wed, 2 Oct 2024 16:51:12 +0200 Subject: [PATCH] Allow force writing values --- nucon/core.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/nucon/core.py b/nucon/core.py index 9a6fd60..4e7100e 100644 --- a/nucon/core.py +++ b/nucon/core.py @@ -141,17 +141,17 @@ class Nucon(Enum): @property def value(self) -> Union[float, int, bool, str, Enum]: - return Nucon.get(self) + return self.read() def read(self) -> Union[float, int, bool, str, Enum]: - return self.value + return Nucon.get(self) @value.setter def value(self, new_value: Union[float, int, bool, str, Enum]) -> None: - Nucon.set(self, new_value) + self.write(new_value) - def write(self, new_value: Union[float, int, bool, str, Enum]) -> None: - self.value = new_value + def write(self, new_value: Union[float, int, bool, str, Enum], force: bool = False) -> None: + Nucon.set(self, new_value, force) @classmethod def set_base_url(cls, url: str) -> None: @@ -212,21 +212,21 @@ class Nucon(Enum): return cls.get_multiple(list(cls)) @classmethod - def set(cls, parameter: 'Nucon', value: Union[float, int, bool, str, Enum]) -> None: - if not parameter.is_writable: + def set(cls, parameter: 'Nucon', value: Union[float, int, bool, str, Enum], force: bool = False) -> None: + if not force and not parameter.is_writable: raise ValueError(f"Parameter {parameter.name} is not writable") if parameter.enum_type and isinstance(value, parameter.enum_type): value = value.value if NuconConfig.dummy_mode: - print(f"Dummy mode: Setting {parameter.name} to {value}") + print(f"Dummy mode: {'Force ' if force else ''}Setting {parameter.name} to {value}") return - response = requests.post(NuconConfig.base_url, params={"variable": parameter.name, "value": str(value)}) + response = requests.post(NuconConfig.base_url, params={"variable": parameter.name, "value": str(value), "force": str(force).lower()}) if response.status_code != 200: - raise Exception(f"Failed to set parameter {parameter.name}. Status code: {response.status_code}") + raise E # Example usage if __name__ == "__main__":