annotate shelltools/query-pr/query.py @ 47:bcd1d06838fd

more better stuff
author David A. Holland
date Wed, 13 Aug 2014 03:03:30 -0400 (2014-08-13)
parents 73e6dac29391
children 3d5adf5a59d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
c013fb703183 Empty placeholder scripts so the build will run.
David A. Holland
parents:
diff changeset
1 #!@PYTHON@
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
2
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
3 import argparse
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
4 import psycopg2
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
5
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
6 program_description = """
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
7 Search for and retrieve problem reports.
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
8 """
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
9 program_version = "@VERSION@"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
10
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
11 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
12 # settings
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
13
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
14 outfile = sys.stdout
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
15 outmaterial = "headers"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
16 outformat = "text"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
17
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
18 ############################################################
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
19 # database field access
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
20
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
21 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
22 # Fields of PRs that we might search are spread across a number of
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
23 # tables and require varying joins to get them. And, because of
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
24 # classication schemes, the set of fields isn't static and we can't
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
25 # just assemble a massive view with one column for each field.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
26 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
27 # The QueryBuilder class knows how to arrange for all known fields to
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
28 # be present.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
29 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
30
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
31 class QueryBuilder:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
32 # these fields are in the PRs table
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
33 prtable_fields = [
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
34 "id", "synopsis", "confidential", "state", "locked",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
35 "timeout_date", "timeout_state",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
36 "arrival_schemaversion", "arrival_date", "modified_date",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
37 "closed_date",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
38 "release", "environment"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
39 ]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
40
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
41 # these fields are aliases for others
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
42 alias_fields = {
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
43 "number" : "id"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
44 "date" : "arrival_date"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
45 }
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
46
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
47 def __init__(self):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
48 self.present = {}
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
49 self.joined = {}
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
50 self.fromitems = []
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
51 self.whereitems = []
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
52 self.order = None
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
53
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
54 def setorder(self, order):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
55 self.order = order
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
56
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
57 # add to present{} and return the value for convenience (internal)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
58 def makepresent(self, field, name):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
59 self.present[field] = name
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
60 return name
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
61
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
62 # add a join item (once only) (internal)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
63 def addjoin(self, table, as = None):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
64 if as is not None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
65 key = table + "-" + as
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
66 val = table + " AS " + as
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
67 else:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
68 key = table
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
69 val = table
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
70 if key not in self.joined:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
71 self.joined[key] = True
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
72 self.fromitems.append(val)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
73
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
74 # returns a sql expression for the field
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
75 def getfield(self, field):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
76 # already-fetched fields
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
77 if field in self.present:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
78 return self.present[field]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
79
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
80 # aliases for other fields
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
81 if field in alias_fields:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
82 return self.getfield(alias_fields[field])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
83
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
84 # simple fields directly in the PRs table
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
85 if field in prtable_fields:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
86 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
87 return self.makepresent(field, "PRs." + field)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
88
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
89 # now it gets more interesting...
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
90 if field == "closed":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
91 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
92 self.addjoin("states")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
93 self.addwhere("PRs.state = states.name")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
94 return self.makepresent(field, "states.closed")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
95
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
96 # XXX let's pick one set of names and use them everywhere
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
97 # (e.g. change "posttime" in the schema to "message_date"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
98 # or something)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
99 if field == "comment_date" or field == "posttime":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
100 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
101 self.addjoin("messages")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
102 self.addwhere("PRs.id = messages.pr")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
103 return self.makepresent(field, "messages.posttime")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
104
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
105 if field == "comment" or field == "message" or field == "post":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
106 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
107 self.addjoin("messages")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
108 self.addwhere("PRs.id = messages.pr")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
109 return self.makepresent(field, "messages.body")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
110
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
111 if field == "attachment":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
112 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
113 self.addjoin("messages")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
114 self.addjoin("attachments")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
115 self.addwhere("PRs.id = messages.pr")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
116 self.addwhere("messages.id = attachments.msgid")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
117 return self.makepresent(field, "attachments.body")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
118
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
119 if field == "patch":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
120 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
121 self.addjoin("messages")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
122 self.addjoin("attachments", "patches")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
123 self.addwhere("PRs.id = messages.pr")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
124 self.addwhere("messages.id = patches.msgid")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
125 self.addwhere("patches.mimetype = " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
126 "'application/x-patch'")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
127 return self.makepresent(field, "patches.body")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
128
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
129 if field == "mimetype":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
130 subquery = "((SELECT mtmessages1.pr as pr, " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
131 "mtmessages1.mimetype as mimetype " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
132 "FROM messages as mtmessages1) " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
133 "UNION " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
134 "(SELECT mtmessages2.pr as pr, " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
135 "mtattach2.mimetype as mimetype " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
136 "FROM messages as mtmessages2, " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
137 " attachments as mtattach2 " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
138 "WHERE mtmessages2.id = mtattach2.msgid))"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
139 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
140 self.addjoin(subquery, "mimetypes")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
141 self.addwhere("PRs.id = mimetypes.pr")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
142 return self.makepresent(field, "mimetypes.mimetype")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
143
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
144 # XXX: need view userstrings
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
145 # select (id, username as name) from users
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
146 # union select (id, realname as name) from users
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
147 # (allow searching emails? ugh)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
148 if field == "originator" or field == "submitter":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
149 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
150 self.addjoin("userstrings", "originators")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
151 self.addwhere("PRs.originator = originators.id")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
152 return self.makepresent(field, "originators.name")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
153
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
154 if field == "reporter" or field == "respondent":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
155 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
156 self.addjoin("subscriptions")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
157 self.addjoin("userstrings", "reporters")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
158 self.addwhere("subscriptions.userid = reporters.id")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
159 self.addwhere("subscriptions.reporter")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
160 return self.makepresent(field, "reporters.name")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
161
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
162 if field == "responsible":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
163 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
164 self.addjoin("subscriptions")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
165 self.addjoin("userstrings", "responsibles")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
166 self.addwhere("subscriptions.userid = responsibles.id")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
167 self.addwhere("subscriptions.responsible")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
168 return self.makepresent(field, "responsibles.name")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
169
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
170 if field in hierclasses:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
171 col = field + "_data"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
172 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
173 self.addjoin("hierclass_data", col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
174 self.addwhere("PRs.id = %s.pr" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
175 self.addwhere("%s.scheme = '%s'" % (col, field))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
176 return self.makepresent(field, "%s.value" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
177
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
178 if field in flatclasses:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
179 col = field + "_data"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
180 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
181 self.addjoin("flatclass_data", col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
182 self.addwhere("PRs.id = %s.pr" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
183 self.addwhere("%s.scheme = '%s'" % (col, field))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
184 return self.makepresent(field, "%s.value" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
185
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
186 if field in textclasses:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
187 col = field + "_data"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
188 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
189 self.addjoin("textclass_data", col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
190 self.addwhere("PRs.id = %s.pr" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
191 self.addwhere("%s.scheme = '%s'" % (col, field))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
192 return self.makepresent(field, "%s.value" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
193
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
194 if field in tagclasses:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
195 col = field + "_data"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
196 self.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
197 self.addjoin("tagclass_data", col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
198 self.addwhere("PRs.id = %s.pr" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
199 self.addwhere("%s.scheme = '%s'" % (col, field))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
200 return self.makepresent(field, "%s.value" % col)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
201
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
202 sys.stderr.write("Unknown field %s" % field)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
203 exit(1)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
204 # end getfield
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
205
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
206 # emit sql
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
207 def build(self, sels):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
208 s = ", ".join(sels)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
209 f = ", ".join(self.fromitems)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
210 w = " and ".join(self.whereitems)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
211 q = "SELECT %s\nFROM %s\nWHERE %s\n" % (s, f, w)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
212 if self.order is not None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
213 q = q + "ORDER BY " + self.order + "\n"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
214 return q
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
215 # endif
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
216
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
217 # end class QueryBuilder
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
218
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
219 # XXX we need to add dynamically:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
220 # hierclass_names.name to hierclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
221 # flatclass_names.name to flatclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
222 # textclass_names.name to textclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
223 # tagclass_names.name to tagclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
224
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
225 ############################################################
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
226 # database
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
227
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
228 dblink = None
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
229
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
230 def opendb():
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
231 global dblink
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
232
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
233 host = "localhost"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
234 user = "swallowtail"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
235 database = "swallowtail"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
236 dblink = psycopg2.connect("host=%s user=%s dbname=%s" %
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
237 (host, user, database))
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
238 # end opendb
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
239
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
240 def closedb():
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
241 global dblink
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
242
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
243 dblink.close()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
244 dblink = None
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
245 # end closedb
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
246
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
247 def querydb(qtext, args):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
248 print "Executing this query:"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
249 print qtext
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
250 print "Args are:"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
251 print args
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
252
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
253 cursor = dblink.cursor()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
254 cursor.execute(qtext, args)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
255 result = cursor.fetchall()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
256 cursor.close()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
257 return result
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
258 # end querydb
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
259
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
260 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
261 # query class for searches
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
262 # XXX: obsolete, remove
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
263
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
264 class Query:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
265 __init__(self):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
266 self.selections = []
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
267 self.tables = []
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
268 self.constraints = []
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
269 self.args = []
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
270 prtables = ["PRs"]
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
271 prconstraints = []
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
272
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
273 def select(self, s):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
274 self.selections.append(s)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
275
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
276 def addtable(self, t):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
277 assert(t not in self.tables)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
278 self.tables.append(t)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
279
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
280 def constrain(self, expr):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
281 self.constraints.append(t)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
282
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
283 def internval(self, val):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
284 num = len(self.args)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
285 self.args[num] = val
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
286 return "$%d" % num
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
287
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
288 def textify(self):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
289 s = "SELECT %s\n" % ",".join(self.selections)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
290 f = "FROM %s\n" % ",".join(self.tables)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
291 w = "WHERE %s\n" % " AND ".join(self.constraints)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
292 return s + f + w
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
293 # end class Query
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
294
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
295 def regexp_constraint(q, field, value):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
296 cleanval = q.internval(value)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
297 if not isregexp(value):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
298 return "%s = %s" % (field, cleanval)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
299 else:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
300 # XXX what's the right operator again?
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
301 return "%s ~= %s" % (field, cleanval)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
302 # end regexp_constraint
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
303
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
304 def intrange_constraint(q, field, value):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
305 (lower, upper) = args.number
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
306 if lower is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
307 assert(typeof(lower) == int)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
308 prq.constrain("%s >= %d" % (field, lower))
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
309 if upper is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
310 assert(typeof(upper) == int)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
311 prq.constrain("%s <= %d" % (field, upper))
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
312 # end intrange_constraint
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
313
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
314 def daterange_constraint(q, field, value):
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
315 # XXX
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
316 assert(0)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
317 # end daterange_constraint
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
318
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
319 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
320
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
321 # this is old code that needs to be merged or deleted into the new stuff
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
322 def oldstuff():
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
323
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
324 # If we're doing something other than a search, do it now
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
325 if args.attach is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
326 get_attachment(args.attach)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
327 exit(0)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
328 if args.message is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
329 get_message(args.message)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
330 exit(0)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
331
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
332 if args.prs is not None and len(args.prs) > 0:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
333 show_prs(args.prs)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
334 exit(0)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
335
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
336 #
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
337 # Collect up the search constraints
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
338 #
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
339
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
340 # 1. Constraints on the PRs table
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
341 checkprtable = False
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
342 prq = Query()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
343 prq.select("PRs.id as id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
344 prq.addtable("PRs")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
345 if not args.closed:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
346 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
347 prq.addtable("states")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
348 prq.constrain("PRs.state = states.name")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
349 prq.constrain("states.closed = FALSE")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
350 if args.public:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
351 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
352 prq.constrain("NOT PRs.confidential")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
353 if args.number is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
354 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
355 intrange_constraint(prq, "PRs.id", args.number)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
356 if args.synopsis is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
357 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
358 regexp_constraint(prq, "PRs.synopsis", args.synopsis)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
359 if args.confidential is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
360 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
361 assert(typeof(args.confidential) == bool)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
362 if args.confidential:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
363 prq.constrain("PRs.confidential")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
364 else:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
365 prq.constrain("not PRs.confidential")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
366 if args.state is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
367 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
368 regexp_constraint(prq, "PRs.state", args.state)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
369 if args.locked is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
370 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
371 assert(typeof(args.locked) == bool)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
372 if args.locked:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
373 prq.constrain("PRs.locked")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
374 else:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
375 prq.constrain("not PRs.locked")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
376 if args.arrival_schemaversion is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
377 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
378 intrange_constraint(prq, "PRs.arrival_schemaversion",
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
379 args.arrival_schemaversion)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
380 if args.arrival_date is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
381 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
382 daterange_constraint(prq, "PRs.arrival_date",
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
383 args.arrival_date)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
384 if args.closed_date is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
385 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
386 daterange_constraint(prq, "PRs.closed_date",
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
387 args.closed_date)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
388 if args.last_modified is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
389 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
390 daterange_constraint(prq, "PRs.last_modified",
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
391 args.last_modified)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
392 if args.release is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
393 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
394 regexp_constraint(prq, "PRs.release", args.release)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
395 if args.environment is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
396 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
397 regexp_constraint(prq, "PRs.environment", args.environment)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
398
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
399 if args.originator_name is not None or
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
400 args.originator_email is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
401 prq.addtable("usermail as originator")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
402 prq.constrain("PRs.originator = originator.id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
403 if args.originator_name is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
404 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
405 regexp_constraint(prq, "originator.realname",
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
406 args.originator_name)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
407 if args.originator_email is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
408 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
409 regexp_constraint(prq, "originator.email",
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
410 args.originator_name)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
411 if args.originator_id is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
412 checkprtable = True
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
413 intrange_constraint(prq, "PRs.originator", args.originator_id)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
414
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
415 queries = []
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
416 if checkprtable:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
417 queries.append(prq)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
418
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
419 if args.responsible is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
420 sq = Query()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
421 sq.select("subscriptions.pr as id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
422 sq.addtable("subscriptions")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
423 sq.addtable("users")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
424 sq.constrain("subscriptions.userid = users.id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
425 regexp_constraint(sq, "users.realname", args.responsible)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
426 sq.constrain("subscriptions.responsible")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
427 queries.append(sq)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
428 if args.respondent is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
429 sq = Query()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
430 sq.select("subscriptions.pr as id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
431 sq.addtable("subscriptions")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
432 sq.addtable("users as subscribed")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
433 sq.constrain("subscriptions.userid = users.id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
434 regexp_constraint(sq, "users.realname", args.respondent)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
435 sq.constrain("subscriptions.reporter")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
436 queries.append(sq)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
437 if args.subscribed is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
438 sq = Query()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
439 sq.select("subscriptions.pr as id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
440 sq.addtable("subscriptions")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
441 sq.addtable("users as subscribed")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
442 sq.constrain("subscriptions.userid = users.id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
443 regexp_constraint(sq, "users.realname", args.subscribed)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
444 queries.append(sq)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
445
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
446 if args.messages is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
447 mq = Query()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
448 mq.select("messages.pr as id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
449 mq.addtable("messages")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
450 regexp_constraint(sq, "messages.text", args.messages)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
451 queries.append(mq)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
452
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
453 if args.adminlog is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
454 aq = Query()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
455 aq.select("adminlog.pr as id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
456 aq.addtable("adminlog")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
457 regexp_constraint(sq, "adminlog.change", args.adminlog)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
458 regexp_constraint(sq, "adminlog.comment", args.adminlog)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
459 assert(len(aq.constraints) == 2)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
460 x = "%s OR %s" % (aq.constraints[0], aq.constraints[1])
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
461 aq.constraints = [x]
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
462 queries.append(aq)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
463
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
464 if args.anytext is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
465 choke("--anytext isn't supported yet")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
466
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
467 for scheme in classification_schemes:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
468 if args[scheme] is not None:
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
469 schemetype = classification_schemetypes[scheme]
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
470 tbl = "%sclass_data" % schemetype
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
471 cq = Query()
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
472 cq.select("scheme.pr as id")
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
473 cq.addtable("%s as scheme" % schemetype)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
474 cq.constrain("scheme.scheme = '%s'" % scheme)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
475 regexp_constraint(cq, "scheme.value", args[scheme])
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
476 queries.append(cq)
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
477 # end loop
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
478
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
479 querytexts = [q.textify() for q in queries]
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
480 return "INTERSECT\n".join(querytexts)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
481
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
482 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
483 # printing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
484
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
485 class PrintText:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
486 def __init__(self, output):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
487 self.lines = (output == "RAW" or output == "LIST")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
488 def printheader(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
489 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
490 def printrow(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
491 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
492 print row
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
493 def printfooter(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
494 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
495 # end class PrintText
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
496
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
497 class PrintCsv:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
498 def __init__(self, output):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
499 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
500 def printheader(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
501 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
502 def printrow(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
503 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
504 def printfooter(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
505 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
506 # end class PrintCsv
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
507
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
508 class PrintXml:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
509 def __init__(self, output):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
510 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
511 def printheader(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
512 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
513 def printrow(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
514 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
515 def printfooter(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
516 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
517 # end class PrintXml
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
518
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
519 class PrintJson:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
520 def __init__(self, output):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
521 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
522 def printheader(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
523 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
524 def printrow(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
525 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
526 def printfooter(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
527 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
528 # end class PrintJson
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
529
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
530 class PrintRdf:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
531 def __init__(self, output):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
532 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
533 def printheader(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
534 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
535 def printrow(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
536 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
537 def printfooter(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
538 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
539 # end class PrintRdf
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
540
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
541 class PrintRdflike:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
542 def __init__(self, output):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
543 # nothing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
544 def printheader(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
545 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
546 def printrow(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
547 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
548 def printfooter(self, row):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
549 # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
550 # end class PrintRdflike
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
551
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
552 def print_prs(ids):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
553 if sel.outformat == "TEXT":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
554 mkprinter = PrintText
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
555 elif sel.outformat == "CSV":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
556 mkprinter = PrintCsv
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
557 elif sel.outformat == "XML":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
558 mkprinter = PrintXml
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
559 elif sel.outformat == "JSON":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
560 mkprinter = PrintJson
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
561 elif sel.outformat == "RDF":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
562 mkprinter = PrintRdf
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
563 elif sel.outformat == "RDFLIKE":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
564 mkprinter = PrintRdflike
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
565 else:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
566 assert(False)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
567
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
568 # reset the printer
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
569 printer = mkprinter(sel.output)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
570
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
571 if sel.output == "RAW":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
572 printer.printheader(ids[0])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
573 for id in ids:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
574 printer(id)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
575 printer.printfooter(ids[0])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
576 return
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
577 elif sel.output == "LIST":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
578 # XXX is there a clean way to do this passing the
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
579 # whole list of ids at once?
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
580 query = "SELECT id, synopsis\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
581 "FROM PRs\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
582 "WHERE id = $1"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
583 elif sel.output == "HEADERS":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
584 query = None # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
585 elif sel.output == "META":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
586 query = None # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
587 elif sel.output == "FULL":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
588 query = None # XXX
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
589 else:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
590 assert(False)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
591
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
592 first = True
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
593 for id in ids:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
594 results = querydb(query, [id])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
595 if first:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
596 printer.printheader(results[0])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
597 first = False
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
598 for r in results:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
599 printer.printrow(r)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
600 printer.printfooter(results[0])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
601 # end print_prs
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
602
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
603 # XXX if in public mode we need to check if the PR is public
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
604 def print_message(pr, msgnum):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
605 query = "SELECT users.username AS username,\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
606 " users.realname AS realname,\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
607 " messages.id AS id, parent_id,\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
608 " posttime, mimetype, body\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
609 "FROM messages, users\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
610 "WHERE messages.who = users.id\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
611 " AND messages.pr = $1\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
612 " AND messages.number_in_pr = $2\n"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
613 # Note that while pr is safe, msgnum came from the commandline
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
614 # and may not be.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
615 results = querydb(query, [pr, msgnum])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
616 [result] = results
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
617 (username, realname, id, parent_id, posttime, mimetype, body) = result
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
618 # XXX honor mimetype
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
619 # XXX honor output format (e.g. html)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
620 sys.stdout.write("From swallowtail@%s %s\n" % (organization,posttime))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
621 sys.stdout.write("From: %s (%s)\n" % (username, realname))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
622 sys.stdout.write("References: %s\n" % parent_id)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
623 sys.stdout.write("Date: %s\n" % posttime)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
624 sys.stdout.write("Content-Type: %s\n" % mimetype)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
625 sys.stdout.write("\n")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
626 sys.stdout.write(body)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
627 # end print_message
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
628
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
629 # XXX if in public mode we need to check if the PR is public
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
630 def print_attachment(pr, attachnum):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
631 query = "SELECT a.mimetype as mimetype, a.body as body\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
632 "FROM messages, attachments as a\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
633 "WHERE messages.pr = $1\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
634 " AND messages.id = a.msgid\n" +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
635 " AND a.number_in_pr = $2\n"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
636 # Note that while pr is safe, attachnum came from the
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
637 # commandline and may not be.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
638 results = querydb(query, [pr, msgnum])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
639 [result] = results
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
640 (mimetype, body) = result
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
641 # XXX honor mimetype
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
642 # XXX need an http output mode so we can send the mimetype!
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
643 sys.stdout.write(body)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
644 # end print_attachment
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
645
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
646 ############################################################
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
647 # AST for query
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
648
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
649 class Invocation:
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
650 class Query:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
651 Q_TERM = 1 # XXX unused so far
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
652 Q_SQL = 2
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
653 Q_AND = 3
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
654 Q_OR = 4
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
655 Q_TSTRING = 5
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
656 Q_QSTRING = 6
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
657
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
658 def __init__(self, type):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
659 self.type = type
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
660 def doterm(term):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
661 self = Query(Q_TERM)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
662 self.term = term
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
663 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
664 def dosql(s):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
665 self = Query(Q_SQL)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
666 self.sql = s
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
667 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
668 def doand(qs):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
669 self = Query(Q_AND)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
670 self.args = qs
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
671 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
672 def door(qs):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
673 self = Query(Q_OR)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
674 self.args = qs
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
675 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
676 # query term string
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
677 def dotstring(q):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
678 self = Query(Q_TSTRING)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
679 self.string = q
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
680 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
681 # whole query string
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
682 def doqstring(q):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
683 self = Query(Q_QSTRING)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
684 self.string = q
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
685 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
686 # end class Query
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
687
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
688 class Order:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
689 def __init__(self, field, rev = False):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
690 self.field = field
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
691 self.rev = rev
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
692 def dooldest(ign):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
693 return Order("number")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
694 def donewest(ign):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
695 return Order("number", True)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
696 def dostaleness(ign):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
697 return Order("modified_date", True)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
698 def dofield(field):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
699 return Order(field)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
700 def dorevfield(field):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
701 return Order(field, True)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
702 # end class Order
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
703
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
704 class Search:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
705 def __init__(self, qs, openonly, publiconly, os)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
706 self.queries = qs
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
707 self.openonly = openonly
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
708 self.publiconly = publiconly
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
709 self.orders = os
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
710 # end class Search
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
711
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
712 class Selection:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
713 S_PR = 1
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
714 S_MESSAGE = 2
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
715 S_ATTACHMENT = 3
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
716
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
717 def __init__(self, type):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
718 self.type = type
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
719 def dopr(output, outformat):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
720 self = Selection(S_PR)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
721 self.output = output
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
722 self.outformat = outformat
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
723 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
724 def domessage(arg):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
725 self = Selection(S_MESSAGE)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
726 self.message = arg
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
727 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
728 def doattachment(arg):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
729 self = Selection(S_ATTACHMENT)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
730 self.attachment = arg
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
731 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
732 # end class Selection
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
733
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
734 class Op:
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
735 # operation codes
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
736 OP_FIELDS = 1
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
737 OP_SHOW = 2
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
738 OP_RANGE = 3
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
739 OP_SEARCH = 4
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
740
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
741 def __init__(self, type):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
742 self.type = type
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
743
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
744 def dofields():
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
745 return Op(OP_FIELDS)
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
746 def doshow(field):
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
747 self = Op(OP_SHOW)
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
748 self.field = field
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
749 return self
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
750 def dorange(field):
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
751 self = Op(OP_RANGE)
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
752 self.field = field
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
753 return self
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
754 def dosearch(s, sels):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
755 self = Op(OP_SEARCH)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
756 self.search = s
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
757 self.sels = sels
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
758 return self
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
759 # end class Op
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
760
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
761 def __init__(self, ops):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
762 self.ops = ops
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
763 # end class Invocation
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
764
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
765 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
766 # run (eval the SQL and print the results)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
767
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
768 def
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
769
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
770 def run_sel(sel, ids):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
771 if sel.type == S_PR:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
772 if ids == []:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
773 sys.stderr.write("No PRs matched.\n")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
774 exit(1)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
775
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
776 print_prs(ids)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
777 elif sel.type == S_MESSAGE:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
778 if len(ids) <> 1:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
779 sys.stderr.write("Cannot retrieve messages " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
780 "from multiple PRs.")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
781 exit(1)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
782 print_message(ids[0], sel.message)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
783 elif sel.type == S_ATTACHMENT:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
784 if len(ids) <> 1:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
785 sys.stderr.write("Cannot retrieve attachments " +
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
786 "from multiple PRs.")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
787 exit(1)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
788 print_message(ids[0], sel.attachment)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
789 else:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
790 assert(False)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
791
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
792 def run_op(op):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
793 if op.type == OP_FIELDS:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
794 list_fields()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
795 elif op.type == OP_SHOW:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
796 describe_field(op.field)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
797 elif op.type == OP_RANGE:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
798 print_field_range(op.field)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
799 elif op.type == OP_SEARCH:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
800 sql = op.search
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
801 args = op.args # XXX not there!
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
802 ids = querydb(op.search, args)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
803 for s in op.sels:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
804 run_sel(s, ids)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
805 else:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
806 assert(False)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
807
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
808 def run(ast):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
809 for op in ast.ops:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
810 run_op(op)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
811
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
812 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
813 # compile (convert the AST so the searches are pure SQL)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
814
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
815 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
816 # XXX this doesn't work, we need to keep the interned strings
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
817 # on return from compile_query.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
818 #
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
819
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
820 def compile_query(q):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
821 if q.type == Q_QSTRING:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
822 # XXX should use a split that honors quotes
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
823 terms = q.string.split()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
824 terms = [dotstring(t) for t in terms]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
825 return compile_query(doand(terms))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
826 if q.type == Q_TSTRING:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
827 qb = QueryBuilder()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
828 s = q.string
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
829 if s ~ "^[0-9]+$":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
830 f = qb.getfield("number")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
831 # Note: s is user-supplied but clean to insert directly
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
832 qb.addwhere("%s = %s" % (f, s))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
833 elif s ~ "^[0-9]+-[0-9]+$":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
834 f = qb.getfield("number")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
835 ss = s.split("-")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
836 # Note: ss[] is user-supplied but clean
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
837 qb.addwhere("%s >= %s" % (f, ss[0]))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
838 qb.addwhere("%s <= %s" % (f, ss[1]))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
839 elif s ~ "^[0-9]+-$":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
840 f = qb.getfield("number")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
841 ss = s.split("-")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
842 # Note: ss[] is user-supplied but clean
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
843 qb.addwhere("%s >= %s" % (f, ss[0]))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
844 elif s ~ "^-[0-9]+$":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
845 f = qb.getfield("number")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
846 ss = s.split("-")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
847 # Note: ss[] is user-supplied but clean
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
848 qb.addwhere("%s <= %s" % (f, ss[1]))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
849 elif s ~ "^[^:]+:[^:]+$":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
850 # XXX honor quoted terms
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
851 # XXX = or LIKE?
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
852 ss = s.split(":")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
853 # ss[0] is not clean but if it's crap it won't match
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
854 f = qb.getfield(ss[0])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
855 # ss[1] is not clean, so intern it for safety
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
856 s = qb.intern(ss[1])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
857 qb.addwhere("%s = %s" % (f, s))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
858 elif s ~ "^-[^:]+:[^:]+$"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
859 # XXX honor quoted terms
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
860 # XXX <> or NOT LIKE?
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
861 ss = s.split(":")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
862 # ss[0] is not clean but if it's crap it won't match
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
863 f = qb.getfield(ss[0])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
864 # ss[1] is not clean, so intern it for safety
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
865 s = qb.intern(ss[1])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
866 qb.addwhere("%s <> %s" % (f, s))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
867 elif s ~ "^-":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
868 # XXX <> or NOT LIKE?
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
869 f = qb.getfield("alltext")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
870 # s is not clean, so intern it for safety
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
871 s = qb.intern(s)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
872 qb.addwhere("%s <> %s" % (f, s))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
873 else:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
874 # XXX = or LIKE?
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
875 f = qb.getfield("alltext")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
876 # s is not clean, so intern it for safety
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
877 s = qb.intern(s)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
878 qb.addwhere("%s = %s" % (f, s))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
879
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
880 # XXX also does not handle:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
881 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
882 # field: with no string (supposed to use a default
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
883 # search string)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
884 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
885 # generated search fields that parse dates:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
886 # {arrived,closed,modified,etc.}-{before,after}:date
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
887 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
888 # stale:time
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
889
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
890 return qb.build("PRs.id")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
891 # end Q_TSTRING case
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
892 if q.type == Q_OR:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
893 subqueries = ["(" + compile_query(sq) + ")" for sq in q.args]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
894 return " UNION ".join(subqueries)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
895 if q.type == Q_AND:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
896 subqueries = ["(" + compile_query(sq) + ")" for sq in q.args]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
897 return " INTERSECT ".join(subqueries)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
898 if q.type == Q_SQL:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
899 return q.sql
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
900 assert(False)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
901 # end compile_query
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
902
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
903 def compile_order(qb, o):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
904 str = qb.getfield(o.field)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
905 if o.rev:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
906 str = str + " DESCENDING"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
907 return str
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
908
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
909 def compile_search(s):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
910 qb2 = QueryBuilder()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
911
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
912 # multiple query strings are treated as OR
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
913 query = door(s.queries)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
914 query = compile_query(q)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
915
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
916 if s.openonly:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
917 qb2.addwhere("not %s" % qb.getfield("closed"))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
918 if s.publiconly:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
919 qb2.addwhere("not %s" % qb.getfield("confidential"))
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
920
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
921 orders = [compile_order(qb2, o) for o in s.orders]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
922 order = ", ".join(orders)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
923 if order <> "":
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
924 qb2.setorder(order)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
925
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
926 if qb2.nonempty():
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
927 qb2.addjoin(query, "search")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
928 qb2.addjoin("PRs")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
929 qb2.addwhere("search = PRs.id")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
930 query = qb2.build(["search"])
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
931
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
932 return query
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
933 # end compile_search
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
934
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
935 def compile_op(op):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
936 if op.type == OP_SEARCH:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
937 op.search = compile_search(op.search)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
938 return op
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
939
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
940 def compile(ast):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
941 ast.ops = [compile_op(op) for op in ast.ops]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
942
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
943 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
944 # arg handling
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
945
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
946 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
947 # I swear, all getopt interfaces suck.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
948 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
949 # Provide an argparse action for constructing something out of the
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
950 # argument value and appending that somewhere, since it can't do this
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
951 # on its own.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
952 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
953 # The way you use this:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
954 # p.add_argument("--foo", action = CtorAppend, dest = 'mylist',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
955 # const = ctor)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
956 # where ctor is a function taking the option arg(s) and producing
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
957 # a value to append to mylist.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
958 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
959 # This itself is mangy even for what it is -- it seems like we should
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
960 # be able to pass action=CtorAppend(ctor), but since it has to be a
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
961 # class that doesn't work... unless you make a new class for every
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
962 # ctor you want to use, which seems completely insane.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
963 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
964 class CtorAppend(argparse.Action):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
965 def __call__(self, parser, namespace, values, option_string=None):
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
966 items = getattr(namespace, self.dest)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
967 item = self.const(values)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
968 items.append(item)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
969 setattr(namespace, self.dest, items)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
970
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
971 def getargs():
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
972 p = argparse.ArgumentParser(program_description)
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
973
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
974 # note: -h/--help is built in by default
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
975 p.add_argument("-v", "--version",
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
976 action='version', version=program_version,
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
977 help="Print program version and exit")
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
978
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
979 p.add_argument("--show", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
980 action=CtorAppend, dest='ops',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
981 const=Invocation.Op.doshow
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
982 help="Show description of field")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
983 p.add_argument("--range", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
984 action=CtorAppend, dest='ops',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
985 const=Invocation.Op.dorange
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
986 help="Show range of extant values for field")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
987
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
988 p.add_argument("--search", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
989 action=CtorAppend, dest='queries',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
990 const=Invocation.Query.doqstring,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
991 help="Force string to be read as a search string")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
992 p.add_argument("-s", "--sql", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
993 action=CtorAppend, dest='queries',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
994 const=Invocation.Query.dosql,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
995 help="Supply explicit sql query as search")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
996
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
997 p.add_argument("--open",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
998 action='store_const', dest='openonly', const="True",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
999 help="Exclude closed PRs (default)")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1000 p.add_argument("--closed",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1001 action='store_const', dest='openonly', const="False",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1002 help="Include closed PRs in search")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1003 p.add_argument("--public",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1004 action='store_const', dest='publiconly', const="True",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1005 help="Exclude confidential PRs")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1006 p.add_argument("--privileged",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1007 action='store_const', dest='publiconly', const="False",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1008 help="Allow confidential PRs (default)")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1009
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1010 p.add_argument("--oldest",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1011 action=CtorAppend, dest='orders',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1012 const=Invocation.Order.dooldest,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1013 help="Sort output with oldest PRs first")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1014 p.add_argument("--newest",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1015 action=CtorAppend, dest='orders',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1016 const=Invocation.Order.donewest,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1017 help="Sort output with newest PRs first")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1018 p.add_argument("--staleness",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1019 action=CtorAppend, dest='orders',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1020 const=Invocation.Order.dostaleness,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1021 help="Sort output by time since last modification")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1022 p.add_argument("--orderby", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1023 action=CtorAppend, dest='orders',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1024 const=Invocation.Order.dofield,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1025 help="Sort output by specific field")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1026 p.add_argument("--revorderby", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1027 action=CtorAppend, dest='orders',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1028 const=Invocation.Order.dorevfield,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1029 help="Sort output by specific field, reversed")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1030
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1031 p.add_argument("-m", "--message", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1032 action=CtorAppend, dest='selections',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1033 const=Invocation.Selection.domessage,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1034 help="Print selected message (single PR only)")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1035 p.add_argument("-a", "--attachment", nargs=1,
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1036 action=CtorAppend, dest='selections',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1037 const=Invocation.Selection.doattachment,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1038 help="Print selected attachment (single PR only)")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1039
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1040 p.add_argument("-r", "--raw",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1041 action = 'store_const', const="RAW",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1042 dest = 'output',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1043 help="Print exactly what the database returns")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1044 p.add_argument("-l", "--list",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1045 action = 'store_const', const="LIST",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1046 dest = 'output',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1047 help="Print in list form (default)")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1048 p.add_argument("--headers",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1049 action = 'store_const', const="HEADERS",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1050 dest = 'output',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1051 help="Print header information only")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1052 p.add_argument("--meta",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1053 action = 'store_const', const="META",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1054 dest = 'output',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1055 help="Print all metadata")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1056 p.add_argument("--metadata",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1057 action = 'store_const', const="META",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1058 dest = 'output')
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1059 p.add_argument("-f", "--full",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1060 action = 'store_const', const="FULL",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1061 dest = 'output',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1062 help="Print everything")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1063
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1064 p.add_argument("--text",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1065 action = 'store_const', const="TEXT",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1066 dest = 'outformat',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1067 help="Print in text format (default)")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1068 p.add_argument("--csv",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1069 action = 'store_const', const="CSV",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1070 dest = 'outformat',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1071 help="Print a CSV file")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1072 p.add_argument("--xml",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1073 action = 'store_const', const="XML",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1074 dest = 'outformat',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1075 help="Print in XML")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1076 p.add_argument("--json",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1077 action = 'store_const', const="JSON",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1078 dest = 'outformat',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1079 help="Print in JSON")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1080 p.add_argument("--rdf",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1081 action = 'store_const', const="RDF",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1082 dest = 'outbformat',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1083 help="Print in RDF")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1084 p.add_argument("--rdflike",
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1085 action = 'store_const', const="RDFLIKE",
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1086 dest = 'outformat',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1087 help="Print RDF-like text")
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1088
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1089 p.add_argument("TERM", nargs='*',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1090 action=CtorAppend, dest='queries',
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1091 const=Invocation.Query.doqstring,
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1092 help="Search term")
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1093
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1094 args = p.parse_args()
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1095
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1096 ops = args.ops
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1097 if ops is None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1098 ops = []
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1099 queries = args.queries
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1100 if queries is not None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1101 openonly = args.openonly
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1102 if openonly is None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1103 openonly = True
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1104 publiconly = args.publiconly
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1105 if publiconly is None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1106 publiconly = False
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1107 orders = args.orders
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1108 if orders is None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1109 orders = [Invocation.Order.dooldest(None)]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1110 output = args.output
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1111 if output is None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1112 output = "LIST"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1113 outformat = args.outformat
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1114 if outformat is None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1115 outformat = "TEXT"
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1116 selections = args.selections
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1117 if selections is None:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1118 sel = Invocation.Selection.dopr(output, outformat)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1119 selections = [sel]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1120 search = Search(queries, openonly, publiconly, orders)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1121 op = dosearch(search, selections)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1122 ops.append(op)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1123 # endif
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1124
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1125 return Invocation(ops)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1126 # end getargs
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1127
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1128 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1129 # main
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1130
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1131 todo = getargs()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1132 opendb()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1133 fetch_classifications()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1134 todo = compile(todo)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1135 run(todo)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1136 closedb()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1137 exit(0)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1138