So here's my shellconfig.py, presented for comment / inspiration.
A couple notes:
* This is an interesting construct that I wasn't aware of:
def quote(s):
for c in s:
if c not in _safechars:
break
else:
return s
...
Apparently, if you have an 'else:' block after 'for:', it gets executed
if you 'break' out of the loop. Neat!
But _parseline is really the interesting part:
def _parseline(self, line):
s = line.strip()
if '#' in s:
s = s[:s.find('#')] # remove from comment to EOL
s = s.strip() # and any unnecessary whitespace
key, eq, val = s.partition('=')
if self.read_unquote:
val = unquote(val)
if key != '' and eq == '=':
return (key, val)
else:
return (None, None)
* You want to remove the comment before the partition(), otherwise this
line would end up with a weird value:
GRAPHICS="vnc" # other choices: "spice", "none"
* quote() and unquote() use pipes._safechars and shlex.split(),
respectively.
* pipes.quote() doesn't work because it puts single-quotes around '$',
assuming that the given string is a literal filename
* There's no write() method but it's pretty obvious - you'd just open a
file and do f.write(str(config)) or similar.
Hope that's useful to somebody,
-w