click_ext¶
extension and interface for parsing with click and configparse
imports callbacks into namespace for convenience
-
clasp.click_ext.
COMPLETION_SCRIPT_BASH
= '\n%(complete_func)s() {\n local cw="${COMP_WORDS[*]}"\n if [[ $cw != *">"* ]]; then\n if [[ ${COMP_WORDS[$COMP_CWORD]} != -* ]] && [[ ${COMP_WORDS[$COMP_CWORD-1]} == -* ]]; then\n :\n else\n local IFS=$\'\n\'\n COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\\n COMP_CWORD=$COMP_CWORD \\\n %(autocomplete_var)s=complete $1 ) )\n fi\n fi\n return 0\n}\n\n%(complete_func)setup() {\n local COMPLETION_OPTIONS=""\n local BASH_VERSION_ARR=(${BASH_VERSION//./ })\n # Only BASH version 4.4 and later have the nosort option.\n if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then\n COMPLETION_OPTIONS="-o nosort"\n fi\n\n complete $COMPLETION_OPTIONS -o default -F %(complete_func)s %(script_names)s\n}\n\n%(complete_func)setup\n'¶ Edited from click completion script to avoid running out of turn (faster)
-
clasp.click_ext.
script_template
= '#!/usr/bin/env python\nfrom clasp import click\nimport clasp.click_ext as clk\nimport clasp.script_tools as cst\n\n"""callbacks for special parsing of command line inputs\n\n**Callbacks By type**\n\n**File input**\n\nfile inputs can be given with wildcard expansion (in quotes so that the\ncallback handles) using glob plus the following:\n\n * [abc] (one of a, b, or c) \n * [!abc] (none of a, b or c)\n * \'-\' (hyphen) collect the stdin into a temporary file (clasp_tmp*)\n * ~ expands user\n\n**callback functions**\n\n * is_file: check if a single path exists (prompts for user input if file\n not found)\n * are_files: recursively calls parse_file_list and prompts on error\n * is_file_iter: use when multiple=True\n * are_files_iter: use when mulitple=True\n * are_files_or_str: tries to parse as files, then tries split_float, then\n split_int, then returns string\n * are_files_or_str_iter: use when mulitple=True\n\n**String parsing**\n\n * split_str: split with shlex.split\n * split_str_iter: use when multiple=True\n * color_inp: return alpha string, split on whitespace,\n convert floats and parse tuples on ,\n * char0: return first character\n\n**Number parsing**\n\n * tup_int: parses integer tuples from comma/space separated string\n * tup_float: parses float tuples from comma/space separated string\n * split_float: splits list of floats and extends ranges based on : notation\n * split_int: splits list of ints and extends ranges based on : notation\n"""\n\n@click.command()\n@click.argument(\'arg1\')\n@clk.shared_decs(clk.command_decs(\'0.1\', wrap=True))\ndef main(ctx, arg1, **kwargs):\n pass\n\nif __name__ == \'__main__\':\n main()'¶ basic script template for one offs
-
clasp.click_ext.
script_template2
= '#!/usr/bin/env python\nfrom clasp import click\nimport clasp.click_ext as clk\nimport clasp.script_tools as cst\n\n"""callbacks for special parsing of command line inputs\n\n**Callbacks By type**\n\n**File input**\n\nfile inputs can be given with wildcard expansion (in quotes so that the\ncallback handles) using glob plus the following:\n\n * [abc] (one of a, b, or c) \n * [!abc] (none of a, b or c)\n * \'-\' (hyphen) collect the stdin into a temporary file (clasp_tmp*)\n * ~ expands user\n\n**callback functions**\n\n * is_file: check if a single path exists (prompts for user input if file\n not found)\n * are_files: recursively calls parse_file_list and prompts on error\n * is_file_iter: use when multiple=True\n * are_files_iter: use when mulitple=True\n * are_files_or_str: tries to parse as files, then tries split_float, then\n split_int, then returns string\n * are_files_or_str_iter: use when mulitple=True\n\n**String parsing**\n\n * split_str: split with shlex.split\n * split_str_iter: use when multiple=True\n * color_inp: return alpha string, split on whitespace,\n convert floats and parse tuples on ,\n * char0: return first character\n\n**Number parsing**\n\n * tup_int: parses integer tuples from comma/space separated string\n * tup_float: parses float tuples from comma/space separated string\n * split_float: splits list of floats and extends ranges based on : notation\n * split_int: splits list of ints and extends ranges based on : notation\n"""\n\n@click.group()\n@clk.shared_decs(clk.main_decs(\'0.1\'))\ndef main(ctx, config, outconfig, configalias, inputalias):\n """docstring"""\n clk.get_config(ctx, config, outconfig, configalias, inputalias)\n\n\n@main.command()\n@click.argument(\'arg1\')\n@clk.shared_decs(clk.command_decs(\'0.1\', wrap=True))\ndef XXX(ctx, arg1, **kwargs):\n pass\n\n\n@main.result_callback\n@click.pass_context\ndef printconfig(ctx, opts, **kwargs):\n """callback to save config file"""\n try:\n clk.tmp_clean(opts[2])\n except Exception:\n pass\n if kwargs[\'outconfig\']:\n clk.print_config(ctx, opts, kwargs[\'outconfig\'], kwargs[\'config\'],\n kwargs[\'configalias\'])\n\n\nif __name__ == \'__main__\':\n main()'¶ basic script template for command subcommand structure (using config files)
-
clasp.click_ext.
wrap_command
()[source]¶ decorator to execute command within standardized error handling
-
clasp.click_ext.
main_decs
(v, writeconfig=True)[source]¶ set of shared decorators for all main command groups
- Parameters
v (str) –
- Returns
md – decorator list for main command to manage config file usage
- Return type
list
-
clasp.click_ext.
command_decs
(v, wrap=False)[source]¶ set of shared decorators for all sub commands
- Parameters
v (str) –
- Returns
cd – decorator list for sub command
- Return type
list
decorator to add decs to function
- Parameters
decs (list of decorator functions to add to function) –
- Returns
decorator – a function that decorates function with list of decorators
- Return type
func
-
clasp.click_ext.
tmp_clean
(ctx)[source]¶ remove files placed int temps context object (called at end of scripts)
-
clasp.click_ext.
index_param
(ctx, param)[source]¶ tag param with index to sort by option type for help display
-
clasp.click_ext.
get_config
(ctx, config, outconfig, configalias, inputalias, template=None)[source]¶ load config file into click options
-
clasp.click_ext.
get_config_chained
(ctx, config, outconfig, configalias, inputalias)[source]¶ load config file into click options
-
clasp.click_ext.
match_multiple
(ctx, subc)[source]¶ identify params with multiple=True for config file parsing
-
clasp.click_ext.
print_config
(ctx, opts, outconfig, config, configalias, chain=False)[source]¶ write config file from click options