Skip to content

Commit

Permalink
Merge pull request #542 from kellyjonbrazil/dev
Browse files Browse the repository at this point in the history
Dev v1.25.1
  • Loading branch information
kellyjonbrazil authored Feb 13, 2024
2 parents a319ec8 + 412f112 commit 2cdbebb
Show file tree
Hide file tree
Showing 36 changed files with 1,279 additions and 573 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
jc changelog

20240212 v1.25.1
- Fix for crash when optional libraries are not installed (e.g. xmltodict)
- Fix for `ini` parser crashing with some keys with no values
- Fix `xrandr` parser to extract more EDID data
- Enhance `uptime` parser to support output with no user information
- Enhance `--quiet` CLI option to cover more warning messages
- Add tests for missing optional libraries
- Documentation updates

20240204 v1.25.0
- Add `--slurp` functionality to wrap output from multiple lines into a single array.
Note, this only works with single-line input parsers. (e.g. `date`, `ip-address`, `url`, etc.)
Expand Down
2 changes: 1 addition & 1 deletion docs/parsers/ini.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ Compatibility: linux, darwin, cygwin, win32, aix, freebsd

Source: [`jc/parsers/ini.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/ini.py)

Version 2.1 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 2.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
2 changes: 1 addition & 1 deletion docs/parsers/plist.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ Compatibility: linux, darwin, cygwin, win32, aix, freebsd

Source: [`jc/parsers/plist.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/plist.py)

Version 1.1 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
4 changes: 2 additions & 2 deletions docs/parsers/proc.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ Specific Proc file parser names can be found with `jc -hh` or `jc -a`.

Schemas can also be found online at:

https://kellyjonbrazil.github.io/jc/docs/parsers/proc_<name>
https://kellyjonbrazil.github.io/jc/docs/parsers/proc_<name>

For example:

https://kellyjonbrazil.github.io/jc/docs/parsers/proc_meminfo
https://kellyjonbrazil.github.io/jc/docs/parsers/proc_meminfo

Examples:

Expand Down
2 changes: 1 addition & 1 deletion docs/parsers/uptime.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ Source: [`jc/parsers/uptime.py`](https://github.com/kellyjonbrazil/jc/blob/maste

This parser can be used with the `--slurp` command-line option.

Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.9 by Kelly Brazil (kellyjonbrazil@gmail.com)
2 changes: 1 addition & 1 deletion docs/parsers/xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ Compatibility: linux, darwin, cygwin, win32, aix, freebsd

Source: [`jc/parsers/xml.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/xml.py)

Version 1.9 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.10 by Kelly Brazil (kellyjonbrazil@gmail.com)
10 changes: 5 additions & 5 deletions docs/parsers/xrandr.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Schema:
"maximum_height": integer,
"devices": [
{
"modes": [
"resolution_modes": [
{
"resolution_width": integer,
"resolution_height": integer,
Expand Down Expand Up @@ -82,7 +82,7 @@ Examples:
"maximum_height": 32767,
"devices": [
{
"modes": [
"resolution_modes": [
{
"resolution_width": 1920,
"resolution_height": 1080,
Expand Down Expand Up @@ -143,7 +143,7 @@ Examples:
"maximum_height": 32767,
"devices": [
{
"modes": [
"resolution_modes": [
{
"resolution_width": 1920,
"resolution_height": 1080,
Expand Down Expand Up @@ -199,7 +199,7 @@ Examples:
### parse

```python
def parse(data: str, raw: bool = False, quiet: bool = False) -> Dict
def parse(data: str, raw: bool = False, quiet: bool = False) -> Response
```

Main text parsing function
Expand All @@ -219,4 +219,4 @@ Compatibility: linux, darwin, cygwin, aix, freebsd

Source: [`jc/parsers/xrandr.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/xrandr.py)

Version 1.4 by Kevin Lyter (code (at) lyterk.com)
Version 2.0 by Kevin Lyter (code (at) lyterk.com)
12 changes: 7 additions & 5 deletions jc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def json_out(self) -> str:
ensure_ascii=self.ascii_only
)

if not self.mono:
if not self.mono and PYGMENTS_INSTALLED:
class JcStyle(Style):
styles: CustomColorType = self.custom_colors

Expand Down Expand Up @@ -540,13 +540,12 @@ def do_magic(self) -> None:
if self.magic_run_command_str.startswith('/proc'):
try:
self.magic_found_parser = 'proc'
filelist = shlex.split(self.magic_run_command_str)

# multiple proc files detected
if ' ' in self.magic_run_command_str:
if len(filelist) > 1:
self.slurp = True
multi_out: List[str] = []
filelist = self.magic_run_command_str.split()
filelist = [x.strip() for x in filelist]
self.inputlist = filelist

for file in self.inputlist:
Expand All @@ -557,7 +556,7 @@ def do_magic(self) -> None:

# single proc file
else:
file = self.magic_run_command_str
file = filelist[0]
# self.magic_stdout = self.open_text_file('/Users/kelly/temp' + file)
self.magic_stdout = self.open_text_file(file)

Expand Down Expand Up @@ -861,6 +860,9 @@ def _run(self) -> None:
self.set_mono()
self.set_custom_colors()

if self.quiet:
utils.CLI_QUIET = True

if self.verbose_debug:
tracebackplus.enable(context=11) # type: ignore

