From a36d3cb9bfdd1eb5ed859b4dd71e28ed1cef222c Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 2 Mar 2023 10:33:24 +0100 Subject: [PATCH] add optional in dynoption --- doc/api_value.md | 35 +++++++++++++++++++---------------- doc/options.md | 16 ++++++++++++++++ tiramisu/autolib.py | 16 ++++++++++++---- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/doc/api_value.md b/doc/api_value.md index 08ce38d..bb288ef 100644 --- a/doc/api_value.md +++ b/doc/api_value.md @@ -42,7 +42,6 @@ def calc_disk_usage(path, size='bytes'): # do not calc if path is None if path is None: return None - if size == 'bytes': div = 1 else: @@ -60,12 +59,13 @@ usage = FloatOption('usage', 'Disk usage', Calculation(calc_disk_usage, Finally add those options in option description and a Config: -``` +```python disk = OptionDescription('disk', 'Verify disk usage', [filename, usage]) root = OptionDescription('root', 'root', [disk]) async def main(): config = await Config(root) await config.property.read_write() + return config config = run(main()) ``` @@ -109,26 +109,29 @@ returns: When you enter a value it is validated: ->>> try: ->>> config.option('disk.path').value.set('/unknown') ->>> except ValueError as err: ->>> print(err) +```python +async def main(): + try: + await config.option('disk.path').value.set('/unknown') + except ValueError as err: + print(err) + +run(main()) +``` + +returns: + +``` "/unknown" is an invalid file name for "Path", this directory does not exist - -We can also set a :doc:`calculation` as value. For example, we want to launch previous function but with in_gb to True as second argument: - ->>> calc = Calculation(calc_disk_usage, Params((ParamOption(filename), -... ParamValue('gigabytes')))) ->>> config.option('disk.usage').value.set(calc) ->>> config.option('disk.usage').value.get() -622.6080360412598 +``` #### Is value is valid? To check is a value is valid: ->>> config.option('disk.path').value.valid() -True +```python +await config.option('disk.path').value.valid() +``` #### Display the default value diff --git a/doc/options.md b/doc/options.md index 0644683..1598a0d 100644 --- a/doc/options.md +++ b/doc/options.md @@ -471,6 +471,22 @@ from tiramisu import FilenameOption FilenameOption('file', 'file', '/etc/tiramisu/tiramisu.conf') ``` +## Unix file permissions: PermissionsOption + +Valid the representing Unix permissions is an octal (base-8) notation. + +```python +from tiramisu import PermissionsOption +PermissionsOption('perms', 'perms', 755) +PermissionsOption('perms', 'perms', 1755) +``` + +This option doesn't allow (or display a warning with warnings_only): + +- 777 (two weak value) +- others have more right than group +- group has more right than user + # Date option ## Date option: DateOption diff --git a/tiramisu/autolib.py b/tiramisu/autolib.py index 1295758..f8e3ac4 100644 --- a/tiramisu/autolib.py +++ b/tiramisu/autolib.py @@ -90,6 +90,7 @@ class ParamDynOption(ParamOption): dynoptiondescription: 'DynOptionDescription', notraisepropertyerror: bool=False, raisepropertyerror: bool=False, + optional: bool=False, todict: bool=False, ) -> None: super().__init__(option, @@ -99,6 +100,7 @@ class ParamDynOption(ParamOption): ) self.suffix = suffix self.dynoptiondescription = dynoptiondescription + self.optional = optional class ParamSelfOption(Param): @@ -273,13 +275,19 @@ async def manager_callback(callbk: Param, except PropertiesOptionError as err: # raise PropertiesOptionError (which is catched) because must not add value None in carry_out_calculation if callbk.notraisepropertyerror or callbk.raisepropertyerror: - raise err + raise err from err raise ConfigError(_('unable to carry out a calculation for "{}"' - ', {}').format(option.impl_get_display_name(), err), err) + ', {}').format(option.impl_get_display_name(), err), err) from err except ValueError as err: - raise ValueError(_('the option "{0}" is used in a calculation but is invalid ({1})').format(option_bag.option.impl_get_display_name(), err)) + raise ValueError(_('the option "{0}" is used in a calculation but is invalid ({1})').format(option_bag.option.impl_get_display_name(), err)) from err except AttributeError as err: - raise ConfigError(_(f'unable to get value for calculating "{option_bag.option.impl_get_display_name()}", {err}')) + if isinstance(callbk, ParamDynOption) and callbk.optional: + # cannot acces, simulate a propertyerror + raise PropertiesOptionError(option_bag, + ['configerror'], + config_bag.context.cfgimpl_get_settings(), + ) + raise ConfigError(_(f'unable to get value for calculating "{option_bag.option.impl_get_display_name()}", {err}')) from err return value async def get_option_bag(config_bag,