mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
ignore non-parser-plugin python files in plugin directory
This commit is contained in:
12
README.md
12
README.md
@ -533,20 +533,22 @@ for item in result:
|
|||||||
print(item["filename"])
|
print(item["filename"])
|
||||||
```
|
```
|
||||||
|
|
||||||
### Custom Parsers
|
### Parser Plugins
|
||||||
Custom local parser plugins may be placed in a `jc/jcparsers` folder in your
|
Parser plugins may be placed in a `jc/jcparsers` folder in your local
|
||||||
local **"App data directory"**:
|
**"App data directory"**:
|
||||||
|
|
||||||
- Linux/unix: `$HOME/.local/share/jc/jcparsers`
|
- Linux/unix: `$HOME/.local/share/jc/jcparsers`
|
||||||
- macOS: `$HOME/Library/Application Support/jc/jcparsers`
|
- macOS: `$HOME/Library/Application Support/jc/jcparsers`
|
||||||
- Windows: `$LOCALAPPDATA\jc\jc\jcparsers`
|
- Windows: `$LOCALAPPDATA\jc\jc\jcparsers`
|
||||||
|
|
||||||
Local parser plugins are standard python module files. Use the
|
Parser plugins are standard python module files. Use the
|
||||||
[`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py)
|
[`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py)
|
||||||
or [`jc/parsers/foo_s.py (streaming)`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo_s.py)
|
or [`jc/parsers/foo_s.py (streaming)`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo_s.py)
|
||||||
parser as a template and simply place a `.py` file in the `jcparsers` subfolder.
|
parser as a template and simply place a `.py` file in the `jcparsers` subfolder.
|
||||||
|
Any dependencies can be placed in the `jc` folder above `jcparsers` and can
|
||||||
|
be imported in the parser code.
|
||||||
|
|
||||||
Local plugin filenames must be valid python module names and therefore must
|
Parser plugin filenames must be valid python module names and therefore must
|
||||||
start with a letter and consist entirely of alphanumerics and underscores.
|
start with a letter and consist entirely of alphanumerics and underscores.
|
||||||
Local plugins may override default parsers.
|
Local plugins may override default parsers.
|
||||||
|
|
||||||
|
12
jc/cli.py
12
jc/cli.py
@ -215,14 +215,14 @@ class JcCli():
|
|||||||
category_text: str = ''
|
category_text: str = ''
|
||||||
padding_char: str = ' '
|
padding_char: str = ' '
|
||||||
all_parsers = all_parser_info(show_hidden=True, show_deprecated=False)
|
all_parsers = all_parser_info(show_hidden=True, show_deprecated=False)
|
||||||
generic = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if 'generic' in x['tags']]
|
generic = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if 'generic' in x.get('tags', [])]
|
||||||
standard = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if 'standard' in x['tags']]
|
standard = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if 'standard' in x.get('tags', [])]
|
||||||
command = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if 'command' in x['tags']]
|
command = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if 'command' in x.get('tags', [])]
|
||||||
file_str_bin = [
|
file_str_bin = [
|
||||||
{'arg': x['argument'], 'desc': x['description']} for x in all_parsers
|
{'arg': x['argument'], 'desc': x['description']} for x in all_parsers
|
||||||
if 'file' in x['tags'] or
|
if 'file' in x.get('tags', []) or
|
||||||
'string' in x['tags'] or
|
'string' in x.get('tags', []) or
|
||||||
'binary' in x['tags']
|
'binary' in x.get('tags', [])
|
||||||
]
|
]
|
||||||
streaming = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if x.get('streaming')]
|
streaming = [{'arg': x['argument'], 'desc': x['description']} for x in all_parsers if x.get('streaming')]
|
||||||
categories: Dict = {
|
categories: Dict = {
|
||||||
|
15
jc/lib.py
15
jc/lib.py
@ -213,6 +213,19 @@ def _modname_to_cliname(parser_mod_name: str) -> str:
|
|||||||
"""Return module's cli name (underscores converted to dashes)"""
|
"""Return module's cli name (underscores converted to dashes)"""
|
||||||
return parser_mod_name.replace('_', '-')
|
return parser_mod_name.replace('_', '-')
|
||||||
|
|
||||||
|
def _is_valid_parser_plugin(name: str, local_parsers_dir: str) -> bool:
|
||||||
|
if re.match(r'\w+\.py$', name) and os.path.isfile(os.path.join(local_parsers_dir, name)):
|
||||||
|
try:
|
||||||
|
parser_mod_name = _cliname_to_modname(name)[0:-3]
|
||||||
|
modpath = 'jcparsers.'
|
||||||
|
plugin = importlib.import_module(f'{modpath}{parser_mod_name}')
|
||||||
|
if hasattr(plugin, 'info') and hasattr(plugin, 'parse'):
|
||||||
|
del plugin
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
return False
|
||||||
|
|
||||||
# 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.
|
||||||
@ -222,7 +235,7 @@ local_parsers_dir = os.path.join(data_dir, 'jcparsers')
|
|||||||
if os.path.isdir(local_parsers_dir):
|
if os.path.isdir(local_parsers_dir):
|
||||||
sys.path.append(data_dir)
|
sys.path.append(data_dir)
|
||||||
for name in os.listdir(local_parsers_dir):
|
for name in os.listdir(local_parsers_dir):
|
||||||
if re.match(r'\w+\.py$', name) and os.path.isfile(os.path.join(local_parsers_dir, name)):
|
if _is_valid_parser_plugin(name, local_parsers_dir):
|
||||||
plugin_name = name[0:-3]
|
plugin_name = name[0:-3]
|
||||||
local_parsers.append(_modname_to_cliname(plugin_name))
|
local_parsers.append(_modname_to_cliname(plugin_name))
|
||||||
if plugin_name not in parsers:
|
if plugin_name not in parsers:
|
||||||
|
12
man/jc.1
12
man/jc.1
@ -1,4 +1,4 @@
|
|||||||
.TH jc 1 2023-03-22 1.23.1 "JSON Convert"
|
.TH jc 1 2023-03-23 1.23.1 "JSON Convert"
|
||||||
.SH NAME
|
.SH NAME
|
||||||
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types,
|
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types,
|
||||||
and strings
|
and strings
|
||||||
@ -1316,8 +1316,8 @@ etc...
|
|||||||
Note: Unbuffered output can be slower for large data streams.
|
Note: Unbuffered output can be slower for large data streams.
|
||||||
.RE
|
.RE
|
||||||
|
|
||||||
.SH CUSTOM PARSERS
|
.SH PARSER PLUGINS
|
||||||
Custom local parser plugins may be placed in a \fBjc/jcparsers\fP folder in your
|
Parser plugins may be placed in a \fBjc/jcparsers\fP folder in your
|
||||||
local "App data directory":
|
local "App data directory":
|
||||||
|
|
||||||
.RS
|
.RS
|
||||||
@ -1328,11 +1328,13 @@ local "App data directory":
|
|||||||
.fi
|
.fi
|
||||||
.RE
|
.RE
|
||||||
|
|
||||||
Local parser plugins are standard python module files. Use the
|
Parser plugins are standard python module files. Use the
|
||||||
\fBjc/parsers/foo.py\fP or \fBjc/parsers/foo_s.py\fP (streaming) parser as a
|
\fBjc/parsers/foo.py\fP or \fBjc/parsers/foo_s.py\fP (streaming) parser as a
|
||||||
template and simply place a \fB.py\fP file in the \fBjcparsers\fP subfolder.
|
template and simply place a \fB.py\fP file in the \fBjcparsers\fP subfolder.
|
||||||
|
Any dependencies can be placed in the \fBjc\fP folder above \fBjcparsers\fP
|
||||||
|
and can be imported in the parser code.
|
||||||
|
|
||||||
Local plugin filenames must be valid python module names and therefore must
|
Parser plugin filenames must be valid python module names and therefore must
|
||||||
start with a letter and consist entirely of alphanumerics and underscores. Local
|
start with a letter and consist entirely of alphanumerics and underscores. Local
|
||||||
plugins may override default parsers.
|
plugins may override default parsers.
|
||||||
|
|
||||||
|
@ -366,8 +366,8 @@ etc...
|
|||||||
Note: Unbuffered output can be slower for large data streams.
|
Note: Unbuffered output can be slower for large data streams.
|
||||||
.RE
|
.RE
|
||||||
|
|
||||||
.SH CUSTOM PARSERS
|
.SH PARSER PLUGINS
|
||||||
Custom local parser plugins may be placed in a \fBjc/jcparsers\fP folder in your
|
Parser plugins may be placed in a \fBjc/jcparsers\fP folder in your
|
||||||
local "App data directory":
|
local "App data directory":
|
||||||
|
|
||||||
.RS
|
.RS
|
||||||
@ -378,11 +378,13 @@ local "App data directory":
|
|||||||
.fi
|
.fi
|
||||||
.RE
|
.RE
|
||||||
|
|
||||||
Local parser plugins are standard python module files. Use the
|
Parser plugins are standard python module files. Use the
|
||||||
\fBjc/parsers/foo.py\fP or \fBjc/parsers/foo_s.py\fP (streaming) parser as a
|
\fBjc/parsers/foo.py\fP or \fBjc/parsers/foo_s.py\fP (streaming) parser as a
|
||||||
template and simply place a \fB.py\fP file in the \fBjcparsers\fP subfolder.
|
template and simply place a \fB.py\fP file in the \fBjcparsers\fP subfolder.
|
||||||
|
Any dependencies can be placed in the \fBjc\fP folder above \fBjcparsers\fP
|
||||||
|
and can be imported in the parser code.
|
||||||
|
|
||||||
Local plugin filenames must be valid python module names and therefore must
|
Parser plugin filenames must be valid python module names and therefore must
|
||||||
start with a letter and consist entirely of alphanumerics and underscores. Local
|
start with a letter and consist entirely of alphanumerics and underscores. Local
|
||||||
plugins may override default parsers.
|
plugins may override default parsers.
|
||||||
|
|
||||||
|
@ -393,20 +393,22 @@ for item in result:
|
|||||||
print(item["filename"])
|
print(item["filename"])
|
||||||
```
|
```
|
||||||
|
|
||||||
### Custom Parsers
|
### Parser Plugins
|
||||||
Custom local parser plugins may be placed in a `jc/jcparsers` folder in your
|
Parser plugins may be placed in a `jc/jcparsers` folder in your local
|
||||||
local **"App data directory"**:
|
**"App data directory"**:
|
||||||
|
|
||||||
- Linux/unix: `$HOME/.local/share/jc/jcparsers`
|
- Linux/unix: `$HOME/.local/share/jc/jcparsers`
|
||||||
- macOS: `$HOME/Library/Application Support/jc/jcparsers`
|
- macOS: `$HOME/Library/Application Support/jc/jcparsers`
|
||||||
- Windows: `$LOCALAPPDATA\jc\jc\jcparsers`
|
- Windows: `$LOCALAPPDATA\jc\jc\jcparsers`
|
||||||
|
|
||||||
Local parser plugins are standard python module files. Use the
|
Parser plugins are standard python module files. Use the
|
||||||
[`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py)
|
[`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py)
|
||||||
or [`jc/parsers/foo_s.py (streaming)`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo_s.py)
|
or [`jc/parsers/foo_s.py (streaming)`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo_s.py)
|
||||||
parser as a template and simply place a `.py` file in the `jcparsers` subfolder.
|
parser as a template and simply place a `.py` file in the `jcparsers` subfolder.
|
||||||
|
Any dependencies can be placed in the `jc` folder above `jcparsers` and can
|
||||||
|
be imported in the parser code.
|
||||||
|
|
||||||
Local plugin filenames must be valid python module names and therefore must
|
Parser plugin filenames must be valid python module names and therefore must
|
||||||
start with a letter and consist entirely of alphanumerics and underscores.
|
start with a letter and consist entirely of alphanumerics and underscores.
|
||||||
Local plugins may override default parsers.
|
Local plugins may override default parsers.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user