comparison lineend.py @ 65:4e028e9ec4c2

punctFinder going to go in
author catherine@Elli.myhome.westell.com
date Mon, 23 Jun 2008 12:51:37 -0400
parents a0f7702f2a4b
children
comparison
equal deleted inserted replaced
64:a0f7702f2a4b 65:4e028e9ec4c2
1 import pyparsing, sys, doctest 1 import pyparsing, sys, doctest
2 2
3 intgr = pyparsing.Optional(pyparsing.Word(pyparsing.nums)) 3 integer_pattern = pyparsing.Optional(pyparsing.Word(pyparsing.nums))
4 terminators = [pyparsing.Literal(';') + intgr, 4 terminators = [pyparsing.Literal(';') + integer_pattern,
5 pyparsing.Literal('\\t') + intgr] 5 pyparsing.Literal('\\t') + integer_pattern]
6 complete_pattern = reduce(lambda x, y: x ^ y, terminators) 6 complete_pattern = reduce(lambda x, y: x ^ y, terminators)
7 redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine 7 redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine
8 pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine 8 pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine
9 9
10 for pattern in (complete_pattern, redirect_pattern, pipe_pattern): 10 for pattern in (complete_pattern, redirect_pattern, pipe_pattern):
11 pattern.ignore(pyparsing.sglQuotedString) 11 pattern.ignore(pyparsing.sglQuotedString)
12 pattern.ignore(pyparsing.dblQuotedString) 12 pattern.ignore(pyparsing.dblQuotedString)
13 13
14 def parse(txt, mustBeTerminated=False): 14 def parse(txt):
15 """ 15 """
16 >>> sorted(parse('select * from dual;', True).items()) 16 >>> sorted(parse('select * from dual;').items())
17 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', [';'])] 17 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', [';'])]
18 >>> sorted(parse('select * from dual E', True).items()) 18 >>> sorted(parse('select * from dual E').items())
19 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] 19 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
20 >>> sorted(parse('select * from', True).items()) 20 >>> sorted(parse('select * from').items())
21 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] 21 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
22 >>> sorted(parse('select * from dual; > result.txt', True).items()) 22 >>> sorted(parse('select * from dual; > result.txt').items())
23 [('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None), ('terminator', [';'])] 23 [('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None), ('terminator', [';'])]
24 >>> sorted(parse("select * from dual where val = 'x > y'", True).items()) 24 >>> sorted(parse("select * from dual where val = 'x > y'").items())
25 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] 25 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
26 >>> sorted(parse('select * from dual; | wc -c', True).items()) 26 >>> sorted(parse('select * from dual; | wc -c').items())
27 [('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c'), ('terminator', [';'])] 27 [('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c'), ('terminator', [';'])]
28 >>> sorted(parse('select * from dual; | sort > sorted.txt', True).items()) 28 >>> sorted(parse('select * from dual; | sort > sorted.txt').items())
29 [('inputFrom', None), ('outputTo', None), ('pipeTo', 'sort > sorted.txt'), ('terminator', [';'])] 29 [('inputFrom', None), ('outputTo', None), ('pipeTo', 'sort > sorted.txt'), ('terminator', [';'])]
30 """ 30 """
31 result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None} 31 result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None}
32 found = complete_pattern.searchString(txt) 32 found = complete_pattern.searchString(txt)
33 if found: 33 if found: