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
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)
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
31 result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None}
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
32 found = complete_pattern.searchString(txt)
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
33 if found:
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
34 result['terminator'] = found[0][:]
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
35 found = pipe_pattern.searchString(txt)
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
36 if found:
63
a96f824d1a24 use searchString
catherine@Elli.myhome.westell.com
parents: 62
diff changeset
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
95e239c87010 use searchString
catherine@Elli.myhome.westell.com
parents: 61
diff changeset
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