1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

type hints update

This commit is contained in:
Kelly Brazil
2022-01-26 15:54:36 -08:00
parent 42fbe40a4a
commit d4d5e206ca
3 changed files with 40 additions and 28 deletions

View File

@ -5,6 +5,7 @@
* [parser\_mod\_list](#jc.lib.parser_mod_list) * [parser\_mod\_list](#jc.lib.parser_mod_list)
* [plugin\_parser\_mod\_list](#jc.lib.plugin_parser_mod_list) * [plugin\_parser\_mod\_list](#jc.lib.plugin_parser_mod_list)
* [parser\_info](#jc.lib.parser_info) * [parser\_info](#jc.lib.parser_info)
* [all\_parser\_info](#jc.lib.all_parser_info)
* [get\_help](#jc.lib.get_help) * [get\_help](#jc.lib.get_help)
<a id="jc.lib"></a> <a id="jc.lib"></a>
@ -19,7 +20,7 @@ JC lib module
### parse ### parse
```python ```python
def parse(parser_mod_name: str, data: Union[str, Iterable[str]], quiet: Optional[bool] = False, raw: Optional[bool] = False, ignore_exceptions: Optional[Union[None, bool]] = None, **kwargs: Any) -> Union[Dict[str, Any], List[Dict[str, Any]], Iterator[Dict[str, Any]]] def parse(parser_mod_name: str, data: Union[str, Iterable[str]], quiet: Optional[bool] = False, raw: Optional[bool] = False, ignore_exceptions: Optional[bool] = None, **kwargs: Any) -> Union[Dict, List[Dict], Iterator[Dict]]
``` ```
Parse the string data using the supplied parser module. Parse the string data using the supplied parser module.
@ -104,7 +105,7 @@ subset of `parser_mod_list()`.
### parser\_info ### parser\_info
```python ```python
def parser_info(parser_mod_name: str) -> Dict[str, Any] def parser_info(parser_mod_name: str) -> Dict
``` ```
Returns a dictionary that includes the module metadata. Returns a dictionary that includes the module metadata.
@ -112,6 +113,16 @@ Returns a dictionary that includes the module metadata.
This function will accept module_name, cli-name, and --argument-name This function will accept module_name, cli-name, and --argument-name
variants of the module name string. variants of the module name string.
<a id="jc.lib.all_parser_info"></a>
### all\_parser\_info
```python
def all_parser_info() -> List[Dict]
```
Returns a list of dictionaris that includes metadata for all modules.
<a id="jc.lib.get_help"></a> <a id="jc.lib.get_help"></a>
### get\_help ### get\_help

View File

@ -27,7 +27,7 @@ jc - JSON CLI output utility utils
### warning\_message ### warning\_message
```python ```python
def warning_message(message_lines) def warning_message(message_lines: List[str]) -> None
``` ```
Prints warning message for non-fatal issues. The first line is Prints warning message for non-fatal issues. The first line is
@ -47,7 +47,7 @@ Returns:
### error\_message ### error\_message
```python ```python
def error_message(message_lines) def error_message(message_lines: List[str]) -> None
``` ```
Prints an error message for fatal issues. The first line is Prints an error message for fatal issues. The first line is
@ -67,7 +67,7 @@ Returns:
### compatibility ### compatibility
```python ```python
def compatibility(mod_name, compatible, quiet=False) def compatibility(mod_name: str, compatible: List, quiet: Optional[bool] = False) -> None
``` ```
Checks for the parser's compatibility with the running OS Checks for the parser's compatibility with the running OS
@ -92,7 +92,7 @@ Returns:
### has\_data ### has\_data
```python ```python
def has_data(data) def has_data(data: str) -> bool
``` ```
Checks if the input contains data. If there are any non-whitespace Checks if the input contains data. If there are any non-whitespace
@ -112,7 +112,7 @@ Returns:
### convert\_to\_int ### convert\_to\_int
```python ```python
def convert_to_int(value) def convert_to_int(value: Union[str, float]) -> int
``` ```
Converts string and float input to int. Strips all non-numeric Converts string and float input to int. Strips all non-numeric
@ -131,7 +131,7 @@ Returns:
### convert\_to\_float ### convert\_to\_float
```python ```python
def convert_to_float(value) def convert_to_float(value: Union[str, int]) -> float
``` ```
Converts string and int input to float. Strips all non-numeric Converts string and int input to float. Strips all non-numeric
@ -150,7 +150,7 @@ Returns:
### convert\_to\_bool ### convert\_to\_bool
```python ```python
def convert_to_bool(value) def convert_to_bool(value: Union[str, int, float]) -> bool
``` ```
Converts string, integer, or float input to boolean by checking Converts string, integer, or float input to boolean by checking
@ -170,7 +170,7 @@ Returns:
### stream\_success ### stream\_success
```python ```python
def stream_success(output_line, ignore_exceptions) def stream_success(output_line: Dict, ignore_exceptions: bool) -> Dict
``` ```
Add `_jc_meta` object to output line if `ignore_exceptions=True` Add `_jc_meta` object to output line if `ignore_exceptions=True`
@ -180,7 +180,7 @@ Add `_jc_meta` object to output line if `ignore_exceptions=True`
### stream\_error ### stream\_error
```python ```python
def stream_error(e, ignore_exceptions, line) def stream_error(e: BaseException, ignore_exceptions: bool, line: str) -> Dict
``` ```
Reraise the stream exception with annotation or print an error Reraise the stream exception with annotation or print an error
@ -191,7 +191,7 @@ Reraise the stream exception with annotation or print an error
### input\_type\_check ### input\_type\_check
```python ```python
def input_type_check(data) def input_type_check(data: str) -> None
``` ```
Ensure input data is a string Ensure input data is a string
@ -201,7 +201,7 @@ Ensure input data is a string
### streaming\_input\_type\_check ### streaming\_input\_type\_check
```python ```python
def streaming_input_type_check(data) def streaming_input_type_check(data: Iterable) -> None
``` ```
Ensure input data is an iterable, but not a string or bytes Ensure input data is an iterable, but not a string or bytes
@ -211,7 +211,7 @@ Ensure input data is an iterable, but not a string or bytes
### streaming\_line\_input\_type\_check ### streaming\_line\_input\_type\_check
```python ```python
def streaming_line_input_type_check(line) def streaming_line_input_type_check(line: str) -> None
``` ```
Ensure each line is a string Ensure each line is a string
@ -229,7 +229,7 @@ class timestamp()
### \_\_init\_\_ ### \_\_init\_\_
```python ```python
def __init__(datetime_string) def __init__(datetime_string: str) -> None
``` ```
Input a date-time text string of several formats and convert to a Input a date-time text string of several formats and convert to a

View File

@ -5,9 +5,10 @@ import locale
import shutil import shutil
from datetime import datetime, timezone from datetime import datetime, timezone
from textwrap import TextWrapper from textwrap import TextWrapper
from typing import Dict, Iterable, List, Any, Union, Iterator, Optional
def warning_message(message_lines): def warning_message(message_lines: List[str]) -> None:
""" """
Prints warning message for non-fatal issues. The first line is Prints warning message for non-fatal issues. The first line is
prepended with 'jc: Warning - ' and subsequent lines are indented. prepended with 'jc: Warning - ' and subsequent lines are indented.
@ -43,7 +44,7 @@ def warning_message(message_lines):
print(message, file=sys.stderr) print(message, file=sys.stderr)
def error_message(message_lines): def error_message(message_lines: List[str]) -> None:
""" """
Prints an error message for fatal issues. The first line is Prints an error message for fatal issues. The first line is
prepended with 'jc: Error - ' and subsequent lines are indented. prepended with 'jc: Error - ' and subsequent lines are indented.
@ -75,7 +76,7 @@ def error_message(message_lines):
print(message, file=sys.stderr) print(message, file=sys.stderr)
def compatibility(mod_name, compatible, quiet=False): def compatibility(mod_name: str, compatible: List, quiet: Optional[bool] = False) -> None:
""" """
Checks for the parser's compatibility with the running OS Checks for the parser's compatibility with the running OS
platform. platform.
@ -109,7 +110,7 @@ def compatibility(mod_name, compatible, quiet=False):
f'Compatible platforms: {compat_list}']) f'Compatible platforms: {compat_list}'])
def has_data(data): def has_data(data: str) -> bool:
""" """
Checks if the input contains data. If there are any non-whitespace Checks if the input contains data. If there are any non-whitespace
characters then return True, else return False. characters then return True, else return False.
@ -126,7 +127,7 @@ def has_data(data):
return bool(data and not data.isspace()) return bool(data and not data.isspace())
def convert_to_int(value): def convert_to_int(value: Union[str, float]) -> int:
""" """
Converts string and float input to int. Strips all non-numeric Converts string and float input to int. Strips all non-numeric
characters from strings. characters from strings.
@ -156,7 +157,7 @@ def convert_to_int(value):
return None return None
def convert_to_float(value): def convert_to_float(value: Union[str, int]) -> float:
""" """
Converts string and int input to float. Strips all non-numeric Converts string and int input to float. Strips all non-numeric
characters from strings. characters from strings.
@ -182,7 +183,7 @@ def convert_to_float(value):
return None return None
def convert_to_bool(value): def convert_to_bool(value: Union[str, int, float]) -> bool:
""" """
Converts string, integer, or float input to boolean by checking Converts string, integer, or float input to boolean by checking
for 'truthy' values. for 'truthy' values.
@ -220,7 +221,7 @@ def convert_to_bool(value):
return False return False
def stream_success(output_line, ignore_exceptions): def stream_success(output_line: Dict, ignore_exceptions: bool) -> Dict:
"""Add `_jc_meta` object to output line if `ignore_exceptions=True`""" """Add `_jc_meta` object to output line if `ignore_exceptions=True`"""
if ignore_exceptions: if ignore_exceptions:
output_line.update({'_jc_meta': {'success': True}}) output_line.update({'_jc_meta': {'success': True}})
@ -228,7 +229,7 @@ def stream_success(output_line, ignore_exceptions):
return output_line return output_line
def stream_error(e, ignore_exceptions, line): def stream_error(e: BaseException, ignore_exceptions: bool, line: str) -> Dict:
""" """
Reraise the stream exception with annotation or print an error Reraise the stream exception with annotation or print an error
`_jc_meta` field if `ignore_exceptions=True`. `_jc_meta` field if `ignore_exceptions=True`.
@ -247,26 +248,26 @@ def stream_error(e, ignore_exceptions, line):
} }
def input_type_check(data): def input_type_check(data: str) -> None:
"""Ensure input data is a string""" """Ensure input data is a string"""
if not isinstance(data, str): if not isinstance(data, str):
raise TypeError("Input data must be a 'str' object.") raise TypeError("Input data must be a 'str' object.")
def streaming_input_type_check(data): def streaming_input_type_check(data: Iterable) -> None:
"""Ensure input data is an iterable, but not a string or bytes""" """Ensure input data is an iterable, but not a string or bytes"""
if not hasattr(data, '__iter__') or isinstance(data, (str, bytes)): if not hasattr(data, '__iter__') or isinstance(data, (str, bytes)):
raise TypeError("Input data must be a non-string iterable object.") raise TypeError("Input data must be a non-string iterable object.")
def streaming_line_input_type_check(line): def streaming_line_input_type_check(line: str) -> None:
"""Ensure each line is a string""" """Ensure each line is a string"""
if not isinstance(line, str): if not isinstance(line, str):
raise TypeError("Input line must be a 'str' object.") raise TypeError("Input line must be a 'str' object.")
class timestamp: class timestamp:
def __init__(self, datetime_string): def __init__(self, datetime_string: str) -> None:
""" """
Input a date-time text string of several formats and convert to a Input a date-time text string of several formats and convert to a
naive or timezone-aware epoch timestamp in UTC. naive or timezone-aware epoch timestamp in UTC.