<s> from collections import defaultdict <EOL> import re <EOL> from flatland . util import ( <EOL> Unspecified , <EOL> assignable_class_property , <EOL> autodocument_from_superclasses , <EOL> class_cloner , <EOL> keyslice_pairs , <EOL> re_uescape , <EOL> to_pairs , <EOL> ) <EOL> from . base import Element , Unevaluated , Slot , validate_element <EOL> from . scalars import Scalar <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class Container ( Element ) : <EOL> """<STR_LIT>""" <EOL> validates_down = '<STR_LIT>' <EOL> validates_up = '<STR_LIT>' <EOL> descent_validators = ( ) <EOL> """<STR_LIT>""" <EOL> @ class_cloner <EOL> def descent_validated_by ( cls , * validators ) : <EOL> """<STR_LIT>""" <EOL> for validator in validators : <EOL> if isinstance ( validator , type ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> validator , validator ) ) <EOL> cls . descent_validators = list ( validators ) <EOL> return cls <EOL> @ class_cloner <EOL> def including_descent_validators ( cls , * validators , ** kw ) : <EOL> """<STR_LIT>""" <EOL> position = kw . pop ( '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> if kw : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> kw . popitem ( ) [ <NUM_LIT:0> ] ) ) <EOL> mutable = list ( cls . descent_validators ) <EOL> if position < <NUM_LIT:0> : <EOL> position = len ( mutable ) + <NUM_LIT:1> + position <EOL> mutable [ position : position ] = list ( validators ) <EOL> cls . descent_validators = mutable <EOL> return cls <EOL> def validate_element ( self , element , state , descending ) : <EOL> """<STR_LIT>""" <EOL> if descending : <EOL> if self . descent_validators : <EOL> return validate_element ( <EOL> element , state , self . descent_validators ) <EOL> else : <EOL> return None <EOL> else : <EOL> return validate_element ( element , state , self . validators ) <EOL> def _validate ( self , state , descending ) : <EOL> """<STR_LIT>""" <EOL> if descending : <EOL> if self . validates_down : <EOL> validators = getattr ( self , self . validates_down , None ) <EOL> if not validators : <EOL> return Unevaluated <EOL> return validate_element ( self , state , validators ) <EOL> else : <EOL> if self . validates_up : <EOL> validators = getattr ( self , self . validates_up , None ) <EOL> return validate_element ( self , state , validators ) <EOL> return Unevaluated <EOL> class Sequence ( Container , list ) : <EOL> """<STR_LIT>""" <EOL> member_schema = None <EOL> """<STR_LIT>""" <EOL> prune_empty = True <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value = Unspecified , ** kw ) : <EOL> Container . __init__ ( self , value , ** kw ) <EOL> if not self . member_schema : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( self ) ) <EOL> @ class_cloner <EOL> def of ( cls , * schema ) : <EOL> """<STR_LIT>""" <EOL> for field in schema : <EOL> if isinstance ( field , Element ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % field ) <EOL> if not schema : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif len ( schema ) == <NUM_LIT:1> : <EOL> cls . member_schema = schema [ <NUM_LIT:0> ] <EOL> else : <EOL> cls . member_schema = Dict . of ( * schema ) <EOL> return cls <EOL> def set ( self , iterable ) : <EOL> """<STR_LIT>""" <EOL> del self [ : ] <EOL> values , converted = [ ] , True <EOL> try : <EOL> for v in iterable : <EOL> el = self . member_schema ( ) <EOL> converted &= el . set ( v ) <EOL> values . append ( el ) <EOL> self . extend ( values ) <EOL> except TypeError : <EOL> return False <EOL> else : <EOL> return converted <EOL> def set_default ( self ) : <EOL> default = self . default_value <EOL> if default is not None and default is not Unspecified : <EOL> del self [ : ] <EOL> self . extend ( default ) <EOL> def _set_flat ( self , pairs , sep ) : <EOL> raise NotImplementedError ( ) <EOL> @ property <EOL> def children ( self ) : <EOL> return iter ( self ) <EOL> @ property <EOL> def is_empty ( self ) : <EOL> return not any ( True for _ in self . children ) <EOL> def _index ( self , name ) : <EOL> try : <EOL> idx = int ( name ) <EOL> except ValueError : <EOL> raise IndexError ( name ) <EOL> return self [ idx ] <EOL> def append ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , Element ) : <EOL> value = self . member_schema ( value = value ) <EOL> value . parent = self <EOL> list . append ( self , value ) <EOL> def extend ( self , iterable ) : <EOL> """<STR_LIT>""" <EOL> for value in iterable : <EOL> self . append ( value ) <EOL> def insert ( self , index , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , Element ) : <EOL> value = self . member_schema ( value = value ) <EOL> value . parent = self <EOL> list . insert ( self , index , value ) <EOL> def __setitem__ ( self , index , value ) : <EOL> if isinstance ( index , slice ) : <EOL> as_elements = [ ] <EOL> for item in value : <EOL> if not isinstance ( item , Element ) : <EOL> item = self . member_schema ( value = item ) <EOL> item . parent = self <EOL> as_elements . append ( item ) <EOL> value = as_elements <EOL> else : <EOL> if not isinstance ( value , Element ) : <EOL> value = self . member_schema ( value = value ) <EOL> value . parent = self <EOL> list . __setitem__ ( self , index , value ) <EOL> def __setslice__ ( self , i , j , value ) : <EOL> self . __setitem__ ( slice ( i , j ) , value ) <EOL> def remove ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , Element ) : <EOL> value = self . member_schema ( value = value ) <EOL> list . remove ( self , value ) <EOL> def index ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , Element ) : <EOL> value = self . member_schema ( value = value ) <EOL> return list . index ( self , value ) <EOL> def count ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , Element ) : <EOL> value = self . member_schema ( value = value ) <EOL> return list . count ( self , value ) <EOL> def __contains__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , Element ) : <EOL> value = self . member_schema ( value = value ) <EOL> return list . __contains__ ( self , value ) <EOL> @ property <EOL> def value ( self ) : <EOL> return list ( value . value for value in self . children ) <EOL> @ property <EOL> def u ( self ) : <EOL> return u'<STR_LIT>' % u'<STR_LIT:U+002CU+0020>' . join ( <EOL> element . u if isinstance ( element , Container ) <EOL> else repr ( element . u ) . decode ( '<STR_LIT>' ) <EOL> for element in self . children ) <EOL> class ListSlot ( Container , Slot ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , parent , element ) : <EOL> self . name = name <EOL> self . parent = parent <EOL> self . element = element <EOL> element . parent = self <EOL> @ property <EOL> def u ( self ) : <EOL> return self . element . u <EOL> @ property <EOL> def value ( self ) : <EOL> return self . element . value <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . name , self . element ) <EOL> class List ( Sequence ) : <EOL> """<STR_LIT>""" <EOL> slot_type = ListSlot <EOL> member_schema = ( ) <EOL> """<STR_LIT>""" <EOL> maximum_set_flat_members = <NUM_LIT> <EOL> """<STR_LIT>""" <EOL> def _as_element ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is Unspecified : <EOL> return self . member_schema ( ) <EOL> if isinstance ( value , Element ) : <EOL> return value <EOL> else : <EOL> return self . member_schema ( value ) <EOL> def _new_slot ( self , value = Unspecified ) : <EOL> """<STR_LIT>""" <EOL> return self . slot_type ( name = str ( len ( self ) ) . decode ( '<STR_LIT:ascii>' ) , <EOL> parent = self , <EOL> element = self . _as_element ( value ) ) <EOL> @ property <EOL> def _slots ( self ) : <EOL> """<STR_LIT>""" <EOL> return list . __iter__ ( self ) <EOL> def append ( self , value ) : <EOL> list . append ( self , self . _new_slot ( value ) ) <EOL> def extend ( self , iterable ) : <EOL> for v in iterable : <EOL> self . append ( v ) <EOL> def __getitem__ ( self , index ) : <EOL> if isinstance ( index , slice ) : <EOL> return [ item . element for item in list . __getitem__ ( self , index ) ] <EOL> return list . __getitem__ ( self , index ) . element <EOL> def __getslice__ ( self , i , j ) : <EOL> return self . __getitem__ ( slice ( i , j ) ) <EOL> def __setitem__ ( self , index , value ) : <EOL> if isinstance ( index , slice ) : <EOL> value = [ self . _new_slot ( item ) for item in value ] <EOL> list . __setitem__ ( self , index , value ) <EOL> self . _renumber ( ) <EOL> else : <EOL> slot = self [ index ] <EOL> slot . set ( value ) <EOL> def __setslice__ ( self , i , j , sequence ) : <EOL> return self . __setitem__ ( slice ( i , j ) , sequence ) <EOL> def __iter__ ( self ) : <EOL> for i in list . __iter__ ( self ) : <EOL> yield i . element <EOL> def __delitem__ ( self , index ) : <EOL> list . __delitem__ ( self , index ) <EOL> self . _renumber ( ) <EOL> def __delslice__ ( self , i , j ) : <EOL> return self . __delitem__ ( slice ( i , j ) ) <EOL> def pop ( self , index = - <NUM_LIT:1> ) : <EOL> value = list . pop ( self , index ) <EOL> self . _renumber ( ) <EOL> value . parent = None <EOL> return value <EOL> def insert ( self , index , value ) : <EOL> list . insert ( self , index , self . _new_slot ( value ) ) <EOL> self . _renumber ( ) <EOL> def remove ( self , value ) : <EOL> list . remove ( self , self . _as_element ( value ) ) <EOL> self . _renumber ( ) <EOL> def sort ( self , cmp = None , key = None , reverse = False ) : <EOL> list . sort ( self , cmp , key , reverse ) <EOL> self . _renumber ( ) <EOL> def reverse ( self ) : <EOL> list . reverse ( self ) <EOL> self . _renumber ( ) <EOL> def _renumber ( self ) : <EOL> for idx , slot in enumerate ( self . _slots ) : <EOL> slot . name = str ( idx ) . decode ( '<STR_LIT:ascii>' ) <EOL> @ property <EOL> def children ( self ) : <EOL> return iter ( child . element for child in self . _slots ) <EOL> def _set_flat ( self , pairs , sep ) : <EOL> del self [ : ] <EOL> if not pairs : <EOL> return <EOL> if self . name : <EOL> regex = re . compile ( ur '<STR_LIT>' % ( <EOL> re_uescape ( self . name + sep ) , re_uescape ( sep ) ) , re . UNICODE ) <EOL> else : <EOL> regex = re . compile ( ur '<STR_LIT>' % ( <EOL> re_uescape ( sep ) ) , re . UNICODE ) <EOL> indexes = defaultdict ( list ) <EOL> prune = self . prune_empty <EOL> for key , value in pairs : <EOL> if value == u'<STR_LIT>' and prune : <EOL> continue <EOL> m = regex . match ( key ) <EOL> if not m : <EOL> continue <EOL> try : <EOL> index = long ( m . group ( <NUM_LIT:1> ) ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> child_key = key [ len ( m . group ( <NUM_LIT:0> ) ) : ] or None <EOL> indexes [ index ] . append ( ( child_key , value ) ) <EOL> if not indexes : <EOL> return <EOL> if prune : <EOL> for offset , index in enumerate ( sorted ( indexes ) ) : <EOL> if offset == self . maximum_set_flat_members : <EOL> break <EOL> slot = self . _new_slot ( ) <EOL> list . append ( self , slot ) <EOL> slot . element . set_flat ( indexes [ index ] , sep ) <EOL> else : <EOL> max_index = min ( max ( indexes ) + <NUM_LIT:1> , self . maximum_set_flat_members ) <EOL> for index in xrange ( <NUM_LIT:0> , max_index ) : <EOL> slot = self . _new_slot ( ) <EOL> list . append ( self , slot ) <EOL> flat = indexes . get ( index , None ) <EOL> if flat : <EOL> slot . element . set_flat ( flat , sep ) <EOL> def set_default ( self ) : <EOL> """<STR_LIT>""" <EOL> default = self . default_value <EOL> if default is None or default is Unspecified : <EOL> return <EOL> del self [ : ] <EOL> if isinstance ( default , int ) : <EOL> for _ in xrange ( <NUM_LIT:0> , default ) : <EOL> slot = self . _new_slot ( ) <EOL> list . append ( self , slot ) <EOL> slot . element . set_default ( ) <EOL> else : <EOL> self . set ( default ) <EOL> def __repr__ ( self ) : <EOL> return repr ( list ( self ) ) <EOL> class Array ( Sequence ) : <EOL> """<STR_LIT>""" <EOL> prune_empty = True <EOL> flattenable = False <EOL> def _set_flat ( self , pairs , sep ) : <EOL> del self [ : ] <EOL> prune = self . prune_empty <EOL> child_name = self . member_schema . name <EOL> assert not issubclass ( self . member_schema , Container ) , "<STR_LIT>" <EOL> if not self . name : <EOL> child_prefix = child_name or u'<STR_LIT>' <EOL> for key , value in pairs : <EOL> if prune and value == u'<STR_LIT>' and key == child_prefix : <EOL> continue <EOL> if key == u'<STR_LIT>' : <EOL> key = None <EOL> if child_name and key != child_name : <EOL> continue <EOL> member = self . member_schema . from_flat ( [ ( key , value ) ] ) <EOL> self . append ( member ) <EOL> else : <EOL> regex = re . compile ( ur '<STR_LIT>' % ( <EOL> re_uescape ( self . name ) , re_uescape ( sep ) ) , re . UNICODE ) <EOL> for key , value in pairs : <EOL> m = regex . match ( key ) <EOL> if not m : <EOL> continue <EOL> remainder = key [ m . end ( ) : ] or None <EOL> if child_name and not remainder : <EOL> continue <EOL> elif prune and value == u'<STR_LIT>' and remainder == child_name : <EOL> continue <EOL> member = self . member_schema . from_flat ( [ ( remainder , value ) ] ) <EOL> self . append ( member ) <EOL> class MultiValue ( Array , Scalar ) : <EOL> """<STR_LIT>""" <EOL> def u ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self : <EOL> return u'<STR_LIT>' <EOL> else : <EOL> return self [ <NUM_LIT:0> ] . u <EOL> def _set_u ( self , value ) : <EOL> if not self : <EOL> self . append ( None ) <EOL> self [ <NUM_LIT:0> ] . u = value <EOL> u = property ( u , _set_u ) <EOL> del _set_u <EOL> def value ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self : <EOL> return None <EOL> else : <EOL> return self [ <NUM_LIT:0> ] . value <EOL> def _set_value ( self , value ) : <EOL> if not self : <EOL> self . append ( None ) <EOL> self [ <NUM_LIT:0> ] . value = value <EOL> value = property ( value , _set_value ) <EOL> del _set_value <EOL> def __nonzero__ ( self ) : <EOL> return len ( self ) <EOL> class Mapping ( Container , dict ) : <EOL> """<STR_LIT>""" <EOL> field_schema = ( ) <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value = Unspecified , ** kw ) : <EOL> Container . __init__ ( self , ** kw ) <EOL> if not self . field_schema : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> type ( self ) . __name__ ) ) <EOL> self . _reset ( ) <EOL> if value is not Unspecified : <EOL> self . set ( value ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if not key in self : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( key , type ( self ) . __name__ , self . name ) ) <EOL> self [ key ] . set ( value ) <EOL> def __delitem__ ( self , key ) : <EOL> if key not in self : <EOL> raise KeyError ( key ) <EOL> raise TypeError ( '<STR_LIT>' % type ( self ) . __name__ ) <EOL> def may_contain ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return key in self <EOL> def clear ( self ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( self ) . __name__ ) <EOL> def _reset ( self ) : <EOL> """<STR_LIT>""" <EOL> for member_schema in self . field_schema : <EOL> key = member_schema . name <EOL> dict . __setitem__ ( <EOL> self , key , member_schema ( parent = self ) ) <EOL> def popitem ( self ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( self ) . __name__ ) <EOL> def pop ( self , key ) : <EOL> if key not in self : <EOL> raise KeyError ( key ) <EOL> raise TypeError ( '<STR_LIT>' % type ( self ) . __name__ ) <EOL> def update ( self , * dictish , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( dictish ) > <NUM_LIT:1> : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % len ( dictish ) ) <EOL> elif dictish : <EOL> for key , value in to_pairs ( dictish [ <NUM_LIT:0> ] ) : <EOL> self [ key ] = value <EOL> for key , value in kwargs . iteritems ( ) : <EOL> self [ key ] = value <EOL> def setdefault ( self , key , default = None ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( self ) . __name__ ) <EOL> def get ( self , key , default = None ) : <EOL> if key not in self : <EOL> raise KeyError ( <EOL> '<STR_LIT>' % ( <EOL> type ( self ) . __name__ , self . name , key ) ) <EOL> return self [ key ] <EOL> @ property <EOL> def children ( self ) : <EOL> return self . itervalues ( ) <EOL> def set ( self , value ) : <EOL> """<STR_LIT>""" <EOL> pairs = to_pairs ( value ) <EOL> self . _reset ( ) <EOL> seen = set ( ) <EOL> converted = True <EOL> for key , value in pairs : <EOL> if not self . may_contain ( key ) : <EOL> raise KeyError ( <EOL> '<STR_LIT>' % ( <EOL> type ( self ) . __name__ , self . name , key ) ) <EOL> converted &= self [ key ] . set ( value ) <EOL> seen . add ( key ) <EOL> required = set ( self . iterkeys ( ) ) <EOL> if seen != required : <EOL> missing = required - seen <EOL> raise TypeError ( <EOL> '<STR_LIT>' % ( <EOL> '<STR_LIT:U+002C>' . join ( repr ( key ) for key in missing ) ) ) <EOL> return converted <EOL> def _set_flat ( self , pairs , sep ) : <EOL> if self . name is None : <EOL> possibles = pairs <EOL> else : <EOL> possibles = [ ] <EOL> prefix = self . name + sep <EOL> plen = len ( prefix ) <EOL> for key , value in pairs : <EOL> if key == prefix : <EOL> pass <EOL> if key . startswith ( prefix ) : <EOL> possibles . append ( ( key [ plen : ] , value ) ) <EOL> if not possibles : <EOL> return <EOL> for schema in self . field_schema : <EOL> field = schema . name <EOL> accum = [ ] <EOL> for key , value in possibles : <EOL> if key . startswith ( field ) : <EOL> accum . append ( ( key , value ) ) <EOL> if accum : <EOL> if dict . __contains__ ( self , field ) : <EOL> self [ field ] . set_flat ( accum , sep ) <EOL> else : <EOL> self [ field ] = schema ( ) <EOL> self [ field ] . set_flat ( accum , sep = sep ) <EOL> def set_default ( self ) : <EOL> default = self . default_value <EOL> if default is not None and default is not Unspecified : <EOL> self . set ( default ) <EOL> else : <EOL> for child in self . children : <EOL> child . set_default ( ) <EOL> def _index ( self , name ) : <EOL> return self [ name ] <EOL> @ property <EOL> def u ( self ) : <EOL> """<STR_LIT>""" <EOL> pairs = ( ( key , value . u if isinstance ( value , Container ) <EOL> else repr ( value . u ) . decode ( '<STR_LIT>' ) ) <EOL> for key , value in self . iteritems ( ) ) <EOL> return u'<STR_LIT>' % u'<STR_LIT:U+002CU+0020>' . join ( <EOL> u"<STR_LIT>" % ( repr ( k ) . decode ( '<STR_LIT>' ) , v ) <EOL> for k , v in pairs ) <EOL> @ property <EOL> def value ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( key , value . value ) for key , value in self . iteritems ( ) ) <EOL> @ property <EOL> def is_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> @ assignable_class_property <EOL> def field_schema_mapping ( instance , cls ) : <EOL> """<STR_LIT>""" <EOL> if instance is not None : <EOL> field_schema = instance . field_schema <EOL> else : <EOL> field_schema = cls . field_schema <EOL> return dict ( ( schema . name , schema ) for schema in field_schema ) <EOL> def _field_schema_for ( self , key ) : <EOL> """<STR_LIT>""" <EOL> for schema in self . field_schema : <EOL> if schema . name == key : <EOL> return schema <EOL> return None <EOL> class Dict ( Mapping , dict ) : <EOL> """<STR_LIT>""" <EOL> policy = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> @ class_cloner <EOL> def of ( cls , * fields ) : <EOL> """<STR_LIT>""" <EOL> for field in fields : <EOL> if isinstance ( field , Element ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % field ) <EOL> unique_names = set ( field . name for field in fields ) <EOL> if len ( unique_names ) != len ( fields ) : <EOL> names = [ field . name for field in fields ] <EOL> dupes = [ name for name in unique_names if names . count ( name ) > <NUM_LIT:1> ] <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> cls . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( repr ( f ) for f in dupes ) ) ) <EOL> cls . field_schema = fields <EOL> return cls <EOL> @ classmethod <EOL> def from_object ( cls , obj , include = None , omit = None , rename = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self = cls ( ** kw ) <EOL> self . set_by_object ( obj = obj , include = include , omit = omit , rename = rename ) <EOL> return self <EOL> def set ( self , value , policy = None ) : <EOL> """<STR_LIT>""" <EOL> pairs = to_pairs ( value ) <EOL> self . _reset ( ) <EOL> if policy is not None : <EOL> assert policy in ( '<STR_LIT:strict>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> policy = self . policy <EOL> fields = self . field_schema_mapping <EOL> seen = set ( ) <EOL> converted = True <EOL> for key , value in pairs : <EOL> if key not in fields : <EOL> if policy != '<STR_LIT>' : <EOL> raise KeyError ( <EOL> '<STR_LIT>' % ( <EOL> self . name , key ) ) <EOL> continue <EOL> if dict . __contains__ ( self , key ) : <EOL> converted &= self [ key ] . set ( value ) <EOL> else : <EOL> self [ key ] = el = fields [ key ] ( ) <EOL> converted &= el . set ( value ) <EOL> seen . add ( key ) <EOL> if policy == '<STR_LIT:strict>' : <EOL> required = set ( fields . iterkeys ( ) ) <EOL> if seen != required : <EOL> missing = required - seen <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> '<STR_LIT:U+002C>' . join ( repr ( key ) for key in missing ) ) ) <EOL> return converted <EOL> def set_by_object ( self , obj , include = None , omit = None , rename = None ) : <EOL> """<STR_LIT>""" <EOL> fields = set ( self . iterkeys ( ) ) <EOL> attributes = fields . copy ( ) <EOL> if rename : <EOL> rename = list ( to_pairs ( rename ) ) <EOL> attributes . update ( key for key , value in rename <EOL> if value in attributes ) <EOL> if omit : <EOL> omit = list ( omit ) <EOL> attributes . difference_update ( omit ) <EOL> possible = ( ( attr , getattr ( obj , attr ) ) <EOL> for attr in attributes <EOL> if hasattr ( obj , attr ) ) <EOL> sliced = keyslice_pairs ( possible , include = include , <EOL> omit = omit , rename = rename ) <EOL> final = dict ( ( key , value ) <EOL> for key , value in sliced <EOL> if key in fields ) <EOL> self . set ( final ) <EOL> def update_object ( self , obj , include = None , omit = None , rename = None , <EOL> key = str ) : <EOL> """<STR_LIT>""" <EOL> data = self . slice ( include = include , omit = omit , rename = rename , key = key ) <EOL> for attribute , value in data . iteritems ( ) : <EOL> setattr ( obj , attribute , value ) <EOL> def slice ( self , include = None , omit = None , rename = None , key = None ) : <EOL> """<STR_LIT>""" <EOL> return dict ( <EOL> keyslice_pairs ( <EOL> ( ( key , element . value ) for key , element in self . iteritems ( ) ) , <EOL> include = include , omit = omit , rename = rename , key = key ) ) <EOL> class SparseDict ( Dict ) : <EOL> """<STR_LIT>""" <EOL> minimum_fields = None <EOL> def may_contain ( self , key ) : <EOL> return key in self or self . _field_schema_for ( key ) is not None <EOL> def _reset ( self ) : <EOL> dict . clear ( self ) <EOL> for member_schema in self . field_schema : <EOL> key = member_schema . name <EOL> if self . minimum_fields is None or member_schema . optional : <EOL> continue <EOL> dict . __setitem__ ( <EOL> self , key , member_schema ( parent = self ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> schema = self . _field_schema_for ( key ) <EOL> if not dict . __contains__ ( self , key ) : <EOL> if schema is None : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( key , type ( self ) . __name__ , self . name ) ) <EOL> elif isinstance ( value , schema ) : <EOL> dict . __setitem__ ( self , key , value ) <EOL> return <EOL> dict . __setitem__ ( self , key , schema ( value ) ) <EOL> elif isinstance ( value , schema ) : <EOL> value . parent = self <EOL> dict . __setitem__ ( self , key , value ) <EOL> else : <EOL> self [ key ] . set ( value ) <EOL> def __delitem__ ( self , key ) : <EOL> if self . minimum_fields is None : <EOL> try : <EOL> dict . __delitem__ ( self , key ) <EOL> return <EOL> except KeyError : <EOL> if not self . may_contain ( key ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % <EOL> ( key , type ( self ) . __name__ , self . name ) ) <EOL> raise <EOL> if key in self : <EOL> optional = self [ key ] . optional <EOL> else : <EOL> schema = self . _field_schema_for ( key ) <EOL> if schema is None : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % <EOL> ( key , type ( self ) . __name__ , self . name ) ) <EOL> optional = schema . optional <EOL> if not optional : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( key , type ( self ) . __name__ , self . name ) ) <EOL> dict . __delitem__ ( self , key ) <EOL> def clear ( self ) : <EOL> self . _reset ( ) <EOL> def popitem ( self ) : <EOL> raise NotImplementedError <EOL> def pop ( self , key ) : <EOL> if key not in self : <EOL> raise KeyError ( key ) <EOL> if self . minimum_fields == '<STR_LIT>' and not self [ key ] . optional : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( key , type ( self ) . __name__ , self . name ) ) <EOL> return dict . pop ( self , key ) <EOL> def setdefault ( self , key , default = None ) : <EOL> if not self . may_contain ( key ) : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( key , type ( self ) . __name__ , self . name ) ) <EOL> if key in self : <EOL> child = self [ key ] <EOL> if not child . is_empty : <EOL> return child . value <EOL> else : <EOL> self [ key ] = child = self . field_schema_mapping [ key ] ( ) <EOL> child . set ( default ) <EOL> return default <EOL> @ property <EOL> def is_empty ( self ) : <EOL> for _ in self . iterkeys ( ) : <EOL> return False <EOL> return True <EOL> def set_default ( self ) : <EOL> default = self . default_value <EOL> if default is not None and default is not Unspecified : <EOL> self . set ( default ) <EOL> elif self . minimum_fields is None : <EOL> self . _reset ( ) <EOL> elif self . minimum_fields == '<STR_LIT>' : <EOL> self . _reset ( ) <EOL> for schema in self . field_schema : <EOL> if schema . optional : <EOL> continue <EOL> self [ schema . name ] = schema . from_defaults ( ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . minimum_fields , ) ) <EOL> for cls_name in __all__ : <EOL> autodocument_from_superclasses ( globals ( ) [ cls_name ] ) <EOL> del cls_name </s>
