Switch to using shlex for config file parsing

This commit is contained in:
Stefano Rivera 2010-12-20 23:36:17 +02:00
parent 432ddc28ff
commit 3d03dcdb3f
2 changed files with 18 additions and 12 deletions

View File

@ -19,7 +19,9 @@ import os
import os.path
import pwd
import re
import shlex
import socket
import StringIO
import sys
class UDTConfig(object):
@ -46,20 +48,20 @@ class UDTConfig(object):
dictionary
"""
config = {}
var_re = re.compile(r'^\s*([A-Z_]+?)=(.+?)\s*$')
for fn in ('/etc/devscripts.conf', '~/.devscripts'):
f = open(os.path.expanduser(fn), 'r')
try:
f = open(os.path.expanduser(fn), 'r')
except IOError:
continue
for line in f:
m = var_re.match(line)
if m:
value = m.group(2)
# This isn't quite the same as bash's parsing, but
# mostly-compatible for configuration files that aren't
# broken like this: KEY=foo bar
if (len(value) > 2 and value[0] == value[-1]
and value[0] in ("'", '"')):
value = value[1:-1]
config[m.group(1)] = value
parsed = shlex.split(line, comments=True)
if len(parsed) > 1 and not isinstance(f, StringIO.StringIO):
print >> sys.stderr, (
"W: Cannot parse variable assignment in %s: %s"
% (f.name, line))
if len(parsed) >= 1 and '=' in parsed[0]:
key, value = parsed[0].split('=', 1)
config[key] = value
f.close()
return config

View File

@ -63,6 +63,8 @@ SPACE_SUFFIX=yes
SINGLE_QUOTE='yes no'
DOUBLE_QUOTE="yes no"
QUOTED_QUOTE="it's"
PAIR_QUOTES="yes "a' no'
COMMAND_EXECUTION=a b
INHERIT=user
REPEAT=no
REPEAT=yes
@ -75,6 +77,8 @@ REPEAT=yes
'SINGLE_QUOTE': 'yes no',
'DOUBLE_QUOTE': 'yes no',
'QUOTED_QUOTE': "it's",
'PAIR_QUOTES': 'yes a no',
'COMMAND_EXECUTION': 'a',
'INHERIT': 'user',
'REPEAT': 'yes',
})