1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

simplify state variables

This commit is contained in:
Kelly Brazil
2019-10-23 08:46:54 -07:00
parent 393e8bc560
commit b15c8c352a

View File

@ -325,42 +325,40 @@ $ sudo iptables -vnL -t filter | jc --iptables -p
""" """
class state(): def parse(data):
output = [] output = []
chain = {} chain = {}
headers = [] headers = []
def parse(data):
cleandata = data.splitlines() cleandata = data.splitlines()
for line in cleandata: for line in cleandata:
if line.find('Chain') == 0: if line.find('Chain') == 0:
state.output.append(state.chain) output.append(chain)
state.chain = {} chain = {}
state.headers = [] headers = []
parsed_line = line.split() parsed_line = line.split()
state.chain['chain'] = parsed_line[1] chain['chain'] = parsed_line[1]
state.chain['rules'] = [] chain['rules'] = []
continue continue
if line.find('target') == 0 or line.find('pkts') == 1: if line.find('target') == 0 or line.find('pkts') == 1:
state.headers = [] headers = []
state.headers = [h for h in ' '.join(line.strip().split()).split() if h] headers = [h for h in ' '.join(line.strip().split()).split() if h]
state.headers.append("options") headers.append("options")
continue continue
else: else:
rule = line.split(maxsplit=len(state.headers) - 1) rule = line.split(maxsplit=len(headers) - 1)
temp_rule = dict(zip(state.headers, rule)) temp_rule = dict(zip(headers, rule))
if temp_rule: if temp_rule:
state.chain['rules'].append(temp_rule) chain['rules'].append(temp_rule)
state.output = list(filter(None, state.output)) output = list(filter(None, output))
return state.output return output