Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions mapper/object_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Copyright (C) 2015, marazt. All rights reserved.
"""
from inspect import getmembers, isroutine
from inspect import getmembers, isroutine, signature
from datetime import date, datetime

from mapper.casedict import CaseDict
Expand Down Expand Up @@ -166,10 +166,6 @@ def map(self, from_obj, to_type=type(None), ignore_case=False, allow_none=False,
.format(key_from.__module__, key_from.__name__, to_type.__module__, to_type.__name__))
key_to = to_type
custom_mappings = self.mappings[key_from][key_to][1]

# Currently, all target class data members need to have default value
# Object with __init__ that carries required non-default arguments are not supported
inst = key_to()

def not_private(s):
return not s.startswith('_')
Expand All @@ -183,6 +179,12 @@ def is_included(s, mapping):
from_obj_attributes = getmembers(from_obj, lambda a: not isroutine(a))
from_obj_dict = {k: v for k, v in from_obj_attributes}

# support __init__ by passing arguments by keyword when instantiating the key_to
sig = signature(key_to.__init__)
kwargs = {x: from_obj_dict[x] for x in sig.parameters if x not in ('self', 'kwargs')}

inst = key_to(**kwargs)

to_obj_attributes = getmembers(inst, lambda a: not isroutine(a))
to_obj_dict = {k: v for k, v in to_obj_attributes if not_excluded(k) and (not_private(k) or is_included(k, custom_mappings))}

Expand Down