Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
import urllib, re, json, socket

"""
Python object that enables connection to RR v3 API.
Errors (not bugs!) and more welcome fixes/suggestions please 
post to https://savannah.cern.ch/projects/cmsrunregistry/
"""

class RRApiError(Exception):
    """
    API Exception class
    """

    def __init__(self, resp):
        """
        Construct exception by providing response object.
        """
        if isinstance(resp, str):
            self.message = resp
        else:
            self.url = resp.geturl()
            self.code = resp.getcode()
            self.stack = None
            for line in resp.read().split("\n"):
                if self.stack == None:
                    m = re.search("<pre>(.*)", line)
                    if m != None:
                        self.stack = m.group(1)
                        m = re.search("^.+\\.([^\\.]+: .*)$", self.stack)
                        if m != None:
                            self.message = m.group(1)
                        else:
                            self.message = line
                else:
                    m = re.search("(.*)</pre>", line)
                    if m != None:
                        self.stack = self.stack + "\n" + m.group(1)
                        break
                    else:
                        self.stack = self.stack + "\n" + line

    def __str__(self):
        """ Get message """
        return self.message

class RRApi:
    """
    RR API object
    """

    def __init__(self, url, debug = False):
        """
        Construct API object.
        url: URL to RRv3 API, i.e. http://localhost:8080/rr_user
        debug: should debug messages be printed out? Verbose!
        """
        self.debug = debug
        self.url = re.sub("/*$", "/api/", url)
        self.app = self.get(["app"])
        self.dprint("app = ", self.app)

    def dprint(self, *args):
        """
        Print debug information
        """
        if self.debug: 
            print("RRAPI:", end=' ')
            for arg in args:
                print(arg, end=' ') 
            print()

    def get(self, parts, data = None):
        """
        General API call (do not use it directly!)
        """

        #
        # Constructing request path
        #

        callurl = self.url + "/".join(urllib.quote(p) for p in parts)

        #
        # Constructing data payload
        #

        sdata = None
        if data != None:
            sdata = json.dumps(data)

        #
        # Do the query and respond
        #

        self.dprint(callurl, "with payload", sdata)

        resp = urllib.urlopen(callurl, sdata)

        has_getcode = "getcode" in dir(resp)
        if self.debug: 
            if has_getcode:
                self.dprint("Response", resp.getcode(), " ".join(str(resp.info()).split("\r\n")))
            else:
                self.dprint("Response", " ".join(str(resp.info()).split("\r\n")))

        if not has_getcode or resp.getcode() == 200:
            rdata = resp.read()
            if re.search("json", resp.info().gettype()):
                try:
                    return json.loads(rdata)
                except TypeError as e:
                    self.dprint(e)
                    return rdata
            else:
                return rdata
        else:
            raise RRApiError(resp)

    def tags(self):
        """
        Get version tags (USER app only)
        """
        if self.app != "user":
            raise RRApiError("Tags call is possible only in user app")
        return self.get(["tags"])

    def workspaces(self):
        """
        Get workspaces (all apps)
        """
        return self.get(["workspaces"])

    def tables(self, workspace):
        """
        Get tables for workspace (all apps)
        """
        return self.get([workspace, "tables"])

    def columns(self, workspace, table):
        """
        Get columns for table for workspace (all apps)
        """
        return self.get([workspace, table, "columns"])

    def templates(self, workspace, table):
        """
        Get output templates for table for workspace (all apps)
        """
        return self.get([workspace, table, "templates"])

    def count(self, workspace, table, filter = None, query = None, tag = None):
        """
        Get number of rows for table for workspace with filter, query (all apps) or tag (USER app only)
        """

        #
        # Constructing request path
        #

        req = [ workspace, table ]
        if tag != None:
            if self.app != "user":
                raise RRApiError("Tags are possible only in user app")
            else:
                req.append(tag)
        req.append("count")

        #
        # Constructing filter/query payload
        #

        filters = {}
        if filter != None:
            filters['filter'] = filter
        if query != None:
            filters['query'] = query

        return int(self.get(req, filters))

    def data(self, workspace, table, template, columns = None, filter = None, query = None, order = None, tag = None):
        """
        Get data for table for workspace with filter, query (all apps) or tag (USER app only)
        """

        #
        # Check req parameters
        #

        if not isinstance(workspace, str):
            raise RRApiError("workspace parameter must be str")

        #
        # Constructing request path
        #

        req = [ workspace, table, template ]
        if columns != None:
            req.append(",".join(columns))
        else:
            req.append("all")
        if order != None:
            req.append(",".join(order))
        else:
            req.append("none")
        if tag != None:
            if self.app != "user":
                raise RRApiError("Tags are possible only in user app")
            else:
                req.append(tag)
        req.append("data")

        #
        # Constructing filter/query payload
        #

        filters = {}
        if filter != None:
            filters['filter'] = filter
        if query != None:
            filters['query'] = query

        return self.get(req, filters)

    def reports(self, workspace):
        """
        Get available reports (USER app only)
        """
        if self.app != "user":
            raise RRApiError("Reports available only in user app")
        return self.get([workspace, "reports"])
    
    def report(self, workspace, report):
        """
        Get report data (USER app only)
        """
        if self.app != "user":
            raise RRApiError("Reports available only in user app")
        return self.get([workspace, report, "data"])


if __name__ == '__main__':

    print("RR API library.")