annotate completion.py @ 141:ddfab7bb9d94 1.4.8

tie to cmd2 0.3.6
author catherine@Elli.myhome.westell.com
date Thu, 04 Sep 2008 11:33:32 -0400
parents 3b3c78bad48f
children
rev   line source
138
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
1 import pyparsing, re
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
2
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
3 sqlStyleComment = pyparsing.Literal("--") + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n"))
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
4 keywords = {'order by': pyparsing.Keyword('order', caseless=True) +
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
5 pyparsing.Keyword('by', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
6 'select': pyparsing.Keyword('select', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
7 'from': pyparsing.Keyword('from', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
8 'having': pyparsing.Keyword('having', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
9 'update': pyparsing.Keyword('update', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
10 'set': pyparsing.Keyword('set', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
11 'delete': pyparsing.Keyword('delete', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
12 'insert into': pyparsing.Keyword('insert', caseless=True) +
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
13 pyparsing.Keyword('into', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
14 'values': pyparsing.Keyword('values', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
15 'group by': pyparsing.Keyword('group', caseless=True) +
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
16 pyparsing.Keyword('by', caseless=True),
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
17 'where': pyparsing.Keyword('where', caseless=True)}
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
18 for (name, parser) in keywords.items():
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
19 parser.ignore(pyparsing.sglQuotedString)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
20 parser.ignore(pyparsing.dblQuotedString)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
21 parser.ignore(pyparsing.cStyleComment)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
22 parser.ignore(sqlStyleComment)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
23 parser.name = name
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
24
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
25 fromClauseFinder = re.compile(r".*(from|update)(.*)(where|set)",
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
26 re.IGNORECASE | re.DOTALL | re.MULTILINE)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
27 oracleTerms = oracleTerms = re.compile(r"[A-Z$_#][0-9A-Z_$#]*", re.IGNORECASE)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
28 def tableNamesFromFromClause(statement):
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
29 result = fromClauseFinder.search(statement)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
30 if not result:
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
31 return []
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
32 result = oracleTerms.findall(result.group(2))
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
33 result = [r.upper() for r in result if r.upper() not in ('JOIN','ON')]
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
34 return result
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
35
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
36 def orderedParseResults(parsers, statement):
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
37 results = []
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
38 for parser in parsers:
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
39 results.extend(parser.scanString(statement))
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
40 results.sort(cmp=lambda x,y:cmp(x[1],y[1]))
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
41 return results
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
42
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
43 def whichSegment(statement):
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
44 results = orderedParseResults(keywords.values(), statement)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
45 if results:
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
46 return ' '.join(results[-1][0])
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
47 else:
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
48 return None
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
49
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
50 oracleIdentifierCharacters = pyparsing.alphanums + '_#$'
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
51 def wordInProgress(statement):
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
52 result = []
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
53 letters = list(statement)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
54 letters.reverse()
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
55 for letter in letters:
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
56 if letter not in oracleIdentifierCharacters:
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
57 result.reverse()
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
58 return ''.join(result)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
59 result.append(letter)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
60 result.reverse()
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
61 return ''.join(result)
3b3c78bad48f need completion too
catherine@Elli.myhome.westell.com
parents:
diff changeset
62