mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
more granular type annotations
This commit is contained in:
@ -162,7 +162,7 @@ subset of `parser_mod_list()`.
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
def parser_info(parser_mod_name: str,
|
def parser_info(parser_mod_name: str,
|
||||||
documentation: bool = False) -> JSONDictType
|
documentation: bool = False) -> ParserInfoType
|
||||||
```
|
```
|
||||||
|
|
||||||
Returns a dictionary that includes the parser module metadata.
|
Returns a dictionary that includes the parser module metadata.
|
||||||
@ -183,7 +183,7 @@ Parameters:
|
|||||||
```python
|
```python
|
||||||
def all_parser_info(documentation: bool = False,
|
def all_parser_info(documentation: bool = False,
|
||||||
show_hidden: bool = False,
|
show_hidden: bool = False,
|
||||||
show_deprecated: bool = False) -> List[JSONDictType]
|
show_deprecated: bool = False) -> List[ParserInfoType]
|
||||||
```
|
```
|
||||||
|
|
||||||
Returns a list of dictionaries that includes metadata for all parser
|
Returns a list of dictionaries that includes metadata for all parser
|
||||||
|
29
jc/cli.py
29
jc/cli.py
@ -14,7 +14,8 @@ from typing import List, Dict, Union, Optional, TextIO
|
|||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from .lib import (
|
from .lib import (
|
||||||
__version__, parser_info, all_parser_info, parsers, _get_parser, _parser_is_streaming,
|
__version__, parser_info, all_parser_info, parsers, _get_parser, _parser_is_streaming,
|
||||||
parser_mod_list, standard_parser_mod_list, plugin_parser_mod_list, streaming_parser_mod_list
|
parser_mod_list, standard_parser_mod_list, plugin_parser_mod_list, streaming_parser_mod_list,
|
||||||
|
JSONDictType
|
||||||
)
|
)
|
||||||
from . import utils
|
from . import utils
|
||||||
from .cli_data import (
|
from .cli_data import (
|
||||||
@ -25,10 +26,8 @@ from .shell_completions import bash_completion, zsh_completion
|
|||||||
from . import tracebackplus
|
from . import tracebackplus
|
||||||
from .exceptions import LibraryNotInstalled, ParseError
|
from .exceptions import LibraryNotInstalled, ParseError
|
||||||
|
|
||||||
MetadataType = Dict[
|
MetadataType = Dict[str, Optional[Union[str, int, float, List[str], datetime]]]
|
||||||
str,
|
AboutJCType = Dict[str, Union[str, int, List[str]]]
|
||||||
Optional[Union[str, int, float, List[str], datetime]]
|
|
||||||
]
|
|
||||||
|
|
||||||
# make pygments import optional
|
# make pygments import optional
|
||||||
PYGMENTS_INSTALLED: bool = False
|
PYGMENTS_INSTALLED: bool = False
|
||||||
@ -79,8 +78,8 @@ class JcCli():
|
|||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.data_in: Optional[Union[str, bytes,TextIO]] = None
|
self.data_in: Optional[Union[str, bytes, TextIO]] = None
|
||||||
self.data_out: Optional[Union[List[Dict], Dict]] = None
|
self.data_out: Optional[Union[List[JSONDictType], JSONDictType, AboutJCType]] = None
|
||||||
self.options: List[str] = []
|
self.options: List[str] = []
|
||||||
self.args: List[str] = []
|
self.args: List[str] = []
|
||||||
self.parser_module: Optional[ModuleType] = None
|
self.parser_module: Optional[ModuleType] = None
|
||||||
@ -195,11 +194,11 @@ class JcCli():
|
|||||||
ptext = ''
|
ptext = ''
|
||||||
padding_char = ' '
|
padding_char = ' '
|
||||||
for p in all_parser_info(show_hidden=self.show_hidden, show_deprecated=False):
|
for p in all_parser_info(show_hidden=self.show_hidden, show_deprecated=False):
|
||||||
parser_arg = p.get('argument', 'UNKNOWN')
|
parser_arg: str = p.get('argument', 'UNKNOWN')
|
||||||
padding = self.pad - len(parser_arg)
|
padding: int = self.pad - len(parser_arg)
|
||||||
parser_desc = p.get('description', 'No description available.')
|
parser_desc: str = p.get('description', 'No description available.')
|
||||||
indent_text = padding_char * self.indent
|
indent_text: str = padding_char * self.indent
|
||||||
padding_text = padding_char * padding
|
padding_text: str = padding_char * padding
|
||||||
ptext += indent_text + parser_arg + padding_text + parser_desc + '\n'
|
ptext += indent_text + parser_arg + padding_text + parser_desc + '\n'
|
||||||
|
|
||||||
return ptext
|
return ptext
|
||||||
@ -615,9 +614,9 @@ class JcCli():
|
|||||||
|
|
||||||
if isinstance(self.data_out, dict):
|
if isinstance(self.data_out, dict):
|
||||||
if '_jc_meta' not in self.data_out:
|
if '_jc_meta' not in self.data_out:
|
||||||
self.data_out['_jc_meta'] = {}
|
self.data_out['_jc_meta'] = {} # type: ignore
|
||||||
|
|
||||||
self.data_out['_jc_meta'].update(meta_obj)
|
self.data_out['_jc_meta'].update(meta_obj) # type: ignore
|
||||||
|
|
||||||
elif isinstance(self.data_out, list):
|
elif isinstance(self.data_out, list):
|
||||||
if not self.data_out:
|
if not self.data_out:
|
||||||
@ -628,7 +627,7 @@ class JcCli():
|
|||||||
if '_jc_meta' not in item:
|
if '_jc_meta' not in item:
|
||||||
item['_jc_meta'] = {}
|
item['_jc_meta'] = {}
|
||||||
|
|
||||||
item['_jc_meta'].update(meta_obj)
|
item['_jc_meta'].update(meta_obj) # type: ignore
|
||||||
|
|
||||||
else:
|
else:
|
||||||
utils.error_message(['Parser returned an unsupported object type.'])
|
utils.error_message(['Parser returned an unsupported object type.'])
|
||||||
|
34
jc/lib.py
34
jc/lib.py
@ -7,6 +7,30 @@ from typing import Dict, List, Iterable, Union, Iterator
|
|||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from jc import appdirs
|
from jc import appdirs
|
||||||
|
|
||||||
|
if sys.version_info >= (3, 8):
|
||||||
|
from typing import TypedDict
|
||||||
|
ParserInfoType = TypedDict(
|
||||||
|
'ParserInfoType',
|
||||||
|
{
|
||||||
|
"name": str,
|
||||||
|
"argument": str,
|
||||||
|
"version": str,
|
||||||
|
"description": str,
|
||||||
|
"author": str,
|
||||||
|
"author_email": str,
|
||||||
|
"compatible": List[str],
|
||||||
|
"magic_commands": List[str],
|
||||||
|
"documentation": str,
|
||||||
|
"streaming": bool,
|
||||||
|
"plugin": bool,
|
||||||
|
"hidden": bool,
|
||||||
|
"deprecated": bool
|
||||||
|
},
|
||||||
|
total=False
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
ParserInfoType = Dict
|
||||||
|
|
||||||
JSONDictType = Dict[str, Union[str, int, float, bool, List, Dict, None]]
|
JSONDictType = Dict[str, Union[str, int, float, bool, List, Dict, None]]
|
||||||
|
|
||||||
__version__ = '1.22.1'
|
__version__ = '1.22.1'
|
||||||
@ -453,7 +477,7 @@ def streaming_parser_mod_list(
|
|||||||
|
|
||||||
return plist
|
return plist
|
||||||
|
|
||||||
def parser_info(parser_mod_name: str, documentation: bool = False) -> JSONDictType:
|
def parser_info(parser_mod_name: str, documentation: bool = False) -> ParserInfoType:
|
||||||
"""
|
"""
|
||||||
Returns a dictionary that includes the parser module metadata.
|
Returns a dictionary that includes the parser module metadata.
|
||||||
|
|
||||||
@ -469,7 +493,7 @@ def parser_info(parser_mod_name: str, documentation: bool = False) -> JSONDictTy
|
|||||||
# ensure parser_mod_name is a true module name and not a cli name
|
# 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_name = _cliname_to_modname(parser_mod_name)
|
||||||
parser_mod = _get_parser(parser_mod_name)
|
parser_mod = _get_parser(parser_mod_name)
|
||||||
info_dict: JSONDictType = {}
|
info_dict: ParserInfoType = {}
|
||||||
|
|
||||||
if hasattr(parser_mod, 'info'):
|
if hasattr(parser_mod, 'info'):
|
||||||
info_dict['name'] = parser_mod_name
|
info_dict['name'] = parser_mod_name
|
||||||
@ -478,7 +502,7 @@ def parser_info(parser_mod_name: str, documentation: bool = False) -> JSONDictTy
|
|||||||
|
|
||||||
for k, v in parser_entry.items():
|
for k, v in parser_entry.items():
|
||||||
if not k.startswith('__'):
|
if not k.startswith('__'):
|
||||||
info_dict[k] = v
|
info_dict[k] = v # type: ignore
|
||||||
|
|
||||||
if _modname_to_cliname(parser_mod_name) in local_parsers:
|
if _modname_to_cliname(parser_mod_name) in local_parsers:
|
||||||
info_dict['plugin'] = True
|
info_dict['plugin'] = True
|
||||||
@ -495,7 +519,7 @@ def all_parser_info(
|
|||||||
documentation: bool = False,
|
documentation: bool = False,
|
||||||
show_hidden: bool = False,
|
show_hidden: bool = False,
|
||||||
show_deprecated: bool = False
|
show_deprecated: bool = False
|
||||||
) -> List[JSONDictType]:
|
) -> List[ParserInfoType]:
|
||||||
"""
|
"""
|
||||||
Returns a list of dictionaries that includes metadata for all parser
|
Returns a list of dictionaries that includes metadata for all parser
|
||||||
modules. By default only non-hidden, non-deprecated parsers are
|
modules. By default only non-hidden, non-deprecated parsers are
|
||||||
@ -521,7 +545,7 @@ def all_parser_info(
|
|||||||
|
|
||||||
plist.append(_cliname_to_modname(p))
|
plist.append(_cliname_to_modname(p))
|
||||||
|
|
||||||
p_info_list: List[JSONDictType] = [parser_info(p, documentation=documentation) for p in plist]
|
p_info_list: List[ParserInfoType] = [parser_info(p, documentation=documentation) for p in plist]
|
||||||
|
|
||||||
return p_info_list
|
return p_info_list
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user