Example #1
0
 def send_request(self, method, kwargs):
     "Send POST request to server with provided parameters"
     url = self.cachesrv
     uinput = getarg(kwargs, 'input', '')
     format = getarg(kwargs, 'format', '')
     idx = getarg(kwargs, 'idx', 0)
     limit = getarg(kwargs, 'limit', 10)
     skey = getarg(kwargs, 'sort', '')
     sdir = getarg(kwargs, 'dir', 'asc')
     params = {
         'query': uinput,
         'idx': idx,
         'limit': limit,
         'skey': skey,
         'order': sdir
     }
     if method == 'POST':
         path = '/rest/create'
     elif method == 'GET':
         path = '/rest/request'
     else:
         raise Exception('Unsupported method %s' % method)
     headers = {
         'Accept': 'application/json',
         'Content-type': 'application/json'
     }
     try:
         data = urllib2_request(method, url + path, params, headers=headers)
         result = json.loads(data)
     except:
         self.daslogger.error(traceback.format_exc())
         result = {'status': 'fail', 'reason': traceback.format_exc()}
     return result
Example #2
0
 def send_request(self, method, kwargs):
     "Send POST request to server with provided parameters"
     url     = self.cachesrv
     uinput  = getarg(kwargs, 'input', '')
     format  = getarg(kwargs, 'format', '')
     idx     = getarg(kwargs, 'idx', 0)
     limit   = getarg(kwargs, 'limit', 10)
     skey    = getarg(kwargs, 'sort', '')
     sdir    = getarg(kwargs, 'dir', 'asc')
     params  = {'query':uinput, 'idx':idx, 'limit':limit, 
               'skey':skey, 'order':sdir}
     if  method == 'POST':
         path    = '/rest/create'
     elif  method == 'GET':
         path    = '/rest/request'
     else:
         raise Exception('Unsupported method %s' % method)
     headers = {'Accept': 'application/json', 
                'Content-type': 'application/json'} 
     try:
         data = urllib2_request(method, url+path, params, headers=headers)
         result = json.loads(data)
     except:
         self.daslogger.error(traceback.format_exc())
         result = {'status':'fail', 'reason':traceback.format_exc()}
     return result
Example #3
0
    def status(self, **kwargs):
        """
        Place request to obtain status about given query
        """
        img  = '<img src="%s/images/loading.gif" alt="loading"/>' % self.base
        req  = """
        <script type="application/javascript">
        setTimeout('ajaxStatus()',3000)
        </script>"""

        def set_header():
            "Set HTTP header parameters"
            tstamp = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
            cherrypy.response.headers['Expire'] = tstamp
            cherrypy.response.headers['Cache-control'] = 'no-cache'

        uinput  = kwargs.get('input', '')
        uinput  = urllib.unquote_plus(uinput)
        ajax    = kwargs.get('ajax', 1)
        view    = kwargs.get('view', 'list')
        params  = {'query':uinput}
        path    = '/rest/status'
        url     = self.cachesrv
        headers = {'Accept': 'application/json'}
        try:
            res  = urllib2_request('GET', url+path, params, headers=headers)
            data = json.loads(res)
        except:
            self.daslogger.error(traceback.format_exc())
            data = {'status':'fail'}
        if  ajax:
            cherrypy.response.headers['Content-Type'] = 'text/xml'
            if  data['status'] == 'ok':
                page  = '<script type="application/javascript">reload()</script>'
            elif data['status'] == 'fail':
                page  = '<script type="application/javascript">reload()</script>'
                page += self.error(self.gen_error_msg(kwargs))
            else:
                page  = img + ' ' + str(data['status']) + ', please wait...'
                img_stop = ''
                page += ', <a href="/das/">stop</a> request' 
                page += req
                set_header()
            page = ajax_response(page)
        else:
            try:
                page = data['status']
            except:
                page = traceback.format_exc()
        return page
Example #4
0
    def status(self, **kwargs):
        """
        Place request to obtain status about given query
        """
        img = '<img src="%s/images/loading.gif" alt="loading"/>' % self.base
        req = """
        <script type="application/javascript">
        setTimeout('ajaxStatus()',3000)
        </script>"""

        def set_header():
            "Set HTTP header parameters"
            tstamp = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
            cherrypy.response.headers['Expire'] = tstamp
            cherrypy.response.headers['Cache-control'] = 'no-cache'

        uinput = kwargs.get('input', '')
        uinput = urllib.unquote_plus(uinput)
        ajax = kwargs.get('ajax', 1)
        view = kwargs.get('view', 'list')
        params = {'query': uinput}
        path = '/rest/status'
        url = self.cachesrv
        headers = {'Accept': 'application/json'}
        try:
            res = urllib2_request('GET', url + path, params, headers=headers)
            data = json.loads(res)
        except:
            self.daslogger.error(traceback.format_exc())
            data = {'status': 'fail'}
        if ajax:
            cherrypy.response.headers['Content-Type'] = 'text/xml'
            if data['status'] == 'ok':
                page = '<script type="application/javascript">reload()</script>'
            elif data['status'] == 'fail':
                page = '<script type="application/javascript">reload()</script>'
                page += self.error(self.gen_error_msg(kwargs))
            else:
                page = img + ' ' + str(data['status']) + ', please wait...'
                img_stop = ''
                page += ', <a href="/das/">stop</a> request'
                page += req
                set_header()
            page = ajax_response(page)
        else:
            try:
                page = data['status']
            except:
                page = traceback.format_exc()
        return page
