-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a basic CLI for converting recipe files
- Usage: `percy convert [-o output] FILE` - Error and warning messages dump to `STDERR` by default (like `curl` does) - By default the converted recipe is dumped to `STDOUT`. The `-o` option allows the user to specify an output file.
- Loading branch information
1 parent
8b6aae4
commit 96062ee
Showing
4 changed files
with
95 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# This surpresses deprecation `pytest` warnings related to using `conda.*` packages. | ||
# TODO Future: remove/upgrade deprecated packages | ||
[pytest] | ||
filterwarnings = | ||
ignore:conda.* is pending deprecation:PendingDeprecationWarning | ||
ignore:conda.* is deprecated:DeprecationWarning | ||
ignore::DeprecationWarning | ||
ignore::PendingDeprecationWarning |
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,69 @@ | ||
""" | ||
File: convert.py | ||
Description: CLI for converting an old recipe file to the "new" format. | ||
""" | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
from typing import Final | ||
|
||
import click | ||
|
||
from percy.parser.recipe_parser import RecipeParser | ||
from percy.parser.types import MessageCategory, MessageTable | ||
|
||
# Required file name for the recipe, specified in CEP-13 | ||
NEW_FORMAT_RECIPE_FILE_NAME: Final[str] = "recipe.yaml" | ||
|
||
|
||
def print_out(*args, **kwargs): | ||
""" | ||
Convenience wrapper that prints to STDOUT | ||
""" | ||
print(*args, file=sys.stdout, **kwargs) | ||
|
||
|
||
def print_err(*args, **kwargs): | ||
""" | ||
Convenience wrapper that prints to STDERR | ||
""" | ||
print(*args, file=sys.stderr, **kwargs) | ||
|
||
|
||
def print_messages(category: MessageCategory, msg_tbl: MessageTable): | ||
""" | ||
Convenience function for dumping a series of messages of a certain category | ||
:param category: Category of messages to print | ||
:param msg_tbl: `MessageTable` instance containing the messages to print | ||
""" | ||
msgs: Final[list[str]] = msg_tbl.get_messages(category) | ||
for msg in msgs: | ||
print_err(f"[{category.upper()}]: {msg}") | ||
|
||
|
||
@click.command(short_help="Converts a `meta.yaml` formatted-recipe file to the new `recipe.yaml` format") | ||
@click.argument("file", type=click.File("r", encoding="utf-8")) | ||
@click.option("--output", "-o", type=click.Path(exists=False), default=None, help="File to dump a new recipe to.") | ||
def convert(file: click.File, output: click.Path) -> None: # pylint: disable=redefined-outer-name | ||
""" | ||
Recipe conversion CLI utility. By default, recipes print to STDOUT. Messages always print to STDERR. | ||
""" | ||
recipe_content: Final[str] = file.read() | ||
|
||
parser = RecipeParser(recipe_content) | ||
result, msg_tbl = parser.render_to_new_recipe_format() | ||
|
||
if output is None: | ||
print_out(result) | ||
else: | ||
if not os.path.basename(output) == "recipe.yaml": | ||
print_err("WARNING: File is not called `recipe.yaml`.") | ||
with open(output, "w", encoding="utf-8") as fptr: | ||
fptr.write(result) | ||
|
||
summary: Final[str] = msg_tbl.get_totals_message() | ||
if summary: | ||
print_messages(MessageCategory.WARNING, msg_tbl) | ||
print_messages(MessageCategory.ERROR, msg_tbl) | ||
print_err(summary) |
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,22 @@ | ||
""" | ||
File: test_convert_cli.py | ||
Description: Tests the `convert` CLI | ||
""" | ||
from click.testing import CliRunner | ||
|
||
from percy.commands.convert import convert | ||
|
||
|
||
def test_usage() -> None: | ||
""" | ||
Ensure failure to provide a sub-command results in rendering the help menu | ||
""" | ||
runner = CliRunner() | ||
# No commands are provided | ||
result = runner.invoke(convert, []) | ||
assert result.exit_code != 0 | ||
assert result.output.startswith("Usage:") | ||
# Help is specified | ||
result = runner.invoke(convert, ["--help"]) | ||
assert result.exit_code == 0 | ||
assert result.output.startswith("Usage:") |