autograder.cli.util.extract-code

Pull the code from a file and output the sanitized version as if it was being graded.

usage: python3 -m autograder.cli.util.extract-code [-h] [--version]
                                                   [--testing-mode]
                                                   [-o OUT_PATH]
                                                   PATH

Pull the code from a file and output the sanitized version as if it was being graded.

positional arguments:
    PATH                Path to the file to extract code from.

options:
    -h, --help          show this help message and exit
    --version           show program's version number and exit
    -o OUT_PATH, --out-path OUT_PATH
                        If specified, the code will be written here instead of
                        output on stdout.

testing options:
    --testing-mode      Run as if a test is being run (default: False).
 1# pylint: disable=invalid-name
 2
 3"""
 4Pull the code from a file and output the sanitized version as if it was being graded.
 5"""
 6
 7import argparse
 8import sys
 9
10import edq.util.code
11import edq.util.dirent
12
13import autograder.cli.parser
14
15def run_cli(args: argparse.Namespace) -> int:
16    """ Run the CLI. """
17
18    text = edq.util.code.extract_code(args.path)
19    ast = edq.util.code.parse_module_code(text)
20    source = edq.util.code.ast_to_source(ast)
21
22    if (args.out_path is None):
23        print(source)
24        return 0
25
26    edq.util.dirent.write_file(args.out_path, source)
27
28    return 0
29
30def main() -> int:
31    """ Get a parser, parse the args, and call run. """
32    return run_cli(_get_parser().parse_args())
33
34def _get_parser() -> argparse.ArgumentParser:
35    parser = autograder.cli.parser.get_parser(__doc__.strip())
36
37    parser.add_argument('path', metavar = 'PATH',
38        action = 'store', type = str,
39        help = 'Path to the file to extract code from.')
40
41    parser.add_argument('-o', '--out-path', dest = 'out_path',
42        action = 'store', type = str, default = None,
43        help = 'If specified, the code will be written here instead of output on stdout.')
44
45    return parser
46
47if (__name__ == '__main__'):
48    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
16def run_cli(args: argparse.Namespace) -> int:
17    """ Run the CLI. """
18
19    text = edq.util.code.extract_code(args.path)
20    ast = edq.util.code.parse_module_code(text)
21    source = edq.util.code.ast_to_source(ast)
22
23    if (args.out_path is None):
24        print(source)
25        return 0
26
27    edq.util.dirent.write_file(args.out_path, source)
28
29    return 0

Run the CLI.

def main() -> int:
31def main() -> int:
32    """ Get a parser, parse the args, and call run. """
33    return run_cli(_get_parser().parse_args())

Get a parser, parse the args, and call run.