mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-17 00:07:37 +02:00
add kv parser
This commit is contained in:
@ -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
|
||||
|
||||
|
53
EXAMPLES.md
53
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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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',
|
||||
|
@ -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'
|
||||
|
67
jc/parsers/kv.py
Normal file
67
jc/parsers/kv.py
Normal file
@ -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)
|
Reference in New Issue
Block a user