Mercurial > python-cmd2
annotate 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 |
rev | line source |
---|---|
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
1 import pyparsing, sys, doctest |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
2 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
3 pattern = pyparsing.Optional(pyparsing.CharsNotIn(';')) + ';' |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
4 pattern.ignore(pyparsing.sglQuotedString) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
5 pattern.ignore(pyparsing.sglQuotedString) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
6 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
7 redirect_pattern = pyparsing.Optional(pyparsing.CharsNotIn('<>')) + pyparsing.oneOf('< >') + pyparsing.Word(pyparsing.printables) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
8 redirect_pattern.ignore(pyparsing.sglQuotedString) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
9 redirect_pattern.ignore(pyparsing.sglQuotedString) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
10 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
11 def parse(txt, mustBeTerminated=False): |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
12 """ |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
13 >>> sorted(parse('select * from dual;', True).items()) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
14 [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
15 >>> sorted(parse('select * from', True).items()) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
16 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
17 >>> sorted(parse('select * from dual; > result.txt', True).items()) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
18 [('complete', True), ('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None)] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
19 """ |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
20 result = {'complete': not mustBeTerminated, 'inputFrom': None, 'outputTo': None, 'pipeTo': None} |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
21 if mustBeTerminated: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
22 try: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
23 pattern.parseString(txt) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
24 result['complete'] = True |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
25 except pyparsing.ParseException: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
26 pass |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
27 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
28 if result['complete']: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
29 try: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
30 parsed = redirect_pattern.parseString(txt) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
31 if parsed[1] == '>': |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
32 result['outputTo'] = parsed[2] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
33 else: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
34 result['inputFrom'] = parsed[2] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
35 except pyparsing.ParseException: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
36 pass |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
37 return result |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
38 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
39 if __name__ == '__main__': |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
40 doctest.testmod() |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
41 |