Skip to content

Commit

Permalink
Add --exclude and --verbose options
Browse files Browse the repository at this point in the history
  • Loading branch information
cblakkan committed Dec 30, 2024
1 parent e82f601 commit 9b7a74e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,26 @@ looking up the directory tree until it finds one. If Yamale can not find a schem
Usage:

```bash
usage: yamale [-h] [-s SCHEMA] [-n CPU_NUM] [-p PARSER] [--no-strict] [PATH]
usage: yamale [-h] [-s SCHEMA] [-e PATTERN] [-p PARSER] [-n CPU_NUM] [-x] [-v] [-V] [PATH ...]

Validate yaml files.

positional arguments:
PATH folder to validate. Default is current directory.
PATH paths to validate, either directories or files. Default is the current directory.

optional arguments:
options:
-h, --help show this help message and exit
-s SCHEMA, --schema SCHEMA
filename of schema. Default is schema.yaml.
-n CPU_NUM, --cpu-num CPU_NUM
number of CPUs to use. Default is 4.
-e PATTERN, --exclude PATTERN
Python regex used to exclude files from validation. Any substring match of a files absolute path will be excluded. Uses deafult Python3 regex. Option can be supplied multiple times.
-p PARSER, --parser PARSER
YAML library to load files. Choices are "ruamel" or
"pyyaml" (default).
--no-strict Disable strict mode, unexpected elements in the data
will be accepted.
YAML library to load files. Choices are "ruamel" or "pyyaml" (default).
-n CPU_NUM, --cpu-num CPU_NUM
number of child processes to spawn for validation. Default is 4. 'auto' to use CPU count
-x, --no-strict disable strict mode, unexpected elements in the data will be accepted.
-v, --verbose show verbose information
-V, --version show program's version number and exit
```
### API
Expand Down
14 changes: 7 additions & 7 deletions yamale/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _find_schema(data_path, schema_name):
return _find_data_path_schema(data_path, schema_name)


def _validate_single(yaml_path, schema_name, parser, strict, should_exclude):
def _validate_file(yaml_path, schema_name, parser, strict, should_exclude):
if should_exclude(yaml_path):
return
print("Validating %s..." % yaml_path)
Expand Down Expand Up @@ -103,7 +103,7 @@ def _validate_dir(root, schema_name, cpus, parser, strict, should_exclude):
raise ValueError("\n----\n".join(set(error_messages)))


def _router(paths, schema_name, cpus, parser, excludes, strict=True, verbose=False):
def _router(paths, schema_name, cpus, parser, excludes=None, strict=True, verbose=False):
EXCLUDE_REGEXES = tuple(re.compile(e) for e in excludes) if excludes else tuple()

def should_exclude(yaml_path):
Expand All @@ -117,7 +117,7 @@ def should_exclude(yaml_path):
if os.path.isdir(path):
_validate_dir(path, schema_name, cpus, parser, strict, should_exclude)
else:
_validate_single(path, schema_name, parser, strict, should_exclude)
_validate_file(path, schema_name, parser, strict, should_exclude)


def main():
Expand All @@ -132,7 +132,7 @@ def int_or_auto(num_cpu):
metavar="PATH",
default=["./"],
nargs="*",
help="Paths to validate, either directories or files. Default is the current directory.",
help="paths to validate, either directories or files. Default is the current directory.",
)
parser.add_argument("-s", "--schema", default="schema.yaml", help="filename of schema. Default is schema.yaml.")
parser.add_argument(
Expand All @@ -153,15 +153,15 @@ def int_or_auto(num_cpu):
"--cpu-num",
default=4,
type=int_or_auto,
help="Number of child processes to spawn for validation. Default is 4. 'auto' to use CPU count",
help="number of child processes to spawn for validation. Default is 4. 'auto' to use CPU count",
)
parser.add_argument(
"-x",
"--no-strict",
action="store_true",
help="Disable strict mode, unexpected elements in the data will be accepted.",
help="disable strict mode, unexpected elements in the data will be accepted.",
)
parser.add_argument("-v", "--verbose", action="store_true", help="Show verbose information")
parser.add_argument("-v", "--verbose", action="store_true", help="show verbose information")
parser.add_argument("-V", "--version", action="version", version=__version__)
args = parser.parse_args()
try:
Expand Down
13 changes: 13 additions & 0 deletions yamale/tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ def test_multiple_paths_bad_yaml():
assert "map.bad: '12.5' is not a int." in e.value.message


def test_excludes():
command_line._router(
paths=[
"yamale/tests/command_line_fixtures/yamls/good.yaml",
"yamale/tests/command_line_fixtures/yamls/bad.yaml",
],
schema_name="schema.yaml",
excludes="bad.yaml",
cpus=1,
parser="PyYAML",
)


@pytest.mark.parametrize("parser", parsers)
def test_good_relative_yaml(parser):
command_line._router(
Expand Down

0 comments on commit 9b7a74e

Please sign in to comment.