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

275 lines
8.0 KiB
Markdown
Raw Normal View History

2024-03-14 21:44:37 -07:00
[Home](https://kellyjonbrazil.github.io/jc/)
2022-01-25 17:07:47 -08:00
<a id="jc.lib"></a>
# jc.lib
2022-01-19 22:20:36 -08:00
2024-03-14 21:58:43 -07:00
## Table of Contents
2024-03-14 21:44:37 -07:00
2024-03-16 18:28:52 -07:00
* [jc.lib](#jc.lib)
* [all_parser_info](#jc.lib.all_parser_info)
* [get_help](#jc.lib.get_help)
* [get_parser](#jc.lib.get_parser)
* [parse](#jc.lib.parse)
* [parser_info](#jc.lib.parser_info)
* [parser_mod_list](#jc.lib.parser_mod_list)
* [plugin_parser_mod_list](#jc.lib.plugin_parser_mod_list)
* [slurpable_parser_mod_list](#jc.lib.slurpable_parser_mod_list)
* [standard_parser_mod_list](#jc.lib.standard_parser_mod_list)
* [streaming_parser_mod_list](#jc.lib.streaming_parser_mod_list)
2024-03-14 21:44:37 -07:00
2022-03-20 10:12:29 -07:00
jc - JSON Convert lib module
2022-01-19 22:20:36 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.lib.all_parser_info"></a>
### all_parser_info
```python
def all_parser_info(
documentation: bool = False,
show_hidden: bool = False,
show_deprecated: bool = False) -> List[jc.jc_types.ParserInfoType]
```
Returns a list of dictionaries that includes metadata for all parser
modules. By default only non-hidden, non-deprecated parsers are
returned.
Parameters:
documentation: (boolean) include parser docstrings if True
show_hidden: (boolean) also show parsers marked as hidden
in their info metadata.
show_deprecated: (boolean) also show parsers marked as
deprecated in their info metadata.
<a id="jc.lib.get_help"></a>
### get_help
```python
def get_help(parser_mod_name: Union[str, module]) -> 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 as well as a
parser module object.
<a id="jc.lib.get_parser"></a>
2024-03-14 21:44:37 -07:00
### get_parser
```python
2024-03-14 21:44:37 -07:00
def get_parser(parser_mod_name: Union[str, module]) -> module
```
2024-01-06 18:23:46 -08:00
Return the parser module object and check that the module is a valid
parser module.
Parameters:
2024-01-06 19:37:20 -08:00
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.
2024-01-06 19:37:20 -08:00
If a Module is given and the Module
is a valid parser Module, then the
same Module is returned.
Returns:
2024-01-06 19:37:20 -08:00
Module: the parser Module object
Raises:
ModuleNotFoundError: If the Module is not found or is not a valid
parser Module, then a ModuleNotFoundError
exception is raised.
2022-01-25 17:07:47 -08:00
<a id="jc.lib.parse"></a>
2022-03-05 12:15:14 -08:00
### parse
2022-01-19 22:20:36 -08:00
```python
2022-10-15 14:36:35 -07:00
def parse(
2024-03-14 21:44:37 -07:00
parser_mod_name: Union[str, module],
2022-10-15 14:36:35 -07:00
data: Union[str, bytes, Iterable[str]],
quiet: bool = False,
raw: bool = False,
2023-01-22 10:37:27 -08:00
ignore_exceptions: Optional[bool] = None,
2022-10-15 14:36:35 -07:00
**kwargs
2024-03-14 21:44:37 -07:00
) -> Union[Dict[str, Any], List[Dict[str, Any]], Iterator[Dict[str, Any]]]
2022-01-19 22:20:36 -08:00
```
2022-10-15 14:43:45 -07:00
Parse the data (string or bytes) using the supplied parser (string or
module object).
2022-01-19 22:20:36 -08:00
This function provides a high-level API to simplify parser use. This
function will call built-in parsers and custom plugin parsers.
2022-08-21 16:20:13 -07:00
Example (standard parsers):
2022-01-25 18:03:34 -08:00
>>> import jc
2022-08-21 16:20:13 -07:00
>>> date_obj = jc.parse('date', 'Tue Jan 18 10:23:07 PST 2022')
>>> print(f'The year is: {date_obj["year"]}')
The year is: 2022
Example (streaming parsers):
>>> import jc
>>> ping_gen = jc.parse('ping_s', ping_output.splitlines())
>>> for item in ping_gen:
>>> print(f'Response time: {item["time_ms"]} ms')
Response time: 102 ms
Response time: 109 ms
...
2022-01-25 18:03:34 -08:00
2022-03-04 08:57:11 -08:00
To get a list of available parser module names, use `parser_mod_list()`.
2022-01-25 18:03:34 -08:00
Alternatively, a parser module object can be supplied:
>>> import jc
2024-01-06 18:23:46 -08:00
>>> jc_date = jc.get_parser('date')
>>> date_obj = jc.parse(jc_date, 'Tue Jan 18 10:23:07 PST 2022')
>>> print(f'The year is: {date_obj["year"]}')
The year is: 2022
2024-01-06 18:23:46 -08:00
You can also use the parser modules directly via `get_parser()`:
>>> import jc
>>> jc_date = jc.get_parser('date')
>>> date_obj = jc_date.parse('Tue Jan 18 10:23:07 PST 2022')
>>> print(f'The year is: {date_obj["year"]}')
The year is: 2022
Finally, you can access the low-level parser modules manually:
2022-01-25 18:03:34 -08:00
>>> import jc.parsers.date
2024-01-06 18:23:46 -08:00
>>> date_obj = jc.parsers.date.parse('Tue Jan 18 10:23:07 PST 2022')
>>> print(f'The year is: {date_obj["year"]}')
The year is: 2022
2022-01-25 18:03:34 -08:00
Though, accessing plugin parsers directly is a bit more cumbersome, so
this higher-level API is recommended. Here is how you can access plugin
parsers without this API:
>>> import os
>>> import sys
>>> import jc.appdirs
>>> data_dir = jc.appdirs.user_data_dir('jc', 'jc')
>>> local_parsers_dir = os.path.join(data_dir, 'jcparsers')
>>> sys.path.append(local_parsers_dir)
>>> import my_custom_parser
>>> my_custom_parser.parse('command_data')
Parameters:
2022-10-14 14:07:05 -07:00
parser_mod_name: (string or name of the parser module. This
Module) function will accept module_name,
2022-01-25 18:03:34 -08:00
cli-name, and --argument-name
variants of the module name.
A Module object can also be passed
2024-01-06 18:23:46 -08:00
directly or via get_parser()
2022-07-18 14:31:13 -07:00
data: (string or data to parse (string or bytes for
bytes or standard parsers, iterable of
iterable) strings for streaming parsers)
2022-01-25 18:03:34 -08:00
raw: (boolean) output preprocessed JSON if True
quiet: (boolean) suppress warning messages if True
ignore_exceptions: (boolean) ignore parsing exceptions if True
(streaming parsers only)
Returns:
Standard Parsers: Dictionary or List of Dictionaries
Streaming Parsers: Generator Object containing Dictionaries
2022-01-25 17:07:47 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.lib.parser_info"></a>
2022-01-25 17:07:47 -08:00
2024-03-14 21:44:37 -07:00
### parser_info
2022-01-19 22:20:36 -08:00
2022-01-25 17:07:47 -08:00
```python
2024-03-14 21:44:37 -07:00
def parser_info(parser_mod_name: Union[str, module],
documentation: bool = False) -> jc.jc_types.ParserInfoType
2022-01-25 17:07:47 -08:00
```
2022-01-19 22:20:36 -08:00
2024-03-14 21:44:37 -07:00
Returns a dictionary that includes the parser module metadata.
2022-01-19 22:20:36 -08:00
2024-03-14 21:44:37 -07:00
Parameters:
2022-01-19 22:20:36 -08:00
2024-03-14 21:44:37 -07:00
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 as well
as a parser module object.
2022-01-19 22:20:36 -08:00
2024-03-14 21:44:37 -07:00
documentation: (boolean) include parser docstring if True
2022-01-19 22:20:36 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.lib.parser_mod_list"></a>
2022-03-03 17:38:47 -08:00
2024-03-14 21:44:37 -07:00
### parser_mod_list
2022-03-03 17:38:47 -08:00
```python
2024-03-14 21:44:37 -07:00
def parser_mod_list(show_hidden: bool = False,
show_deprecated: bool = False) -> List[str]
2022-03-03 17:38:47 -08:00
```
2024-03-14 21:44:37 -07:00
Returns a list of all available parser module names.
2022-03-03 17:38:47 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.lib.plugin_parser_mod_list"></a>
2022-03-03 17:38:47 -08:00
2024-03-14 21:44:37 -07:00
### plugin_parser_mod_list
2022-03-03 17:38:47 -08:00
```python
2024-03-14 21:44:37 -07:00
def plugin_parser_mod_list(show_hidden: bool = False,
show_deprecated: bool = False) -> List[str]
2022-03-03 17:38:47 -08:00
```
2024-03-14 21:44:37 -07:00
Returns a list of plugin parser module names. This function is a
2022-03-03 17:38:47 -08:00
subset of `parser_mod_list()`.
2024-01-03 15:57:08 -08:00
<a id="jc.lib.slurpable_parser_mod_list"></a>
2024-03-14 21:44:37 -07:00
### slurpable_parser_mod_list
2024-01-03 15:57:08 -08:00
```python
def slurpable_parser_mod_list(show_hidden: bool = False,
show_deprecated: bool = False) -> List[str]
```
Returns a list of slurpable parser module names. This function is a
subset of `parser_mod_list()`.
2024-03-14 21:44:37 -07:00
<a id="jc.lib.standard_parser_mod_list"></a>
2022-01-26 15:54:36 -08:00
2024-03-14 21:44:37 -07:00
### standard_parser_mod_list
2022-01-26 15:54:36 -08:00
```python
2024-03-14 21:44:37 -07:00
def standard_parser_mod_list(show_hidden: bool = False,
show_deprecated: bool = False) -> List[str]
2022-01-26 15:54:36 -08:00
```
2024-03-14 21:44:37 -07:00
Returns a list of standard parser module names. This function is a
subset of `parser_mod_list()` and does not contain any streaming
parsers.
2022-01-26 15:54:36 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.lib.streaming_parser_mod_list"></a>
2022-01-19 22:20:36 -08:00
2024-03-14 21:44:37 -07:00
### streaming_parser_mod_list
2022-01-19 22:20:36 -08:00
```python
2024-03-14 21:44:37 -07:00
def streaming_parser_mod_list(show_hidden: bool = False,
show_deprecated: bool = False) -> List[str]
2022-01-19 22:20:36 -08:00
```
2022-01-20 07:49:45 -08:00
2024-03-14 21:44:37 -07:00
Returns a list of streaming parser module names. This function is a
subset of `parser_mod_list()`.
2022-01-20 07:49:45 -08:00
2022-01-19 22:20:36 -08:00