Mercurial > python-cmd2
annotate 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 |
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 |
62 | 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) | |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
8 complete_pattern.ignore(pyparsing.sglQuotedString) |
62 | 9 complete_pattern.ignore(pyparsing.dblQuotedString) |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
10 |
62 | 11 redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
12 redirect_pattern.ignore(pyparsing.sglQuotedString) |
62 | 13 redirect_pattern.ignore(pyparsing.dblQuotedString) |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
14 |
62 | 15 pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
16 pipe_pattern.ignore(pyparsing.sglQuotedString) |
62 | 17 pipe_pattern.ignore(pyparsing.dblQuotedString) |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
18 |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
19 def parse(txt, mustBeTerminated=False): |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
20 """ |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
21 >>> sorted(parse('select * from dual;', True).items()) |
62 | 22 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', [';'])] |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
23 >>> sorted(parse('select * from dual E', True).items()) |
62 | 24 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
25 >>> sorted(parse('select * from', True).items()) |
62 | 26 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
27 >>> sorted(parse('select * from dual; > result.txt', True).items()) |
62 | 28 [('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None), ('terminator', [';'])] |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
29 >>> sorted(parse("select * from dual where val = 'x > y'", True).items()) |
62 | 30 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
31 >>> sorted(parse('select * from dual; | wc -c', True).items()) |
62 | 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', [';'])] | |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
35 """ |
62 | 36 result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None} |
37 found = complete_pattern.searchString(txt) | |
38 if found: | |
39 result['terminator'] = found[0][:] | |
40 found = redirect_pattern.searchString(txt) | |
41 if found: | |
42 if found[0][0] == '>': | |
43 result['outputTo'] = found[0][1].strip() | |
44 else: | |
45 result['inputFrom'] = found[0][1].strip() | |
46 found = pipe_pattern.searchString(txt) | |
47 if found: | |
48 result['pipeTo'] = found[0][1] | |
49 | |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
50 return result |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
51 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
52 if __name__ == '__main__': |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
53 doctest.testmod() |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
54 |