Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions pathschema/__main__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import argparse
from html import parser
import json
import sys
from functools import partial
from pathlib import Path

from colorama import Fore, Style

from pathschema import validate
from pathschema.models import ValidationResult
from pathschema.exceptions import SchemaError

from pathschema.models import ValidationResult


def parse_arguments():
parser = argparse.ArgumentParser(description="Validate a directory against a schema")
parser.add_argument('schema', type=Path, help='Path to schema file')
parser.add_argument('directory', type=Path, help='Path to directory to validate')
parser.add_argument('--errors-only', action='store_true', default=False, help='Only show errors')
parser.add_argument('-e', '--errors-only', action='store_true', default=False, help='Only show errors')
parser.add_argument('-q', '--quiet', action='count', default=0, help='Do not print results. If repeated, do not print schema errors either.')
group = parser.add_argument_group('format options')
group.add_argument('-j', '--json', action='count', default=False, help='Print results as JSON. If repeated, pretty-print results as JSON.')
group.add_argument('-p', '--posix-paths', action='store_true', default=False, help='Format printed result paths as POSIX')
return parser.parse_args()


printerr = partial(print, file=sys.stderr)

def main(args):

def main():
args = parse_arguments()

schema = ''
with open(args.schema, 'r') as f:
Expand All @@ -27,21 +38,36 @@ def main(args):
try:
result = validate(args.directory, schema)
except SchemaError as e:
print('Error in schema definition')
print(str(e))
exit(1)
if args.quiet < 2:
printerr('Error in schema definition')
printerr(str(e))
exit(-1)

if args.quiet < 1:
kwargs = {
'errors_only': args.errors_only,
'as_posix': args.posix_paths,
}
if args.json:
print_results_json(result, **kwargs, pretty=(args.json > 1))
else:
print_results(result, **kwargs)

print_results(result, errors_only=args.errors_only)
num_errors = sum(len(errors) for errors in result.errors_by_path.values())
exit(num_errors)

if(result.has_error()):
exit(1)
else:
exit(0)

def print_results_json(results: ValidationResult, errors_only=False, as_posix=False, pretty=False):
"""Prints the validation results to the console as JSON"""
format_path = Path.as_posix if as_posix else str
data = {format_path(path): errors for (path, errors) in results.errors_by_path.items() if not errors_only or errors}
kwargs = {'indent': '\t', 'sort_keys': True} if pretty else {}
print(json.dumps(data, **kwargs))


def print_results(results: ValidationResult, errors_only=False):
def print_results(results: ValidationResult, errors_only=False, as_posix=False):
"""Prints the validation results to the console in a human readable way"""
format_path = Path.as_posix if as_posix else str

print()

Expand All @@ -57,7 +83,7 @@ def print_results(results: ValidationResult, errors_only=False):
if errors_only : continue
print(' OK ', end='')

print(f'{path}{Style.RESET_ALL}')
print(f'{format_path(path)}{Style.RESET_ALL}')

for error in errors:
print(f'{Fore.RED}\t{error}{Style.RESET_ALL}')
Expand All @@ -77,5 +103,4 @@ def print_results(results: ValidationResult, errors_only=False):


if __name__ == '__main__':
args = parse_arguments()
main(args)
main()
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ classifiers = [
"Topic :: Utilities",
]

[project.scripts]
pathschema = "pathschema.__main__:main"

[project.urls]
"Source" = "https://github.com/Apollo-Roboto/python-pathschema"