Fantastic. Patch file is attached
Glad I could contribute :)
Any idea of when the next egg will be released on pypi?
Tim
On Thu, May 21, 2009 at 11:07 PM, Chris McDonough <[email protected]> wrote:
> Hi Tim.
>
> This looks fine I think (although I haven't tested it myself, the idea
> sounds fine to me). Do you think you can send that patch over?
>
>
> On 5/21/09 1:10 AM, Tim Godfrey wrote:
>
>> 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
>>
>
>
82c82,84
< """ parse KEY=val,KEY2=val2 into {'KEY':'val', 'KEY2':'val2'} """
---
> """ parse KEY=val,KEY2=val2 into {'KEY':'val', 'KEY2':'val2'}
> Quotes can be used to allow commas in the value
> """
84,93c86,102
< 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))
---
> 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"[ \t]*(?P<key>\w+)[ \t]*=[ \t]*(?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]
>
_______________________________________________
Supervisor-users mailing list
[email protected]
http://lists.supervisord.org/mailman/listinfo/supervisor-users