diff options
author | Andrew Shadura <andrew@shadura.me> | 2015-08-20 15:58:30 +0200 |
---|---|---|
committer | Andrew Shadura <andrew@shadura.me> | 2015-08-20 15:58:30 +0200 |
commit | fec1da7e6ea8a3b3b03befa4ff8dd31d539f163d (patch) | |
tree | f349cf4e0bf6198660dfc1f0cfeec567b0bb84c5 /reconfigure/parsers/shell.py | |
parent | 25d6c405aff4167e801d0a4995083a56160b969e (diff) |
Imported Upstream version 0.1.50+git20140603
Diffstat (limited to 'reconfigure/parsers/shell.py')
-rw-r--r-- | reconfigure/parsers/shell.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/reconfigure/parsers/shell.py b/reconfigure/parsers/shell.py new file mode 100644 index 0000000..c151a64 --- /dev/null +++ b/reconfigure/parsers/shell.py @@ -0,0 +1,61 @@ +from reconfigure.nodes import * +from reconfigure.parsers import BaseParser + + +class ShellParser (BaseParser): + """ + A parser for shell scripts with variables + """ + + def __init__(self, *args, **kwargs): + self.comment = '#' + self.continuation = '\\' + BaseParser.__init__(self, *args, **kwargs) + + def parse(self, content): + rawlines = content.splitlines() + lines = [] + while rawlines: + l = rawlines.pop(0).strip() + while self.continuation and rawlines and l.endswith(self.continuation): + l = l[:-len(self.continuation)] + l += rawlines.pop(0) + lines.append(l) + + root = RootNode() + last_comment = None + for line in lines: + line = line.strip() + if line: + if line.startswith(self.comment): + c = line.strip(self.comment).strip() + if last_comment: + last_comment += '\n' + c + else: + last_comment = c + continue + if len(line) == 0: + continue + + name, value = line.split('=', 1) + comment = None + if '#' in value: + value, comment = value.split('#', 1) + last_comment = (last_comment or '') + comment.strip() + node = PropertyNode(name.strip(), value.strip().strip('"')) + if last_comment: + node.comment = last_comment + last_comment = None + root.append(node) + return root + + def stringify(self, tree): + r = '' + for node in tree.children: + if node.comment and '\n' in node.comment: + r += '\n' + ''.join('# %s\n' % x for x in node.comment.splitlines()) + r += '%s="%s"' % (node.name, node.value) + if node.comment and not '\n' in node.comment: + r += ' # %s' % node.comment + r += '\n' + return r |