This commit is contained in:
gwen 2024-09-24 16:42:03 +02:00
parent ff112d9012
commit 763788858a
4 changed files with 44 additions and 39 deletions

View file

@ -67,7 +67,7 @@ class RougailUserDataEnvironment:
if option.isoptiondescription(): if option.isoptiondescription():
self.parse(option) self.parse(option)
else: else:
# FIXME gerer les erreurs # FIXME handle exceptions
# try except # try except
# ValueError suivant le type # ValueError suivant le type
# PropertyError -> hidden ou frozen... # PropertyError -> hidden ou frozen...
@ -76,6 +76,7 @@ class RougailUserDataEnvironment:
self.load_environment(option) self.load_environment(option)
# except Exception as exc: # except Exception as exc:
# print(exc) # print(exc)
def load_environment(self, option): def load_environment(self, option):
option_name = option.path() option_name = option.path()
# the bash variable are in upper case # the bash variable are in upper case
@ -94,7 +95,7 @@ class RougailUserDataEnvironment:
option_bash_value = self.rougail_environment_vars.pop(option_name) option_bash_value = self.rougail_environment_vars.pop(option_name)
if ismulti: if ismulti:
# here we expect the bash option value of a multi to be coma separated: # here we expect the bash option value of a multi to be coma separated:
option_bash_value = [opt.strip() for opt in option_bash_value.split(",")] option_bash_value = [opt.strip() for opt in option_bash_value.split(",")]
if type_obj: if type_obj:
option_bash_value = [type_obj(opt) for opt in option_bash_value] option_bash_value = [type_obj(opt) for opt in option_bash_value]
elif type_obj: elif type_obj:
@ -103,4 +104,4 @@ class RougailUserDataEnvironment:
option.value.set(option_bash_value) option.value.set(option_bash_value)
except Exception as exc: except Exception as exc:
print(exc) print(exc)

7
tests/custom.py Normal file
View file

@ -0,0 +1,7 @@
from os import environ
environ['TIRAMISU_LOCALE'] = 'en'
from tiramisu import StrOption
class CustomOption(StrOption):
pass

View file

@ -0,0 +1,8 @@
ROUGAIL_VARNAME2="tata"
ROUGAIL_VARNAME1="titi"
ROUGAIL_VARNAME23=" 18, 25 "
ROUGAIL_VARINT="5"
ROUGAIL_VARNAME34=" 58, 22 "
ROUGAIL_IDONTEXIST="blah"
ROUGAIL_IDONTEXIST2="hum"
ROUGAIL_myfamily.varname3='spam'

View file

@ -1,24 +1,20 @@
from .custom import CustomOption from .custom import CustomOption
from pathlib import Path from pathlib import Path
import os
from pytest import fixture # , raises from pytest import fixture # , raises
from ruamel.yaml import YAML from ruamel.yaml import YAML
from dotenv import load_dotenv
from rougail import RougailConfig, Rougail from rougail import RougailConfig, Rougail
#from tiramisu import Config #from tiramisu import Config
from rougail.user_data_environment.data import RougailUserDataEnvironment
#from rougail.output_doc import RougailOutputDoc
dico_dirs = Path('../rougail/tests/dictionaries') dico_dirs = Path('../rougail/tests/dictionaries')
#test_ok = set() #test_ok = set()
#excludes = set([
# '60_5family_dynamic_unknown_suffix',
# '60_5family_dynamic_variable_outside_sub_suffix',
#])
#for test in dico_dirs.iterdir(): #for test in dico_dirs.iterdir():
# if (test / 'tiramisu').is_dir() and test.name not in excludes: # if (test / 'tiramisu').is_dir() and test.name not in excludes:
# test_ok.add(test) # test_ok.add(test)
@ -26,7 +22,7 @@ dico_dirs = Path('../rougail/tests/dictionaries')
#test_ok = list(test_ok) #test_ok = list(test_ok)
#test_ok.sort() #test_ok.sort()
test_ok = [dico_dirs / '60_0family_dynamic_variable_suffix'] test_ok = [dico_dirs / '00_10_env_var']
@fixture(scope="module", params=test_ok) @fixture(scope="module", params=test_ok)
@ -36,46 +32,39 @@ def test_dir(request):
def _test_dictionaries(test_dir): def _test_dictionaries(test_dir):
rougailconfig = RougailConfig.copy() rougailconfig = RougailConfig.copy()
# rougailconfig['step.output'] = 'environment' rougailconfig['step.user_data'] = ['environment']
rougailconfig['main_namespace'] = None
dirs = [str(test_dir / 'dictionaries' / 'rougail')] dirs = [str(test_dir / 'dictionaries' / 'rougail')]
rougailconfig['custom_types']['custom'] = CustomOption rougailconfig['custom_types']['custom'] = CustomOption
rougailconfig['main_dictionaries'] = dirs rougailconfig['main_dictionaries'] = dirs
return rougailconfig return rougailconfig
#def option_value(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(option_value(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 option_value(value, key_is_option)
# elif key_is_option:
# yield option, value
# else:
# yield option.path(), value
def test_dictionaries_warning(test_dir): def test_dictionaries_warning(test_dir):
rougailconfig = _test_dictionaries(test_dir) rougailconfig = _test_dictionaries(test_dir)
# loads the config in the tiramisu's meaning # loads the config in the tiramisu's meaning
config = Rougail(rougailconfig).run() #Config(optiondescription["option_0"]) config = Rougail(rougailconfig).run() #Config(optiondescription["option_0"])
print(type(config)) config_dict = dict(config.value.get())
# config_dict = dict(option_value(config.value.get())) #print(config_dict)
# print(config_dict) user_datas = {'errors': [],
'warnings': [],
}
# loading the env file
envfile = Path('tests') / 'envvars' / '00_10_env_var' / 'env'
load_dotenv(envfile)
environment = RougailUserDataEnvironment(config,
rougailconfig=rougailconfig,
user_datas=user_datas,
)
environment.run()
new_config = environment.config
#print(user_datas)
#print(os.getenv("ROUGAIL_VARNAME23"))
new_config_dict = dict(new_config.value.get())
# print(new_config_dict)
assert str(new_config_dict) == """{<TiramisuOption path="varname23">: ['18', '25'], <TiramisuOption path="varname34">: [58, 22], <TiramisuOption path="varname1">: 'titi', <TiramisuOption path="varint">: 2, <TiramisuOption path="varname2">: 'tata', <TiramisuOption path="myfamily">: {<TiramisuOption path="myfamily.varname3">: 'egg'}}"""
#def test_dictionaries_asciidoc(test_dir): #def test_dictionaries_asciidoc(test_dir):
# _test_dictionaries(test_dir, 'asciidoc', True) # _test_dictionaries(test_dir, 'asciidoc', True)