On 18/02/2012 16:34, Jason Friedman wrote:
I have a file I use for shell scripts that looks like this:

export VAR1=/path/to/dir
export VAR2=7
export VAR3=${VAR1}/further/path
# comment
. /another/file

And a file /another/file:
export VAR4=database-name

Is there an existing package that will read such a file and return a
dictionary like this:
{
     'VAR1': '/path/to/dir',
     'VAR2': 7,
     'VAR3': '/path/to/dir/further/path',
     'VAR4': 'database-name',
}

I would probably do something like this:

import re

# Parse the file into a dict.
with open(path) as f:
    d = dict(re.findall(r"(?m)^export (VAR\d+)=(.*)", f.read()))

# Expand any references.
for key, value in d.items():
    d[key] = re.sub(r"\$\{(VAR\d+)\}", lambda m: d[m.group(1)], value)
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to