summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-03-19 01:40:58 +0100
committerMattias Andrée <maandree@operamail.com>2014-03-19 01:40:58 +0100
commit2eec3194e0ec12e10ab3fa1ea437f0714eb9fb3e (patch)
tree38596e47be3eaf43717e3f64abad5b6780fa3186 /examples
parentfix time/solar elevation to dayness mapping (diff)
downloadblueshift-2eec3194e0ec12e10ab3fa1ea437f0714eb9fb3e.tar.gz
blueshift-2eec3194e0ec12e10ab3fa1ea437f0714eb9fb3e.tar.bz2
blueshift-2eec3194e0ec12e10ab3fa1ea437f0714eb9fb3e.tar.xz
allow comments on lines with meaning and fix spawn command parse so that brackets can be used
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/textconf47
1 files changed, 40 insertions, 7 deletions
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 == '':