comparison lineend.py @ 62:95e239c87010

use searchString
author catherine@Elli.myhome.westell.com
date Sat, 21 Jun 2008 19:45:50 -0400
parents 82891de09dbf
children a96f824d1a24
comparison
equal deleted inserted replaced
61:82891de09dbf 62:95e239c87010
1 import pyparsing, sys, doctest 1 import pyparsing, sys, doctest
2 2
3 terminators = (';', 'EOF') 3 intgr = pyparsing.Optional(pyparsing.Word(pyparsing.nums))
4 terminators = [pyparsing.Literal(';') + intgr,
5 pyparsing.Literal('\\t') + intgr]
6 complete_pattern = reduce(lambda x, y: x ^ y, terminators)
7 #complete_pattern = (pyparsing.Literal(';') + intgr) ^ (pyparsing.Literal('\\t') + intgr)
8 complete_pattern.ignore(pyparsing.sglQuotedString)
9 complete_pattern.ignore(pyparsing.dblQuotedString)
4 10
5 #pattern = pyparsing.Optional(pyparsing.CharsNotIn(terminators)) + pyparsing.oneOf(';') 11 redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine
6 complete_pattern = pyparsing.SkipTo(pyparsing.oneOf(terminators)) 12 redirect_pattern.ignore(pyparsing.sglQuotedString)
7 complete_pattern.ignore(pyparsing.sglQuotedString) 13 redirect_pattern.ignore(pyparsing.dblQuotedString)
8 complete_pattern.ignore(pyparsing.sglQuotedString)
9 14
10 redirect_pattern = pyparsing.Optional(pyparsing.CharsNotIn('<>')) + pyparsing.oneOf('< >') + pyparsing.Word(pyparsing.printables) 15 pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine
11 redirect_pattern.ignore(pyparsing.sglQuotedString)
12 redirect_pattern.ignore(pyparsing.sglQuotedString)
13
14 pipe_pattern = pyparsing.Optional(pyparsing.CharsNotIn('|')) + '|' + pyparsing.SkipTo(pyparsing.StringEnd())
15 pipe_pattern.ignore(pyparsing.sglQuotedString) 16 pipe_pattern.ignore(pyparsing.sglQuotedString)
16 pipe_pattern.ignore(pyparsing.sglQuotedString) 17 pipe_pattern.ignore(pyparsing.dblQuotedString)
17 18
18 def parse(txt, mustBeTerminated=False): 19 def parse(txt, mustBeTerminated=False):
19 """ 20 """
20 >>> sorted(parse('select * from dual;', True).items()) 21 >>> sorted(parse('select * from dual;', True).items())
21 [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] 22 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', [';'])]
22 >>> sorted(parse('select * from dual E', True).items()) 23 >>> sorted(parse('select * from dual E', True).items())
23 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] 24 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
24 >>> sorted(parse('select * from', True).items()) 25 >>> sorted(parse('select * from', True).items())
25 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] 26 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
26 >>> sorted(parse('select * from dual; > result.txt', True).items()) 27 >>> sorted(parse('select * from dual; > result.txt', True).items())
27 [('complete', True), ('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None)] 28 [('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None), ('terminator', [';'])]
28 >>> sorted(parse("select * from dual where val = 'x > y'", True).items()) 29 >>> sorted(parse("select * from dual where val = 'x > y'", True).items())
29 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] 30 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
30 >>> sorted(parse('select * from dual; | wc -c', True).items()) 31 >>> sorted(parse('select * from dual; | wc -c', True).items())
31 [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c')] 32 [('inputFrom', None), ('outputTo', None), ('pipeTo', ' wc -c'), ('terminator', [';'])]
33 >>> sorted(parse('select * from dual; | sort > sorted.txt', True).items())
34 [('inputFrom', None), ('outputTo', None), ('pipeTo', 'sort > sorted.txt'), ('terminator', [';'])]
32 """ 35 """
33 result = {'complete': not mustBeTerminated, 'inputFrom': None, 'outputTo': None, 'pipeTo': None} 36 result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None}
34 if mustBeTerminated: 37 found = complete_pattern.searchString(txt)
35 try: 38 if found:
36 complete_pattern.parseString(txt) 39 result['terminator'] = found[0][:]
37 result['complete'] = True 40 found = redirect_pattern.searchString(txt)
38 except pyparsing.ParseException: 41 if found:
39 pass 42 if found[0][0] == '>':
40 43 result['outputTo'] = found[0][1].strip()
41 if result['complete']: 44 else:
42 try: 45 result['inputFrom'] = found[0][1].strip()
43 parsed = redirect_pattern.parseString(txt) 46 found = pipe_pattern.searchString(txt)
44 if parsed[1] == '>': 47 if found:
45 result['outputTo'] = parsed[2] 48 result['pipeTo'] = found[0][1]
46 else: 49
47 result['inputFrom'] = parsed[2]
48 except pyparsing.ParseException:
49 pass
50 try:
51 parsed = pipe_pattern.parseString(txt)
52 result['pipeTo'] = parsed[2]
53 except pyparsing.ParseException:
54 pass
55
56 return result 50 return result
57 51
58 if __name__ == '__main__': 52 if __name__ == '__main__':
59 doctest.testmod() 53 doctest.testmod()
60 54