Beispiel #1
0
 def test_dynamicbase_add_attributes(self):
     class TempType(types.DynamicBase):
         pass
     types.registry.register(TempType)
     attrs = types.list_attributes(TempType)
     self.assertEqual(attrs, [])
     TempType.add_attributes(one=str)
     after = types.list_attributes(TempType)
     self.assertEqual(len(after), 1)
Beispiel #2
0
 def test_reregister_and_add_attr(self):
     class TempType(object):
         pass
     types.registry.register(TempType)
     attrs = types.list_attributes(TempType)
     self.assertEqual(attrs, [])
     TempType.one = str
     types.registry.reregister(TempType)
     after = types.list_attributes(TempType)
     self.assertNotEqual(after, [])
Beispiel #3
0
    def test_dynamicbase_add_attributes(self):
        class TempType(types.DynamicBase):
            pass

        types.registry.register(TempType)
        attrs = types.list_attributes(TempType)
        self.assertEqual(attrs, [])
        TempType.add_attributes(one=str)
        after = types.list_attributes(TempType)
        self.assertEqual(len(after), 1)
Beispiel #4
0
    def test_reregister_and_add_attr(self):
        class TempType(object):
            pass

        types.registry.register(TempType)
        attrs = types.list_attributes(TempType)
        self.assertEqual(attrs, [])
        TempType.one = str
        types.registry.reregister(TempType)
        after = types.list_attributes(TempType)
        self.assertNotEqual(after, [])
Beispiel #5
0
    def test_list_attribute_no_auto_register(self):
        class MyType(object):
            aint = int

        assert not hasattr(MyType, '_wsme_attributes')

        try:
            types.list_attributes(MyType)
            assert False, "TypeError was not raised"
        except TypeError:
            pass

        assert not hasattr(MyType, '_wsme_attributes')
Beispiel #6
0
def array_from_params(datatype, params, path, hit_paths):
    if hasattr(params, 'getall'):
        # webob multidict
        def getall(params, path):
            return params.getall(path)
    elif hasattr(params, 'getlist'):
        # werkzeug multidict
        def getall(params, path):  # noqa
            return params.getlist(path)
    if path in params:
        hit_paths.add(path)
        return [
            from_param(datatype.item_type, value)
            for value in getall(params, path)]

    if iscomplex(datatype.item_type):
        attributes = set()
        r = re.compile('^%s\.(?P<attrname>[^\.])' % re.escape(path))
        for p in params.keys():
            m = r.match(p)
            if m:
                attributes.add(m.group('attrname'))
        if attributes:
            value = []
            for attrdef in list_attributes(datatype.item_type):
                attrpath = '%s.%s' % (path, attrdef.key)
                hit_paths.add(attrpath)
                attrvalues = getall(params, attrpath)
                if len(value) < len(attrvalues):
                    value[-1:] = [
                        datatype.item_type()
                        for i in moves.range(len(attrvalues) - len(value))
                    ]
                for i, attrvalue in enumerate(attrvalues):
                    setattr(
                        value[i],
                        attrdef.key,
                        from_param(attrdef.datatype, attrvalue)
                    )
            return value

    indexes = set()
    r = re.compile('^%s\[(?P<index>\d+)\]' % re.escape(path))

    for p in params.keys():
        m = r.match(p)
        if m:
            indexes.add(int(m.group('index')))

    if not indexes:
        return Unset

    indexes = list(indexes)
    indexes.sort()

    return [from_params(datatype.item_type, params,
                        '%s[%s]' % (path, index), hit_paths)
            for index in indexes]
Beispiel #7
0
def array_from_params(datatype, params, path, hit_paths):
    if hasattr(params, 'getall'):
        # webob multidict
        def getall(params, path):
            return params.getall(path)
    elif hasattr(params, 'getlist'):
        # werkzeug multidict
        def getall(params, path):  # noqa
            return params.getlist(path)

    if path in params:
        hit_paths.add(path)
        return [
            from_param(datatype.item_type, value)
            for value in getall(params, path)
        ]

    if iscomplex(datatype.item_type):
        attributes = set()
        r = re.compile(r'^%s\.(?P<attrname>[^\.])' % re.escape(path))
        for p in params.keys():
            m = r.match(p)
            if m:
                attributes.add(m.group('attrname'))
        if attributes:
            value = []
            for attrdef in list_attributes(datatype.item_type):
                attrpath = '%s.%s' % (path, attrdef.key)
                hit_paths.add(attrpath)
                attrvalues = getall(params, attrpath)
                if len(value) < len(attrvalues):
                    value[-1:] = [
                        datatype.item_type()
                        for i in moves.range(len(attrvalues) - len(value))
                    ]
                for i, attrvalue in enumerate(attrvalues):
                    setattr(value[i], attrdef.key,
                            from_param(attrdef.datatype, attrvalue))
            return value

    indexes = set()
    r = re.compile(r'^%s\[(?P<index>\d+)\]' % re.escape(path))

    for p in params.keys():
        m = r.match(p)
        if m:
            indexes.add(int(m.group('index')))

    if not indexes:
        return Unset

    indexes = list(indexes)
    indexes.sort()

    return [
        from_params(datatype.item_type, params, '%s[%s]' % (path, index),
                    hit_paths) for index in indexes
    ]
