1 import urllib, re, json, socket
4 Python object that enables connection to RR v3 API.
5 Errors (not bugs!) and more welcome fixes/suggestions please
6 post to https://savannah.cern.ch/projects/cmsrunregistry/
16 Construct exception by providing response object.
21 self.
url = resp.geturl()
24 for line
in resp.read().
split(
"\n"):
25 if self.
stack ==
None:
26 m = re.search(
"<pre>(.*)", line)
28 self.
stack = m.group(1)
29 m = re.search(
"^.+\.([^\.]+: .*)$", self.
stack)
35 m = re.search(
"(.*)</pre>", line)
54 url: URL to RRv3 API, i.e. http://localhost:8080/rr_user
55 debug: should debug messages be printed out? Verbose!
58 self.
url = re.sub(
"/*$",
"/api/", url)
64 Print debug information
72 def get(self, parts, data = None):
74 General API call (do not use it directly!)
81 callurl = self.
url +
"/".
join(urllib.quote(p)
for p
in parts)
89 sdata = json.dumps(data)
95 self.
dprint(callurl,
"with payload", sdata)
97 resp = urllib.urlopen(callurl, sdata)
99 has_getcode =
"getcode" in dir(resp)
102 self.
dprint(
"Response", resp.getcode(),
" ".
join(str(resp.info()).
split(
"\r\n")))
106 if not has_getcode
or resp.getcode() == 200:
108 if re.search(
"json", resp.info().gettype()):
110 return json.loads(rdata)
111 except TypeError
as e:
121 Get version tags (USER app only)
123 if self.
app !=
"user":
124 raise RRApiError(
"Tags call is possible only in user app")
125 return self.
get([
"tags"])
129 Get workspaces (all apps)
131 return self.
get([
"workspaces"])
135 Get tables for workspace (all apps)
137 return self.
get([workspace,
"tables"])
141 Get columns for table for workspace (all apps)
143 return self.
get([workspace, table,
"columns"])
147 Get output templates for table for workspace (all apps)
149 return self.
get([workspace, table,
"templates"])
151 def count(self, workspace, table, filter = None, query = None, tag = None):
153 Get number of rows for table for workspace with filter, query (all apps) or tag (USER app only)
160 req = [ workspace, table ]
162 if self.
app !=
"user":
163 raise RRApiError(
"Tags are possible only in user app")
174 filters[
'filter'] = filter
176 filters[
'query'] = query
178 return int(self.
get(req, filters))
180 def data(self, workspace, table, template, columns = None, filter = None, query = None, order = None, tag = None):
182 Get data for table for workspace with filter, query (all apps) or tag (USER app only)
189 if type(workspace) != str:
190 raise RRApiError(
"workspace parameter must be str")
196 req = [ workspace, table, template ]
198 req.append(
",".
join(columns))
202 req.append(
",".
join(order))
206 if self.
app !=
"user":
207 raise RRApiError(
"Tags are possible only in user app")
218 filters[
'filter'] = filter
220 filters[
'query'] = query
222 return self.
get(req, filters)
226 Get available reports (USER app only)
228 if self.
app !=
"user":
229 raise RRApiError(
"Reports available only in user app")
230 return self.
get([workspace,
"reports"])
234 Get report data (USER app only)
236 if self.
app !=
"user":
237 raise RRApiError(
"Reports available only in user app")
238 return self.
get([workspace, report,
"data"])
241 if __name__ ==
'__main__':
243 print "RR API library."
static std::string join(char **cmd)