1 from __future__
import print_function
2 import urllib, re, json, socket
5 Python object that enables connection to RR v3 API. 6 Errors (not bugs!) and more welcome fixes/suggestions please 7 post to https://savannah.cern.ch/projects/cmsrunregistry/ 17 Construct exception by providing response object. 19 if isinstance(resp, str):
22 self.
url = resp.geturl()
25 for line
in resp.read().
split(
"\n"):
26 if self.
stack ==
None:
27 m = re.search(
"<pre>(.*)", line)
29 self.
stack = m.group(1)
30 m = re.search(
"^.+\.([^\.]+: .*)$", self.
stack)
36 m = re.search(
"(.*)</pre>", line)
55 url: URL to RRv3 API, i.e. http://localhost:8080/rr_user 56 debug: should debug messages be printed out? Verbose! 59 self.
url = re.sub(
"/*$",
"/api/", url)
65 Print debug information 68 print(
"RRAPI:", end=
' ')
73 def get(self, parts, data = None):
75 General API call (do not use it directly!) 82 callurl = self.
url +
"/".
join(urllib.quote(p)
for p
in parts)
90 sdata = json.dumps(data)
96 self.
dprint(callurl,
"with payload", sdata)
98 resp = urllib.urlopen(callurl, sdata)
100 has_getcode =
"getcode" in dir(resp)
107 if not has_getcode
or resp.getcode() == 200:
109 if re.search(
"json", resp.info().gettype()):
111 return json.loads(rdata)
112 except TypeError
as e:
122 Get version tags (USER app only) 124 if self.
app !=
"user":
125 raise RRApiError(
"Tags call is possible only in user app")
126 return self.
get([
"tags"])
130 Get workspaces (all apps) 132 return self.
get([
"workspaces"])
136 Get tables for workspace (all apps) 138 return self.
get([workspace,
"tables"])
142 Get columns for table for workspace (all apps) 144 return self.
get([workspace, table,
"columns"])
148 Get output templates for table for workspace (all apps) 150 return self.
get([workspace, table,
"templates"])
152 def count(self, workspace, table, filter = None, query = None, tag = None):
154 Get number of rows for table for workspace with filter, query (all apps) or tag (USER app only) 161 req = [ workspace, table ]
163 if self.
app !=
"user":
164 raise RRApiError(
"Tags are possible only in user app")
175 filters[
'filter'] = filter
177 filters[
'query'] = query
179 return int(self.
get(req, filters))
181 def data(self, workspace, table, template, columns = None, filter = None, query = None, order = None, tag = None):
183 Get data for table for workspace with filter, query (all apps) or tag (USER app only) 190 if not isinstance(workspace, str):
191 raise RRApiError(
"workspace parameter must be str")
197 req = [ workspace, table, template ]
199 req.append(
",".
join(columns))
203 req.append(
",".
join(order))
207 if self.
app !=
"user":
208 raise RRApiError(
"Tags are possible only in user app")
219 filters[
'filter'] = filter
221 filters[
'query'] = query
223 return self.
get(req, filters)
227 Get available reports (USER app only) 229 if self.
app !=
"user":
230 raise RRApiError(
"Reports available only in user app")
231 return self.
get([workspace,
"reports"])
235 Get report data (USER app only) 237 if self.
app !=
"user":
238 raise RRApiError(
"Reports available only in user app")
239 return self.
get([workspace, report,
"data"])
242 if __name__ ==
'__main__':
244 print(
"RR API library.")
def get(self, parts, data=None)
def count(self, workspace, table, filter=None, query=None, tag=None)
def data(self, workspace, table, template, columns=None, filter=None, query=None, order=None, tag=None)
def templates(self, workspace, table)
S & print(S &os, JobReport::InputFile const &f)
def __init__(self, url, debug=False)
def reports(self, workspace)
def columns(self, workspace, table)
def report(self, workspace, report)
static std::string join(char **cmd)
def tables(self, workspace)