Beispiel #8
0
 def convert(cls, rpc_obj):
     extra_keys = [key for key in rpc_obj
                   if key not in
                   [i.key for i in wtypes.list_attributes(Host)]]
     extra_capas = dict((capa, rpc_obj[capa])
                        for capa in extra_keys if capa not in ['status'])
     rpc_obj['extra_capas'] = extra_capas
     obj = cls(**rpc_obj)
     return obj
Beispiel #9
0
 def convert(cls, rpc_obj):
     extra_keys = [
         key for key in rpc_obj
         if key not in [i.key for i in wtypes.list_attributes(Host)]
     ]
     extra_capas = dict((capa, rpc_obj[capa]) for capa in extra_keys
                        if capa not in ['status'])
     rpc_obj['extra_capas'] = extra_capas
     obj = cls(**rpc_obj)
     return obj
Beispiel #10
0
def parse_multipart_data_from_body(datatypes, params):
    kw = dict()
    for name, datatype in datatypes.items():
        r = datatype()
        for attrdef in list_attributes(datatype):
            value = from_param(attrdef.datatype, params.get(attrdef.key))
            if value is not None:
                setattr(r, attrdef.key, value)
            elif attrdef.mandatory:
                raise wsme.exc.InvalidInput(attrdef.name, None,
                                            "Mandatory field missing.")
        kw[name] = r
    return (), kw
Beispiel #11
0
def tojson(datatype, value):
    if value is None:
        return value
    if iscomplex(datatype):
        d = {}
        for attrdef in list_attributes(datatype):
            attrvalue = getattr(value, attrdef.key)
            if attrvalue is not Unset:
                d[attrdef.name] = tojson(attrdef.datatype, attrvalue)
        value = d
    elif isusertype(datatype):
        value = tojson(datatype.basetype, datatype.tobasetype(value))
    return value
Beispiel #12
0
def fromjson(datatype, value):
    if value is None:
        return None
    if iscomplex(datatype):
        newvalue = datatype()
        for attrdef in list_attributes(datatype):
            if attrdef.name in value:
                setattr(newvalue, attrdef.key,
                        fromjson(attrdef.datatype, value[attrdef.name]))
        value = newvalue
    elif isusertype(datatype):
        value = datatype.frombasetype(fromjson(datatype.basetype, value))
    return value
Beispiel #13
0
def tojson(datatype, value):
    if value is None:
        return value
    if iscomplex(datatype):
        d = {}
        for attrdef in list_attributes(datatype):
            attrvalue = getattr(value, attrdef.key)
            if attrvalue is not Unset:
                d[attrdef.name] = tojson(attrdef.datatype, attrvalue)
        value = d
    elif isusertype(datatype):
        value = tojson(datatype.basetype, datatype.tobasetype(value))
    return value
Beispiel #14
0
def fromjson(datatype, value):
    if value is None:
        return None
    if iscomplex(datatype):
        newvalue = datatype()
        for attrdef in list_attributes(datatype):
            if attrdef.name in value:
                setattr(newvalue, attrdef.key,
                        fromjson(attrdef.datatype, value[attrdef.name]))
        value = newvalue
    elif isusertype(datatype):
        value = datatype.frombasetype(fromjson(datatype.basetype, value))
    return value
Beispiel #15
0
def from_params(datatype, params, path, hit_paths):
    if iscomplex(datatype) and datatype is not File:
        objfound = False
        for key in params:
            if key.startswith(path + '.'):
                objfound = True
                break
        if objfound:
            r = datatype()
            for attrdef in list_attributes(datatype):
                value = from_params(attrdef.datatype, params,
                                    '%s.%s' % (path, attrdef.key), hit_paths)
                if value is not Unset:
                    setattr(r, attrdef.key, value)
            return r
    else:
        if path in params:
            hit_paths.add(path)
            return from_param(datatype, params[path])
    return Unset
Beispiel #16
0
def from_params(datatype, params, path, hit_paths):
    if iscomplex(datatype) and datatype is not File:
        objfound = False
        for key in params:
            if key.startswith(path + '.'):
                objfound = True
                break
        if objfound:
            r = datatype()
            for attrdef in list_attributes(datatype):
                value = from_params(
                    attrdef.datatype,
                    params, '%s.%s' % (path, attrdef.key), hit_paths
                )
                if value is not Unset:
                    setattr(r, attrdef.key, value)
            return r
    else:
        if path in params:
            hit_paths.add(path)
            return from_param(datatype, params[path])
    return Unset