Mercurial > python-cmd2
diff lineend.py @ 64:a0f7702f2a4b
ready to integrate lineend?
author | catherine@Elli.myhome.westell.com |
---|---|
date | Sat, 21 Jun 2008 19:54:57 -0400 |
parents | a96f824d1a24 |
children | 4e028e9ec4c2 |
line wrap: on
line diff
--- 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