comparison lineend.py @ 60:682588392eaf

experiments with parsing in lineend
author catherine@Elli.myhome.westell.com
date Fri, 20 Jun 2008 19:52:39 -0400
parents
children 82891de09dbf
comparison
equal deleted inserted replaced
59:37f471fb29b1 60:682588392eaf
1 import pyparsing, sys, doctest
2
3 pattern = pyparsing.Optional(pyparsing.CharsNotIn(';')) + ';'
4 pattern.ignore(pyparsing.sglQuotedString)
5 pattern.ignore(pyparsing.sglQuotedString)
6
7 redirect_pattern = pyparsing.Optional(pyparsing.CharsNotIn('<>')) + pyparsing.oneOf('< >') + pyparsing.Word(pyparsing.printables)
8 redirect_pattern.ignore(pyparsing.sglQuotedString)
9 redirect_pattern.ignore(pyparsing.sglQuotedString)
10
11 def parse(txt, mustBeTerminated=False):
12 """
13 >>> sorted(parse('select * from dual;', True).items())
14 [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)]
15 >>> sorted(parse('select * from', True).items())
16 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)]
17 >>> sorted(parse('select * from dual; > result.txt', True).items())
18 [('complete', True), ('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None)]
19 """
20 result = {'complete': not mustBeTerminated, 'inputFrom': None, 'outputTo': None, 'pipeTo': None}
21 if mustBeTerminated:
22 try:
23 pattern.parseString(txt)
24 result['complete'] = True
25 except pyparsing.ParseException:
26 pass
27
28 if result['complete']:
29 try:
30 parsed = redirect_pattern.parseString(txt)
31 if parsed[1] == '>':
32 result['outputTo'] = parsed[2]
33 else:
34 result['inputFrom'] = parsed[2]
35 except pyparsing.ParseException:
36 pass
37 return result
38
39 if __name__ == '__main__':
40 doctest.testmod()
41