From cb51efa2bbd7d93c36249ed794dd66dc310d0b4d Mon Sep 17 00:00:00 2001 From: X3nom <100533068+X3nom@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:34:59 +0100 Subject: [PATCH] update readme, parameters --- README.md | 18 +++++++++++++ src/testcase-help.py | 64 ++++++++++++++++++++++++++++++-------------- test/testfile.txt | 3 ++- 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1c45267..9f79bdf 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ # domjudge-testcase-tools Tooling for checking correct file encodings, and correctness of testcases + +## usage +``` +usage: testcase-help.py [-h] [-V] [-r] [-f] [-g CMD] [-c CMD] PATH + +positional arguments: + PATH Path to a file or directory + +options: + -h, --help show this help message and exit + -V, --verbose enable verbose output + -r, --recursive process directories recursively + -f, --fix-encoding fix encoding of file(s) to UTF-8 LF + -g CMD, --generate-results CMD + run CMD on every in.txt and generate out.txt + -c CMD, --check-results CMD + run CMD for in.txt and verify correctness of out.txt +``` \ No newline at end of file diff --git a/src/testcase-help.py b/src/testcase-help.py index 6617a02..ac18a74 100644 --- a/src/testcase-help.py +++ b/src/testcase-help.py @@ -3,19 +3,29 @@ import glob import chardet +VERBOSE = False # global variable holding whether do verbose prints or not +verb_print = lambda message: print(message) if VERBOSE else None + + def fix_encoding_file(file_path): ''' Change file's encoding to UTF-8 LE (the UNIX standard) ''' + verb_print(f" fixing encoding of {file_path}") + with open(file_path, "rb") as file: raw_data = file.read() detected_encoding = chardet.detect(raw_data)["encoding"] # Read the file with the detected encoding - with open(file_path, "r", encoding=detected_encoding) as file: - content = file.read() + try: + with open(file_path, "r", encoding=detected_encoding) as file: + content = file.read() + except: + verb_print(f"ERROR: skipping \"{file_path}\", the file could not be processed!") + return 1 # Normalize line endings to LF and prepare UTF-8 content normalized_content = content.replace("\r\n", "\n").replace("\r", "\n") @@ -37,51 +47,65 @@ def fix_encoding_dir(path, recursive=False): def fix_encoding(args :argparse.Namespace): - print(f"Fixing encoding of {args.path}") - - if args.dir: - if not os.path.isdir(args.path): - print(f"Error: {args.path} is not a valid directory!") - return 1 + verb_print(f"TASK: fix-encoding of {args.path}") + + if os.path.isdir(args.path): + verb_print(f" directory detected, processing") fix_encoding_dir(args.path, args.recursive) - else: - if not os.path.isfile(args.path): - print(f"Error: {args.path} is not a valid file!") - return 1 + elif os.path.isfile(args.path): fix_encoding_file(args.path) - print("Success!") + else: + print(f"Error: {args.path} is not a valid file or directory!") + return 1 + + + verb_print("TASK DONE!") + + def main(): + global VERBOSE arg_parser = argparse.ArgumentParser() arg_parser.add_argument("path", metavar="PATH", help="Path to a file or directory") arg_parser.add_argument( - "-d", "--dir", + "-V", "--verbose", action="store_true", - help="Treat the path as a directory (default: treat as a file)" + help="enable verbose output" ) - arg_parser.add_argument( - "-rec", "--recursive", + "-r", "--recursive", action="store_true", - help="Process directories recursively" + help="process directories recursively" ) - arg_parser.add_argument( - "-fe", "--fix-encoding", + "-f", "--fix-encoding", action="store_true", help="fix encoding of file(s) to UTF-8 LF" ) + arg_parser.add_argument( + "-g", "--generate-results", + metavar="CMD", + help="run CMD on every in.txt and generate out.txt" + ) + arg_parser.add_argument( + "-c", "--check-results", + metavar="CMD", + help="run CMD for in.txt and verify correctness of out.txt" + ) + args = arg_parser.parse_args() + VERBOSE = args.verbose + if args.fix_encoding: if fix_encoding(args) == 1: return 1 diff --git a/test/testfile.txt b/test/testfile.txt index 6bb70ad..4020a1d 100644 --- a/test/testfile.txt +++ b/test/testfile.txt @@ -1,2 +1,3 @@ -asdfbn;l +adfbn;l as;dfk +sd \ No newline at end of file