quizcomp.cli.parser
Customize an argument parser for the Quiz Composer.
1""" 2Customize an argument parser for the Quiz Composer. 3""" 4 5import argparse 6import os 7import typing 8 9import edq.core.argparser 10import edq.util.dirent 11 12import quizcomp 13import quizcomp.external.katex 14import quizcomp.external.latex 15 16CONFIG_FILENAME: str = 'edq-quiz-composer.json' 17 18def get_parser(description: str, 19 include_net: bool = False, 20 include_katex: bool = False, 21 include_latex: bool = False, 22 ) -> argparse.ArgumentParser: 23 """ 24 Get an argument parser specialized for the Quiz Composer. 25 """ 26 27 config_options = { 28 'config_filename': CONFIG_FILENAME, 29 } 30 31 parser = edq.core.argparser.get_default_parser( 32 description, 33 version = f"v{quizcomp.__version__}", 34 include_net = include_net, 35 config_options = config_options, 36 ) 37 38 if (include_katex): 39 parser.register_callbacks('katex', quizcomp.external.katex.set_cli_args, quizcomp.external.katex.init_from_args) 40 41 if (include_latex): 42 parser.register_callbacks('latex', quizcomp.external.latex.set_cli_args, quizcomp.external.latex.init_from_args) 43 44 return typing.cast(argparse.ArgumentParser, parser) 45 46def add_out_arg( 47 parser: argparse.ArgumentParser, 48 default_filename: str, 49 name: str = 'out', 50 dest: str = 'out', 51 default: str = '.', 52 ) -> None: 53 """ 54 Add a standard output path argument to an argparse parser. 55 Sibling to resolve_out_arg(). 56 """ 57 58 parser.add_argument(f'--{name}', dest = dest, 59 action = 'store', type = str, default = default, 60 help = ('The path specifying where to put the output.' 61 + f' If the path points to an existing dir, the result will be written to `<{name}>/{default_filename}`.' 62 + ' If the path point to an existing file, the file will be overwritten with the result.' 63 + ' If the path points to a non-existing dir (denoted with a trailing path separator (e.g., slash)),' 64 + ' the dir will be created and the output will be written as it is to an existing dir.' 65 + ' Finally if the path does not exist, the result will be written to the full path (creating any parent directories along the way).' 66 + ' (default: %(default)s).')) 67 68def resolve_out_arg(raw_path: str, default_filename: str) -> str: 69 """ 70 Resolve the out argument with the semantics of add_out_arg(), 71 and return a resolved normalized path. 72 """ 73 74 path = os.path.abspath(raw_path) 75 76 if (os.path.isfile(path)): 77 return path 78 elif (os.path.isdir(path)): 79 return os.path.join(path, default_filename) 80 elif (raw_path.endswith(os.sep)): 81 edq.util.dirent.mkdir(path) 82 return os.path.join(path, default_filename) 83 else: 84 edq.util.dirent.mkdir(os.path.dirname(path)) 85 return path
CONFIG_FILENAME: str =
'edq-quiz-composer.json'
def
get_parser( description: str, include_net: bool = False, include_katex: bool = False, include_latex: bool = False) -> argparse.ArgumentParser:
19def get_parser(description: str, 20 include_net: bool = False, 21 include_katex: bool = False, 22 include_latex: bool = False, 23 ) -> argparse.ArgumentParser: 24 """ 25 Get an argument parser specialized for the Quiz Composer. 26 """ 27 28 config_options = { 29 'config_filename': CONFIG_FILENAME, 30 } 31 32 parser = edq.core.argparser.get_default_parser( 33 description, 34 version = f"v{quizcomp.__version__}", 35 include_net = include_net, 36 config_options = config_options, 37 ) 38 39 if (include_katex): 40 parser.register_callbacks('katex', quizcomp.external.katex.set_cli_args, quizcomp.external.katex.init_from_args) 41 42 if (include_latex): 43 parser.register_callbacks('latex', quizcomp.external.latex.set_cli_args, quizcomp.external.latex.init_from_args) 44 45 return typing.cast(argparse.ArgumentParser, parser)
Get an argument parser specialized for the Quiz Composer.
def
add_out_arg( parser: argparse.ArgumentParser, default_filename: str, name: str = 'out', dest: str = 'out', default: str = '.') -> None:
47def add_out_arg( 48 parser: argparse.ArgumentParser, 49 default_filename: str, 50 name: str = 'out', 51 dest: str = 'out', 52 default: str = '.', 53 ) -> None: 54 """ 55 Add a standard output path argument to an argparse parser. 56 Sibling to resolve_out_arg(). 57 """ 58 59 parser.add_argument(f'--{name}', dest = dest, 60 action = 'store', type = str, default = default, 61 help = ('The path specifying where to put the output.' 62 + f' If the path points to an existing dir, the result will be written to `<{name}>/{default_filename}`.' 63 + ' If the path point to an existing file, the file will be overwritten with the result.' 64 + ' If the path points to a non-existing dir (denoted with a trailing path separator (e.g., slash)),' 65 + ' the dir will be created and the output will be written as it is to an existing dir.' 66 + ' Finally if the path does not exist, the result will be written to the full path (creating any parent directories along the way).' 67 + ' (default: %(default)s).'))
Add a standard output path argument to an argparse parser. Sibling to resolve_out_arg().
def
resolve_out_arg(raw_path: str, default_filename: str) -> str:
69def resolve_out_arg(raw_path: str, default_filename: str) -> str: 70 """ 71 Resolve the out argument with the semantics of add_out_arg(), 72 and return a resolved normalized path. 73 """ 74 75 path = os.path.abspath(raw_path) 76 77 if (os.path.isfile(path)): 78 return path 79 elif (os.path.isdir(path)): 80 return os.path.join(path, default_filename) 81 elif (raw_path.endswith(os.sep)): 82 edq.util.dirent.mkdir(path) 83 return os.path.join(path, default_filename) 84 else: 85 edq.util.dirent.mkdir(os.path.dirname(path)) 86 return path
Resolve the out argument with the semantics of add_out_arg(), and return a resolved normalized path.