Example #5
0
    def status(self, **kwargs):
        """
        Place request to obtain status about given query
        """
        img = '<img src="%s/images/loading.gif" alt="loading"/>' % self.base
        req = """
        <script type="application/javascript">
        setTimeout('ajaxStatus()',3000)
        </script>"""

        def set_header():
            "Set HTTP header parameters"
            tstamp = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
            cherrypy.response.headers["Expire"] = tstamp
            cherrypy.response.headers["Cache-control"] = "no-cache"

        uinput = kwargs.get("input", "")
        uinput = urllib.unquote_plus(uinput)
        ajax = kwargs.get("ajax", 1)
        view = kwargs.get("view", "list")
        params = {"query": uinput}
        path = "/rest/status"
        url = self.cachesrv
        headers = {"Accept": "application/json"}
        try:
            res = urllib2_request("GET", url + path, params, headers=headers)
            data = json.loads(res)
        except:
            self.daslogger.error(traceback.format_exc())
            data = {"status": "fail"}
        if ajax:
            cherrypy.response.headers["Content-Type"] = "text/xml"
            if data["status"] == "ok":
                page = '<script type="application/javascript">reload()</script>'
            elif data["status"] == "fail":
                page = '<script type="application/javascript">reload()</script>'
                page += self.error(self.gen_error_msg(kwargs))
            else:
                page = img + " " + str(data["status"]) + ", please wait..."
                img_stop = ""
                page += ', <a href="/das/">stop</a> request'
                page += req
                set_header()
            page = ajax_response(page)
        else:
            try:
                page = data["status"]
            except:
                page = traceback.format_exc()
        return page
Example #6
0
 def nresults(self, kwargs):
     """
     invoke DAS search call, parse results and return them to
     web methods
     """
     url     = self.cachesrv
     uinput  = getarg(kwargs, 'input', '')
     params  = {'query':uinput}
     path    = '/rest/nresults'
     headers = {"Accept": "application/json"}
     try:
         data = urllib2_request('GET', url+path, params, headers=headers)
         record = json.loads(data)
     except:
         self.daslogger.error(traceback.format_exc())
         record = {'status':'fail', 'reason':traceback.format_exc()}
     if  record['status'] == 'success':
         return record['nresults']
     else:
         msg = "nresults returns status: %s" % str(record)
         self.daslogger.info(msg)
     return -1
Example #7
0
 def nresults(self, kwargs):
     """
     invoke DAS search call, parse results and return them to
     web methods
     """
     url = self.cachesrv
     uinput = getarg(kwargs, 'input', '')
     params = {'query': uinput}
     path = '/rest/nresults'
     headers = {"Accept": "application/json"}
     try:
         data = urllib2_request('GET', url + path, params, headers=headers)
         record = json.loads(data)
     except:
         self.daslogger.error(traceback.format_exc())
         record = {'status': 'fail', 'reason': traceback.format_exc()}
     if record['status'] == 'success':
         return record['nresults']
     else:
         msg = "nresults returns status: %s" % str(record)
         self.daslogger.info(msg)
     return -1
Example #8
0
 def nresults(self, kwargs):
     """
     invoke DAS search call, parse results and return them to
     web methods
     """
     url = self.cachesrv
     uinput = getarg(kwargs, "input", "")
     params = {"query": uinput}
     path = "/rest/nresults"
     headers = {"Accept": "application/json"}
     try:
         data = urllib2_request("GET", url + path, params, headers=headers)
         record = json.loads(data)
     except:
         self.daslogger.error(traceback.format_exc())
         record = {"status": "fail", "reason": traceback.format_exc()}
     if record["status"] == "success":
         return record["nresults"]
     else:
         msg = "nresults returns status: %s" % str(record)
         self.daslogger.info(msg)
     return -1
