feat: loads user datas before and after cli params
This commit is contained in:
parent
2bc7e3914c
commit
82e9e5bc30
8 changed files with 102 additions and 44 deletions
|
|
@ -16,11 +16,13 @@ You should have received a copy of the GNU General Public License
|
||||||
along with Mtools. If not, see <http://www.gnu.org/licenses/>.
|
along with Mtools. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from warnings import warn
|
||||||
|
from pathlib import Path
|
||||||
|
from sys import exit
|
||||||
|
|
||||||
from tiramisu_cmdline_parser import TiramisuCmdlineParser
|
from tiramisu_cmdline_parser import TiramisuCmdlineParser
|
||||||
from tiramisu.error import PropertiesOptionError
|
from tiramisu.error import PropertiesOptionError
|
||||||
from tiramisu import Config
|
from tiramisu import Config
|
||||||
from pathlib import Path
|
|
||||||
from sys import exit
|
|
||||||
|
|
||||||
from rougail import Rougail
|
from rougail import Rougail
|
||||||
from rougail.config import get_rougail_config
|
from rougail.config import get_rougail_config
|
||||||
|
|
@ -76,38 +78,44 @@ def _main(arguments, do_not_print):
|
||||||
).run()
|
).run()
|
||||||
)
|
)
|
||||||
if fake_user_datas:
|
if fake_user_datas:
|
||||||
user_data = UserDatas(cmd_config).user_datas(fake_user_datas)
|
user_datas = UserDatas(cmd_config).user_datas(fake_user_datas, return_values_not_error=True)
|
||||||
if user_data["errors"]:
|
else:
|
||||||
raise Exception(user_data["errors"][0])
|
user_datas = {}
|
||||||
# if user_data["warnings"]:
|
|
||||||
# raise Exception(user_data["warnings"][0])
|
|
||||||
parser = TiramisuCmdlineParser(
|
parser = TiramisuCmdlineParser(
|
||||||
cmd_config,
|
cmd_config,
|
||||||
# add_extra_options=False,
|
# add_extra_options=False,
|
||||||
short_name_max_len=2,
|
short_name_max_len=2,
|
||||||
)
|
)
|
||||||
parser.parse_args(arguments)
|
parser.parse_args(arguments)
|
||||||
|
if user_datas:
|
||||||
|
user_data = UserDatas(cmd_config).user_datas(user_datas, user_datas_type="values")
|
||||||
|
if user_data["warnings"]:
|
||||||
|
for warning in user_data["warnings"]:
|
||||||
|
warn(warning)
|
||||||
|
if user_data["errors"]:
|
||||||
|
raise Exception(user_data["errors"][0])
|
||||||
global print_traceback
|
global print_traceback
|
||||||
print_traceback = rougailconfig["cli.debug"]
|
print_traceback = rougailconfig["cli.debug"]
|
||||||
cmd_config.property.setdefault(origin_prop, "read_write", "append")
|
cmd_config.property.setdefault(origin_prop, "read_write", "append")
|
||||||
cmd_config.property.remove("not_for_commandline")
|
cmd_config.property.remove("not_for_commandline")
|
||||||
cmd_config.property.read_only()
|
cmd_config.property.read_only()
|
||||||
if rougailconfig["cli.versions"]:
|
if rougailconfig["cli.versions"]:
|
||||||
|
versions = []
|
||||||
from tiramisu import __version__
|
from tiramisu import __version__
|
||||||
|
|
||||||
print(f"tiramisu: {__version__}")
|
versions.append(f"tiramisu: {__version__}")
|
||||||
from tiramisu_cmdline_parser import __version__
|
from tiramisu_cmdline_parser import __version__
|
||||||
|
|
||||||
print(f"tiramisu-cmdline-parser: {__version__}")
|
versions.append(f"tiramisu-cmdline-parser: {__version__}")
|
||||||
from rougail import __version__
|
from rougail import __version__
|
||||||
|
|
||||||
print(f"rougail: {__version__}")
|
versions.append(f"rougail: {__version__}")
|
||||||
from . import __version__
|
from . import __version__
|
||||||
|
|
||||||
print(f"rougail-cli: {__version__}")
|
versions.append(f"rougail-cli: {__version__}")
|
||||||
for step in ["structural", "user_data", "output"]:
|
for step in ["structural", "user_data", "output"]:
|
||||||
display_step = step.replace("_", "-")
|
display_step = step.replace("_", "-")
|
||||||
for step_name in sorted(cmd_config.option(f"step.{step}").value.list()):
|
for step_name in sorted(cmd_config.unrestraint.option(f"step.{step}").value.list()):
|
||||||
path = (
|
path = (
|
||||||
Path(__file__).parent.parent
|
Path(__file__).parent.parent
|
||||||
/ (step + "_" + step_name)
|
/ (step + "_" + step_name)
|
||||||
|
|
@ -118,11 +126,15 @@ def _main(arguments, do_not_print):
|
||||||
module = load_modules(
|
module = load_modules(
|
||||||
"rougail." + step + "_" + step_name, str(path)
|
"rougail." + step + "_" + step_name, str(path)
|
||||||
)
|
)
|
||||||
print(
|
versions.append(
|
||||||
f"rougail-{display_step}-{step_name}: {module.__version__}"
|
f"rougail-{display_step}-{step_name}: {module.__version__}"
|
||||||
)
|
)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
pass
|
pass
|
||||||
|
if do_not_print:
|
||||||
|
return versions
|
||||||
|
for version in versions:
|
||||||
|
print(version)
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
user_data_names = rougailconfig["step.user_data"]
|
user_data_names = rougailconfig["step.user_data"]
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,4 @@
|
||||||
│ Variable [38;5;220mDefault value[0m │
|
│ Variable [38;5;220mDefault value[0m │
|
||||||
╰────────────────────────╯
|
╰────────────────────────╯
|
||||||
Variables:
|
Variables:
|
||||||
[94m┗━━ [0m📓 my_variable: [38;5;220mmy_value[0m
|
[94m┗━━ [0m📓 a description: [38;5;220mmy_value[0m
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
---
|
---
|
||||||
version: 1.1
|
version: 1.1
|
||||||
|
|
||||||
my_variable: my_value
|
my_variable: my_value # a description
|
||||||
|
|
|
||||||
1
tests/cli/versions.txt
Normal file
1
tests/cli/versions.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
["tiramisu: 5.2.0a9", "tiramisu-cmdline-parser: 0.7.0a1", "rougail: 1.2.0a24", "rougail-cli: 0.2.0a17", "rougail-structural-bitwarden: 0.1.0a4", "rougail-user-data-ansible: 0.1.0a3", "rougail-user-data-bitwarden: 0.1.0a20", "rougail-user-data-environment: 0.1.0a8", "rougail-user-data-yaml: 0.2.0a9", "rougail-output-ansible: 0.2.0a8", "rougail-output-console: 0.2.0a11", "rougail-output-doc: 0.2.0a21", "rougail-output-json: 0.2.0a8"]
|
||||||
3
tests/second_step/.rougailcli.yml
Normal file
3
tests/second_step/.rougailcli.yml
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
doc:
|
||||||
|
output_format: json
|
||||||
4
tests/second_step/structures/file.yml
Normal file
4
tests/second_step/structures/file.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
version: 1.1
|
||||||
|
|
||||||
|
my_variable: my_value # a description
|
||||||
1
tests/second_step/yaml.txt
Normal file
1
tests/second_step/yaml.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
"{\n \"my_variable\": {\n \"type\": \"variable\",\n \"default\": \"my_value\",\n \"properties\": [\n {\n \"type\": \"type\",\n \"name\": \"string\"\n },\n {\n \"type\": \"property\",\n \"name\": \"mandatory\"\n }\n ],\n \"paths\": [\n \"my_variable\"\n ],\n \"names\": [\n \"my_variable\"\n ],\n \"descriptions\": [\n \"A description.\"\n ]\n }\n}"
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from os import chdir
|
from contextlib import chdir
|
||||||
|
from json import dumps, loads
|
||||||
|
|
||||||
from rougail.cli.__main__ import main
|
from rougail.cli.__main__ import main
|
||||||
|
|
||||||
|
|
@ -8,36 +9,72 @@ test_dir = Path(__file__).parent
|
||||||
|
|
||||||
|
|
||||||
def test_cli():
|
def test_cli():
|
||||||
chdir(test_dir / 'cli')
|
with chdir(test_dir / 'cli'):
|
||||||
ret = main(['--main_dictionaries', 'structures'], do_not_print=True)
|
ret = main(['--main_dictionaries', 'structures'], do_not_print=True)
|
||||||
filename = Path('result.txt')
|
filename = Path('result.txt')
|
||||||
if not filename.is_file():
|
if not filename.is_file():
|
||||||
with filename.open('w') as fh:
|
with filename.open('w') as fh:
|
||||||
fh.write(ret[1])
|
fh.write(ret[1])
|
||||||
with filename.open() as fh:
|
with filename.open() as fh:
|
||||||
data = fh.read()
|
data = fh.read()
|
||||||
assert ret == (True, data)
|
assert ret == (True, data)
|
||||||
|
|
||||||
|
|
||||||
def test_cli_boolean():
|
def test_cli_boolean():
|
||||||
chdir(test_dir / 'cli')
|
with chdir(test_dir / 'cli'):
|
||||||
ret = main(['--main_dictionaries', 'structures', '--console.mandatory'], do_not_print=True)
|
ret = main(['--main_dictionaries', 'structures', '--console.mandatory'], do_not_print=True)
|
||||||
filename = Path('result.txt')
|
filename = Path('result.txt')
|
||||||
if not filename.is_file():
|
if not filename.is_file():
|
||||||
with filename.open('w') as fh:
|
with filename.open('w') as fh:
|
||||||
fh.write(ret[1])
|
fh.write(ret[1])
|
||||||
with filename.open() as fh:
|
with filename.open() as fh:
|
||||||
data = fh.read()
|
data = fh.read()
|
||||||
assert ret == (True, data)
|
assert ret == (True, data)
|
||||||
|
|
||||||
|
|
||||||
def test_cli_boolean_2():
|
def test_cli_boolean_no():
|
||||||
chdir(test_dir / 'cli')
|
with chdir(test_dir / 'cli'):
|
||||||
ret = main(['--main_dictionaries', 'structures', '--console.no-mandatory'], do_not_print=True)
|
ret = main(['--main_dictionaries', 'structures', '--console.no-mandatory'], do_not_print=True)
|
||||||
filename = Path('result.txt')
|
filename = Path('result.txt')
|
||||||
if not filename.is_file():
|
if not filename.is_file():
|
||||||
with filename.open('w') as fh:
|
with filename.open('w') as fh:
|
||||||
fh.write(ret[1])
|
fh.write(ret[1])
|
||||||
with filename.open() as fh:
|
with filename.open() as fh:
|
||||||
data = fh.read()
|
data = fh.read()
|
||||||
assert ret == (True, data)
|
assert ret == (True, data)
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_version():
|
||||||
|
with chdir(test_dir / 'cli'):
|
||||||
|
ret = main(['--cli.version'], do_not_print=True)
|
||||||
|
filename = Path('versions.txt')
|
||||||
|
if not filename.is_file():
|
||||||
|
with filename.open('w') as fh:
|
||||||
|
fh.write(dumps(ret))
|
||||||
|
with filename.open() as fh:
|
||||||
|
data = loads(fh.read())
|
||||||
|
assert ret == data
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_version_user_data_disabled():
|
||||||
|
with chdir(test_dir / 'cli'):
|
||||||
|
ret = main(['--cli.version', '--step.output', 'doc'], do_not_print=True)
|
||||||
|
filename = Path('versions.txt')
|
||||||
|
if not filename.is_file():
|
||||||
|
with filename.open('w') as fh:
|
||||||
|
fh.write(dumps(ret))
|
||||||
|
with filename.open() as fh:
|
||||||
|
data = loads(fh.read())
|
||||||
|
assert ret == data
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_version_user_data_disabled():
|
||||||
|
with chdir(test_dir / 'second_step'):
|
||||||
|
ret = main(['--main_dictionaries', 'structures', '--step.output', 'doc'], do_not_print=True)
|
||||||
|
filename = Path('yaml.txt')
|
||||||
|
if not filename.is_file():
|
||||||
|
with filename.open('w') as fh:
|
||||||
|
fh.write(dumps(ret[1]))
|
||||||
|
with filename.open() as fh:
|
||||||
|
data = loads(fh.read())
|
||||||
|
assert ret == (True, data)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue