add optional in dynoption
This commit is contained in:
parent
add176331e
commit
a36d3cb9bf
3 changed files with 47 additions and 20 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue