Пример #1
0
 def extract(self):
     res = self.get()
     if res is None:
         if self.required:
             raise errors.BadRequest(parameter=self.name, status='missing')
         else:
             return self.defaultValue
     try:
         mimetype = flask.request.content_type if self.location == 'body' else converter.application.text
         res = self.service.revert(mimetype,
                                   res,
                                   cls=self.type,
                                   many=self.array,
                                   encoding=flask.request.content_encoding)
         if self.enum is not None:
             if res not in self.enum:
                 raise errors.BadRequest(parameter=self.name,
                                         enum=self.enum,
                                         status='unknown')
         return res
     except errors.PyrolysisException as e:
         e.put(parameter=self.name)
         raise e
     except Exception as e:
         raise errors.BadRequest(parameter=self.name, status='conflict')
Пример #2
0
 def str_convert(self, val, **kw):
     if val is None:
         return ''
     elif isinstance(val, str):
         return val
     elif isinstance(val, bytes):
         return base64.b64encode(val)
     elif isinstance(val, bytearray):
         return base64.b64encode(val)
     elif isinstance(val, date):
         return val.isoformat()
     elif isinstance(val, datetime):
         return val.isoformat()
     elif isinstance(val, int):
         return str(val)
     elif isinstance(val, float):
         return str(val)
     elif isinstance(val, Enum):
         return val.name
     elif isinstance(val, type):
         return val.__module__ + '.' + val.__name__
     elif isinstance(val, bool):
         return 'true' if val else 'false'
     elif isinstance(val, Mapping):
         raise errors.BadRequest(type=type(val), status='unknown')
     elif isinstance(val, Iterable):
         return ','.join(self.str_convert(x) for x in val)
     raise errors.BadRequest(type=type(val), status='unknown')
Пример #3
0
def check_param(val, type, mandatory):
    if val is None:
        if mandatory:
            raise errors.BadRequest(status='missing')
    elif isinstance(type, list):
        if val not in type:
            raise errors.BadRequest(val=val, enum=type, status='unknown')
    elif not isinstance(val, type):
        raise errors.BadRequest(type=type.__name__, val=type(val).__name__, status='conflict')
Пример #4
0
 def check(val):
     if not isinstance(val,dict):
         raise errors.BadRequest(type=type(val), status='unknown')
     if not mandatory.issubset(set(val.keys())):
         raise errors.BadRequest(fields=mandatory.sub(set(val.keys())), status='missing')
     if len(fields) > 0:
         for k,v in val.items():
             if k not in fields:
                 raise errors.BadRequest(field=k, status='unknown')
             fields[k].check(v)
Пример #5
0
 def check(val):
     if val in enum:
         pass
     elif isinstance(val, Enum) and val.name in enum:
         pass
     else:
         raise errors.BadRequest(val=val)
Пример #6
0
    def xml_revert(self, val, cls, checker=None, **kw):
        from xml.dom import minidom

        def revert_node(n):
            res = {}
            txt = ''
            for c in n.childNodes:
                if c.nodeType == c.TEXT_NODE:
                    txt += c.data
                else:
                    e = revert_node(c)
                    if c.hasAttribute('type'):
                        e = self.str_revert(e, c.getAttribute('type'), False)
                    if c.nodeName in res:
                        v = res[c.nodeName]
                        if type(v) == list:
                            v.append(e)
                        else:
                            res[c.nodeName] = [v, e]
                    else:
                        res[c.nodeName] = e
            if len(res) > 0:
                return res
            return txt

        if isinstance(val, requests.Response):
            val = val.text
        elif isinstance(val, bytes):
            val = val.decode(**keep(kw, []))
        elif not isinstance(val, str):
            raise errors.BadRequest(type=type(val).__name__, status='unknown')

        res = revert_node(minidom.parse(io.StringIO(val)).firstChild)

        return self.schema_revert(res, cls, checker=checker)
Пример #7
0
 def csv_convert(self, val, checker=None, **kw):
     if type(val) != list:
         val = [val]
     elif len(val) == 0:
         return ''
     import csv
     buf = io.StringIO()
     if isinstance(val, pandas_df_type):
         val.to_csv(buf, **keep(kw, []))
     else:
         val = self.schema_convert(val, checker)
         t = val[0]
         if isinstance(t, Mapping):
             if checker:
                 checker(val)
             w = csv.DictWriter(buf,
                                sorted(t.keys()),
                                lineterminator='\n',
                                **keep(kw, []))
             w.writeheader()
             w.writerows(val)
         elif isinstance(t, Iterable):
             if checker:
                 checker(val)
             w = csv.writer(buf, lineterminator='\n', **keep(kw, []))
             w.writerows(val)
         else:
             raise errors.BadRequest(type=t.__name__, status='unknown')
     return buf.getvalue()