Example #9
0
 def send_request(self, method, kwargs):
     "Send POST request to server with provided parameters"
     url = self.cachesrv
     uinput = getarg(kwargs, "input", "")
     format = getarg(kwargs, "format", "")
     idx = getarg(kwargs, "idx", 0)
     limit = getarg(kwargs, "limit", 10)
     skey = getarg(kwargs, "sort", "")
     sdir = getarg(kwargs, "dir", "asc")
     params = {"query": uinput, "idx": idx, "limit": limit, "skey": skey, "order": sdir}
     if method == "POST":
         path = "/rest/create"
     elif method == "GET":
         path = "/rest/request"
     else:
         raise Exception("Unsupported method %s" % method)
     headers = {"Accept": "application/json", "Content-type": "application/json"}
     try:
         data = urllib2_request(method, url + path, params, headers=headers)
         result = json.loads(data)
     except:
         self.daslogger.error(traceback.format_exc())
         result = {"status": "fail", "reason": traceback.format_exc()}
     return result
Example #10
0
    def records(self, *args, **kwargs):
        """
        Retieve all records id's.
        """
        try:
            recordid = None
            format = ''
            if  args:
                recordid = args[0]
                spec = {'_id':recordid}
                fields = None
                query = dict(fields=fields, spec=spec)
                if  len(args) == 2:
                    format = args[1]
            elif  kwargs and '_id' in kwargs:
                spec = {'_id': kwargs['_id']}
                fields = None
                query = dict(fields=fields, spec=spec)
            else: # return all ids
                query = dict(fields=None, spec={})

            nresults = self.nresults(query)
            time0    = time.time()
            url      = self.cachesrv
            idx      = getarg(kwargs, 'idx', 0)
            limit    = getarg(kwargs, 'limit', 10)
            show     = getarg(kwargs, 'show', 'json')
            coll     = getarg(kwargs, 'collection', 'merge')
#            params   = {'query':json.dumps(query), 'idx':idx, 'limit':limit}
#            path     = '/rest/request'
            params   = {'query':json.dumps(query), 'idx':idx, 'limit':limit, 
                        'collection':coll}
            path     = '/rest/records'
            headers  = {"Accept": "application/json"}
            try:
                data = urllib2_request('GET', url+path, params, headers=headers)
                result = json.loads(data)
            except:
                self.daslogger.error(traceback.format_exc())
                result = {'status':'fail', 'reason':traceback.format_exc()}
            res = ""
            if  result['status'] == 'success':
                if  recordid: # we got id
                    for row in result['data']:
                        if  show == 'json':
                            jsoncode = {'jsoncode': json2html(row, "")}
                            res += self.templatepage('das_json', **jsoncode)
                        elif show == 'code':
                            code  = pformat(row, indent=1, width=100)
                            res += self.templatepage('das_code', code=code)
                        else:
                            code = yaml.dump(row, width=100, indent=4, 
                                        default_flow_style=False)
                            res += self.templatepage('das_code', code=code)
                else:
                    for row in result['data']:
                        rid  = row['_id']
                        del row['_id']
                        record = dict(id=rid, daskeys=', '.join(row))
                        res += self.templatepage('das_record', **record)
            else:
                res = result['status']
                if  'reason' in res:
                    return self.error(res['reason'])
                else:
                    msg = 'Uknown error, kwargs=' % kwargs
                    return self.error(msg)
            if  recordid:
                if  format:
                    if  format == 'xml':
                        return self.wrap2dasxml(result['data'])
                    elif  format == 'json':
                        return self.wrap2dasjson(result['data'])
                    else:
                        return self.error('Unsupported data format %s' % format)
                page  = res
            else:
                url   = '/das/records?'
                idict = dict(nrows=nresults, idx=idx, 
                            limit=limit, results=res, url=url)
                page  = self.templatepage('das_pagination', **idict)

            form    = self.form(uinput="")
            ctime   = (time.time()-time0)
            page = self.page(form + page, ctime=ctime)
            return page
        except:
            return self.error(self.gen_error_msg(kwargs))
