<s> from __future__ import unicode_literals , absolute_import <EOL> import collections <EOL> import itertools <EOL> import operator <EOL> import types <EOL> from . common import * <EOL> from . datastructures import Context <EOL> from . exceptions import * <EOL> from . undefined import Undefined <EOL> from . util import listify <EOL> try : <EOL> from collections import OrderedDict <EOL> except ImportError : <EOL> from . datastructures import OrderedDict <EOL> def import_loop ( cls , instance_or_dict , field_converter = None , trusted_data = None , <EOL> mapping = None , partial = False , strict = False , init_values = False , <EOL> apply_defaults = False , convert = True , validate = False , new = False , <EOL> oo = False , recursive = False , app_data = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> if instance_or_dict is None : <EOL> got_data = False <EOL> else : <EOL> got_data = True <EOL> if got_data and not isinstance ( instance_or_dict , ( cls , dict ) ) : <EOL> raise ConversionError ( '<STR_LIT>' ) <EOL> context = Context . _make ( context ) <EOL> try : <EOL> context . initialized <EOL> except : <EOL> if type ( field_converter ) is types . FunctionType : <EOL> field_converter = BasicConverter ( field_converter ) <EOL> context . _setdefaults ( { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : field_converter , <EOL> '<STR_LIT>' : trusted_data or { } , <EOL> '<STR_LIT>' : mapping or { } , <EOL> '<STR_LIT>' : partial , <EOL> '<STR_LIT:strict>' : strict , <EOL> '<STR_LIT>' : init_values , <EOL> '<STR_LIT>' : apply_defaults , <EOL> '<STR_LIT>' : convert , <EOL> '<STR_LIT>' : validate , <EOL> '<STR_LIT>' : new , <EOL> '<STR_LIT>' : oo , <EOL> '<STR_LIT>' : recursive , <EOL> '<STR_LIT>' : app_data if app_data is not None else { } <EOL> } ) <EOL> instance_or_dict = context . field_converter . pre ( cls , instance_or_dict , context ) <EOL> _field_converter = context . field_converter <EOL> _model_mapping = context . mapping . get ( '<STR_LIT>' ) <EOL> data = dict ( context . trusted_data ) if context . trusted_data else { } <EOL> errors = { } <EOL> if got_data : <EOL> all_fields = cls . _valid_input_keys <EOL> if context . mapping : <EOL> mapped_keys = ( set ( itertools . chain ( * ( <EOL> listify ( input_keys ) for target_key , input_keys in context . mapping . items ( ) <EOL> if target_key != '<STR_LIT>' ) ) ) ) <EOL> all_fields = all_fields | mapped_keys <EOL> if context . strict : <EOL> rogue_fields = set ( instance_or_dict ) - all_fields <EOL> if rogue_fields : <EOL> for field in rogue_fields : <EOL> errors [ field ] = '<STR_LIT>' <EOL> for field_name , field in cls . _field_list : <EOL> value = Undefined <EOL> serialized_field_name = field . serialized_name or field_name <EOL> if got_data : <EOL> for key in field . get_input_keys ( context . mapping ) : <EOL> if key and key in instance_or_dict : <EOL> value = instance_or_dict [ key ] <EOL> break <EOL> if value is Undefined : <EOL> if field_name in data : <EOL> continue <EOL> if context . apply_defaults : <EOL> value = field . default <EOL> if value is Undefined and context . init_values : <EOL> value = None <EOL> if got_data : <EOL> if field . is_compound : <EOL> if context . trusted_data and context . recursive : <EOL> td = context . trusted_data . get ( field_name ) <EOL> else : <EOL> td = { } <EOL> if _model_mapping : <EOL> submap = _model_mapping . get ( field_name ) <EOL> else : <EOL> submap = { } <EOL> field_context = context . _branch ( trusted_data = td , mapping = submap ) <EOL> else : <EOL> field_context = context <EOL> try : <EOL> value = _field_converter ( field , value , field_context ) <EOL> except ( FieldError , CompoundError ) as exc : <EOL> errors [ serialized_field_name ] = exc <EOL> if isinstance ( exc , DataError ) : <EOL> data [ field_name ] = exc . partial_data <EOL> continue <EOL> if not context . oo and value is Undefined : <EOL> continue <EOL> data [ field_name ] = value <EOL> if errors : <EOL> partial_data = dict ( ( ( key , value ) for key , value in data . items ( ) if value is not Undefined ) ) <EOL> raise DataError ( errors , partial_data ) <EOL> data = context . field_converter . post ( cls , data , context ) <EOL> return data <EOL> def export_loop ( cls , instance_or_dict , field_converter = None , role = None , raise_error_on_role = True , <EOL> export_level = None , app_data = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> context = Context . _make ( context ) <EOL> try : <EOL> context . initialized <EOL> except : <EOL> if type ( field_converter ) is types . FunctionType : <EOL> field_converter = BasicConverter ( field_converter ) <EOL> context . _setdefaults ( { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : field_converter , <EOL> '<STR_LIT>' : role , <EOL> '<STR_LIT>' : raise_error_on_role , <EOL> '<STR_LIT>' : export_level , <EOL> '<STR_LIT>' : app_data if app_data is not None else { } <EOL> } ) <EOL> instance_or_dict = context . field_converter . pre ( cls , instance_or_dict , context ) <EOL> if cls . _options . export_order : <EOL> data = OrderedDict ( ) <EOL> else : <EOL> data = { } <EOL> filter_func = cls . _options . roles . get ( context . role ) <EOL> if filter_func is None : <EOL> if context . role and context . raise_error_on_role : <EOL> error_msg = '<STR_LIT>' <EOL> raise ValueError ( error_msg % ( cls . __name__ , context . role ) ) <EOL> else : <EOL> filter_func = cls . _options . roles . get ( "<STR_LIT:default>" ) <EOL> _field_converter = context . field_converter <EOL> for field_name , field , value in atoms ( cls , instance_or_dict ) : <EOL> serialized_name = field . serialized_name or field_name <EOL> if filter_func is not None and filter_func ( field_name , value ) : <EOL> continue <EOL> _export_level = field . get_export_level ( context ) <EOL> if _export_level == DROP : <EOL> continue <EOL> elif value not in ( None , Undefined ) : <EOL> value = _field_converter ( field , value , context ) <EOL> if value is Undefined : <EOL> if _export_level <= DEFAULT : <EOL> continue <EOL> elif value is None : <EOL> if _export_level <= NOT_NONE : <EOL> continue <EOL> elif field . is_compound and len ( value ) == <NUM_LIT:0> : <EOL> if _export_level <= NONEMPTY : <EOL> continue <EOL> if value is Undefined : <EOL> value = None <EOL> data [ serialized_name ] = value <EOL> data = context . field_converter . post ( cls , data , context ) <EOL> return data <EOL> def atoms ( cls , instance_or_dict ) : <EOL> """<STR_LIT>""" <EOL> field_getter = serializable_getter = instance_or_dict . get <EOL> try : <EOL> field_getter = instance_or_dict . _data . get <EOL> except AttributeError : <EOL> pass <EOL> sequences = ( ( cls . _field_list , field_getter ) , <EOL> ( cls . _serializables . items ( ) , serializable_getter ) ) <EOL> for sequence , get in sequences : <EOL> for field_name , field in sequence : <EOL> yield ( field_name , field , get ( field_name , Undefined ) ) <EOL> @ str_compat <EOL> class Role ( collections . Set ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , function , fields ) : <EOL> self . function = function <EOL> self . fields = set ( fields ) <EOL> def _from_iterable ( self , iterable ) : <EOL> return Role ( self . function , iterable ) <EOL> def __contains__ ( self , value ) : <EOL> return value in self . fields <EOL> def __iter__ ( self ) : <EOL> return iter ( self . fields ) <EOL> def __len__ ( self ) : <EOL> return len ( self . fields ) <EOL> def __eq__ ( self , other ) : <EOL> return ( self . function . __name__ == other . function . __name__ and <EOL> self . fields == other . fields ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . function . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( "<STR_LIT>" % f for f in self . fields ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % str ( self ) <EOL> def __add__ ( self , other ) : <EOL> fields = self . fields . union ( other ) <EOL> return self . _from_iterable ( fields ) <EOL> def __sub__ ( self , other ) : <EOL> fields = self . fields . difference ( other ) <EOL> return self . _from_iterable ( fields ) <EOL> def __call__ ( self , name , value ) : <EOL> return self . function ( name , value , self . fields ) <EOL> @ staticmethod <EOL> def wholelist ( name , value , seq ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> @ staticmethod <EOL> def whitelist ( name , value , seq ) : <EOL> """<STR_LIT>""" <EOL> if seq is not None and len ( seq ) > <NUM_LIT:0> : <EOL> return name not in seq <EOL> return True <EOL> @ staticmethod <EOL> def blacklist ( name , value , seq ) : <EOL> """<STR_LIT>""" <EOL> if seq is not None and len ( seq ) > <NUM_LIT:0> : <EOL> return name in seq <EOL> return False <EOL> def wholelist ( * field_list ) : <EOL> """<STR_LIT>""" <EOL> return Role ( Role . wholelist , field_list ) <EOL> def whitelist ( * field_list ) : <EOL> """<STR_LIT>""" <EOL> return Role ( Role . whitelist , field_list ) <EOL> def blacklist ( * field_list ) : <EOL> """<STR_LIT>""" <EOL> return Role ( Role . blacklist , field_list ) <EOL> class FieldConverter ( object ) : <EOL> def __call__ ( self , field , value , context ) : <EOL> raise NotImplementedError <EOL> def pre ( self , model_class , instance_or_dict , context ) : <EOL> return instance_or_dict <EOL> def post ( self , model_class , data , context ) : <EOL> return data <EOL> class BasicConverter ( FieldConverter ) : <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> def __call__ ( self , * args ) : <EOL> return self . func ( * args ) <EOL> class ExportConverter ( FieldConverter ) : <EOL> def __init__ ( self , format , exceptions = None ) : <EOL> self . primary = format <EOL> self . secondary = not format <EOL> self . exceptions = set ( exceptions ) if exceptions else None <EOL> def __call__ ( self , field , value , context ) : <EOL> format = self . primary <EOL> if self . exceptions : <EOL> if any ( ( issubclass ( field . typeclass , cls ) for cls in self . exceptions ) ) : <EOL> format = self . secondary <EOL> return field . export ( value , format , context ) <EOL> class NativeConverter ( ExportConverter ) : <EOL> def __init__ ( self , exceptions = None ) : <EOL> ExportConverter . __init__ ( self , NATIVE , exceptions ) <EOL> def post ( self , model_class , data , context ) : <EOL> return model_class ( data , init = False ) <EOL> to_native_converter = NativeConverter ( ) <EOL> to_dict_converter = ExportConverter ( NATIVE ) <EOL> to_primitive_converter = ExportConverter ( PRIMITIVE ) <EOL> class ImportConverter ( FieldConverter ) : <EOL> def __init__ ( self , action ) : <EOL> self . action = action <EOL> self . method = operator . attrgetter ( self . action ) <EOL> def __call__ ( self , field , value , context ) : <EOL> field . check_required ( value , context ) <EOL> if value in ( None , Undefined ) : <EOL> return value <EOL> return self . method ( field ) ( value , context ) <EOL> def pre ( self , model_class , instance_or_dict , context ) : <EOL> return instance_or_dict <EOL> def post ( self , model_class , data , context ) : <EOL> return data <EOL> import_converter = ImportConverter ( '<STR_LIT>' ) <EOL> validation_converter = ImportConverter ( '<STR_LIT>' ) <EOL> def get_import_context ( field_converter = import_converter , ** options ) : <EOL> import_options = { <EOL> '<STR_LIT>' : field_converter , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:strict>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False <EOL> } <EOL> import_options . update ( options ) <EOL> return Context ( ** import_options ) <EOL> def get_export_context ( field_converter = to_native_converter , ** options ) : <EOL> export_options = { <EOL> '<STR_LIT>' : field_converter , <EOL> '<STR_LIT>' : None <EOL> } <EOL> export_options . update ( options ) <EOL> return Context ( ** export_options ) <EOL> def convert ( cls , instance_or_dict , ** kwargs ) : <EOL> return import_loop ( cls , instance_or_dict , import_converter , ** kwargs ) <EOL> def to_native ( cls , instance_or_dict , ** kwargs ) : <EOL> return export_loop ( cls , instance_or_dict , to_native_converter , ** kwargs ) <EOL> def to_dict ( cls , instance_or_dict , ** kwargs ) : <EOL> return export_loop ( cls , instance_or_dict , to_dict_converter , ** kwargs ) <EOL> def to_primitive ( cls , instance_or_dict , ** kwargs ) : <EOL> return export_loop ( cls , instance_or_dict , to_primitive_converter , ** kwargs ) <EOL> __all__ = module_exports ( __name__ ) </s>
