-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(cli): add command to export rules context in current environm…
…ent (#492) Following up #491. This PR adds a new CLI command to allow export rules context in the current environment. It aims to make rules writing easier: ```sh > qdt export-rules-context -o qdt_rules_context.json Rules context exported in qdt_rules_context.json ```
- Loading branch information
Showing
9 changed files
with
166 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +0,0 @@ | ||
#! python3 # noqa: E265 | ||
|
||
# submodules | ||
from .deployment import parser_main_deployment # noqa: F401 | ||
from .upgrade import parser_upgrade # noqa: F401 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#! python3 # noqa: E265 | ||
|
||
|
||
""" | ||
Sub-command to export local rules context. | ||
Author: Julien M. (https://github.com/guts) | ||
""" | ||
|
||
|
||
# ############################################################################ | ||
# ########## IMPORTS ############# | ||
# ################################ | ||
|
||
# standard library | ||
import argparse | ||
import logging | ||
from pathlib import Path | ||
|
||
# package | ||
from qgis_deployment_toolbelt.constants import get_qdt_working_directory | ||
from qgis_deployment_toolbelt.profiles.rules_context import QdtRulesContext | ||
from qgis_deployment_toolbelt.utils.bouncer import exit_cli_error, exit_cli_success | ||
|
||
# ############################################################################ | ||
# ########## GLOBALS ############# | ||
# ################################ | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# ############################################################################ | ||
# ########## CLI ################# | ||
# ################################ | ||
|
||
|
||
def parser_rules_context_export( | ||
subparser: argparse.ArgumentParser, | ||
) -> argparse.ArgumentParser: | ||
"""Set the argument parser for subcommand. | ||
Args: | ||
subparser (argparse.ArgumentParser): parser to set up | ||
Returns: | ||
argparse.ArgumentParser: parser ready to use | ||
""" | ||
|
||
subparser.add_argument( | ||
"-o", | ||
"--output", | ||
help="Path to the output file where to write rules context.", | ||
default=get_qdt_working_directory().joinpath("export/qdt_rules_context.json"), | ||
type=Path, | ||
dest="output_path", | ||
) | ||
|
||
subparser.set_defaults(func=run) | ||
|
||
return subparser | ||
|
||
|
||
# ############################################################################ | ||
# ########## MAIN ################ | ||
# ################################ | ||
|
||
|
||
def run(args: argparse.Namespace): | ||
"""Run the sub command logic. | ||
Open result of a previous command. | ||
Args: | ||
args (argparse.Namespace): arguments passed to the subcommand | ||
""" | ||
logger.debug(f"Running {args.command} with {args}") | ||
|
||
try: | ||
context_json_path = Path(args.output_path) | ||
context_json_path.parent.mkdir(parents=True, exist_ok=True) | ||
rules_context = QdtRulesContext() | ||
|
||
# write into the file passing extra parameters to json.dumps | ||
with context_json_path.open("w", encoding="UTF8") as wf: | ||
wf.write(rules_context.to_json(indent=4, sort_keys=True)) | ||
|
||
# exit nicely | ||
print(f"Rules context exported in {args.output_path}") | ||
exit_cli_success(f"Rules context exported in {args.output_path}") | ||
except Exception as err: | ||
exit_cli_error(err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,6 @@ | |
|
||
|
||
class QdtRulesContext: | ||
pass | ||
|
||
@property | ||
def _context_date(self) -> dict: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#! python3 # noqa: E265 | ||
|
||
""" | ||
Test CLI's rules context export command. | ||
Author: Julien Moura (Oslandia) | ||
""" | ||
|
||
# ############################################################################# | ||
# ########## Libraries ############# | ||
# ################################## | ||
|
||
|
||
# 3rd party | ||
import pytest | ||
|
||
# project | ||
from qgis_deployment_toolbelt import cli | ||
|
||
# ############################################################################# | ||
# ######## Classes ################# | ||
# ################################## | ||
|
||
|
||
@pytest.mark.parametrize("option", ("-h", "--help")) | ||
def test_cli_export_rules_context_help(capsys, option): | ||
"""Test CLI help.""" | ||
with pytest.raises(SystemExit): | ||
cli.main(["export-rules-context", option]) | ||
|
||
_, err = capsys.readouterr() | ||
|
||
assert err == "" | ||
|
||
|
||
def test_cli_export_rules_context(capsys): | ||
"""Test CLI.""" | ||
with pytest.raises(SystemExit): | ||
cli.main(["export-rules-context"]) | ||
|
||
_, err = capsys.readouterr() | ||
|
||
assert err == "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters