All pastes #2041 Raw Edit

duck

public python v1 · immutable
#2041 ·published 2004-11-11 07:03 UTC
rendered paste body
import reimport shleximport stringdef shlex_split(s):    """    Like shlex.split(), but works on Python 2.2 too    based on http://oregonstate.edu/~barnesc/htmldata/    """    ans = []    i = 0    while i < len(s):        c = s[i]        if c in string.whitespace:            # Whitespace.  Find non-whitespace.            for i2 in range(i, len(s)):                if s[i2] not in string.whitespace:                    break            # Skip the entire string if the last char is whitespace.            if s[i2] in string.whitespace:                i2 += 1            i = i2        else:            # Match 'name = "value"'            c = re.compile(r'\S+\s*\=\s*"[^"]*"')            m = c.match(s, i)            if m:                # remove the double-quotes                match = string.replace(s[i:m.end()], '"', '')                ans.append(match)                i = m.end()                continue            # Match 'name = value'            c = re.compile(r'\S+\s*\=\s*\S*')            m = c.match(s, i)            if m:                ans.append(s[i:m.end()])                i = m.end()                continue            # Match 'name'            c = re.compile(r'\S+')            m = c.match(s, i)            if m:                ans.append(s[i:m.end()])                i = m.end()                continue    return ans  def _test_shlex_split():    """    Unit test for _shlex_split().    """    assert shlex_split('') == []    assert shlex_split(' ') == []    assert shlex_split('HELLO VERSION MIN=1.0 MAX=1.0') == \           ['HELLO', 'VERSION', 'MIN=1.0', 'MAX=1.0']    assert shlex_split('HELLO VERSION MIN=1.0 MAX=1.0 MSG="hello" SIZE=3') == \        ['HELLO', 'VERSION', 'MIN=1.0', 'MAX=1.0', 'MSG=hello', 'SIZE=3']    assert shlex_split('HELLO VERSION MIN=1.0 MAX=1.0 MSG="hello there" SIZE=3') == \        ['HELLO', 'VERSION', 'MIN=1.0', 'MAX=1.0', 'MSG=hello there', 'SIZE=3']# -------------------------------------------------------------------# Unit Test Main Routine# -------------------------------------------------------------------def _test():    """    Unit test main routine.    """    print 'Unit tests:'    _test_shlex_split()    print '  _shlex_split:           OK'if __name__ == '__main__':    _test()