2
2
"""
3
3
Copyright (C) 2015, marazt. All rights reserved.
4
4
"""
5
- from typing import List , Dict
6
5
from mapper .object_mapper_exception import ObjectMapperException
7
6
from mapper .casedict import CaseDict
8
7
from inspect import getmembers , isroutine
@@ -88,7 +87,8 @@ class 'B' with attributes 'name' and 'age' and we want to map 'A' to 'B' in a wa
88
87
self .mappings = {}
89
88
pass
90
89
91
- def create_map (self , type_from : type , type_to : type , mapping : Dict = None ):
90
+ def create_map (self , type_from , type_to , mapping = None ):
91
+ # type: (type, type, Dict) -> None
92
92
"""Method for adding mapping definitions
93
93
94
94
:param type_from: source type
@@ -100,16 +100,16 @@ def create_map(self, type_from: type, type_to: type, mapping: Dict =None):
100
100
"""
101
101
102
102
if (type (type_from ) is not type ):
103
- raise ObjectMapperException (f "type_from must be a type" )
103
+ raise ObjectMapperException ("type_from must be a type" )
104
104
105
105
if (type (type_to ) is not type ):
106
- raise ObjectMapperException (f "type_to must be a type" )
106
+ raise ObjectMapperException ("type_to must be a type" )
107
107
108
- if (mapping is not None and not isinstance (mapping , Dict )):
109
- raise ObjectMapperException (f "mapping, if provided, must be a Dict type" )
108
+ if (mapping is not None and not isinstance (mapping , dict )):
109
+ raise ObjectMapperException ("mapping, if provided, must be a Dict type" )
110
110
111
- key_from = f" { type_from . __module__ } .{ type_from .__name__ } "
112
- key_to = f" { type_to . __module__ } .{ type_to .__name__ } "
111
+ key_from = "{0 }.{1}" . format ( type_from .__module__ , type_from . __name__ )
112
+ key_to = "{0 }.{1}" . format ( type_to .__module__ , type_to . __name__ )
113
113
114
114
if key_from in self .mappings :
115
115
inner_map = self .mappings [key_from ]
@@ -122,7 +122,8 @@ def create_map(self, type_from: type, type_to: type, mapping: Dict =None):
122
122
self .mappings [key_from ][key_to ] = (type_to , mapping )
123
123
124
124
125
- def map (self , from_obj , to_type : type = type (None ), ignore_case : bool = False , allow_none : bool = False , excluded : List [str ]= None , included : List [str ]= None , allow_unmapped : bool = False ):
125
+ def map (self , from_obj , to_type = type (None ), ignore_case = False , allow_none = False , excluded = None , included = None , allow_unmapped = False ):
126
+ # type: (object, type, bool, bool, List[str], List[str], bool) -> object
126
127
"""Method for creating target object instance
127
128
128
129
:param from_obj: source object to be mapped from
@@ -141,10 +142,10 @@ def map(self, from_obj, to_type: type=type(None), ignore_case: bool=False, allow
141
142
# one of the tests is explicitly checking for an attribute error on __dict__ if it's not set
142
143
from_obj .__dict__
143
144
144
- key_from = f" { from_obj .__class__ .__module__ } . { from_obj .__class__ .__name__ } "
145
+ key_from = "{0}.{1}" . format ( from_obj .__class__ .__module__ , from_obj .__class__ .__name__ )
145
146
146
147
if key_from not in self .mappings :
147
- raise ObjectMapperException (f "No mapping defined for { key_from } " )
148
+ raise ObjectMapperException ("No mapping defined for {0}" . format ( key_from ) )
148
149
149
150
custom_mappings = None
150
151
key_to_cls = type (None )
@@ -154,14 +155,14 @@ def map(self, from_obj, to_type: type=type(None), ignore_case: bool=False, allow
154
155
# if this is a nested call and we do not currently support more than one to_types
155
156
assert (len (self .mappings [key_from ]) > 0 )
156
157
if len (self .mappings [key_from ]) > 1 :
157
- raise ObjectMapperException (f "Ambiguous type mapping exists for { key_from } , must specifiy to_type explicitly" )
158
+ raise ObjectMapperException ("Ambiguous type mapping exists for {0 }, must specifiy to_type explicitly" . format ( key_from ) )
158
159
the_only_key_to = next (iter (self .mappings [key_from ]))
159
160
key_to_cls = self .mappings [key_from ][the_only_key_to ][0 ]
160
161
custom_mappings = self .mappings [key_from ][the_only_key_to ][1 ]
161
162
else :
162
- key_to = f" { to_type . __module__ } .{ to_type .__name__ } "
163
+ key_to = "{0 }.{1}" . format ( to_type .__module__ , to_type . __name__ )
163
164
if key_to not in self .mappings [key_from ]:
164
- raise ObjectMapperException (f "No mapping defined for { key_from } to { key_to } " )
165
+ raise ObjectMapperException ("No mapping defined for {0 } to {1}" . format ( key_from , key_to ) )
165
166
key_to_cls = self .mappings [key_from ][key_to ][0 ]
166
167
custom_mappings = self .mappings [key_from ][key_to ][1 ]
167
168
@@ -194,7 +195,7 @@ def is_included(s, mapping):
194
195
def map_obj (o , allow_unmapped ):
195
196
if o is not None :
196
197
key_from_child_cls = o .__class__
197
- key_from_child = f" { key_from_child_cls . __module__ } .{ key_from_child_cls .__name__ } "
198
+ key_from_child = "{0 }.{1}" . format ( key_from_child_cls .__module__ , key_from_child_cls . __name__ )
198
199
if (key_from_child in self .mappings ):
199
200
# if key_to has a mapping defined, nests the map() call
200
201
return self .map (o , type (None ), ignore_case , allow_none , excluded , included , allow_unmapped )
@@ -206,7 +207,7 @@ def map_obj(o, allow_unmapped):
206
207
if allow_unmapped :
207
208
return o
208
209
else :
209
- raise ObjectMapperException (f "No mapping defined for { key_from_child } " )
210
+ raise ObjectMapperException ("No mapping defined for {0}" . format ( key_from_child ) )
210
211
else :
211
212
return None
212
213
@@ -230,7 +231,7 @@ def map_obj(o, allow_unmapped):
230
231
elif prop in from_props :
231
232
# try find property with the same name in the source
232
233
from_obj_child = from_props [prop ]
233
- if isinstance (from_obj_child , List ):
234
+ if isinstance (from_obj_child , list ):
234
235
val = [map_obj (from_obj_child_i , allow_unmapped ) for from_obj_child_i in from_obj_child ]
235
236
else :
236
237
val = map_obj (from_obj_child , allow_unmapped )
0 commit comments