annotate shelltools/query-pr/query.py @ 54:36d91dfe017f

use valid sql syntax, mostly from yetoo on freenode
author David A. Holland
date Sun, 10 Apr 2022 17:41:24 -0400
parents c0be30249ffe
children 40f64a96481f
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
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
3 import sys
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
4 import argparse
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
5 import psycopg2
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
6
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
7 program_version = "@VERSION@"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
8
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
9 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
10 # settings
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
11
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
12 outfile = sys.stdout
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
13 outmaterial = "headers"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
14 outformat = "text"
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
15
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
16 ############################################################
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
17 # simple dump widget
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
18
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
19 class Dumper:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
20 def __init__(self, f):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
21 self.f = f
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
22 self.indentation = 0
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
23 def indent(self):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
24 self.indentation += 3
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
25 def unindent(self):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
26 self.indentation -= 3
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
27 def write(self, msg):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
28 self.f.write(" " * self.indentation + msg + "\n")
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
29
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
30 ############################################################
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
31 # database field access
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
32
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
33 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
34 # Fields of PRs that we might search are spread across a number of
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
35 # tables and require varying joins to get them. And, because of
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
36 # classication schemes, the set of fields isn't static and we can't
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
37 # just assemble a massive view with one column for each field.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
38 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
39 # The QueryBuilder class knows how to arrange for all known fields to
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
40 # be present.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
41 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
42
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
43 class QueryBuilder:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
44 # these fields are in the PRs table
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
45 prtable_fields = [
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
46 "id", "synopsis", "confidential", "state", "locked",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
47 "timeout_date", "timeout_state",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
48 "arrival_schemaversion", "arrival_date", "modified_date",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
49 "closed_date",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
50 "release", "environment"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
51 ]
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
52
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
53 # these fields are aliases for others
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
54 alias_fields = {
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
55 "number" : "id",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
56 "date" : "arrival_date",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
57 }
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
58
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
59 def __init__(self):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
60 self.present = {}
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
61 self.joined = {}
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
62 self.fromitems = []
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
63 self.whereitems = []
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
64 self.order = None
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
65
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
66 def setorder(self, order):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
67 self.order = order
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
68
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
69 # add to present{} and return the value for convenience (internal)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
70 def makepresent(self, field, name):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
71 self.present[field] = name
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
72 return name
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
73
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
74 # add a join item (once only) (internal)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
75 def addjoin(self, table, as_ = None):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
76 if as_ is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
77 key = table + "-" + as_
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
78 val = table + " AS " + as_
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
79 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
80 key = table
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
81 val = table
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
82 if key not in self.joined:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
83 self.joined[key] = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
84 self.fromitems.append(val)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
85
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
86 # returns a sql expression for the field
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
87 def getfield(self, field):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
88 # already-fetched fields
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
89 if field in self.present:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
90 return self.present[field]
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
91
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
92 # aliases for other fields
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
93 if field in alias_fields:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
94 return self.getfield(alias_fields[field])
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
95
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
96 # simple fields directly in the PRs table
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
97 if field in prtable_fields:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
98 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
99 return self.makepresent(field, "PRs." + field)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
100
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
101 # now it gets more interesting...
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
102 if field == "closed":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
103 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
104 self.addjoin("states")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
105 self.addwhere("PRs.state = states.name")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
106 return self.makepresent(field, "states.closed")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
107
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
108 # XXX let's pick one set of names and use them everywhere
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
109 # (e.g. change "posttime" in the schema to "message_date"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
110 # or something)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
111 if field == "comment_date" or field == "posttime":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
112 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
113 self.addjoin("messages")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
114 self.addwhere("PRs.id = messages.pr")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
115 return self.makepresent(field, "messages.posttime")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
116
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
117 if field == "comment" or field == "message" or field == "post":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
118 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
119 self.addjoin("messages")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
120 self.addwhere("PRs.id = messages.pr")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
121 return self.makepresent(field, "messages.body")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
122
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
123 if field == "attachment":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
124 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
125 self.addjoin("messages")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
126 self.addjoin("attachments")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
127 self.addwhere("PRs.id = messages.pr")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
128 self.addwhere("messages.id = attachments.msgid")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
129 return self.makepresent(field, "attachments.body")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
130
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
131 if field == "patch":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
132 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
133 self.addjoin("messages")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
134 self.addjoin("attachments", "patches")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
135 self.addwhere("PRs.id = messages.pr")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
136 self.addwhere("messages.id = patches.msgid")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
137 self.addwhere("patches.mimetype = " +
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
138 "'application/x-patch'")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
139 return self.makepresent(field, "patches.body")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
140
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
141 if field == "mimetype":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
142 subquery = "((SELECT mtmessages1.pr as pr, " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
143 "mtmessages1.mimetype as mimetype " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
144 "FROM messages as mtmessages1) " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
145 "UNION " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
146 "(SELECT mtmessages2.pr as pr, " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
147 "mtattach2.mimetype as mimetype " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
148 "FROM messages as mtmessages2, " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
149 " attachments as mtattach2 " + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
150 "WHERE mtmessages2.id = mtattach2.msgid))"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
151 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
152 self.addjoin(subquery, "mimetypes")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
153 self.addwhere("PRs.id = mimetypes.pr")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
154 return self.makepresent(field, "mimetypes.mimetype")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
155
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
156 # XXX: need view userstrings
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
157 # select (id, username as name) from users
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
158 # union select (id, realname as name) from users
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
159 # (allow searching emails? ugh)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
160 if field == "originator" or field == "submitter":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
161 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
162 self.addjoin("userstrings", "originators")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
163 self.addwhere("PRs.originator = originators.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
164 return self.makepresent(field, "originators.name")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
165
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
166 if field == "reporter" or field == "respondent":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
167 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
168 self.addjoin("subscriptions")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
169 self.addjoin("userstrings", "reporters")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
170 self.addwhere("subscriptions.userid = reporters.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
171 self.addwhere("subscriptions.reporter")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
172 return self.makepresent(field, "reporters.name")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
173
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
174 if field == "responsible":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
175 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
176 self.addjoin("subscriptions")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
177 self.addjoin("userstrings", "responsibles")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
178 self.addwhere("subscriptions.userid = responsibles.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
179 self.addwhere("subscriptions.responsible")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
180 return self.makepresent(field, "responsibles.name")
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
181
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
182 if field in hierclasses:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
183 col = field + "_data"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
184 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
185 self.addjoin("hierclass_data", col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
186 self.addwhere("PRs.id = %s.pr" % col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
187 self.addwhere("%s.scheme = '%s'" % (col, field))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
188 return self.makepresent(field, "%s.value" % col)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
189
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
190 if field in flatclasses:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
191 col = field + "_data"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
192 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
193 self.addjoin("flatclass_data", col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
194 self.addwhere("PRs.id = %s.pr" % col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
195 self.addwhere("%s.scheme = '%s'" % (col, field))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
196 return self.makepresent(field, "%s.value" % col)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
197
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
198 if field in textclasses:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
199 col = field + "_data"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
200 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
201 self.addjoin("textclass_data", col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
202 self.addwhere("PRs.id = %s.pr" % col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
203 self.addwhere("%s.scheme = '%s'" % (col, field))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
204 return self.makepresent(field, "%s.value" % col)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
205
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
206 if field in tagclasses:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
207 col = field + "_data"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
208 self.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
209 self.addjoin("tagclass_data", col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
210 self.addwhere("PRs.id = %s.pr" % col)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
211 self.addwhere("%s.scheme = '%s'" % (col, field))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
212 return self.makepresent(field, "%s.value" % col)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
213
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
214 sys.stderr.write("Unknown field %s" % field)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
215 exit(1)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
216 # end getfield
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
217
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
218 # emit sql
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
219 def build(self, sels):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
220 s = ", ".join(sels)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
221 f = ", ".join(self.fromitems)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
222 w = " and ".join(self.whereitems)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
223 q = "SELECT %s\nFROM %s\nWHERE %s\n" % (s, f, w)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
224 if self.order is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
225 q = q + "ORDER BY " + self.order + "\n"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
226 return q
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
227 # endif
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
228
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
229 # end class QueryBuilder
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
230
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
231 # XXX we need to add dynamically:
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
232 # hierclass_names.name to hierclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
233 # flatclass_names.name to flatclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
234 # textclass_names.name to textclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
235 # tagclass_names.name to tagclasses[]
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
236
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
237 ############################################################
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
238 # database
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
239
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
240 dblink = None
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
241
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
242 def opendb():
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
243 global dblink
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
244
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
245 host = "localhost"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
246 user = "swallowtail"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
247 database = "swallowtail"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
248 dblink = psycopg2.connect("host=%s user=%s dbname=%s" %
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
249 (host, user, database))
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
250 # end opendb
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
251
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
252 def closedb():
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
253 global dblink
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
254
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
255 dblink.close()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
256 dblink = None
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
257 # end closedb
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
258
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
259 def querydb(qtext, args):
51
ef6d572c4e1e switch to python3 style print()
David A. Holland
parents: 50
diff changeset
260 print("Executing this query:")
ef6d572c4e1e switch to python3 style print()
David A. Holland
parents: 50
diff changeset
261 print(qtext)
ef6d572c4e1e switch to python3 style print()
David A. Holland
parents: 50
diff changeset
262 print("Args are:")
ef6d572c4e1e switch to python3 style print()
David A. Holland
parents: 50
diff changeset
263 print(args)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
264
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
265 cursor = dblink.cursor()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
266 cursor.execute(qtext, args)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
267 result = cursor.fetchall()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
268 cursor.close()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
269 return result
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
270 # end querydb
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
271
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
272 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
273 # query class for searches
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
274 # XXX: obsolete, remove
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
275
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
276 class Query:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
277 def __init__(self):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
278 self.selections = []
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
279 self.tables = []
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
280 self.constraints = []
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
281 self.args = []
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
282 prtables = ["PRs"]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
283 prconstraints = []
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
284
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
285 def select(self, s):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
286 self.selections.append(s)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
287
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
288 def addtable(self, t):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
289 assert(t not in self.tables)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
290 self.tables.append(t)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
291
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
292 def constrain(self, expr):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
293 self.constraints.append(t)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
294
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
295 def internval(self, val):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
296 num = len(self.args)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
297 self.args[num] = val
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
298 return "$%d" % num
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
299
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
300 def textify(self):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
301 s = "SELECT %s\n" % ",".join(self.selections)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
302 f = "FROM %s\n" % ",".join(self.tables)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
303 w = "WHERE %s\n" % " AND ".join(self.constraints)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
304 return s + f + w
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
305 # end class Query
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
306
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
307 def regexp_constraint(q, field, value):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
308 cleanval = q.internval(value)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
309 if not isregexp(value):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
310 return "%s = %s" % (field, cleanval)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
311 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
312 # XXX what's the right operator again?
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
313 return "%s ~= %s" % (field, cleanval)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
314 # end regexp_constraint
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
315
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
316 def intrange_constraint(q, field, value):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
317 (lower, upper) = args.number
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
318 if lower is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
319 assert(typeof(lower) == int)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
320 prq.constrain("%s >= %d" % (field, lower))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
321 if upper is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
322 assert(typeof(upper) == int)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
323 prq.constrain("%s <= %d" % (field, upper))
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
324 # end intrange_constraint
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
325
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
326 def daterange_constraint(q, field, value):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
327 # XXX
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
328 assert(0)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
329 # end daterange_constraint
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
330
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
331 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
332
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
333 # 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
334 def oldstuff():
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
335
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
336 # If we're doing something other than a search, do it now
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
337 if args.attach is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
338 get_attachment(args.attach)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
339 exit(0)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
340 if args.message is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
341 get_message(args.message)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
342 exit(0)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
343
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
344 if args.prs is not None and len(args.prs) > 0:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
345 show_prs(args.prs)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
346 exit(0)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
347
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
348 #
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
349 # Collect up the search constraints
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
350 #
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
351
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
352 # 1. Constraints on the PRs table
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
353 checkprtable = False
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
354 prq = Query()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
355 prq.select("PRs.id as id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
356 prq.addtable("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
357 if not args.closed:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
358 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
359 prq.addtable("states")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
360 prq.constrain("PRs.state = states.name")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
361 prq.constrain("states.closed = FALSE")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
362 if args.public:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
363 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
364 prq.constrain("NOT PRs.confidential")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
365 if args.number is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
366 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
367 intrange_constraint(prq, "PRs.id", args.number)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
368 if args.synopsis is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
369 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
370 regexp_constraint(prq, "PRs.synopsis", args.synopsis)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
371 if args.confidential is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
372 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
373 assert(typeof(args.confidential) == bool)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
374 if args.confidential:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
375 prq.constrain("PRs.confidential")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
376 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
377 prq.constrain("not PRs.confidential")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
378 if args.state is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
379 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
380 regexp_constraint(prq, "PRs.state", args.state)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
381 if args.locked is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
382 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
383 assert(typeof(args.locked) == bool)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
384 if args.locked:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
385 prq.constrain("PRs.locked")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
386 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
387 prq.constrain("not PRs.locked")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
388 if args.arrival_schemaversion is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
389 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
390 intrange_constraint(prq, "PRs.arrival_schemaversion",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
391 args.arrival_schemaversion)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
392 if args.arrival_date is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
393 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
394 daterange_constraint(prq, "PRs.arrival_date",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
395 args.arrival_date)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
396 if args.closed_date is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
397 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
398 daterange_constraint(prq, "PRs.closed_date",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
399 args.closed_date)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
400 if args.last_modified is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
401 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
402 daterange_constraint(prq, "PRs.last_modified",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
403 args.last_modified)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
404 if args.release is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
405 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
406 regexp_constraint(prq, "PRs.release", args.release)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
407 if args.environment is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
408 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
409 regexp_constraint(prq, "PRs.environment", args.environment)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
410
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
411 if args.originator_name is not None or \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
412 args.originator_email is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
413 prq.addtable("usermail as originator")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
414 prq.constrain("PRs.originator = originator.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
415 if args.originator_name is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
416 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
417 regexp_constraint(prq, "originator.realname",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
418 args.originator_name)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
419 if args.originator_email is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
420 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
421 regexp_constraint(prq, "originator.email",
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
422 args.originator_name)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
423 if args.originator_id is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
424 checkprtable = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
425 intrange_constraint(prq, "PRs.originator", args.originator_id)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
426
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
427 queries = []
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
428 if checkprtable:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
429 queries.append(prq)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
430
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
431 if args.responsible is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
432 sq = Query()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
433 sq.select("subscriptions.pr as id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
434 sq.addtable("subscriptions")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
435 sq.addtable("users")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
436 sq.constrain("subscriptions.userid = users.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
437 regexp_constraint(sq, "users.realname", args.responsible)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
438 sq.constrain("subscriptions.responsible")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
439 queries.append(sq)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
440 if args.respondent is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
441 sq = Query()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
442 sq.select("subscriptions.pr as id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
443 sq.addtable("subscriptions")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
444 sq.addtable("users as subscribed")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
445 sq.constrain("subscriptions.userid = users.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
446 regexp_constraint(sq, "users.realname", args.respondent)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
447 sq.constrain("subscriptions.reporter")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
448 queries.append(sq)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
449 if args.subscribed is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
450 sq = Query()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
451 sq.select("subscriptions.pr as id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
452 sq.addtable("subscriptions")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
453 sq.addtable("users as subscribed")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
454 sq.constrain("subscriptions.userid = users.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
455 regexp_constraint(sq, "users.realname", args.subscribed)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
456 queries.append(sq)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
457
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
458 if args.messages is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
459 mq = Query()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
460 mq.select("messages.pr as id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
461 mq.addtable("messages")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
462 regexp_constraint(sq, "messages.text", args.messages)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
463 queries.append(mq)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
464
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
465 if args.adminlog is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
466 aq = Query()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
467 aq.select("adminlog.pr as id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
468 aq.addtable("adminlog")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
469 regexp_constraint(sq, "adminlog.change", args.adminlog)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
470 regexp_constraint(sq, "adminlog.comment", args.adminlog)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
471 assert(len(aq.constraints) == 2)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
472 x = "%s OR %s" % (aq.constraints[0], aq.constraints[1])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
473 aq.constraints = [x]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
474 queries.append(aq)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
475
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
476 if args.anytext is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
477 choke("--anytext isn't supported yet")
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
478
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
479 for scheme in classification_schemes:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
480 if args[scheme] is not None:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
481 schemetype = classification_schemetypes[scheme]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
482 tbl = "%sclass_data" % schemetype
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
483 cq = Query()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
484 cq.select("scheme.pr as id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
485 cq.addtable("%s as scheme" % schemetype)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
486 cq.constrain("scheme.scheme = '%s'" % scheme)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
487 regexp_constraint(cq, "scheme.value", args[scheme])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
488 queries.append(cq)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
489 # end loop
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
490
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
491 querytexts = [q.textify() for q in queries]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
492 return "INTERSECT\n".join(querytexts)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
493
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
494 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
495 # printing
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 PrintText:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
498 def __init__(self, output):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
499 self.lines = (output == "RAW" or output == "LIST")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
500 def printheader(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
501 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
502 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
503 def printrow(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
504 # XXX
51
ef6d572c4e1e switch to python3 style print()
David A. Holland
parents: 50
diff changeset
505 print(row)
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
506 def printfooter(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
507 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
508 pass
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
509 # end class PrintText
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
510
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
511 class PrintCsv:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
512 def __init__(self, output):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
513 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
514 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
515 def printheader(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
516 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
517 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
518 def printrow(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
519 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
520 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
521 def printfooter(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
522 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
523 pass
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
524 # end class PrintCsv
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
525
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
526 class PrintXml:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
527 def __init__(self, output):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
528 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
529 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
530 def printheader(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
531 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
532 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
533 def printrow(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
534 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
535 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
536 def printfooter(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
537 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
538 pass
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
539 # end class PrintXml
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 PrintJson:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
542 def __init__(self, output):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
543 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
544 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
545 def printheader(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
546 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
547 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
548 def printrow(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
549 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
550 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
551 def printfooter(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
552 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
553 pass
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
554 # end class PrintJson
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
555
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
556 class PrintRdf:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
557 def __init__(self, output):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
558 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
559 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
560 def printheader(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
561 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
562 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
563 def printrow(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
564 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
565 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
566 def printfooter(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
567 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
568 pass
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
569 # end class PrintRdf
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
570
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
571 class PrintRdflike:
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
572 def __init__(self, output):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
573 # nothing
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
574 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
575 def printheader(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
576 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
577 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
578 def printrow(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
579 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
580 pass
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
581 def printfooter(self, row):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
582 # XXX
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
583 pass
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
584 # end class PrintRdflike
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
585
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
586 def print_prs(ids):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
587 if sel.outformat == "TEXT":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
588 mkprinter = PrintText
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
589 elif sel.outformat == "CSV":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
590 mkprinter = PrintCsv
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
591 elif sel.outformat == "XML":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
592 mkprinter = PrintXml
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
593 elif sel.outformat == "JSON":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
594 mkprinter = PrintJson
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
595 elif sel.outformat == "RDF":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
596 mkprinter = PrintRdf
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
597 elif sel.outformat == "RDFLIKE":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
598 mkprinter = PrintRdflike
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
599 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
600 assert(False)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
601
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
602 # reset the printer
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
603 printer = mkprinter(sel.output)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
604
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
605 if sel.output == "RAW":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
606 printer.printheader(ids[0])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
607 for id in ids:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
608 printer(id)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
609 printer.printfooter(ids[0])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
610 return
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
611 elif sel.output == "LIST":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
612 # XXX is there a clean way to do this passing the
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
613 # whole list of ids at once?
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
614 query = "SELECT id, synopsis\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
615 "FROM PRs\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
616 "WHERE id = $1"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
617 elif sel.output == "HEADERS":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
618 query = None # XXX
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
619 elif sel.output == "META":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
620 query = None # XXX
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
621 elif sel.output == "FULL":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
622 query = None # XXX
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
623 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
624 assert(False)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
625
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
626 first = True
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
627 for id in ids:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
628 results = querydb(query, [id])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
629 if first:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
630 printer.printheader(results[0])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
631 first = False
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
632 for r in results:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
633 printer.printrow(r)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
634 printer.printfooter(results[0])
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
635 # end print_prs
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
636
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
637 # 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
638 def print_message(pr, msgnum):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
639 query = "SELECT users.username AS username,\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
640 " users.realname AS realname,\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
641 " messages.id AS id, parent_id,\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
642 " posttime, mimetype, body\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
643 "FROM messages, users\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
644 "WHERE messages.who = users.id\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
645 " AND messages.pr = $1\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
646 " AND messages.number_in_pr = $2\n"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
647 # Note that while pr is safe, msgnum came from the commandline
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
648 # and may not be.
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
649 results = querydb(query, [pr, msgnum])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
650 [result] = results
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
651 (username, realname, id, parent_id, posttime, mimetype, body) = result
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
652 # XXX honor mimetype
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
653 # XXX honor output format (e.g. html)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
654 sys.stdout.write("From swallowtail@%s %s\n" % (organization,posttime))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
655 sys.stdout.write("From: %s (%s)\n" % (username, realname))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
656 sys.stdout.write("References: %s\n" % parent_id)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
657 sys.stdout.write("Date: %s\n" % posttime)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
658 sys.stdout.write("Content-Type: %s\n" % mimetype)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
659 sys.stdout.write("\n")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
660 sys.stdout.write(body)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
661 # end print_message
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
662
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
663 # 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
664 def print_attachment(pr, attachnum):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
665 query = "SELECT a.mimetype as mimetype, a.body as body\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
666 "FROM messages, attachments as a\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
667 "WHERE messages.pr = $1\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
668 " AND messages.id = a.msgid\n" + \
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
669 " AND a.number_in_pr = $2\n"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
670 # Note that while pr is safe, attachnum came from the
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
671 # commandline and may not be.
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
672 results = querydb(query, [pr, msgnum])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
673 [result] = results
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
674 (mimetype, body) = result
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
675 # XXX honor mimetype
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
676 # XXX need an http output mode so we can send the mimetype!
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
677 sys.stdout.write(body)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
678 # end print_attachment
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
679
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
680 ############################################################
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
681 # AST for input query
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
682
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
683 class Invocation:
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
684 Q_TERM = 1
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
685 Q_SQL = 2
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
686 class Query:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
687 def __init__(self, type):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
688 self.type = type
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
689 def dump(self, d):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
690 if self.type == Invocation.Q_TERM:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
691 d.write("query.term({})".format(self.term))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
692 else:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
693 d.write("query.sql({})".format(self.sql))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
694 def mkterm(term):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
695 self = Invocation.Query(Invocation.Q_TERM)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
696 self.term = term
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
697 return self
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
698 def mksql(s):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
699 self = Invocation.Query(Invocation.Q_SQL)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
700 self.sql = s
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
701 return self
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
702
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
703 class Order:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
704 def __init__(self, field, rev = False):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
705 self.field = field
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
706 self.rev = rev
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
707 def dump(self, d):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
708 d.write("order({}, {})".format(self.field, self.rev))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
709 def mkoldest():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
710 return Invocation.Order("number")
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
711 def mknewest():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
712 return Invocation.Order("number", True)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
713 def mkstaleness():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
714 return Invocation.Order("modified_date", True)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
715 def mkfield(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
716 return Invocation.Order(field)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
717 def mkrevfield(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
718 return Invocation.Order(field, True)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
719
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
720 class Search:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
721 def __init__(self, qs, openonly, publiconly, os):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
722 self.queries = qs
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
723 self.openonly = openonly
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
724 self.publiconly = publiconly
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
725 self.orders = os
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
726 def dump(self, d):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
727 d.write("search({}, {})".format(
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
728 "open" if self.openonly else "closed",
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
729 "public" if self.publiconly else "privileged"))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
730 d.indent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
731 d.write("queries")
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
732 d.indent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
733 for query in self.queries:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
734 query.dump(d)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
735 d.unindent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
736 d.write("orders")
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
737 d.indent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
738 for order in self.orders:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
739 order.dump(d)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
740 d.unindent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
741 d.unindent()
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
742
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
743 S_PR = 1
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
744 S_MESSAGE = 2
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
745 S_ATTACHMENT = 3
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
746 class Selection:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
747 def __init__(self, type):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
748 self.type = type
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
749 def dump(self, d):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
750 if self.type == Invocation.S_PR:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
751 d.write("selection.pr({}, {})".format(
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
752 self.output, self.outformat))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
753 elif self.type == Invocation.S_MESSAGE:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
754 d.write("selection.message({})".format(
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
755 self.message))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
756 else:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
757 d.write("selection.attachment({})".format(
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
758 self.attachment))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
759 def mkpr(output, outformat):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
760 self = Invocation.Selection(Invocation.S_PR)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
761 self.output = output
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
762 self.outformat = outformat
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
763 return self
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
764 def mkmessage(arg):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
765 self = Invocation.Selection(Invocation.S_MESSAGE)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
766 self.message = arg
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
767 return self
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
768 def mkattachment(arg):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
769 self = Invocation.Selection(Invocation.S_ATTACHMENT)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
770 self.attachment = arg
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
771 return self
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
772
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
773 OP_FIELDS = 1
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
774 OP_SHOW = 2
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
775 OP_RANGE = 3
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
776 OP_SEARCH = 4
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
777 class Op:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
778 def __init__(self, type):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
779 self.type = type
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
780 def dump(self, d):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
781 if self.type == Invocation.OP_FIELDS:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
782 d.write("op.fields")
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
783 elif self.type == Invocation.OP_SHOW:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
784 d.write("op.show({})".format(self.field))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
785 elif self.type == Invocation.OP_RANGE:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
786 d.write("op.range({})".format(self.field))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
787 else:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
788 d.write("op.search:")
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
789 d.indent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
790 self.search.dump(d)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
791 for sel in self.sels:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
792 sel.dump(d)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
793 d.unindent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
794 def mkfields():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
795 return Invocation.Op(Invocation.OP_FIELDS)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
796 def mkshow(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
797 self = Invocation.Op(Invocation.OP_SHOW)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
798 self.field = field
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
799 return self
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
800 def mkrange(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
801 self = Invocation.Op(Invocation.OP_RANGE)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
802 self.field = field
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
803 return self
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
804 def mksearch(s, sels):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
805 self = Invocation.Op(Invocation.OP_SEARCH)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
806 self.search = s
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
807 self.sels = sels
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
808 return self
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
809
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
810 def __init__(self, ops):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
811 self.ops = ops
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
812 def dump(self, d):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
813 d.write("invocation: {} ops".format(len(self.ops)))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
814 d.indent()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
815 for op in self.ops:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
816 op.dump(d)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
817 d.unindent()
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
818 # end class Invocation
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
819
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
820 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
821 # run (eval the SQL and print the results)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
822
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
823 def run_sel(sel, ids):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
824 if sel.type == S_PR:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
825 if ids == []:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
826 sys.stderr.write("No PRs matched.\n")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
827 exit(1)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
828
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
829 print_prs(ids)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
830 elif sel.type == S_MESSAGE:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
831 if len(ids) != 1:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
832 sys.stderr.write("Cannot retrieve messages " +
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
833 "from multiple PRs.")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
834 exit(1)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
835 print_message(ids[0], sel.message)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
836 elif sel.type == S_ATTACHMENT:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
837 if len(ids) != 1:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
838 sys.stderr.write("Cannot retrieve attachments " +
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
839 "from multiple PRs.")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
840 exit(1)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
841 print_message(ids[0], sel.attachment)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
842 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
843 assert(False)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
844
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
845 def run_op(op):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
846 if op.type == OP_FIELDS:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
847 list_fields()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
848 elif op.type == OP_SHOW:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
849 describe_field(op.field)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
850 elif op.type == OP_RANGE:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
851 print_field_range(op.field)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
852 elif op.type == OP_SEARCH:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
853 sql = op.search
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
854 args = op.args # XXX not there!
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
855 ids = querydb(op.search, args)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
856 for s in op.sels:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
857 run_sel(s, ids)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
858 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
859 assert(False)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
860
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
861 def run(ast):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
862 for op in ast.ops:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
863 run_op(op)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
864
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
865 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
866 # compile (convert the AST so the searches are pure SQL)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
867
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
868 #
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
869 # XXX this doesn't work, we need to keep the interned strings
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
870 # on return from compile_query.
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
871 #
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
872
48
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
873 def matches(s, rx):
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
874 # XXX
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
875 return True
3d5adf5a59d0 Fix python2.7 syntax errors.
David A. Holland
parents: 47
diff changeset
876
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
877 def compile_query(q):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
878 if q.type == Q_QSTRING:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
879 # XXX should use a split that honors quotes
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
880 terms = q.string.split()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
881 terms = [dotstring(t) for t in terms]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
882 return compile_query(doand(terms))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
883 if q.type == Q_TSTRING:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
884 qb = QueryBuilder()
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
885 s = q.string
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
886 if matches(s, "^[0-9]+$"):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
887 f = qb.getfield("number")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
888 # Note: s is user-supplied but clean to insert directly
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
889 qb.addwhere("%s = %s" % (f, s))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
890 elif matches(s, "^[0-9]+-[0-9]+$"):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
891 f = qb.getfield("number")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
892 ss = s.split("-")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
893 # Note: ss[] is user-supplied but clean
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
894 qb.addwhere("%s >= %s" % (f, ss[0]))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
895 qb.addwhere("%s <= %s" % (f, ss[1]))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
896 elif matches(s, "^[0-9]+-$"):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
897 f = qb.getfield("number")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
898 ss = s.split("-")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
899 # Note: ss[] is user-supplied but clean
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
900 qb.addwhere("%s >= %s" % (f, ss[0]))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
901 elif matches(s, "^-[0-9]+$"):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
902 f = qb.getfield("number")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
903 ss = s.split("-")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
904 # Note: ss[] is user-supplied but clean
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
905 qb.addwhere("%s <= %s" % (f, ss[1]))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
906 elif matches(s, "^[^:]+:[^:]+$"):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
907 # XXX honor quoted terms
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
908 # XXX = or LIKE?
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
909 ss = s.split(":")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
910 # ss[0] is not clean but if it's crap it won't match
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
911 f = qb.getfield(ss[0])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
912 # ss[1] is not clean, so intern it for safety
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
913 s = qb.intern(ss[1])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
914 qb.addwhere("%s = %s" % (f, s))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
915 elif matches(s, "^-[^:]+:[^:]+$"):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
916 # XXX honor quoted terms
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
917 # XXX <> or NOT LIKE?
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
918 ss = s.split(":")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
919 # ss[0] is not clean but if it's crap it won't match
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
920 f = qb.getfield(ss[0])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
921 # ss[1] is not clean, so intern it for safety
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
922 s = qb.intern(ss[1])
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
923 qb.addwhere("%s <> %s" % (f, s))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
924 elif matches(s, "^-"):
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
925 # XXX <> or NOT LIKE?
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
926 f = qb.getfield("alltext")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
927 # s is not clean, so intern it for safety
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
928 s = qb.intern(s)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
929 qb.addwhere("%s <> %s" % (f, s))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
930 else:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
931 # XXX = or LIKE?
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
932 f = qb.getfield("alltext")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
933 # s is not clean, so intern it for safety
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
934 s = qb.intern(s)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
935 qb.addwhere("%s = %s" % (f, s))
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
936
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
937 # XXX also does not handle:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
938 #
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
939 # field: with no string (supposed to use a default
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
940 # search string)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
941 #
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
942 # generated search fields that parse dates:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
943 # {arrived,closed,modified,etc.}-{before,after}:date
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
944 #
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
945 # stale:time
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
946
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
947 return qb.build("PRs.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
948 # end Q_TSTRING case
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
949 if q.type == Q_OR:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
950 subqueries = ["(" + compile_query(sq) + ")" for sq in q.args]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
951 return " UNION ".join(subqueries)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
952 if q.type == Q_AND:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
953 subqueries = ["(" + compile_query(sq) + ")" for sq in q.args]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
954 return " INTERSECT ".join(subqueries)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
955 if q.type == Q_SQL:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
956 return q.sql
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
957 assert(False)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
958 # end compile_query
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
959
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
960 def compile_order(qb, o):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
961 str = qb.getfield(o.field)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
962 if o.rev:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
963 str = str + " DESCENDING"
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
964 return str
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
965
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
966 def compile_search(s):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
967 qb2 = QueryBuilder()
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
968
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
969 # multiple query strings are treated as OR
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
970 query = door(s.queries)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
971 query = compile_query(q)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
972
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
973 if s.openonly:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
974 qb2.addwhere("not %s" % qb.getfield("closed"))
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
975 if s.publiconly:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
976 qb2.addwhere("not %s" % qb.getfield("confidential"))
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
977
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
978 orders = [compile_order(qb2, o) for o in s.orders]
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
979 order = ", ".join(orders)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
980 if order != "":
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
981 qb2.setorder(order)
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
982
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
983 if qb2.nonempty():
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
984 qb2.addjoin(query, "search")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
985 qb2.addjoin("PRs")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
986 qb2.addwhere("search = PRs.id")
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
987 query = qb2.build(["search"])
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
988
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
989 return query
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
990 # end compile_search
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
991
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
992 def compile_op(op):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
993 if op.type == OP_SEARCH:
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
994 op.search = compile_search(op.search)
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
995 return op
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
996
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
997 def compile(ast):
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
998 ast.ops = [compile_op(op) for op in ast.ops]
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
999
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1000 ############################################################
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1001 # arg handling
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1002
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1003 #
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1004 # I swear, all getopt interfaces suck. You have to write your own to
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1005 # not go mad.
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1006 #
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1007
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1008 # Usage.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1009 def usage():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1010 sys.stderr.write("""
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1011 query-pr: search for and retrieve problem reports
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1012 usage: query-pr [options] [searchterms] Query the database.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1013 query-pr [options] --sql QUERY Execute QUERY as the search.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1014 query-pr [options] -s QUERY Same as --sql.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1015 query-pr --fields List database fields.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1016 query-pr --show FIELD Print info about database field.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1017 query-pr --range FIELD Print extant range for database field.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1018 query-pr --help / -h Print this message.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1019 query-pr --version / -v Print version and exit.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1020 options:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1021 --search-string STRING Forcibly treat STRING as a search term.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1022 --message NUM / -m NUM Print a message by its ID number.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1023 --attachment NUM / -a NUM Print an attachment by its ID number.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1024 --paranoid Deny unsafe settings.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1025 filter options:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1026 --open Exclude closed PRs. (default)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1027 --closed Include closed PRs.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1028 --public Exclude confidential PRs.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1029 --privileged Include confidential PRs. (default)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1030 sort options:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1031 --oldest Sort with oldest PRs first. (default)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1032 --newest Sort with newest PRs first.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1033 --staleness Sort by last modification time.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1034 --orderby FIELD Sort by specific field.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1035 output options:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1036 --raw / -r Print raw SQL output.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1037 --list / -l Print in list form.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1038 --headers Print headers only.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1039 --meta / --metadata Print all metadata.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1040 --full / -f Print entire PR.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1041 --text Print text. (default)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1042 --csv Print CSV.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1043 --xml Print XML.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1044 --json Print JSON.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1045 --rdf Print RDF.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1046 --rdflike Print RDF-like text.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1047 search terms:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1048 NUM Single PR by number.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1049 NUM-[NUM] Range of PRs by number.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1050 TEXT Search string
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1051 FIELD:TEXT Search string for a particular field.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1052 FIELD: Use the field's default search string.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1053 derived fields:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1054 arrived-before:DATE Arrival date before DATE.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1055 arrived-after:DATE Arrival date after DATE.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1056 closed-before:DATE Close date before DATE.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1057 closed-after:DATE Close date after DATE, or none.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1058 last-modified-before:DATE Last modified before DATE.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1059 last-modified-after:DATE Last modified after DATE.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1060 stale:TIME Last modified at least TIME ago.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1061 Explicit SQL queries should return lists of PR numbers (only).
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1062 """)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1063
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1064 # Widget to hold argv and allow peeling args off one at a time.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1065 class ArgHolder:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1066 def __init__(self, argv):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1067 self.argc = len(argv)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1068 self.argv = argv
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1069 self.pos = 1
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1070 def next(self):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1071 if self.pos >= self.argc:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1072 return None
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1073 ret = self.argv[self.pos]
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1074 self.pos += 1
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1075 return ret
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1076 def getarg(self, opt):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1077 ret = self.next()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1078 if ret is None:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1079 msg = "Option {} requires an argument\n".format(opt)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1080 sys.stderr.write(msg)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1081 exit(1)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1082 return ret
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1083
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1084 # Read the argument list and convert it into an Invocation.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1085 def getargs(argv):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1086 # Results
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1087 ops = []
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1088 orders = []
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1089 queries = []
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1090 selections = []
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1091 output = "LIST"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1092 outformat = "TEXT"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1093 openonly = True
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1094 publiconly = False
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1095 paranoid = False
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1096 nomoreoptions = False
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1097
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1098 #
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1099 # Functions for the options
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1100 #
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1101
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1102 def do_paranoid():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1103 nonlocal paranoid, publiconly
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1104 paranoid = True
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1105 publiconly = True
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1106
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1107 def do_help():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1108 usage()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1109 exit(0)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1110 def do_version():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1111 msg = "query-pr {}\n".format(program_version)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1112 sys.stdout.write(msg)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1113 exit(0)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1114 def do_fields():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1115 ops.append(Invocation.mkfields())
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1116 def do_show(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1117 ops.append(Invocation.mkshow(field))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1118 def do_range(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1119 ops.append(Invocation.mkrange(field))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1120
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1121 def do_search(term):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1122 queries.append(Invocation.mkterm(term))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1123 def do_sql(text):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1124 assert(not paranoid)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1125 queries.append(Invocation.mksql(text))
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1126
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1127 def do_open():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1128 nonlocal openonly
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1129 openonly = True
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1130 def do_closed():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1131 nonlocal openonly
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1132 openonly = False
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1133 def do_public():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1134 nonlocal publiconly
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1135 publiconly = True
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1136 def do_privileged():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1137 nonlocal publiconly
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1138 assert(not paranoid)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1139 publiconly = False
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1140
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1141 def do_oldest():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1142 orders.append(Invocation.mkoldest())
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1143 def do_newest():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1144 orders.append(Invocation.mknewest())
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1145 def do_staleness():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1146 orders.append(Invocation.mkstaleness())
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1147 def do_orderby(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1148 orders.append(Invocation.mkfield(field))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1149 def do_revorderby(field):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1150 orders.append(Invocation.mkrevfield(field))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1151
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1152 def do_message(n):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1153 selections.append(Invocation.mkmessage(n))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1154 def do_attachment(n):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1155 selections.append(Invocation.mkattachment(n))
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1156
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1157 def do_raw():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1158 nonlocal output
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1159 output = "RAW"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1160 def do_list():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1161 nonlocal output
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1162 output = "LIST"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1163 def do_headers():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1164 nonlocal output
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1165 output = "HEADERS"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1166 def do_meta():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1167 nonlocal output
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1168 output = "META"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1169 def do_full():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1170 nonlocal output
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1171 output = "FULL"
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1172
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1173 def do_text():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1174 nonlocal outformat
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1175 outformat = "TEXT"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1176 def do_csv():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1177 nonlocal outformat
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1178 outformat = "CSV"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1179 def do_xml():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1180 nonlocal outformat
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1181 outformat = "XML"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1182 def do_json():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1183 nonlocal outformat
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1184 outformat = "JSON"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1185 def do_rdf():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1186 nonlocal outformat
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1187 outformat = "RDF"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1188 def do_rdflike():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1189 nonlocal outformat
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1190 outformat = "RDFLIKE"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1191
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1192 def do_unknown(opt):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1193 sys.stderr.write("Unknown option {}\n".format(opt))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1194 exit(1)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1195
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1196 args = ArgHolder(argv)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1197 while True:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1198 arg = args.next()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1199 if arg is None:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1200 break
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1201
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1202 if nomoreoptions or arg[0] != "-":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1203 do_search(arg)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1204 elif arg == "--":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1205 nomoreoptions = True
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1206 # Long options
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1207 elif arg == "--attachment":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1208 do_attachment(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1209 elif arg.startswith("--attachment="):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1210 do_message(arg[13:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1211 elif arg == "--closed":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1212 do_closed()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1213 elif arg == "--csv":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1214 do_csv()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1215 elif arg == "--fields":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1216 do_fields()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1217 elif arg == "--full":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1218 do_full()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1219 elif arg == "--headers":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1220 do_headers()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1221 elif arg == "--help":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1222 do_help()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1223 elif arg == "--json":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1224 do_json()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1225 elif arg == "--list":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1226 do_list()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1227 elif arg == "--message":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1228 do_message(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1229 elif arg.startswith("--message="):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1230 do_message(arg[10:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1231 elif arg == "--meta":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1232 do_meta()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1233 elif arg == "--metadata":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1234 do_meta()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1235 elif arg == "--newest":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1236 do_newest()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1237 elif arg == "--oldest":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1238 do_oldest()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1239 elif arg == "--orderby":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1240 do_orderby(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1241 elif arg.startswith("--orderby="):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1242 do_orderby(arg[10:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1243 elif arg == "--open":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1244 do_open()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1245 elif arg == "--paranoid":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1246 do_paranoid()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1247 elif arg == "--public":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1248 do_public()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1249 elif arg == "--privileged" and not paranoid:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1250 do_privileged()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1251 elif arg == "--range":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1252 do_range(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1253 elif arg.startswith("--range="):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1254 do_range(arg[8:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1255 elif arg == "--raw":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1256 do_raw()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1257 elif arg == "--rdf":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1258 do_rdf()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1259 elif arg == "--rdflike":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1260 do_rdflike()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1261 elif arg == "--revorderby":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1262 do_revorderby(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1263 elif arg.startswith("--revorderby="):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1264 do_revorderby(arg[13:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1265 elif arg == "--search":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1266 do_search(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1267 elif arg.startswith("--search="):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1268 do_search(arg[9:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1269 elif arg == "--show":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1270 do_show(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1271 elif arg.startswith("--show="):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1272 do_show(arg[7:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1273 elif arg == "--sql" and not paranoid:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1274 do_sql(args.getarg(arg))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1275 elif arg.startswith("--sql=") and not paranoid:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1276 do_sql(arg[7:])
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1277 elif arg == "--staleness":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1278 do_staleness()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1279 elif arg == "--text":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1280 do_text()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1281 elif arg == "--version":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1282 do_version()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1283 elif arg == "--xml":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1284 do_xml()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1285 elif arg.startswith("--"):
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1286 do_unknown(arg)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1287 else:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1288 # short options
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1289 i = 1
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1290 n = len(arg)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1291 while i < n:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1292 opt = arg[i]
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1293 i += 1
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1294 def getarg():
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1295 nonlocal i
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1296 if i < n:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1297 ret = arg[i:]
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1298 else:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1299 ret = args.getarg("-" + opt)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1300 i = n
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1301 return ret
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1302
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1303 if opt == "a":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1304 do_attachment(getarg())
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1305 elif opt == "f":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1306 do_full()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1307 elif opt == "h":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1308 do_help()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1309 elif opt == "l":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1310 do_list()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1311 elif opt == "m":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1312 do_message(getarg())
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1313 elif opt == "s" and not paranoid:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1314 do_sql(getarg())
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1315 elif opt == "r":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1316 do_raw()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1317 elif opt == "v":
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1318 do_version()
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1319 else:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1320 do_unknown("-" + opt)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1321
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1322 # Now convert what we got to a single thing.
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1323 if queries != []:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1324 if orders == []:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1325 orders = [Invocation.mkoldest()]
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1326 if selections == []:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1327 selections = [Invocation.mkpr(output, outformat)]
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1328 search = Invocation.Search(queries, openonly, publiconly, orders)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1329 ops.append(Invocation.mksearch(search, selections))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1330 else:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1331 if orders != []:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1332 msg = "No queries given for requested orderings\n"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1333 sys.stderr.write(msg)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1334 exit(1)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1335 if selections != []:
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1336 msg = "No queries given for requested selections\n"
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1337 sys.stderr.write(msg)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1338 exit(1)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1339
50
bb0ece71355e Untabify.
David A. Holland
parents: 49
diff changeset
1340 return Invocation(ops)
33
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1341 # end getargs
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1342
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1343 ############################################################
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1344 # main
298c8a7f5181 begin hacking
David A. Holland
parents: 7
diff changeset
1345
52
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1346 todo = getargs(sys.argv)
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1347 #todo.dump(Dumper(sys.stdout))
c0be30249ffe Fix the argument handling.
David A. Holland
parents: 51
diff changeset
1348
47
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1349 opendb()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1350 fetch_classifications()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1351 todo = compile(todo)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1352 run(todo)
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1353 closedb()
bcd1d06838fd more better stuff
David A. Holland
parents: 46
diff changeset
1354 exit(0)
46
73e6dac29391 new stuff (checkpoint when moved between machines)
David A. Holland
parents: 33
diff changeset
1355