summaryrefslogtreecommitdiff
path: root/reconfigure/parsers/squid.py
diff options
context:
space:
mode:
authorAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:26 +0200
committerAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:26 +0200
commitff1408420159488a106492ccd11dd234967029b6 (patch)
tree473420cee1c5229a427ec4cafead1aa6c0a26800 /reconfigure/parsers/squid.py
Imported Upstream version 0.1.29
Diffstat (limited to 'reconfigure/parsers/squid.py')
-rw-r--r--reconfigure/parsers/squid.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/reconfigure/parsers/squid.py b/reconfigure/parsers/squid.py
new file mode 100644
index 0000000..b9b4f97
--- /dev/null
+++ b/reconfigure/parsers/squid.py
@@ -0,0 +1,53 @@
+from reconfigure.nodes import *
+from reconfigure.parsers import BaseParser
+
+
+class SquidParser (BaseParser):
+ """
+ A parser for Squid configs
+ """
+
+ def parse(self, content):
+ lines = filter(None, [x.strip() for x in content.splitlines()])
+ root = RootNode()
+ last_comment = None
+ for line in lines:
+ line = line.strip()
+ if line.startswith('#'):
+ c = line.strip('#').strip()
+ if last_comment:
+ last_comment += '\n' + c
+ else:
+ last_comment = c
+ continue
+ if len(line) == 0:
+ continue
+ tokens = line.split()
+ node = Node('line', Node('arguments'))
+ if last_comment:
+ node.comment = last_comment
+ last_comment = None
+
+ index = 0
+ for token in tokens:
+ if token.startswith('#'):
+ node.comment = ' '.join(tokens[tokens.index(token):])[1:].strip()
+ break
+ if index == 0:
+ node.set_property('name', token)
+ else:
+ node.get('arguments').set_property(str(index), token)
+ index += 1
+ root.append(node)
+ return root
+
+ def stringify(self, tree):
+ r = ''
+ for node in tree.children:
+ if node.comment and '\n' in node.comment:
+ r += ''.join('%s %s\n' % ('#', x) for x in node.comment.splitlines())
+ r += node.get('name').value + ' ' + ' '.join(x.value for x in node.get('arguments').children)
+ if node.comment and not '\n' in node.comment:
+ r += ' # %s' % node.comment
+ r += '\n'
+ return r