1 # This file is part of python-rwhoisd
3 # Copyright (C) 2003, David E. Blacka
5 # $Id: QueryParser.py,v 1.2 2003/04/28 16:43:19 davidb Exp $
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 # queryparser.db must be set to a DB class instance.
26 # Define the Lexer for the RWhois query language
46 # for now, quoted values must have the wildcards inside the quotes.
47 # I kind of wonder if anyone would ever notice this.
48 t_QUOTEDVALUE = r'["\']\*?[^"*\n]+\*{0,2}["\']'
51 r'^\*?[^\s"\'=*]+\*{0,2}'
53 if db.is_objectclass(t.value):
60 r'\*?[^\s"\'=!*]+\*{0,2}'
62 if t.value.upper() == 'AND':
64 t.value = t.value.upper()
66 if t.value.upper() == 'OR':
68 t.value = t.value.upper()
70 if db.is_attribute(t.value):
79 # print "Illegal character '%r'" % t.value[0]
87 # Define the parser for the query language
89 # 'value' productions are simple strings
90 # 'querystr' productions are tuples (either 1 or 3 values)
91 # 'query' productions are Query objects
92 # 'total' productions are Query objects
94 def p_total_class_query(t):
100 def p_total_query(t):
106 def p_query_oper_querystr(t):
107 '''query : query AND querystr
108 | query OR querystr'''
112 t[0].cur_clause = [ t[3] ]
113 t[0].clauses.append(t[0].cur_clause)
115 t[0].cur_clause.append(t[3])
117 def p_query_querystr(t):
121 t[0].cur_clause = [ t[1] ]
122 t[0].clauses.append(t[0].cur_clause)
124 def p_querystr_attr_value(t):
125 '''querystr : ATTR EQ value
128 t[0] = (t[1], t[2], t[3])
130 def p_querystr_attr(t):
133 t[0] = (None, '=', t[1])
135 def p_querystr_value(t):
138 t[0] = (None, '=', t[1])
148 def p_quotedvalue(t):
149 'value : QUOTEDVALUE'
151 t[0] = t[1].strip('"')
155 # print "Syntax error at '%s:%s'" % (t.type, t.value)
156 raise yacc.YaccError, "Syntax error at %r" % t.value
161 """A representation of a parsed RWhois query."""
165 self.cur_clause = None
166 self.objectclass = None
167 self.prepared = False
172 for i in range(len(self.clauses)):
174 res += "clause %d:\n" % i
176 res += " " + repr(item) + "\n"
180 return "<Query:\n" + str(self) + ">"
183 """Prepare the query for use. For now, this means propagating
184 an objectclass restriction to all query clauses."""
186 if self.prepared: return
188 for c in self.clauses:
189 c.append(("class-name", "=", self.objectclass))
192 """Return the query clauses. This is a list of AND clauses,
193 which are, in turn, lists of query terms. Query terms are 3
194 element tuples: (attr, op, value)."""
198 def set_class(self, objectclass):
199 """Set the query-wide objectclass restriction."""
201 # note: we don't allow the code to set this more than once,
202 # because we would have to code the removal of the previous
203 # class restriction from the query clauses, and it just isn't
204 # worth it. Queries are built, used and thrown away.
205 assert not self.prepared
206 self.objectclass = objectclass
214 """Return a parser instances. Parser objects should not be shared
220 """Parse a query, raising a RwhoisError in case of parse failure.
221 Returns a Query object."""
223 # before using any parser objects, the database backend must be
224 # set (and it shared by all parsers).
227 return p.parse(query)
228 except (lex.LexError, yacc.YaccError):
229 raise Rwhois.RwhoisError, (350, "")
231 if __name__ == "__main__":
237 print "loading schema:", sys.argv[1]
238 mydb.init_schema(sys.argv[1])
239 for data_file in sys.argv[2:]:
240 print "loading data file:", data_file
241 mydb.load_data(data_file)
247 for line in sys.stdin.readlines():
248 line = line.rstrip('\n')
250 if not line: continue
251 print 'inputting:', `line`
255 except (lex.LexError, yacc.YaccError), x:
256 print "parse error occurred:", x