Example #1
0
    def test_web_code(self):
        """Test DAS web codes"""
        for code, msg in DAS_WEB_CODES:
            result = web_code(msg)
            self.assertEqual(code, result)
            result = decode_code(code)
            self.assertEqual(msg, result)

        result = web_code('lkjlkj')
        self.assertEqual(-1, result)

        result = decode_code(-1)
        self.assertEqual('N/A', result)
Example #2
0
    def test_web_code(self):
        """Test DAS web codes"""
        for code, msg in DAS_WEB_CODES:
            result = web_code(msg)
            self.assertEqual(code, result)
            result = decode_code(code)
            self.assertEqual(msg, result)

        result = web_code('lkjlkj')
        self.assertEqual(-1, result)

        result = decode_code(-1)
        self.assertEqual('N/A', result)
Example #3
0
 def gridfs(self, **kwargs):
     """
     Retieve records from GridFS
     """
     time0 = time.time()
     if  'fid' not in kwargs:
         code = web_code('No file id')
         raise HTTPError(500, 'DAS error, code=%s' % code)
     fid  = kwargs.get('fid')
     data = {'status':'requested', 'fid':fid}
     try:
         fds = self.gfs.get(ObjectId(fid))
         return fds.read()
     except Exception as exc:
         print_exc(exc)
         code = web_code('Exception')
         raise HTTPError(500, 'DAS error, code=%s' % code)
     data['ctime'] = time.time() - time0
     return json.dumps(data)
Example #4
0
 def gridfs(self, **kwargs):
     """
     Retieve records from GridFS
     """
     time0 = time.time()
     if "fid" not in kwargs:
         code = web_code("No file id")
         raise HTTPError(500, "DAS error, code=%s" % code)
     fid = kwargs.get("fid")
     data = {"status": "requested", "fid": fid}
     try:
         fds = self.gfs.get(ObjectId(fid))
         return fds.read()
     except Exception as exc:
         print_exc(exc)
         code = web_code("Exception")
         raise HTTPError(500, "DAS error, code=%s" % code)
     data["ctime"] = time.time() - time0
     return json.dumps(data)
Example #5
0
 def sitedb(self, **kwargs):
     """
     Test SiteDB data-service
     """
     try:
         if  kwargs.has_key('name'):
             site = kwargs.get('name')
         elif kwargs.has_key('site'):
             site = kwargs.get('site')
         else:
             code = web_code('Unsupported key')
             HTTPError(500, 'DAS error, code=%s' % code)
         data = {"0": {"name":site}}
     except:
         data = {}
     return wrap2dasjson(data)
Example #6
0
 def sitedb(self, **kwargs):
     """
     Test SiteDB data-service
     """
     try:
         if 'name' in kwargs:
             site = kwargs.get('name')
         elif 'site' in kwargs:
             site = kwargs.get('site')
         else:
             code = web_code('Unsupported key')
             HTTPError(500, 'DAS error, code=%s' % code)
         data = {"0": {"name": site}}
     except:
         data = {}
     return wrap2dasjson(data)
