diff --git a/changelog.txt b/changelog.txt index 68fe5f6e..35bd791d 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,7 +2,8 @@ jc changelog 20200xxxx vX.X.X - Added ntpq command parser -- Added timedatectl status parser +- Added timedatectl status command parser +- Added airport command parser 20200308 v1.8.1 - CLI and history parser optimizations by https://github.com/philippeitis diff --git a/docgen.sh b/docgen.sh index 244adc54..462f76eb 100755 --- a/docgen.sh +++ b/docgen.sh @@ -4,6 +4,7 @@ cd jc pydocmd simple jc+ > ../docs/readme.md pydocmd simple utils+ > ../docs/utils.md +pydocmd simple jc.parsers.airport+ > ../docs/parsers/airport.md pydocmd simple jc.parsers.arp+ > ../docs/parsers/arp.md pydocmd simple jc.parsers.blkid+ > ../docs/parsers/blkid.md pydocmd simple jc.parsers.crontab+ > ../docs/parsers/crontab.md diff --git a/docs/parsers/airport.md b/docs/parsers/airport.md new file mode 100644 index 00000000..08e848f7 --- /dev/null +++ b/docs/parsers/airport.md @@ -0,0 +1,64 @@ +# jc.parsers.airport +jc - JSON CLI output utility airport Parser + +Usage: + + specify --airport as the first argument if the piped input is coming from airport + +Compatibility: + + 'darwin' + +Examples: + + $ airport | jc --airport -p + { + + } + + $ airport | jc --airport -p -r + { + + } + +## info +```python +info(self, /, *args, **kwargs) +``` + +## process +```python +process(proc_data) +``` + +Final processing to conform to the schema. + +Parameters: + + proc_data: (dictionary) raw structured data to process + +Returns: + + Dictionary. Structured data with the following schema: + + { + + } + +## parse +```python +parse(data, raw=False, quiet=False) +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + +Returns: + + Dictionary. Raw or processed structured data. + diff --git a/jc/cli.py b/jc/cli.py index 5e819aab..c4a1e326 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -22,6 +22,7 @@ class info(): __version__ = info.version parsers = [ + 'airport', 'arp', 'blkid', 'crontab', diff --git a/jc/parsers/airport.py b/jc/parsers/airport.py new file mode 100644 index 00000000..c8968efe --- /dev/null +++ b/jc/parsers/airport.py @@ -0,0 +1,98 @@ +"""jc - JSON CLI output utility airport Parser + +Usage: + + specify --airport as the first argument if the piped input is coming from airport + +Compatibility: + + 'darwin' + +Examples: + + $ airport | jc --airport -p + { + + } + + $ airport | jc --airport -p -r + { + + } +""" +import jc.utils + + +class info(): + version = '1.0' + description = 'airport command parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + # details = 'enter any other details here' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['darwin'] + magic_commands = ['airport'] + + +__version__ = info.version + + +def process(proc_data): + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (dictionary) raw structured data to process + + Returns: + + Dictionary. Structured data with the following schema: + + { + + } + """ + # boolean changes + ''' + bool_list = ['ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_active', + 'system_clock_synchronized', 'systemd-timesyncd.service_active'] + for key in proc_data: + if key in bool_list: + try: + proc_data[key] = True if proc_data[key] == 'yes' else False + except (ValueError): + proc_data[key] = None + ''' + + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary. Raw or processed structured data. + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + raw_output = {} + + for line in filter(None, data.splitlines()): + linedata = line.split(':', maxsplit=1) + raw_output[linedata[0].strip().lower().replace(' ', '_')] = linedata[1].strip() + + if raw: + return raw_output + else: + return process(raw_output) diff --git a/tests/fixtures/osx-10.14.6/airport-I.out b/tests/fixtures/osx-10.14.6/airport-I.out new file mode 100644 index 00000000..c2dad159 --- /dev/null +++ b/tests/fixtures/osx-10.14.6/airport-I.out @@ -0,0 +1,15 @@ + agrCtlRSSI: -66 + agrExtRSSI: 0 + agrCtlNoise: -90 + agrExtNoise: 0 + state: running + op mode: station + lastTxRate: 195 + maxRate: 867 +lastAssocStatus: 0 + 802.11 auth: open + link auth: wpa2-psk + BSSID: 3c:37:86:15:ad:f9 + SSID: BrazzleDazzle + MCS: 0 + channel: 48,80 diff --git a/tests/fixtures/osx-10.14.6/airport-s.out b/tests/fixtures/osx-10.14.6/airport-s.out new file mode 100644 index 00000000..1f0254ce --- /dev/null +++ b/tests/fixtures/osx-10.14.6/airport-s.out @@ -0,0 +1,15 @@ + SSID BSSID RSSI CHANNEL HT CC SECURITY (auth/unicast/group) + DIRECT-4A-HP OfficeJet 3830 00:68:eb:2b:b7:3b -91 6 Y -- WPA2(PSK/AES/AES) + xfinitywifi 6e:e3:0e:c1:74:df -87 36 Y US NONE + XFINITY 8e:e3:0e:c1:74:df -86 36 Y US WPA2(802.1x/AES/AES) + YouAreInfected-5 5c:e3:0e:c1:74:df -86 36 Y US WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP) + xfinitywifi 6e:e3:0e:b8:55:8f -85 11 Y US NONE + xfinitywifi 6e:e3:0e:c1:74:de -79 6 Y US NONE + ngHub_319451N814D6D 04:a1:51:56:5c:eb -77 11 Y US WPA2(PSK/AES/AES) + YouAreInfected 5c:e3:0e:c1:74:de -76 6 Y US WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP) + YuanFamily_Extender2G a0:04:60:86:11:89 -76 11 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP) + YuanFamily 5c:e3:0e:b8:15:9f -73 11 Y US WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP) + SnazzleDazzle 3c:37:86:15:fd:b5 -70 48 Y US WPA2(PSK/AES/AES) + SnazzleDazzle 42:37:86:15:fd:b3 -62 3,+1 Y US WPA2(PSK/AES/AES) + SnazzleDazzle 42:37:86:15:aa:f7 -54 3 Y US WPA2(PSK/AES/AES) + SnazzleDazzle 3c:37:86:15:aa:f9 -64 48 Y US WPA2(PSK/AES/AES)