Hi all
I'm a little new to this but I'd like to propose a change to the
dict_of_key_value_pairs function in datatypes.py
Basically my problem is this: I have a process I need to monitor defined in
my supervisor.conf. I need to set an environment variable on it as well.
This environment variable has commas in the string, therefore the
dict_of_key_value_pairs parser wont accept it. I've checked svn trunk and
the function currently looks like this:
def dict_of_key_value_pairs(arg):
""" parse KEY=val,KEY2=val2 into {'KEY':'val', 'KEY2':'val2'} """
D = {}
try:
pairs = filter(None, arg.split(','))
for pair in pairs:
try:
k, v = pair.split('=', 1)
except ValueError:
raise ValueError('Unknown key/value pair %s' % pair)
D[k.strip()] = v.strip()
except:
raise ValueError("not a list of key/value pairs: " + repr(arg))
return D
I propose that it gets changed to this:
def dict_of_key_value_pairs(arg):
""" parse KEY=val,KEY2=val2 into {'KEY':'val',
'KEY2':'val2'}
Quotes can be used to allow commas in the value """
D = {}
import re
# Regular expression to match key=value, key='value' or key="value"
strings
# Quoted values can allow commas, unquoted will
not
regex =
re.compile(r"(?P<key>\w+)=(?P<quote>['\"])?(?P<value>.*?)(?(quote)['\"])[,$]")
# Work-around for the fact that the non-greedy value match will not let
the
# regular expression parse the last key=value pair if value is not
quoted
if len(arg) and arg[-1] != ',':
arg += ','
matches = regex.findall(arg)
for match in matches:
# Match will be a tuple of length 3 containing (key, quote,
value)
D[match[0]] = match[2]
return D
I've done thorough testing of my version of the function, and it seems to
work fine. Let me know what you think.
If you would prefer I send a patch please let me know.
Cheers,
Tim
_______________________________________________
Supervisor-users mailing list
[email protected]
http://lists.supervisord.org/mailman/listinfo/supervisor-users