diff --git a/CHANGELOG b/CHANGELOG index 2c5f7e66..3b15aac3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ jc changelog - Add mpstat command parser tested on linux - Add mpstat command streaming parser tested on linux - Add ASCII table parser +- Add documentation option to parser_info() and all_parser_info() 20220305 v1.18.5 - Fix date parser to ensure AM/PM period string is always uppercase diff --git a/jc/__init__.py b/jc/__init__.py index 71053c42..3430c527 100644 --- a/jc/__init__.py +++ b/jc/__init__.py @@ -67,13 +67,16 @@ built-in parsers and local plugin parsers. ### parser_info - parser_info(parser_module_name: str) -> dict + parser_info( + parser_module_name: str, + documentation: bool = False + ) -> dict Get the metadata for a particular parser. ### all_parser_info - all_parser_info() -> list[dict] + all_parser_info(documentation: bool = False) -> list[dict] Get the metadata for all parsers. diff --git a/jc/lib.py b/jc/lib.py index e2944fa4..01a27218 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -1,7 +1,6 @@ """jc - JSON Convert JC lib module """ - import sys import os import re @@ -266,12 +265,18 @@ def streaming_parser_mod_list() -> List[str]: plist.append(_cliname_to_modname(p)) return plist -def parser_info(parser_mod_name: str) -> Dict: +def parser_info(parser_mod_name: str, documentation: bool = False) -> Dict: """ - Returns a dictionary that includes the module metadata. + Returns a dictionary that includes the parser module metadata. - This function will accept **module_name**, **cli-name**, and - **--argument-name** variants of the module name string. + Parameters: + + parser_mod_name: (string) name of the parser module. This + function will accept module_name, + cli-name, and --argument-name + variants of the module name. + + 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) @@ -291,13 +296,21 @@ def parser_info(parser_mod_name: str) -> Dict: if _modname_to_cliname(parser_mod_name) in local_parsers: info_dict['plugin'] = True + if documentation: + info_dict['documentation'] = parser_mod.__doc__ + return info_dict -def all_parser_info() -> List[Dict]: +def all_parser_info(documentation: bool = False) -> List[Dict]: """ - Returns a list of dictionaries that includes metadata for all modules. + Returns a list of dictionaries that includes metadata for all parser + modules. + + Parameters: + + documentation: (boolean) include parser docstrings if True """ - return [parser_info(p) for p in parsers] + return [parser_info(p, documentation=documentation) for p in parsers] def get_help(parser_mod_name: str) -> None: """