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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
3 intgr = pyparsing.Optional(pyparsing.Word(pyparsing.nums))
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
4 terminators = [pyparsing.Literal(';') + intgr,
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
5 pyparsing.Literal('\\t') + intgr]
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
6 complete_pattern = reduce(lambda x, y: x ^ y, terminators)
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
9 complete_pattern.ignore(pyparsing.dblQuotedString)
60
682588392eaf experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff changeset
10
62
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
13 redirect_pattern.ignore(pyparsing.dblQuotedString)
60
682588392eaf experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff changeset
14
62
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
32 [('inputFrom', None), ('outputTo', None), ('pipeTo', ' wc -c'), ('terminator', [';'])]
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
33 >>> sorted(parse('select * from dual; | sort > sorted.txt', True).items())
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
36 result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None}
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
37 found = complete_pattern.searchString(txt)
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
38 if found:
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
39 result['terminator'] = found[0][:]
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
40 found = redirect_pattern.searchString(txt)
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
41 if found:
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
42 if found[0][0] == '>':
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
43 result['outputTo'] = found[0][1].strip()
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
44 else:
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
45 result['inputFrom'] = found[0][1].strip()
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
46 found = pipe_pattern.searchString(txt)
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
47 if found:
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
48 result['pipeTo'] = found[0][1]
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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