Mercurial > python-cmd2
annotate lineend.py @ 71:ea1697be3d00
getting suffix
author | catherine@Elli.myhome.westell.com |
---|---|
date | Wed, 25 Jun 2008 12:32:21 -0400 |
parents | 4e028e9ec4c2 |
children |
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 |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
3 integer_pattern = pyparsing.Optional(pyparsing.Word(pyparsing.nums)) |
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
4 terminators = [pyparsing.Literal(';') + integer_pattern, |
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
5 pyparsing.Literal('\\t') + integer_pattern] |
62 | 6 complete_pattern = reduce(lambda x, y: x ^ y, terminators) |
64
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
7 redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
8 pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
9 |
64
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
10 for pattern in (complete_pattern, redirect_pattern, pipe_pattern): |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
11 pattern.ignore(pyparsing.sglQuotedString) |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
12 pattern.ignore(pyparsing.dblQuotedString) |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
13 |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
14 def parse(txt): |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
15 """ |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
16 >>> sorted(parse('select * from dual;').items()) |
62 | 17 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', [';'])] |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
18 >>> sorted(parse('select * from dual E').items()) |
62 | 19 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
20 >>> sorted(parse('select * from').items()) |
62 | 21 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
22 >>> sorted(parse('select * from dual; > result.txt').items()) |
62 | 23 [('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None), ('terminator', [';'])] |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
24 >>> sorted(parse("select * from dual where val = 'x > y'").items()) |
62 | 25 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
26 >>> sorted(parse('select * from dual; | wc -c').items()) |
64
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
27 [('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c'), ('terminator', [';'])] |
65
4e028e9ec4c2
punctFinder going to go in
catherine@Elli.myhome.westell.com
parents:
64
diff
changeset
|
28 >>> sorted(parse('select * from dual; | sort > sorted.txt').items()) |
64
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
29 [('inputFrom', None), ('outputTo', None), ('pipeTo', 'sort > sorted.txt'), ('terminator', [';'])] |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
30 """ |
62 | 31 result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None} |
32 found = complete_pattern.searchString(txt) | |
33 if found: | |
34 result['terminator'] = found[0][:] | |
35 found = pipe_pattern.searchString(txt) | |
36 if found: | |
63 | 37 result['pipeTo'] = found[0][1].strip() |
64
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
38 else: # pipe overrides redirects |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
39 found = redirect_pattern.searchString(txt) |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
40 if found: |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
41 if found[0][0] == '>': |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
42 result['outputTo'] = found[0][1].strip() |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
43 else: |
a0f7702f2a4b
ready to integrate lineend?
catherine@Elli.myhome.westell.com
parents:
63
diff
changeset
|
44 result['inputFrom'] = found[0][1].strip() |
62 | 45 |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
46 return result |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
47 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
48 if __name__ == '__main__': |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
49 doctest.testmod() |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
50 |