From d37dffc4ab634c76f28bfc4523fd6ce65fb4a3d7 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 25 Jan 2021 12:43:00 -0600 Subject: [PATCH 1/4] Fix comments and quotes Fixes #1 reduce brain damage in parsing loop might not be right in all situations still, but better --- fw_mk.py | 59 +++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/fw_mk.py b/fw_mk.py index 92a6e49..39bd377 100644 --- a/fw_mk.py +++ b/fw_mk.py @@ -9,17 +9,19 @@ n = 1 with open('firewall.rsc') as fp: for line in fp: - if line.endswith('\\\n'): - cad += line[:len(line) - 2].strip() + ' ' + line = line.rstrip() + if line.endswith('\\'): + # continuation + cad += line[:-1] + continue + + cad += line + # print cad + if cad.startswith('#') or cad.startswith('/'): + print "{} ERROR: {}".format(n, cad) else: - cad += line[:len(line) - 1].strip() - cad = cad.replace('= ', '=') - # print cad - if cad.startswith('#'): - print "{} ERROR: {}".format(n, cad) - else: - flines.append(cad) - cad = '' + flines.append(cad) + cad = '' n += 1 rules = [] @@ -27,37 +29,32 @@ in_quoted_str = False for l in flines: l = l.split(' ') - print "{} Analitzant {}".format(n, l) + print "{} Analyzing {}".format(n, l) n += 1 - cad = '' + token_segments = [] in_quoted_str = False tokens = [] # for each token in list for t in l: # if the token has double quotes, we find the closing - if (('"' in t) and (not t.endswith('"'))): - in_quoted_str = True - cad += t - elif t.endswith('"'): - in_quoted_str = False - cad += t - print "\t{}".format(cad) - cad = '' - elif (not '"' in t) and (in_quoted_str): - cad += t + ' ' - elif (not '"' in t) and (not in_quoted_str): - # equal sign without continuation - if t.endswith('='): - cad += t - else: - cad = t - print "\t{}".format(cad) - tokens.append(cad) - cad = '' + if (t.count('"') == 1): + in_quoted_str = not in_quoted_str + + token_segments.append(t) + + if not in_quoted_str: + # finalize token + token = ' '.join(token_segments) + print "\t{}".format(token) + tokens.append(token) + token_segments = [] + rules.append([n, l, tokens]) n += 1 + + # extract different labels labels = {} for r in rules: From 57b44ad61f4f4d75a6480aad8c66b68f2c410234 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 25 Jan 2021 12:44:18 -0600 Subject: [PATCH 2/4] Update fw_mk.py --- fw_mk.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/fw_mk.py b/fw_mk.py index 39bd377..98518e7 100644 --- a/fw_mk.py +++ b/fw_mk.py @@ -53,8 +53,6 @@ rules.append([n, l, tokens]) n += 1 - - # extract different labels labels = {} for r in rules: From 839ffb1734fc5c93d849705ff0bd41b0e82223da Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 25 Jan 2021 13:07:26 -0600 Subject: [PATCH 3/4] handle commas regular quotes for quoting --- fw_mk.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fw_mk.py b/fw_mk.py index 98518e7..4572250 100644 --- a/fw_mk.py +++ b/fw_mk.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -import csv +import csv,re # format each rule in one single line and append it to flines @@ -9,7 +9,7 @@ n = 1 with open('firewall.rsc') as fp: for line in fp: - line = line.rstrip() + line = line.strip() if line.endswith('\\'): # continuation cad += line[:-1] @@ -23,7 +23,7 @@ flines.append(cad) cad = '' n += 1 - +print flines rules = [] n = 1 in_quoted_str = False @@ -72,7 +72,7 @@ # create the csv with open('mk-out.csv', 'wb') as csvfile: - w = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) + w = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) # write header w.writerow(labels) @@ -88,5 +88,5 @@ for t in r[2]: if '=' in t: g = t.split('=') - line[labels.index(g[0])] = g[1] + line[labels.index(g[0])] = re.sub(r'"$', '', re.sub(r'^"', '', g[1])) w.writerow(line) From 3006a87cd349dd133ab6fd032233f268c1083465 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 25 Jan 2021 13:10:11 -0600 Subject: [PATCH 4/4] Update fw_mk.py --- fw_mk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fw_mk.py b/fw_mk.py index 4572250..e364741 100644 --- a/fw_mk.py +++ b/fw_mk.py @@ -23,7 +23,7 @@ flines.append(cad) cad = '' n += 1 -print flines + rules = [] n = 1 in_quoted_str = False