feat: yaml.filename could be the name of a directory
This commit is contained in:
parent
d75e447ec9
commit
4d3d742a68
3 changed files with 70 additions and 42 deletions
|
|
@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ruamel.yaml import YAML
|
from ruamel.yaml import YAML
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from rougail.error import ExtentionError
|
from rougail.error import ExtentionError
|
||||||
from tiramisu.error import ValueOptionError, PropertiesOptionError, LeadershipError
|
from tiramisu.error import ValueOptionError, PropertiesOptionError, LeadershipError
|
||||||
|
|
@ -56,6 +57,12 @@ class RougailUserDataYaml:
|
||||||
self.yaml = YAML()
|
self.yaml = YAML()
|
||||||
user_datas = []
|
user_datas = []
|
||||||
for idx, filename in enumerate(self.filenames):
|
for idx, filename in enumerate(self.filenames):
|
||||||
|
filename = Path(filename)
|
||||||
|
if filename.is_file():
|
||||||
|
filenames = [filename]
|
||||||
|
else:
|
||||||
|
filenames = list(filename.glob('*.yml')) + list(filename.glob('*.yaml'))
|
||||||
|
for filename in sorted(filenames):
|
||||||
file_values = self.open(filename)
|
file_values = self.open(filename)
|
||||||
if not file_values:
|
if not file_values:
|
||||||
continue
|
continue
|
||||||
|
|
@ -97,7 +104,7 @@ class RougailUserDataYaml:
|
||||||
return user_datas
|
return user_datas
|
||||||
|
|
||||||
def open(self, filename: str) -> dict:
|
def open(self, filename: str) -> dict:
|
||||||
with open(filename) as fh_config:
|
with filename.open() as fh_config:
|
||||||
return self.yaml.load(fh_config)
|
return self.yaml.load(fh_config)
|
||||||
|
|
||||||
def parse(
|
def parse(
|
||||||
|
|
|
||||||
|
|
@ -43,8 +43,6 @@ yaml:
|
||||||
params:
|
params:
|
||||||
allow_relative: True
|
allow_relative: True
|
||||||
test_existence: True
|
test_existence: True
|
||||||
types:
|
|
||||||
- file
|
|
||||||
|
|
||||||
file_with_secrets:
|
file_with_secrets:
|
||||||
description: {_("File that may contain secrets")}
|
description: {_("File that may contain secrets")}
|
||||||
|
|
|
||||||
|
|
@ -163,11 +163,11 @@ def test_errors_2():
|
||||||
|
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
error_env = list((Path(__file__).parent / 'errors' / 'yaml').glob("*.yaml"))
|
error_yaml = list((Path(__file__).parent / 'errors' / 'yaml').glob("*.yaml"))
|
||||||
error_env.sort()
|
error_yaml.sort()
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope="module", params=error_env, ids=idfn)
|
@fixture(scope="module", params=error_yaml, ids=idfn)
|
||||||
def test_file_error(request):
|
def test_file_error(request):
|
||||||
return request.param
|
return request.param
|
||||||
|
|
||||||
|
|
@ -191,3 +191,26 @@ def test_dictionaries_error(test_file_error):
|
||||||
with open(errors_file) as json_file:
|
with open(errors_file) as json_file:
|
||||||
expected_errors = load(json_file)
|
expected_errors = load(json_file)
|
||||||
assert expected_errors == errors, errors_file
|
assert expected_errors == errors, errors_file
|
||||||
|
|
||||||
|
|
||||||
|
def test_dictionaries_directory():
|
||||||
|
test_dir = Path(__file__).parent / 'directory'
|
||||||
|
rougailconfig = get_rougail_config(test_dir / 'structure')
|
||||||
|
##################################
|
||||||
|
rougailconfig['step.user_data'] = ['yaml']
|
||||||
|
rougailconfig['yaml.filename'] = [str(test_dir / 'yaml')]
|
||||||
|
rougail = Rougail(rougailconfig)
|
||||||
|
config = rougail.run()
|
||||||
|
##################################
|
||||||
|
# loads variables in the tiramisu config
|
||||||
|
generated_user_data = RougailUserData(config, rougailconfig=rougailconfig).run()
|
||||||
|
rougail.user_datas(generated_user_data)
|
||||||
|
config.property.read_only()
|
||||||
|
data = dict(config_to_dict(config.value.get()))
|
||||||
|
data_file = test_dir / "result.txt"
|
||||||
|
if not data_file.is_file():
|
||||||
|
with open(data_file, 'a') as json_file:
|
||||||
|
dump(data, json_file, indent=4)
|
||||||
|
with open(data_file) as json_file:
|
||||||
|
expected = load(json_file)
|
||||||
|
assert expected == data, data_file
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue