From 2eec3194e0ec12e10ab3fa1ea437f0714eb9fb3e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 19 Mar 2014 01:40:58 +0100 Subject: allow comments on lines with meaning and fix spawn command parse so that brackets can be used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- examples/textconf | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'examples/textconf') diff --git a/examples/textconf b/examples/textconf index 07495b0..1e00c6b 100644 --- a/examples/textconf +++ b/examples/textconf @@ -44,9 +44,33 @@ with open(conf, 'r') as file: sections = {'blueshift' : []} section = [] sections['blueshift'].append(section) +def remove_comment(text, spawn_aware): + if (';' in text) or ('#' in text): + if not spawn_aware: + return text[:text.replace('#', ';').find(';')] + buf, cmd, stack = '', 0, 0 + for c in buf: + if cmd > 0: + if c == '(': + stack += 1 + elif c == ')': + stack -= 1 + if cmd == 1: + cmd = 2 if c == '(' else 0 + elif (c == ')') and (stack == 0): + cmd = 0 + elif c == '$': + cmd = 1 + stack = 0 + elif (c == ';') or (c == '#'): + break + buf += c + return buf + return text for line in conf.split('\n'): line = line.strip() - if line.startswith('[') and line.endswith(']'): + if line.startswith('[') and remove_comment(line, False).rstrip().endswith(']'): + line = remove_comment(line, False).rstrip() section_name = line[1 : -1].strip().lower() if section_name not in sections: sections[section_name] = [] @@ -55,10 +79,14 @@ for line in conf.split('\n'): elif line.startswith(';') or line.startswith('#'): continue elif ('=' in line) or (':' in line): - eq = len(line) if '=' not in line else line.find('=') - cl = len(line) if ':' not in line else line.find(':') - eq = min(eq, cl) - section.append((line[:eq].strip().lower(), line[eq + 1:].strip())) + line = remove_comment(line, True) + if ('=' in line) or (':' in line): + eq = len(line) if '=' not in line else line.find('=') + cl = len(line) if ':' not in line else line.find(':') + eq = min(eq, cl) + section.append((line[:eq].strip().lower(), line[eq + 1:].strip())) + elif len(line.strip()) > 0: + sys.stderr.buffer.write(('Malformated line: %s\n' % line).encode('utf-8')) elif len(line.strip()) > 0: sys.stderr.buffer.write(('Malformated line: %s\n' % line).encode('utf-8')) sys.stderr.buffer.flush() @@ -100,9 +128,13 @@ def parse_value(value): if (proc.returncode == 0) and (len(output) > 0): return output return None - words, buf, cmd = [], '', None + words, buf, cmd, stack = [], '', None, 0 for c in value: if cmd is not None: + if c == '(': + stack += 1 + elif c == ')': + stack -= 1 if cmd == '': if c == '(': cmd += '(' @@ -111,7 +143,7 @@ def parse_value(value): buf += '$' else: cmd += c - if c == ')': + if (c == ')') and (stack == 0): cmd = cmd[1 : -1] cmd = spawn(cmd) if cmd is not None: @@ -123,6 +155,7 @@ def parse_value(value): buf = '' elif c == '$': cmd = '' + stack = 0 else: buf += c if not buf == '': -- cgit v1.2.3-70-g09d2