""" DictForArgs.py See the doc string for the DictForArgs() function. Also, there is a test suite in Testing/TestDictForArgs.py """ import re, string class DictForArgsError(Exception): pass def _SyntaxError(s): raise DictForArgsError, 'Syntax error: %s' % repr(s) def DictForArgs(s): """ Takes an input such as: x=3 name="foo" first='john' last='doe' required border=3 And returns a dictionary representing the same. For keys that aren't given an explicit value (such as 'required' above), the value is '1'. All values are interpreted as strings. If you want ints and floats, you'll have to convert them yourself. This syntax is equivalent to what you find in HTML and close to other ML languages such as XML. Returns {} for an empty string. The informal grammar is: (NAME [=NAME|STRING])* Will raise DictForArgsError if the string is invalid. See also: PyDictForArgs() and ExpandDictWithExtras() in this module """ s = string.strip(s) # Tokenize # @@ 2001-09-29 ce: push these outside for better performance nameRE = re.compile(r'\w+') equalsRE = re.compile(r'\=') stringRE = re.compile(r''' "[^"]+"| '[^']+'| \S+''', re.VERBOSE) #' whiteRE = re.compile(r'\s+') REs = [nameRE, equalsRE, stringRE, whiteRE] verbose = 0 matches = [] start = 0 sLen = len(s) if verbose: print '>> DictForArgs(%s)' % repr(s) print '>> sLen:', sLen while start> try:', regEx match = regEx.match(s, start) if verbose: print '>> match:', match if match is not None: if match.re is not whiteRE: matches.append(match) start = match.end() if verbose: print '>> new start:', start break else: _SyntaxError(s) if verbose: names = [] for match in matches: if match.re is nameRE: name = 'name' elif match.re is equalsRE: name = 'equals' elif match.re is stringRE: name = 'string' elif match.re is whiteRE: name = 'white' names.append(name) #print '>> match =', name, match print '>> names =', names # Process tokens # At this point we have a list of all the tokens (as re.Match objects) # We need to process these into a dictionary. dict = {} matchesLen = len(matches) i = 0 while i