Example #11
0
    def records(self, *args, **kwargs):
        """
        Retieve all records id's.
        """
        try:
            recordid = None
            format = ''
            if args:
                recordid = args[0]
                spec = {'_id': recordid}
                fields = None
                query = dict(fields=fields, spec=spec)
                if len(args) == 2:
                    format = args[1]
            elif kwargs and '_id' in kwargs:
                spec = {'_id': kwargs['_id']}
                fields = None
                query = dict(fields=fields, spec=spec)
            else:  # return all ids
                query = dict(fields=None, spec={})

            nresults = self.nresults(query)
            time0 = time.time()
            url = self.cachesrv
            idx = getarg(kwargs, 'idx', 0)
            limit = getarg(kwargs, 'limit', 10)
            show = getarg(kwargs, 'show', 'json')
            coll = getarg(kwargs, 'collection', 'merge')
            #            params   = {'query':json.dumps(query), 'idx':idx, 'limit':limit}
            #            path     = '/rest/request'
            params = {
                'query': json.dumps(query),
                'idx': idx,
                'limit': limit,
                'collection': coll
            }
            path = '/rest/records'
            headers = {"Accept": "application/json"}
            try:
                data = urllib2_request('GET',
                                       url + path,
                                       params,
                                       headers=headers)
                result = json.loads(data)
            except:
                self.daslogger.error(traceback.format_exc())
                result = {'status': 'fail', 'reason': traceback.format_exc()}
            res = ""
            if result['status'] == 'success':
                if recordid:  # we got id
                    for row in result['data']:
                        if show == 'json':
                            jsoncode = {'jsoncode': json2html(row, "")}
                            res += self.templatepage('das_json', **jsoncode)
                        elif show == 'code':
                            code = pformat(row, indent=1, width=100)
                            res += self.templatepage('das_code', code=code)
                        else:
                            code = yaml.dump(row,
                                             width=100,
                                             indent=4,
                                             default_flow_style=False)
                            res += self.templatepage('das_code', code=code)
                else:
                    for row in result['data']:
                        rid = row['_id']
                        del row['_id']
                        record = dict(id=rid, daskeys=', '.join(row))
                        res += self.templatepage('das_record', **record)
            else:
                res = result['status']
                if 'reason' in res:
                    return self.error(res['reason'])
                else:
                    msg = 'Uknown error, kwargs=' % kwargs
                    return self.error(msg)
            if recordid:
                if format:
                    if format == 'xml':
                        return self.wrap2dasxml(result['data'])
                    elif format == 'json':
                        return self.wrap2dasjson(result['data'])
                    else:
                        return self.error('Unsupported data format %s' %
                                          format)
                page = res
            else:
                url = '/das/records?'
                idict = dict(nrows=nresults,
                             idx=idx,
                             limit=limit,
                             results=res,
                             url=url)
                page = self.templatepage('das_pagination', **idict)

            form = self.form(uinput="")
            ctime = (time.time() - time0)
            page = self.page(form + page, ctime=ctime)
            return page
        except:
            return self.error(self.gen_error_msg(kwargs))
Example #12
0
    def records(self, *args, **kwargs):
        """
        Retieve all records id's.
        """
        try:
            recordid = None
            format = ""
            if args:
                recordid = args[0]
                spec = {"_id": recordid}
                fields = None
                query = dict(fields=fields, spec=spec)
                if len(args) == 2:
                    format = args[1]
            elif kwargs and kwargs.has_key("_id"):
                spec = {"_id": kwargs["_id"]}
                fields = None
                query = dict(fields=fields, spec=spec)
            else:  # return all ids
                query = dict(fields=None, spec={})

            nresults = self.nresults(query)
            time0 = time.time()
            url = self.cachesrv
            idx = getarg(kwargs, "idx", 0)
            limit = getarg(kwargs, "limit", 10)
            show = getarg(kwargs, "show", "json")
            coll = getarg(kwargs, "collection", "merge")
            #            params   = {'query':json.dumps(query), 'idx':idx, 'limit':limit}
            #            path     = '/rest/request'
            params = {"query": json.dumps(query), "idx": idx, "limit": limit, "collection": coll}
            path = "/rest/records"
            headers = {"Accept": "application/json"}
            try:
                data = urllib2_request("GET", url + path, params, headers=headers)
                result = json.loads(data)
            except:
                self.daslogger.error(traceback.format_exc())
                result = {"status": "fail", "reason": traceback.format_exc()}
            res = ""
            if result["status"] == "success":
                if recordid:  # we got id
                    for row in result["data"]:
                        if show == "json":
                            jsoncode = {"jsoncode": json2html(row, "")}
                            res += self.templatepage("das_json", **jsoncode)
                        elif show == "code":
                            code = pformat(row, indent=1, width=100)
                            res += self.templatepage("das_code", code=code)
                        else:
                            code = yaml.dump(row, width=100, indent=4, default_flow_style=False)
                            res += self.templatepage("das_code", code=code)
                else:
                    for row in result["data"]:
                        rid = row["_id"]
                        del row["_id"]
                        record = dict(id=rid, daskeys=", ".join(row))
                        res += self.templatepage("das_record", **record)
            else:
                res = result["status"]
                if res.has_key("reason"):
                    return self.error(res["reason"])
                else:
                    msg = "Uknown error, kwargs=" % kwargs
                    return self.error(msg)
            if recordid:
                if format:
                    if format == "xml":
                        return self.wrap2dasxml(result["data"])
                    elif format == "json":
                        return self.wrap2dasjson(result["data"])
                    else:
                        return self.error("Unsupported data format %s" % format)
                page = res
            else:
                url = "/das/records?"
                idict = dict(nrows=nresults, idx=idx, limit=limit, results=res, url=url)
                page = self.templatepage("das_pagination", **idict)

            form = self.form(uinput="")
            ctime = time.time() - time0
            page = self.page(form + page, ctime=ctime)
            return page
        except:
            return self.error(self.gen_error_msg(kwargs))