rougail-tests/src/rougail_tests/utils.py

248 lines
9.7 KiB
Python

from tiramisu import owners
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, relative_to=None):
rougailconfig = RougailConfig
rougailconfig['functions_files'] = get_funcs()
if relative_to:
dirs = [str((test_dir / 'rougail').relative_to(relative_to, walk_up=True))]
else:
dirs = [str((test_dir / 'rougail'))]
subfolder = test_dir / 'rougail2'
if subfolder.is_dir():
if relative_to:
dirs.append(str(subfolder.relative_to(relative_to, walk_up=True)))
else:
dirs.append(str(subfolder))
rougailconfig['main_structural_directories'] = dirs
if namespace:
if (test_dir / 'force_no_namespace').is_file():
return None
rougailconfig['main_namespace'] = 'Rougail'
else:
if (test_dir / 'force_namespace').is_file():
return None
rougailconfig['main_namespace'] = None
if (test_dir / 'default_structural_format_version').is_file():
rougailconfig["default_structural_format_version"] = "1.1"
else:
rougailconfig["default_structural_format_version"] = None
extra_namespaces = {}
extras = list(test_dir.iterdir())
extras.sort()
for extra in extras:
if extra.name in ['rougail', 'rougail2', 'file']:
continue
if extra.is_dir():
if relative_to:
extra_namespaces[extra.name] = [str(extra.relative_to(relative_to, walk_up=True))]
else:
extra_namespaces[extra.name] = [str(extra)]
if extra_namespaces:
if not namespace:
return None
rougailconfig['extra_namespaces'] = extra_namespaces
else:
rougailconfig['extra_namespaces'] = {}
rougailconfig['custom_types']['custom'] = CustomOption
# rougailconfig['tiramisu_cache'] = "cache.py"
return rougailconfig
def get_values_for_config(config, specify_dynamic_id=True, level="all", use_unrestraint=True, exclude_namespace=None):
# level is "all" or "mandatories"
if use_unrestraint:
uconfig = config.unrestraint
else:
uconfig = config
excludes = []
get_excludes(uconfig, excludes)
config.property.read_write()
root_config = uconfig
if level == 'all':
only = False
else:
only = True
values = {}
get_variables(root_config, root_config, values, only, excludes, specify_dynamic_id, use_unrestraint, exclude_namespace)
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, config, use_unrestraint):
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.6/32', '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/24', '10.0.10.0/24']
else:
tests = ['192.168.1.6', '10.0.10.10']
elif variable.type() == 'email address':
tests = ["user1@domain1.lan", "user2@domain1.lan"]
elif variable.type() == 'file name':
tests = ["/directory1/file.txt", "/directory2/file.txt"]
elif variable.type() == 'URL':
tests = ['http://domain1.lan', 'https://domain2.lan']
elif variable.type() == 'password':
password = 'onE7vaLues_len1'
if variable.extra('max_len'):
password = password[:variable.extra('max_len')]
tests = [password]
else:
tests = ['string1', 'string2', 'string3']
if not variable.ismulti():
tests = tests[0]
elif variable.isleader() and variable.owner.get() == owners.default:
len_leader = len(variable.value.get())
if len_leader:
for idx in range(len_leader - 1, -1, -1):
variable.value.pop(idx)
elif index is not None and variable.isfollower() and variable.issubmulti() is False:
if len(tests) > index:
tests = tests[index]
else:
tests = tests[0]
# if not use_unrestraint:
# config.property.read_write()
variable.value.set(tests)
if variable.index() is None:
variable.information.set('loaded_from', 'loaded from rougail-test')
else:
no_index_variable = config.option(variable.path()).information.set(f'loaded_from_{index}', 'loaded from rougail-test')
# if not use_unrestraint:
# config.property.read_only()
# 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, use_unrestraint, exclude_namespace, *, index=None, leader_is_mandatory=False):
for idx, key in enumerate(config):
if key.name() == exclude_namespace:
continue
if not use_unrestraint and 'hidden' in key.property.get():
continue
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, root_config, use_unrestraint)
leader_is_mandatory = True
has_value = False
for idx_, val in enumerate(leader_value):
value.append({})
get_variables(root_config, key, value[-1], only, excludes, specify_dynamic_id, use_unrestraint, None, index=idx_, leader_is_mandatory=leader_is_mandatory)
if value[-1]:
has_value = True
if has_value:
values[key.name()] = value
else:
value = {}
get_variables(root_config, key, value, only, excludes, specify_dynamic_id, use_unrestraint, None)
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, root_config, use_unrestraint)
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