Example #7
0
        def wrapped_f(self, *args, **kwds):
            """Wrap function arguments"""
            # check request headers. For methods POST/PUT
            # we need to read request body to get parameters
            if  cherrypy.request.method == 'POST' or\
                cherrypy.request.method == 'PUT':
                try:
                    body = cherrypy.request.body.read()
                except:
                    body = None
                if  args and kwds:
                    code = web_code('Misleading request')
                    raise HTTPError(500, 'DAS error, code=%s' % code)
                if  body:
                    jsondict = json.loads(body, encoding='latin-1')
                else:
                    jsondict = kwds
                for key, val in jsondict.items():
                    kwds[str(key)] = str(val)

            pat = web_arg_pattern
            if  not kwds:
                if  args:
                    kwds = args[-1]
            keys = []
            if  not isinstance(kwds, dict):
                code  = web_code('Unsupported kwds')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  kwds:
                keys = [i for i in kwds.keys() if i not in supported]
            if  keys:
                code  = web_code('Unsupported key')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'query') and not len(kwds['query']):
                code  = web_code('Invalid query')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  'input' in kwds:
                require_string(kwds['input'])
            if  'kwquery' in kwds:
                require_string(kwds['kwquery'])
            if  checkarg(kwds, 'idx') and not pat.match(str(kwds['idx'])):
                code  = web_code('Unsupported idx value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'limit') and not pat.match(str(kwds['limit'])):
                code  = web_code('Unsupported limit value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'qcache') and not pat.match(str(kwds['qcache'])):
                code  = web_code('Unsupported query cache value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'view'):
                if  kwds['view'] not in \
                        ['list', 'xml', 'json', 'plain', 'table']:
                    code  = web_code('Unsupported view')
                    raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'collection'):
                if  kwds['collection'] not in ['cache', 'merge']:
                    code  = web_code('Unsupported collection')
                    raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'msg') and not isinstance(kwds['msg'], str):
                code = web_code('Unsupported msg value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'fid') and len(kwds['fid']) != 24:
                code = web_code('Unsupported id value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'pid') and \
                    len(str(kwds['pid'])) not in [16, 24, 32]:
                code  = web_code('Unsupported pid value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'ahash') and len(str(kwds['ahash'])) != 32:
                code  = web_code('Unsupported ahash value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            dbses = ["prod/global", "prod/phys01", "prod/phys02", "prod/phys03", "prod/caf",
                        "int/global", "int/phys01", "int/phys02", "int/phys03",
                        "dev/global", "dev/phys01", "dev/phys02", "dev/phys03"]
            if  checkarg(kwds, 'instance') and \
                    str(kwds['instance']) not in dbses:
                code  = web_code('Unsupported dbs instance')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            data = func (self, *args, **kwds)
            return data
Example #8
0
 def require_string(val):
     """Check that provided input is a string"""
     if not (isinstance(val, str) or isinstance(val, unicode)):
         code = web_code('Invalid input')
         raise HTTPError(500, 'DAS error, code=%s' % code)
Example #9
0
        def wrapped_f(self, *args, **kwds):
            """Wrap function arguments"""
            # check request headers. For methods POST/PUT
            # we need to read request body to get parameters
            if  cherrypy.request.method == 'POST' or\
                cherrypy.request.method == 'PUT':
                try:
                    body = cherrypy.request.body.read()
                except:
                    body = None
                if args and kwds:
                    code = web_code('Misleading request')
                    raise HTTPError(500, 'DAS error, code=%s' % code)
                if body:
                    jsondict = json.loads(body, encoding='latin-1')
                else:
                    jsondict = kwds
                for key, val in jsondict.items():
                    kwds[str(key)] = str(val)

            pat = web_arg_pattern
            if not kwds:
                if args:
                    kwds = args[-1]
            keys = []
            if not isinstance(kwds, dict):
                code = web_code('Unsupported kwds')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if kwds:
                keys = [i for i in kwds.keys() if i not in supported]
            if keys:
                code = web_code('Unsupported key')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'query') and not len(kwds['query']):
                code = web_code('Invalid query')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if 'input' in kwds:
                require_string(kwds['input'])
            if 'kwquery' in kwds:
                require_string(kwds['kwquery'])
            if checkarg(kwds, 'idx') and not pat.match(str(kwds['idx'])):
                code = web_code('Unsupported idx value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'limit') and not pat.match(str(kwds['limit'])):
                code = web_code('Unsupported limit value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'qcache') and not pat.match(str(kwds['qcache'])):
                code = web_code('Unsupported query cache value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'view'):
                if  kwds['view'] not in \
                        ['list', 'xml', 'json', 'plain', 'table']:
                    code = web_code('Unsupported view')
                    raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'collection'):
                if kwds['collection'] not in ['cache', 'merge']:
                    code = web_code('Unsupported collection')
                    raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'msg') and not isinstance(kwds['msg'], str):
                code = web_code('Unsupported msg value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'fid') and len(kwds['fid']) != 24:
                code = web_code('Unsupported id value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if  checkarg(kwds, 'pid') and \
                    len(str(kwds['pid'])) not in [16, 24, 32]:
                code = web_code('Unsupported pid value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            if checkarg(kwds, 'ahash') and len(str(kwds['ahash'])) != 32:
                code = web_code('Unsupported ahash value')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            dbses = [
                "prod/global", "prod/phys01", "prod/phys02", "prod/phys03",
                "prod/caf", "int/global", "int/phys01", "int/phys02",
                "int/phys03", "dev/global", "dev/phys01", "dev/phys02",
                "dev/phys03"
            ]
            if  checkarg(kwds, 'instance') and \
                    str(kwds['instance']) not in dbses:
                code = web_code('Unsupported dbs instance')
                raise HTTPError(500, 'DAS error, code=%s' % code)
            data = func(self, *args, **kwds)
            return data
Example #10
0
 def require_string(val):
     """Check that provided input is a string"""
     if not (isinstance(val, str) or isinstance(val, unicode)):
         code = web_code('Invalid input')
         raise HTTPError(500, 'DAS error, code=%s' % code)