<s> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> from django . utils import six <EOL> from rest_framework . serializers import ListSerializer <EOL> from rest_framework . utils . serializer_helpers import ReturnDict <EOL> from dynamic_rest . conf import settings <EOL> from dynamic_rest . tagged import TaggedDict <EOL> class SideloadingProcessor ( object ) : <EOL> """<STR_LIT>""" <EOL> prefix = settings . ADDITIONAL_PRIMARY_RESOURCE_PREFIX <EOL> def __init__ ( self , serializer , data ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( serializer , ListSerializer ) : <EOL> serializer = serializer . child <EOL> self . data = { } <EOL> self . seen = defaultdict ( set ) <EOL> self . plural_name = serializer . get_plural_name ( ) <EOL> self . name = serializer . get_name ( ) <EOL> self . process ( data ) <EOL> resource_name = self . name if isinstance ( <EOL> data , <EOL> dict <EOL> ) else self . plural_name <EOL> self . data [ resource_name ] = data <EOL> def is_dynamic ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( data , TaggedDict ) <EOL> def process ( self , obj , parent = None , parent_key = None , depth = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , list ) : <EOL> for key , o in enumerate ( obj ) : <EOL> self . process ( o , parent = obj , parent_key = key , depth = depth ) <EOL> elif isinstance ( obj , dict ) : <EOL> dynamic = self . is_dynamic ( obj ) <EOL> returned = isinstance ( obj , ReturnDict ) <EOL> if dynamic or returned : <EOL> for key , o in six . iteritems ( obj ) : <EOL> if isinstance ( o , list ) or isinstance ( o , dict ) : <EOL> self . process ( <EOL> o , <EOL> parent = obj , <EOL> parent_key = key , <EOL> depth = depth + <EOL> <NUM_LIT:1> <EOL> ) <EOL> if not dynamic or getattr ( obj , '<STR_LIT>' , False ) : <EOL> return <EOL> name = obj . serializer . get_plural_name ( ) <EOL> pk = obj . pk_value or obj . instance . pk <EOL> pk_key = repr ( pk ) <EOL> seen = True <EOL> if pk_key not in self . seen [ name ] : <EOL> seen = False <EOL> self . seen [ name ] . add ( pk_key ) <EOL> if depth == <NUM_LIT:0> : <EOL> return <EOL> if name == self . plural_name : <EOL> name = '<STR_LIT>' % ( <EOL> self . prefix , <EOL> name <EOL> ) <EOL> if not seen : <EOL> if name not in self . data : <EOL> self . data [ name ] = [ ] <EOL> self . data [ name ] . append ( obj ) <EOL> if parent is not None and parent_key is not None : <EOL> parent [ parent_key ] = pk </s>
