diff options
author | Andrew Shadura <andrew@shadura.me> | 2015-08-20 15:58:26 +0200 |
---|---|---|
committer | Andrew Shadura <andrew@shadura.me> | 2015-08-20 15:58:26 +0200 |
commit | ff1408420159488a106492ccd11dd234967029b6 (patch) | |
tree | 473420cee1c5229a427ec4cafead1aa6c0a26800 /reconfigure/parsers/nsd.py |
Imported Upstream version 0.1.29
Diffstat (limited to 'reconfigure/parsers/nsd.py')
-rw-r--r-- | reconfigure/parsers/nsd.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/reconfigure/parsers/nsd.py b/reconfigure/parsers/nsd.py new file mode 100644 index 0000000..a1c0522 --- /dev/null +++ b/reconfigure/parsers/nsd.py @@ -0,0 +1,50 @@ +from reconfigure.nodes import * +from reconfigure.parsers import BaseParser + + +class NSDParser (BaseParser): + """ + A parser for NSD DNS server nsd.conf file + """ + + def parse(self, content): + lines = content.splitlines() + root = RootNode() + last_comment = None + node = root + for line in lines: + line = line.strip() + if line: + if line.startswith('#'): + c = line.strip('#').strip() + if last_comment: + last_comment += '\n' + c + else: + last_comment = c + continue + + key, value = line.split(':') + value = value.strip() + key = key.strip() + if key in ['server', 'zone', 'key']: + node = Node(key, comment=last_comment) + root.append(node) + else: + node.append(PropertyNode(key, value, comment=last_comment)) + last_comment = None + return root + + def stringify_comment(self, line, comment): + if comment: + return ''.join('# %s\n' % x for x in comment.splitlines()) + line + return line + + def stringify(self, tree): + r = '' + for node in tree.children: + r += self.stringify_comment(node.name + ':', node.comment) + '\n' + for subnode in node.children: + l = '%s: %s' % (subnode.name, subnode.value) + r += self.stringify_comment(l, subnode.comment) + '\n' + r += '\n' + return r |