diff --git a/CHANGELOG b/CHANGELOG index a5c9ceb9..317366db 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ jc changelog +20200730 v1.13.2 +- Add key/value parser (wrapper for ini parser) + 20200727 v1.13.1 - Add route -6 tests diff --git a/EXAMPLES.md b/EXAMPLES.md index 95c54b51..3ddda486 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -1005,7 +1005,7 @@ ifconfig | jc --ifconfig -p # or: jc -p ifconfig } ] ``` -### INI and plain key/value pair files +### INI files ```bash cat example.ini ``` @@ -1044,31 +1044,6 @@ cat example.ini | jc --ini -p } } ``` -```bash -cat keyvalue.txt -``` -``` -# this file contains key/value pairs -name = John Doe -address=555 California Drive -age: 34 -; comments can include # or ; -# delimiter can be = or : -# quoted values have quotation marks stripped by default -# but can be preserved with the -r argument -occupation:"Engineer" -``` -```bash -cat keyvalue.txt | jc --ini -p -``` -```json -{ - "name": "John Doe", - "address": "555 California Drive", - "age": "34", - "occupation": "Engineer" -} -``` ### iptables ```bash iptables --line-numbers -v -L -t nat | jc --iptables -p # or: jc -p iptables --line-numbers -v -L -t nat @@ -1165,6 +1140,32 @@ jobs -l | jc --jobs -p # or: jc -p jobs } ] ``` +### Key/Value files +```bash +cat keyvalue.txt +``` +``` +# this file contains key/value pairs +name = John Doe +address=555 California Drive +age: 34 +; comments can include # or ; +# delimiter can be = or : +# quoted values have quotation marks stripped by default +# but can be preserved with the -r argument +occupation:"Engineer" +``` +```bash +cat keyvalue.txt | jc --ini -p +``` +```json +{ + "name": "John Doe", + "address": "555 California Drive", + "age": "34", + "occupation": "Engineer" +} +``` ### last and lastb ```bash last | jc --last -p # or: jc -p last diff --git a/README.md b/README.md index 3162ebe2..f59eee5a 100644 --- a/README.md +++ b/README.md @@ -133,9 +133,10 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio - `--hosts` enables the `/etc/hosts` file parser - `--id` enables the `id` command parser - `--ifconfig` enables the `ifconfig` command parser -- `--ini` enables the `INI` file parser. Also parses files/output containing simple key/value pairs +- `--ini` enables the `INI` file parser - `--iptables` enables the `iptables` command parser - `--jobs` enables the `jobs` command parser +- `--kv` enables the `Key/Value` file parser - `--last` enables the `last` and `lastb` command parser - `--ls` enables the `ls` command parser - `--lsblk` enables the `lsblk` command parser diff --git a/docgen.sh b/docgen.sh index 27f78227..6a9bbdc1 100755 --- a/docgen.sh +++ b/docgen.sh @@ -28,6 +28,7 @@ pydocmd simple jc.parsers.ifconfig+ > ../docs/parsers/ifconfig.md pydocmd simple jc.parsers.ini+ > ../docs/parsers/ini.md pydocmd simple jc.parsers.iptables+ > ../docs/parsers/iptables.md pydocmd simple jc.parsers.jobs+ > ../docs/parsers/jobs.md +pydocmd simple jc.parsers.kv+ > ../docs/parsers/kv.md pydocmd simple jc.parsers.last+ > ../docs/parsers/last.md pydocmd simple jc.parsers.ls+ > ../docs/parsers/ls.md pydocmd simple jc.parsers.lsblk+ > ../docs/parsers/lsblk.md diff --git a/jc/cli.py b/jc/cli.py index 7eeeba24..d3f8bff3 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -21,7 +21,7 @@ import jc.appdirs as appdirs class info(): - version = '1.13.1' + version = '1.13.2' description = 'JSON CLI output utility' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -54,6 +54,7 @@ parsers = [ 'ini', 'iptables', 'jobs', + 'kv', 'last', 'ls', 'lsblk', diff --git a/jc/parsers/ini.py b/jc/parsers/ini.py index 0ff13746..86ae76d4 100644 --- a/jc/parsers/ini.py +++ b/jc/parsers/ini.py @@ -49,8 +49,8 @@ import configparser class info(): - version = '1.2' - description = 'INI file parser. Also parses files/output containing simple key/value pairs' + version = '1.3' + description = 'INI file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' details = 'Using configparser from the standard library' diff --git a/jc/parsers/kv.py b/jc/parsers/kv.py new file mode 100644 index 00000000..88a21e15 --- /dev/null +++ b/jc/parsers/kv.py @@ -0,0 +1,67 @@ +"""jc - JSON CLI output utility Key/Value File Parser + +Usage: + + Specify --kv as the first argument if the piped input is coming from a simple + key/value pair file. Delimiter can be '=' or ':'. Missing values are supported. + Comment prefix can be '#' or ';'. Comments must be on their own line. + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd' + +Examples: + + $ cat keyvalue.txt + # this file contains key/value pairs + name = John Doe + address=555 California Drive + age: 34 + ; comments can include # or ; + # delimiter can be = or : + # quoted values have quotation marks stripped by default + # but can be preserved with the -r argument + occupation:"Engineer" + + $ cat keyvalue.txt | jc --ini -p + { + "name": "John Doe", + "address": "555 California Drive", + "age": "34", + "occupation": "Engineer" + } +""" + + +class info(): + version = '1.0' + description = 'Key/Value file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + details = 'This is a wrapper for the INI parser' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] + + +__version__ = info.version + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Note: this is just a wrapper for jc.parsers.ini + + Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary representing the key/value file + """ + import jc.parsers.ini + return jc.parsers.ini.parse(data, raw=raw, quiet=quiet) diff --git a/setup.py b/setup.py index 903dc6dc..74c716de 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='1.13.1', + version='1.13.2', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='Converts the output of popular command-line tools and file-types to JSON.',