Expand Down
4 changes: 2 additions & 2 deletions jc/cli_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
$ jc --pretty /proc/meminfo
Line Slicing:
$ $ cat output.txt | jc 4:15 --parser # Parse from line 4 to 14
with parser (zero-based)
$ cat output.txt | jc 4:15 --parser # Parse from line 4 to 14
with parser (zero-based)
Parser Documentation:
$ jc --help --dig
Expand Down
16 changes: 13 additions & 3 deletions jc/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from jc import utils


__version__ = '1.25.0'
__version__ = '1.25.1'

parsers: List[str] = [
'acpi',
Expand Down Expand Up @@ -251,7 +251,8 @@ def _is_valid_parser_plugin(name: str, local_parsers_dir: str) -> bool:
else:
utils.warning_message([f'Not installing invalid parser plugin "{parser_mod_name}" at {local_parsers_dir}'])
return False
except Exception:
except Exception as e:
utils.warning_message([f'Not installing parser plugin "{parser_mod_name}" at {local_parsers_dir} due to error: {e}'])
return False
return False

Expand Down Expand Up @@ -324,7 +325,16 @@ def _get_parser(parser_mod_name: str) -> ModuleType:
parser_mod_name = _cliname_to_modname(parser_mod_name)
parser_cli_name = _modname_to_cliname(parser_mod_name)
modpath: str = 'jcparsers.' if parser_cli_name in local_parsers else 'jc.parsers.'
return importlib.import_module(f'{modpath}{parser_mod_name}')
mod = None

try:
mod = importlib.import_module(f'{modpath}{parser_mod_name}')
except Exception as e:
mod = importlib.import_module(f'jc.parsers.disabled_parser')
mod.__name__ = parser_mod_name
utils.warning_message([f'"{parser_mod_name}" parser disabled due to error: {e}'])

return mod

def _parser_is_slurpable(parser: ModuleType) -> bool:
"""
Expand Down
23 changes: 23 additions & 0 deletions jc/parsers/broken_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""jc - JSON Convert broken parser - for testing purposes only"""
import non_existent_library

class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.0'
description = 'broken parser'
author = 'N/A'
author_email = 'N/A'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
hidden = True


__version__ = info.version


def parse(
data: str,
raw: bool = False,
quiet: bool = False
) -> dict:
"""Main text parsing function"""
return {}
26 changes: 26 additions & 0 deletions jc/parsers/disabled_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""jc - JSON Convert disabled parser
This parser has been disabled due to an error in the parser code.
"""
from jc.exceptions import ParseError

class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.0'
description = 'Disabled parser'
author = 'N/A'
author_email = 'N/A'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
hidden = True


__version__ = info.version


def parse(
data: str,
raw: bool = False,
quiet: bool = False
) -> dict:
"""Main text parsing function"""
raise ParseError('This parser is disabled.')
25 changes: 12 additions & 13 deletions jc/parsers/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

class info():
"""Provides parser metadata (version, author, etc.)"""
version = '2.1'
version = '2.2'
description = 'INI file parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
Expand All @@ -87,14 +87,10 @@ class info():
__version__ = info.version


class MyDict(dict):
def __setitem__(self, key, value):
# convert None values to empty string
if value is None:
self[key] = ''

else:
super().__setitem__(key, value)
def _none_to_empty_string(data):
if data is None:
return ''
return data


def _process(proc_data):
Expand All @@ -110,13 +106,18 @@ def _process(proc_data):
Dictionary representing the INI file.
"""
# remove quotation marks from beginning and end of values
# and convert None to empty string
for k, v in proc_data.items():
if isinstance(v, dict):
for key, value in v.items():
v[key] = jc.utils.remove_quotes(value)
value = _none_to_empty_string(value)
value = jc.utils.remove_quotes(value)
v[key] = value
continue

proc_data[k] = jc.utils.remove_quotes(v)
v = _none_to_empty_string(v)
v = jc.utils.remove_quotes(v)
proc_data[k] = v

return proc_data

Expand All @@ -143,7 +144,6 @@ def parse(data, raw=False, quiet=False):
if jc.utils.has_data(data):

ini_parser = configparser.ConfigParser(
dict_type = MyDict,
allow_no_value=True,
interpolation=None,
default_section=None,
Expand Down Expand Up @@ -175,4 +175,3 @@ def parse(data, raw=False, quiet=False):
raw_output.update(temp_dict)

return raw_output if raw else _process(raw_output)

8 changes: 7 additions & 1 deletion jc/parsers/plist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
...
}
"""
import sys

# ugly hack because I accidentally shadowed the xml module from the
# standard library with the xml parser. :(
sys.path = [x for x in sys.path if 'jc/jc/parsers' not in x]

from typing import Dict, Union
import plistlib
import binascii
Expand All @@ -53,7 +59,7 @@

class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.1'
version = '1.2'
description = 'PLIST file parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
Expand Down
4 changes: 2 additions & 2 deletions jc/parsers/proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
Schemas can also be found online at:
https://kellyjonbrazil.github.io/jc/docs/parsers/proc_<name>
https://kellyjonbrazil.github.io/jc/docs/parsers/proc_<name>
For example:
https://kellyjonbrazil.github.io/jc/docs/parsers/proc_meminfo
https://kellyjonbrazil.github.io/jc/docs/parsers/proc_meminfo
Examples:
Expand Down
Loading

0 comments on commit 2cdbebb

Please sign in to comment.