Mercurial > python-cmd2
annotate lineend.py @ 64:a0f7702f2a4b
ready to integrate lineend?
author | catherine@Elli.myhome.westell.com |
---|---|
date | Sat, 21 Jun 2008 19:54:57 -0400 |
parents | a96f824d1a24 |
children | 4e028e9ec4c2 |
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) | |
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 |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
14 def parse(txt, mustBeTerminated=False): |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
15 """ |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
16 >>> sorted(parse('select * from dual;', True).items()) |
62 | 17 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', [';'])] |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
18 >>> sorted(parse('select * from dual E', True).items()) |
62 | 19 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
20 >>> sorted(parse('select * from', True).items()) |
62 | 21 [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)] |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
22 >>> sorted(parse('select * from dual; > result.txt', True).items()) |
62 | 23 [('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
|
24 >>> sorted(parse("select * from dual where val = 'x > y'", True).items()) |
62 | 25 [('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
|
26 >>> sorted(parse('select * from dual; | wc -c', True).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', [';'])] |
62 | 28 >>> sorted(parse('select * from dual; | sort > sorted.txt', True).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 |