mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-17 00:07:37 +02:00
160 lines
3.3 KiB
Python
160 lines
3.3 KiB
Python
"""jc - JSON CLI output utility `/etc/hosts` file parser
|
|
|
|
Usage (cli):
|
|
|
|
$ cat /etc/hosts | jc --hosts
|
|
|
|
Usage (module):
|
|
|
|
import jc.parsers.hosts
|
|
result = jc.parsers.hosts.parse(hosts_file_output)
|
|
|
|
Schema:
|
|
|
|
[
|
|
{
|
|
"ip": string,
|
|
"hostname": [
|
|
string
|
|
]
|
|
}
|
|
]
|
|
|
|
Examples:
|
|
|
|
$ cat /etc/hosts | jc --hosts -p
|
|
[
|
|
{
|
|
"ip": "127.0.0.1",
|
|
"hostname": [
|
|
"localhost"
|
|
]
|
|
},
|
|
{
|
|
"ip": "127.0.1.1",
|
|
"hostname": [
|
|
"root-ubuntu"
|
|
]
|
|
},
|
|
{
|
|
"ip": "::1",
|
|
"hostname": [
|
|
"ip6-localhost",
|
|
"ip6-loopback"
|
|
]
|
|
},
|
|
{
|
|
"ip": "fe00::0",
|
|
"hostname": [
|
|
"ip6-localnet"
|
|
]
|
|
},
|
|
{
|
|
"ip": "ff00::0",
|
|
"hostname": [
|
|
"ip6-mcastprefix"
|
|
]
|
|
},
|
|
{
|
|
"ip": "ff02::1",
|
|
"hostname": [
|
|
"ip6-allnodes"
|
|
]
|
|
},
|
|
{
|
|
"ip": "ff02::2",
|
|
"hostname": [
|
|
"ip6-allrouters"
|
|
]
|
|
}
|
|
]
|
|
"""
|
|
import jc.utils
|
|
|
|
|
|
class info():
|
|
"""Provides parser metadata (version, author, etc.)"""
|
|
version = '1.4'
|
|
description = '`/etc/hosts` file parser'
|
|
author = 'Kelly Brazil'
|
|
author_email = 'kellyjonbrazil@gmail.com'
|
|
|
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
|
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
|
|
|
|
|
__version__ = info.version
|
|
|
|
|
|
def _process(proc_data):
|
|
"""
|
|
Final processing to conform to the schema.
|
|
|
|
Parameters:
|
|
|
|
proc_data: (List of Dictionaries) raw structured data to process
|
|
|
|
Returns:
|
|
|
|
List of Dictionaries. Structured data to conform to the schema.
|
|
"""
|
|
|
|
# no additional processing needed
|
|
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:
|
|
|
|
List of Dictionaries. Raw or processed structured data.
|
|
"""
|
|
if not quiet: jc.utils.compatibility(__name__, info.compatible)
|
|
if not isinstance(data, str): raise TypeError("Input data must be a 'str' object.")
|
|
|
|
raw_output = []
|
|
|
|
# Clear any blank lines
|
|
cleandata = list(filter(None, data.splitlines()))
|
|
|
|
if jc.utils.has_data(data):
|
|
|
|
for line in cleandata:
|
|
output_line = {}
|
|
# ignore commented lines
|
|
if line.strip().startswith('#'):
|
|
continue
|
|
|
|
line_list = line.split(maxsplit=1)
|
|
ip = line_list[0]
|
|
hosts = line_list[1]
|
|
hosts_list = hosts.split()
|
|
|
|
comment_found = False
|
|
for i, item in enumerate(hosts_list):
|
|
if '#' in item:
|
|
comment_found = True
|
|
comment_item = i
|
|
break
|
|
|
|
if comment_found:
|
|
hosts_list = hosts_list[:comment_item]
|
|
|
|
output_line['ip'] = ip
|
|
output_line['hostname'] = hosts_list
|
|
|
|
raw_output.append(output_line)
|
|
|
|
if raw:
|
|
return raw_output
|
|
else:
|
|
return _process(raw_output)
|