Пример #8
0
 def str_revert(self, val, cls, many=False, **kw):
     if val is None:
         return val
     if type(val) == cls:
         return val
     if isinstance(val, requests.Response):
         val = val.text
     if isinstance(val, bytes):
         val = val.decode(**kw)
     if len(val) == 0:
         return None
     if many:
         return [
             self.str_revert(x, cls, False, **kw) for x in val.split(',')
         ]
     try:
         if cls == 'date' or cls == date:
             return datetime.strptime(val, '%Y-%m-%d').date()
         elif cls == 'datetime' or cls == datetime:
             return datetime.strptime(val, '%Y-%m-%dT%H:%M:%S')
         elif cls == 'int' or cls == int:
             return int(val)
         elif cls == 'float' or cls == float:
             return float(val)
         elif cls == 'str' or cls == str:
             return val
         elif cls == 'type' or cls == type:
             p = val.rindex('.')
             return getattr(sys.modules[val[:p]], val[p + 1:])
         elif cls == 'bytes' or cls == bytes:
             return base64.b64decode(val)
         elif cls == 'bytearray' or cls == bytearray:
             return bytearray(base64.b64decode(val))
         elif cls == 'bool' or cls == bool:
             val = val.lower()
             if val in ['true', 'false']:
                 return val == 'true'
         elif issubclass(cls, Enum):
             return cls[val]
     except Exception:
         raise errors.BadRequest(val=val, type=cls, status='conflict')
     raise errors.BadRequest(val=val, type=cls, status='unknown')
Пример #9
0
 def schema_convert(self, data, checker=None):
     if isinstance(data, Iterable) and not isinstance(
             data, Mapping) and not isinstance(data, str):
         return [self.schema_convert(x) for x in data]
     cls = type(data).__name__
     if cls in self.schemas:
         try:
             data = self.schemas[cls].dump(data)
         except marshmallow.exceptions.ValidationError as e:
             raise errors.BadRequest(errors=e.data, status='conflict')
     if checker:
         checker(data)
     return data
Пример #10
0
 def revert(self, ct, val, **kw):
     if ct == application.json:
         return self.json_revert(val, **kw)
     if ct == application.xml or ct == text.xml:
         return self.xml_revert(val, **kw)
     if ct == application.pickle:
         return self.pickle_revert(val, **kw)
     if ct == application.msgpack:
         return self.msgpack_revert(val, **kw)
     if ct == application.csv or ct == text.csv:
         return self.csv_revert(val, **kw)
     if ct == application.text or ct == text.plain:
         return self.str_revert(val, **kw)
     raise errors.BadRequest()
Пример #11
0
 def schema_revert(self, data, cls, checker=None):
     if isinstance(data, Iterable) and not isinstance(
             data, Mapping) and not isinstance(data, str):
         return [self.schema_revert(x, cls, checker) for x in data]
     if isinstance(data, str) and cls != 'str' and cls != str:
         data = self.str_revert(data, cls)
     if checker:
         checker(data)
     if isinstance(cls, type):
         cls = cls.__name__
     if cls in self.schemas:
         try:
             data = self.schemas[cls].load(data)
         except marshmallow.exceptions.ValidationError as e:
             raise errors.BadRequest(errors=e.data, status='conflict')
     return data
Пример #12
0
 def json_revert(self, val, cls=dict, checker=None, **kw):
     if cls == pandas_df_type.__name__ or cls == pandas_df_type:
         import pandas
         if isinstance(val, requests.Response):
             return pandas.read_json(val.content, **keep(kw, []))
         elif isinstance(val, bytes):
             return pandas.read_json(io.BytesIO(val), **keep(kw, []))
         elif isinstance(val, str):
             return pandas.read_json(io.StringIO(val), **keep(kw, []))
         else:
             raise errors.BadRequest(type=type(val).__name__,
                                     status='unknown')
     if isinstance(val, requests.Response):
         val = val.text
     import json
     val = json.loads(val, **keep(kw, ['encoding']))
     val = self.schema_revert(val, cls, checker=checker)
     return val
Пример #13
0
 def extract(self, *args, **kwargs):
     res = None
     if self.pos < len(args):
         res = args[self.pos]
     elif self.name in kwargs:
         res = kwargs.get(self.name)
     elif self.required:
         raise errors.BadRequest(name=self.name, status='missing')
     if res is None:
         return None
     try:
         srv = self.parent.parent
         return srv.convert(self.encoding,
                            res,
                            checker=self.checker,
                            **srv.convert_options)
     except errors.ClientError as e:
         e.put(name=self.name)
         raise e
Пример #14
0
 def check(val):
     for t in types:
         if isinstance(val, t):
             return
     raise errors.BadRequest(type=type(val), status='unknown')