From 747d12224f52ec1aae8ec54f96c2dfda0fb1990e Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 28 Oct 2022 10:36:54 -0700 Subject: [PATCH] allow parser_info and get_help to use module objects as input --- docs/lib.md | 14 ++++++++------ jc/lib.py | 35 +++++++++++++++++++++++++---------- man/jc.1 | 2 +- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/docs/lib.md b/docs/lib.md index b6f72d97..7853b606 100644 --- a/docs/lib.md +++ b/docs/lib.md @@ -161,7 +161,7 @@ subset of `parser_mod_list()`. ### parser\_info ```python -def parser_info(parser_mod_name: str, +def parser_info(parser_mod_name: Union[str, ModuleType], documentation: bool = False) -> ParserInfoType ``` @@ -169,10 +169,11 @@ Returns a dictionary that includes the parser module metadata. Parameters: - parser_mod_name: (string) name of the parser module. This - function will accept module_name, + parser_mod_name: (string or name of the parser module. This + Module) function will accept module_name, cli-name, and --argument-name - variants of the module name. + variants of the module name as well + as a parser module object. documentation: (boolean) include parser docstring if True @@ -203,11 +204,12 @@ Parameters: ### get\_help ```python -def get_help(parser_mod_name: str) -> None +def get_help(parser_mod_name: Union[str, ModuleType]) -> None ``` Show help screen for the selected parser. This function will accept **module_name**, **cli-name**, and -**--argument-name** variants of the module name string. +**--argument-name** variants of the module name string as well as a +parser module object. diff --git a/jc/lib.py b/jc/lib.py index a403b314..d3e3a80f 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -453,22 +453,31 @@ def streaming_parser_mod_list( return plist -def parser_info(parser_mod_name: str, documentation: bool = False) -> ParserInfoType: +def parser_info( + parser_mod_name: Union[str, ModuleType], + documentation: bool = False +) -> ParserInfoType: """ Returns a dictionary that includes the parser module metadata. Parameters: - parser_mod_name: (string) name of the parser module. This - function will accept module_name, + parser_mod_name: (string or name of the parser module. This + Module) function will accept module_name, cli-name, and --argument-name - variants of the module name. + variants of the module name as well + as a parser module object. documentation: (boolean) include parser docstring if True """ - # ensure parser_mod_name is a true module name and not a cli name - parser_mod_name = _cliname_to_modname(parser_mod_name) - parser_mod = _get_parser(parser_mod_name) + if isinstance(parser_mod_name, ModuleType): + parser_mod = parser_mod_name + parser_mod_name = parser_mod.__name__.split('.')[-1] + else: + # ensure parser_mod_name is a true module name and not a cli name + parser_mod_name = _cliname_to_modname(parser_mod_name) + parser_mod = _get_parser(parser_mod_name) + info_dict: ParserInfoType = {} if hasattr(parser_mod, 'info'): @@ -525,11 +534,17 @@ def all_parser_info( return p_info_list -def get_help(parser_mod_name: str) -> None: +def get_help(parser_mod_name: Union[str, ModuleType]) -> None: """ Show help screen for the selected parser. This function will accept **module_name**, **cli-name**, and - **--argument-name** variants of the module name string. + **--argument-name** variants of the module name string as well as a + parser module object. """ - help(_get_parser(parser_mod_name)) + if isinstance(parser_mod_name, ModuleType): + jc_parser = parser_mod_name + else: + jc_parser = _get_parser(parser_mod_name) + + help(jc_parser) diff --git a/man/jc.1 b/man/jc.1 index 60314f11..abac7b3c 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-10-25 1.22.2 "JSON Convert" +.TH jc 1 2022-10-28 1.22.2 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings .SH SYNOPSIS