From dd61d9b6a3be3416dfa7e38a92a6af798f51ee0b Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 21:17:14 -0600 Subject: [PATCH 1/8] Print schema errors to stderr instead of stdout --- pathschema/__main__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index ef8982e..025e56b 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -1,11 +1,13 @@ import argparse +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(): @@ -16,6 +18,8 @@ def parse_arguments(): return parser.parse_args() +printerr = partial(print, file=sys.stderr) + def main(args): @@ -27,8 +31,8 @@ def main(args): try: result = validate(args.directory, schema) except SchemaError as e: - print('Error in schema definition') - print(str(e)) + printerr('Error in schema definition') + printerr(str(e)) exit(1) print_results(result, errors_only=args.errors_only) From f1d60e0c6248d9cd41741809f92b93205caeed34 Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 21:19:17 -0600 Subject: [PATCH 2/8] Add --quiet/-q CLI option --- pathschema/__main__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index 025e56b..326284f 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -15,6 +15,7 @@ def parse_arguments(): 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('--quiet', '-q', action='count', default=0, help='Do not print results. If repeated, do not print schema errors either.') return parser.parse_args() @@ -31,11 +32,13 @@ def main(args): try: result = validate(args.directory, schema) except SchemaError as e: - printerr('Error in schema definition') - printerr(str(e)) + if args.quiet < 2: + printerr('Error in schema definition') + printerr(str(e)) exit(1) - print_results(result, errors_only=args.errors_only) + if args.quiet < 1: + print_results(result, errors_only=args.errors_only) if(result.has_error()): exit(1) From f29ce3bd2fdff4bb8c7368f7d9f1cba5acf2ae81 Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 21:20:56 -0600 Subject: [PATCH 3/8] Differeniate exit codes --- pathschema/__main__.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index 326284f..401b8f2 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -35,15 +35,13 @@ def main(args): if args.quiet < 2: printerr('Error in schema definition') printerr(str(e)) - exit(1) + exit(-1) if args.quiet < 1: print_results(result, errors_only=args.errors_only) - if(result.has_error()): - exit(1) - else: - exit(0) + num_errors = sum(len(errors) for errors in result.errors_by_path.values()) + exit(num_errors) From fe8248a7ecf45decc9c7ee825b7fd641c8125d1f Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 22:24:36 -0600 Subject: [PATCH 4/8] Add option to print results as JSON --- pathschema/__main__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index 401b8f2..68a67c3 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -1,4 +1,5 @@ import argparse +import json import sys from functools import partial from pathlib import Path @@ -16,6 +17,7 @@ def parse_arguments(): 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('--quiet', '-q', action='count', default=0, help='Do not print results. If repeated, do not print schema errors either.') + parser.add_argument('--json', '-j', action='count', default=False, help='Print results as JSON. If repeated, pretty-print results as JSON.') return parser.parse_args() @@ -38,12 +40,21 @@ def main(args): exit(-1) if args.quiet < 1: - print_results(result, errors_only=args.errors_only) + if args.json: + print_results_json(result, errors_only=args.errors_only, pretty=(args.json > 1)) + else: + print_results(result, errors_only=args.errors_only) num_errors = sum(len(errors) for errors in result.errors_by_path.values()) exit(num_errors) +def print_results_json(results: ValidationResult, errors_only=False, pretty=False): + """Prints the validation results to the console as JSON""" + data = {str(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): """Prints the validation results to the console in a human readable way""" From 05cc4a0cd1a5ce39ea5f29878b7564e1bc284ea3 Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 22:26:47 -0600 Subject: [PATCH 5/8] Organize short options for CLI flags --- pathschema/__main__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index 68a67c3..44a8f19 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -15,9 +15,9 @@ 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('--quiet', '-q', action='count', default=0, help='Do not print results. If repeated, do not print schema errors either.') - parser.add_argument('--json', '-j', action='count', default=False, help='Print results as JSON. If repeated, pretty-print results as JSON.') + parser.add_argument('-e', '--errors-only', action='store_true', default=False, help='Only show errors') + parser.add_argument('-j', '--json', action='count', default=False, help='Print results as JSON. If repeated, pretty-print results as JSON.') + parser.add_argument('-q', '--quiet', action='count', default=0, help='Do not print results. If repeated, do not print schema errors either.') return parser.parse_args() From b438476a54d36adaa3ec5650916e6f54f863cb48 Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 22:35:29 -0600 Subject: [PATCH 6/8] Add option to format printed result paths as POSIX --- pathschema/__main__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index 44a8f19..9bf51a6 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -17,6 +17,7 @@ def parse_arguments(): parser.add_argument('directory', type=Path, help='Path to directory to validate') parser.add_argument('-e', '--errors-only', action='store_true', default=False, help='Only show errors') parser.add_argument('-j', '--json', action='count', default=False, help='Print results as JSON. If repeated, pretty-print results as JSON.') + parser.add_argument('-p', '--posix-paths', action='store_true', default=False, help='Format printed result paths as POSIX') parser.add_argument('-q', '--quiet', action='count', default=0, help='Do not print results. If repeated, do not print schema errors either.') return parser.parse_args() @@ -40,24 +41,30 @@ def main(args): exit(-1) if args.quiet < 1: + kwargs = { + 'errors_only': args.errors_only, + 'as_posix': args.posix_paths, + } if args.json: - print_results_json(result, errors_only=args.errors_only, pretty=(args.json > 1)) + print_results_json(result, **kwargs, pretty=(args.json > 1)) else: - print_results(result, errors_only=args.errors_only) + print_results(result, **kwargs) num_errors = sum(len(errors) for errors in result.errors_by_path.values()) exit(num_errors) -def print_results_json(results: ValidationResult, errors_only=False, pretty=False): +def print_results_json(results: ValidationResult, errors_only=False, as_posix=False, pretty=False): """Prints the validation results to the console as JSON""" - data = {str(path): errors for (path, errors) in results.errors_by_path.items() if not errors_only or errors} + 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() @@ -73,7 +80,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}') From a9eda994d6796201fc0f09443f16c2c9cbbe9218 Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 22:42:57 -0600 Subject: [PATCH 7/8] Reconfigure project to build a console script upon installation --- pathschema/__main__.py | 6 +++--- pyproject.toml | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index 9bf51a6..93c45fe 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -25,7 +25,8 @@ def parse_arguments(): printerr = partial(print, file=sys.stderr) -def main(args): +def main(): + args = parse_arguments() schema = '' with open(args.schema, 'r') as f: @@ -100,5 +101,4 @@ def print_results(results: ValidationResult, errors_only=False, as_posix=False): if __name__ == '__main__': - args = parse_arguments() - main(args) + main() diff --git a/pyproject.toml b/pyproject.toml index 9c26808..4ab25aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,5 +33,8 @@ classifiers = [ "Topic :: Utilities", ] +[project.scripts] +pathschema = "pathschema.__main__:main" + [project.urls] "Source" = "https://github.com/Apollo-Roboto/python-pathschema" From 1b0e5d485dafcbdc44a0ed56c7f875292584b09e Mon Sep 17 00:00:00 2001 From: Travis LaGrone Date: Thu, 11 Dec 2025 22:45:32 -0600 Subject: [PATCH 8/8] Group CLI options when displaying help --- pathschema/__main__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pathschema/__main__.py b/pathschema/__main__.py index 93c45fe..01e7353 100644 --- a/pathschema/__main__.py +++ b/pathschema/__main__.py @@ -1,4 +1,5 @@ import argparse +from html import parser import json import sys from functools import partial @@ -16,9 +17,10 @@ def parse_arguments(): 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('-e', '--errors-only', action='store_true', default=False, help='Only show errors') - parser.add_argument('-j', '--json', action='count', default=False, help='Print results as JSON. If repeated, pretty-print results as JSON.') - parser.add_argument('-p', '--posix-paths', action='store_true', default=False, help='Format printed result paths as POSIX') 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()