1 # This file is part of python-rwhoisd
3 # Copyright (C) 2003, David E. Blacka
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 # queryparser.db must be set to a DB class instance.
24 # Define the Lexer for the RWhois query language
44 # for now, quoted values must have the wildcards inside the quotes.
45 # I kind of wonder if anyone would ever notice this.
46 t_QUOTEDVALUE = r'["\']\*?[^"*\n]+\*{0,2}["\']'
49 r'^\*?[^\s"\'=*]+\*{0,2}'
51 if db.is_objectclass(t.value):
58 r'\*?[^\s"\'=!*]+\*{0,2}'
60 if t.value.upper() == 'AND':
62 t.value = t.value.upper()
64 if t.value.upper() == 'OR':
66 t.value = t.value.upper()
68 if db.is_attribute(t.value):
77 # print "Illegal character '%r'" % t.value[0]
85 # Define the parser for the query language
87 # 'value' productions are simple strings
88 # 'querystr' productions are tuples (either 1 or 3 values)
89 # 'query' productions are Query objects
90 # 'total' productions are Query objects
92 def p_total_class_query(t):
104 def p_query_oper_querystr(t):
105 '''query : query AND querystr
106 | query OR querystr'''
110 t[0].cur_clause = [ t[3] ]
111 t[0].clauses.append(t[0].cur_clause)
113 t[0].cur_clause.append(t[3])
115 def p_query_querystr(t):
119 t[0].cur_clause = [ t[1] ]
120 t[0].clauses.append(t[0].cur_clause)
122 def p_querystr_attr_value(t):
123 '''querystr : ATTR EQ value
126 t[0] = (t[1], t[2], t[3])
128 def p_querystr_attr(t):
131 t[0] = (None, '=', t[1])
133 def p_querystr_value(t):
136 t[0] = (None, '=', t[1])
146 def p_quotedvalue(t):
147 'value : QUOTEDVALUE'
149 t[0] = t[1].strip('"')
153 # print "Syntax error at '%s:%s'" % (t.type, t.value)
154 raise yacc.YaccError, "Syntax error at %r" % t.value
159 """A representation of a parsed RWhois query."""
163 self.cur_clause = None
164 self.objectclass = None
165 self.prepared = False
170 for i in range(len(self.clauses)):
172 res += "clause %d:\n" % i
174 res += " " + repr(item) + "\n"
178 return "<Query:\n" + str(self) + ">"
181 """Prepare the query for use. For now, this means propagating
182 an objectclass restriction to all query clauses."""
184 if self.prepared: return
186 for c in self.clauses:
187 c.append(("class-name", "=", self.objectclass))
190 """Return the query clauses. This is a list of AND clauses,
191 which are, in turn, lists of query terms. Query terms are 3
192 element tuples: (attr, op, value)."""
196 def set_class(self, objectclass):
197 """Set the query-wide objectclass restriction."""
199 # note: we don't allow the code to set this more than once,
200 # because we would have to code the removal of the previous
201 # class restriction from the query clauses, and it just isn't
202 # worth it. Queries are built, used and thrown away.
203 assert not self.prepared
204 self.objectclass = objectclass
212 """Return a parser instances. Parser objects should not be shared
218 """Parse a query, raising a RwhoisError in case of parse failure.
219 Returns a Query object."""
221 # before using any parser objects, the database backend must be
222 # set (and it shared by all parsers).
225 return p.parse(query)
226 except (lex.LexError, yacc.YaccError):
227 raise Rwhois.RwhoisError, (350, "")
229 if __name__ == "__main__":
235 print "loading schema:", sys.argv[1]
236 mydb.init_schema(sys.argv[1])
237 for data_file in sys.argv[2:]:
238 print "loading data file:", data_file
239 mydb.load_data(data_file)
245 for line in sys.stdin.readlines():
246 line = line.rstrip('\n')
248 if not line: continue
249 print 'inputting:', `line`
253 except (lex.LexError, yacc.YaccError), x:
254 print "parse error occurred:", x