1
0
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:
Kelly Brazil
2022-10-15 14:34:09 -07:00
parent ef1055a9b6
commit f67bd02283
2 changed files with 25 additions and 18 deletions

View File

@ -7,9 +7,11 @@ from typing import Dict, List, Iterable, Union, Iterator
from types import ModuleType from types import ModuleType
from jc import appdirs from jc import appdirs
JSONDictType = Dict[str, Union[str, int, float, bool, List, Dict, None]]
__version__ = '1.22.1' __version__ = '1.22.1'
parsers: List = [ parsers: List[str] = [
'acpi', 'acpi',
'airport', 'airport',
'airport-s', 'airport-s',
@ -197,7 +199,7 @@ def _modname_to_cliname(parser_mod_name: str) -> str:
# Create the local_parsers list. This is a list of custom or # Create the local_parsers list. This is a list of custom or
# override parsers from <user_data_dir>/jc/jcparsers/*.py. # override parsers from <user_data_dir>/jc/jcparsers/*.py.
# Once this list is created, extend the parsers list with it. # Once this list is created, extend the parsers list with it.
local_parsers = [] local_parsers: List[str] = []
data_dir = appdirs.user_data_dir('jc', 'jc') data_dir = appdirs.user_data_dir('jc', 'jc')
local_parsers_dir = os.path.join(data_dir, 'jcparsers') local_parsers_dir = os.path.join(data_dir, 'jcparsers')
if os.path.isdir(local_parsers_dir): if os.path.isdir(local_parsers_dir):
@ -213,7 +215,7 @@ if os.path.isdir(local_parsers_dir):
except Exception: except Exception:
pass pass
def _parser_argument(parser_mod_name:str) -> str: def _parser_argument(parser_mod_name: str) -> str:
"""Return short name of the parser with dashes and with -- prefix""" """Return short name of the parser with dashes and with -- prefix"""
parser = _modname_to_cliname(parser_mod_name) parser = _modname_to_cliname(parser_mod_name)
return f'--{parser}' return f'--{parser}'
@ -223,7 +225,7 @@ def _get_parser(parser_mod_name: str) -> ModuleType:
# 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_cli_name = _modname_to_cliname(parser_mod_name) parser_cli_name = _modname_to_cliname(parser_mod_name)
modpath = 'jcparsers.' if parser_cli_name in local_parsers else 'jc.parsers.' modpath: str = 'jcparsers.' if parser_cli_name in local_parsers else 'jc.parsers.'
return importlib.import_module(f'{modpath}{parser_mod_name}') return importlib.import_module(f'{modpath}{parser_mod_name}')
def _parser_is_streaming(parser: ModuleType) -> bool: def _parser_is_streaming(parser: ModuleType) -> bool:
@ -266,7 +268,7 @@ def parse(
raw: bool = False, raw: bool = False,
ignore_exceptions: bool = None, ignore_exceptions: bool = None,
**kwargs **kwargs
) -> Union[Dict, List[Dict], Iterator[Dict]]: ) -> Union[JSONDictType, List[JSONDictType], Iterator[JSONDictType]]:
""" """
Parse the string or bytes data using the supplied parser module. Parse the string or bytes data using the supplied parser module.
@ -350,8 +352,13 @@ def parse(
jc_parser = _get_parser(parser_mod_name) jc_parser = _get_parser(parser_mod_name)
if ignore_exceptions is not None: if ignore_exceptions is not None:
return jc_parser.parse(data, quiet=quiet, raw=raw, return jc_parser.parse(
ignore_exceptions=ignore_exceptions, **kwargs) data,
quiet=quiet,
raw=raw,
ignore_exceptions=ignore_exceptions,
**kwargs
)
return jc_parser.parse(data, quiet=quiet, raw=raw, **kwargs) return jc_parser.parse(data, quiet=quiet, raw=raw, **kwargs)
@ -360,7 +367,7 @@ def parser_mod_list(
show_deprecated: bool = True show_deprecated: bool = True
) -> List[str]: ) -> List[str]:
"""Returns a list of all available parser module names.""" """Returns a list of all available parser module names."""
plist = [] plist: List[str] = []
for p in parsers: for p in parsers:
parser = _get_parser(p) parser = _get_parser(p)
@ -382,7 +389,7 @@ def plugin_parser_mod_list(
Returns a list of plugin parser module names. This function is a Returns a list of plugin parser module names. This function is a
subset of `parser_mod_list()`. subset of `parser_mod_list()`.
""" """
plist = [] plist: List[str] = []
for p in local_parsers: for p in local_parsers:
parser = _get_parser(p) parser = _get_parser(p)
@ -405,7 +412,7 @@ def standard_parser_mod_list(
subset of `parser_mod_list()` and does not contain any streaming subset of `parser_mod_list()` and does not contain any streaming
parsers. parsers.
""" """
plist = [] plist: List[str] = []
for p in parsers: for p in parsers:
parser = _get_parser(p) parser = _get_parser(p)
@ -429,7 +436,7 @@ def streaming_parser_mod_list(
Returns a list of streaming parser module names. This function is a Returns a list of streaming parser module names. This function is a
subset of `parser_mod_list()`. subset of `parser_mod_list()`.
""" """
plist = [] plist: List[str] = []
for p in parsers: for p in parsers:
parser = _get_parser(p) parser = _get_parser(p)
@ -445,7 +452,7 @@ def streaming_parser_mod_list(
return plist return plist
def parser_info(parser_mod_name: str, documentation: bool = False) -> Dict: def parser_info(parser_mod_name: str, documentation: bool = False) -> JSONDictType:
""" """
Returns a dictionary that includes the parser module metadata. Returns a dictionary that includes the parser module metadata.
@ -461,7 +468,7 @@ def parser_info(parser_mod_name: str, documentation: bool = False) -> Dict:
# 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: Dict = {} info_dict: JSONDictType = {}
if hasattr(parser_mod, 'info'): if hasattr(parser_mod, 'info'):
info_dict['name'] = parser_mod_name info_dict['name'] = parser_mod_name
@ -487,7 +494,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[Dict]: ) -> List[JSONDictType]:
""" """
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
@ -501,7 +508,7 @@ def all_parser_info(
show_deprecated: (boolean) also show parsers marked as show_deprecated: (boolean) also show parsers marked as
deprecated in their info metadata. deprecated in their info metadata.
""" """
plist = [] plist: List[str] = []
for p in parsers: for p in parsers:
parser = _get_parser(p) parser = _get_parser(p)
@ -513,7 +520,7 @@ def all_parser_info(
plist.append(_cliname_to_modname(p)) plist.append(_cliname_to_modname(p))
p_info_list = [parser_info(p, documentation=documentation) for p in plist] p_info_list: List[JSONDictType] = [parser_info(p, documentation=documentation) for p in plist]
return p_info_list return p_info_list

View File

@ -125,7 +125,7 @@ def error_message(message_lines: List[str]) -> None:
_safe_print(message, file=sys.stderr) _safe_print(message, file=sys.stderr)
def is_compatible(compatible: List) -> bool: def is_compatible(compatible: List[str]) -> bool:
""" """
Returns True if the parser is compatible with the running OS platform. Returns True if the parser is compatible with the running OS platform.
""" """
@ -139,7 +139,7 @@ def is_compatible(compatible: List) -> bool:
return platform_found return platform_found
def compatibility(mod_name: str, compatible: List, quiet: bool = False) -> None: def compatibility(mod_name: str, compatible: List[str], quiet: bool = False) -> None:
""" """
Checks for the parser's compatibility with the running OS platform and Checks for the parser's compatibility with the running OS platform and
prints a warning message to `STDERR` if not compatible and prints a warning message to `STDERR` if not compatible and