rougail-tests/src/rougail_tests/utils.py

197 lines
7.2 KiB
Python
Raw Normal View History

2025-03-02 16:18:24 +01:00
from pathlib import Path
from rougail import RougailConfig
from .custom import CustomOption
def get_structures_list(excludes):
return [test for test in sorted((Path(__file__).parent.parent.parent / 'structures').iterdir()) if test.name not in excludes]
def get_funcs():
return [str(test) for test in sorted((Path(__file__).parent.parent.parent / 'funcs').iterdir()) if test.name.endswith('.py')]
def get_rougail_config(test_dir, namespace=False):
rougailconfig = RougailConfig
rougailconfig['functions_files'] = get_funcs()
dirs = [str(test_dir / 'rougail')]
subfolder = test_dir / 'rougail2'
if subfolder.is_dir():
dirs.append(str(subfolder))
rougailconfig['main_dictionaries'] = dirs
if namespace:
rougailconfig['main_namespace'] = 'Rougail'
if (test_dir / 'force_no_namespace').is_file():
return None
else:
rougailconfig['main_namespace'] = None
if (test_dir / 'force_namespace').is_file():
return None
extra_dictionaries = {}
extras = list(test_dir.iterdir())
extras.sort()
for extra in extras:
if extra.name in ['rougail', 'rougail2']:
continue
if extra.is_dir():
extra_dictionaries[extra.name] = [str(extra)]
if extra_dictionaries:
if not namespace:
return None
rougailconfig['extra_dictionaries'] = extra_dictionaries
else:
rougailconfig['extra_dictionaries'] = {}
rougailconfig['custom_types']['custom'] = CustomOption
# rougailconfig['tiramisu_cache'] = "cache.py"
return rougailconfig
def get_values_for_config(config, specify_dynamic_id=True, level="all"):
# level is "all" or "mandatories"
excludes = []
get_excludes(config.unrestraint, excludes)
config.property.read_only()
root_config = config.unrestraint
if level == 'all':
only = False
else:
only = True
values = {}
get_variables(root_config, root_config, values, only, excludes, specify_dynamic_id)
if not specify_dynamic_id:
for exclude in excludes:
_values = values
*s_exclude, name = exclude.split('.')
for _name in s_exclude:
if _name not in _values:
break
_values = _values[_name]
else:
if name in _values:
del _values[name]
return values
def get_excludes(config, excludes):
for option in config.list(uncalculated=True):
if option.isoptiondescription():
exclude = option.information.get('dynamic_variable',
None,
)
if exclude:
excludes.append(exclude)
get_excludes(option, excludes)
def get_value(variable, index, excludes):
if 'force_store_value' in variable.property.get():
return variable.value.get()
tests = variable.information.get('test', None)
if variable.path(uncalculated=True) in excludes and variable.value.get(uncalculated=True):
tests = variable.value.get()
elif tests:
tests = list(tests)
else:
if variable.type() == 'integer':
tests = [1, 2, 3]
elif variable.type() == 'float':
tests = [1.1, 2.2, 3.3]
elif variable.type() == 'port':
tests = ['80', '443']
elif variable.type() == 'boolean':
tests = [True]
elif variable.type() == 'domain name':
tests = ['domain1.lan', 'domain2.lan']
elif variable.type() == 'choice':
tests = variable.value.list()
elif variable.type() == 'network address':
if variable.extra('_cidr'):
tests = ['192.168.1.0/24', '10.0.0.0/24']
else:
tests = ['192.168.1.0', '10.0.0.0']
elif variable.type() == 'netmask address':
tests = ['255.255.255.0', '255.255.0.0']
elif variable.type() == 'IP':
if variable.extra('_cidr'):
tests = ['192.168.1.6/32', '10.0.10.0/24']
else:
tests = ['192.168.1.6', '10.0.10.10']
else:
tests = ['string1', 'string2', 'string3']
if not variable.ismulti():
tests = tests[0]
elif index is not None and variable.isfollower() and variable.issubmulti() is False:
tests = tests[index]
variable.value.set(tests)
# if tests == None:
# tests = ""
if index is not None and variable.isleader():
tests = tests[index]
return tests
def get_variables(root_config, config, values, only, excludes, specify_dynamic_id, *, index=None, leader_is_mandatory=False):
for idx, key in enumerate(config):
if key.isoptiondescription():
if key.isleadership():
value = []
leader = key.leader()
if only and not leader.value.mandatory():
leader_value = leader.value.get()
leader_is_mandatory = False
else:
leader_value = get_value(leader, None, excludes)
leader_is_mandatory = True
for idx_, val in enumerate(leader_value):
value.append({})
get_variables(root_config, key, value[-1], only, excludes, specify_dynamic_id, index=idx_, leader_is_mandatory=leader_is_mandatory)
if value:
values[key.name()] = value
else:
value = {}
get_variables(root_config, key, value, only, excludes, specify_dynamic_id)
if value:
values[key.name()] = value
else:
if only:
if key.isleader():
mandatory = leader_is_mandatory
else:
try:
mandatory = key.value.mandatory()
except:
mandatory = False
if not only or mandatory:
if key.index() is not None and index is not None and index != key.index():
continue
value = get_value(key, index, excludes)
if specify_dynamic_id or key.path(uncalculated=True) not in excludes:
values[key.name()] = value
def config_to_dict(parent, key_is_option=False):
for option, value in parent.items():
if option.isoptiondescription():
if not key_is_option and option.isleadership():
ret = []
for idx, datas in enumerate(config_to_dict(value, key_is_option=True)):
sub_option, sub_value = datas
if not idx:
sub_option = sub_option.path()
key = sub_option
for val in sub_value:
ret.append({sub_option: val})
else:
index = sub_option.index()
sub_option = sub_option.path()
ret[index][sub_option] = sub_value
yield key, ret
else:
yield from config_to_dict(value, key_is_option)
elif key_is_option:
yield option, value
else:
yield option.path(), value