# HG changeset patch # User catherine@Elli.myhome.westell.com # Date 1214092497 14400 # Node ID a0f7702f2a4b3516222bd0d0d5dc9e2ef1d6274f # Parent a96f824d1a24c00d25d3ec8c676ad4960ecf4f74 ready to integrate lineend? diff -r a96f824d1a24 -r a0f7702f2a4b lineend.py --- a/lineend.py Sat Jun 21 19:46:33 2008 -0400 +++ b/lineend.py Sat Jun 21 19:54:57 2008 -0400 @@ -4,17 +4,12 @@ terminators = [pyparsing.Literal(';') + intgr, pyparsing.Literal('\\t') + intgr] complete_pattern = reduce(lambda x, y: x ^ y, terminators) -#complete_pattern = (pyparsing.Literal(';') + intgr) ^ (pyparsing.Literal('\\t') + intgr) -complete_pattern.ignore(pyparsing.sglQuotedString) -complete_pattern.ignore(pyparsing.dblQuotedString) +redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine +pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine -redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine -redirect_pattern.ignore(pyparsing.sglQuotedString) -redirect_pattern.ignore(pyparsing.dblQuotedString) - -pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine -pipe_pattern.ignore(pyparsing.sglQuotedString) -pipe_pattern.ignore(pyparsing.dblQuotedString) +for pattern in (complete_pattern, redirect_pattern, pipe_pattern): + pattern.ignore(pyparsing.sglQuotedString) + pattern.ignore(pyparsing.dblQuotedString) def parse(txt, mustBeTerminated=False): """ @@ -29,23 +24,24 @@ >>> sorted(parse("select * from dual where val = 'x > y'", True).items()) [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] >>> sorted(parse('select * from dual; | wc -c', True).items()) - [('inputFrom', None), ('outputTo', None), ('pipeTo', ' wc -c'), ('terminator', [';'])] + [('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c'), ('terminator', [';'])] >>> sorted(parse('select * from dual; | sort > sorted.txt', True).items()) - [('inputFrom', None), ('outputTo', None), ('pipeTo', 'sort > sorted.txt'), ('terminator', [';'])] + [('inputFrom', None), ('outputTo', None), ('pipeTo', 'sort > sorted.txt'), ('terminator', [';'])] """ result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None} found = complete_pattern.searchString(txt) if found: result['terminator'] = found[0][:] - found = redirect_pattern.searchString(txt) - if found: - if found[0][0] == '>': - result['outputTo'] = found[0][1].strip() - else: - result['inputFrom'] = found[0][1].strip() found = pipe_pattern.searchString(txt) if found: result['pipeTo'] = found[0][1].strip() + else: # pipe overrides redirects + found = redirect_pattern.searchString(txt) + if found: + if found[0][0] == '>': + result['outputTo'] = found[0][1].strip() + else: + result['inputFrom'] = found[0][1].strip() return result