<s> import types <EOL> from django import template <EOL> from django . template . defaultfilters import mark_safe , force_escape , pluralize <EOL> from django . forms import Form <EOL> from django . db . models import Model <EOL> from django . db . models . query import QuerySet <EOL> from django . core import urlresolvers <EOL> """<STR_LIT>""" <EOL> MAX_DEPTH = <NUM_LIT:5> <EOL> def _type_helper ( o ) : <EOL> if isinstance ( o , Model ) : <EOL> return "<STR_LIT>" % o . _meta . app_label , o . _meta . object_name . lower ( ) <EOL> elif isinstance ( o , ( list , dict , set , QuerySet ) ) : <EOL> return "<STR_LIT>" % ( str ( type ( o ) ) , len ( o ) , pluralize ( len ( o ) ) ) <EOL> else : <EOL> return str ( type ( o ) ) <EOL> def _hash ( o , seen = None ) : <EOL> if seen is None : <EOL> seen = set ( ) <EOL> if id ( o ) in seen : <EOL> return "<STR_LIT>" % id ( o ) <EOL> else : <EOL> seen . add ( id ( o ) ) <EOL> print "<STR_LIT>" % o <EOL> try : <EOL> return hash ( o ) <EOL> except TypeError : <EOL> pass <EOL> if isinstance ( o , dict ) : <EOL> return ( '<STR_LIT>' , ( tuple ( ( _hash ( k , seen ) , _hash ( v , seen ) ) for k , v in sorted ( o . items ( ) ) ) ) ) <EOL> elif isinstance ( o , list ) : <EOL> return ( '<STR_LIT:list>' , tuple ( _hash ( v , seen ) for v in o ) ) <EOL> elif isinstance ( o , set ) : <EOL> return ( '<STR_LIT>' , tuple ( _hash ( v , seen ) for v in sorted ( o ) ) ) <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % o <EOL> def _context_helper ( path , o , depth , seen , result ) : <EOL> if depth > MAX_DEPTH : <EOL> result . append ( "<STR_LIT>" ) <EOL> return <EOL> h = _hash ( o ) <EOL> if h in seen : <EOL> result . append ( "<STR_LIT>" % ( force_escape ( "<STR_LIT:.>" . join ( path ) ) , ) ) <EOL> return <EOL> seen . add ( h ) <EOL> result . append ( "<STR_LIT>" % ( force_escape ( "<STR_LIT:.>" . join ( path ) ) , force_escape ( _type_helper ( o ) [ : <NUM_LIT:100> ] ) ) ) <EOL> if isinstance ( o , ( list , dict , set , tuple ) ) : <EOL> result . append ( "<STR_LIT>" ) <EOL> if isinstance ( o , dict ) : <EOL> iter_ = lambda o : o . items ( ) [ : <NUM_LIT:100> ] <EOL> elif isinstance ( o , tuple ) : <EOL> iter_ = lambda o : list ( enumerate ( o ) ) <EOL> else : <EOL> iter_ = lambda o : list ( enumerate ( o ) ) [ : <NUM_LIT:5> ] <EOL> for k , v in iter_ ( o ) : <EOL> _context_helper ( path + [ k ] , v , depth + <NUM_LIT:1> , seen , result ) <EOL> result . append ( "<STR_LIT>" ) <EOL> elif isinstance ( o , ( QuerySet , Model ) ) : <EOL> if isinstance ( o , Model ) : <EOL> meta = o . _meta <EOL> else : <EOL> meta = o . model . _meta <EOL> app_label , model_name = meta . app_label , meta . object_name . lower ( ) <EOL> url = urlresolvers . reverse ( '<STR_LIT>' , args = [ app_label , model_name ] ) <EOL> label = "<STR_LIT>" % ( app_label , model_name ) <EOL> result . append ( "<STR_LIT>" % ( url , label ) ) <EOL> elif isinstance ( o , Form ) : <EOL> result . append ( "<STR_LIT>" ) <EOL> for bound_field in o : <EOL> _context_helper ( path + [ bound_field . name ] , bound_field , depth + <NUM_LIT:1> , seen , result ) <EOL> result . append ( "<STR_LIT>" ) <EOL> else : <EOL> if hasattr ( o , '<STR_LIT>' ) : <EOL> val = unicode ( o ) <EOL> else : <EOL> val = repr ( o ) <EOL> if len ( val ) > <NUM_LIT:100> : <EOL> val = val [ : <NUM_LIT> ] + "<STR_LIT>" <EOL> result . append ( force_escape ( val ) ) <EOL> result . append ( "<STR_LIT>" ) <EOL> def context_helper ( context ) : <EOL> result = [ ] <EOL> seen = set ( ) <EOL> if not context : <EOL> result . append ( "<STR_LIT>" ) <EOL> else : <EOL> result . append ( "<STR_LIT>" ) <EOL> result . append ( "<STR_LIT>" ) <EOL> for k , v in context . items ( ) : <EOL> _context_helper ( [ k ] , context [ k ] , <NUM_LIT:0> , seen , result ) <EOL> result . append ( "<STR_LIT>" ) <EOL> return mark_safe ( "<STR_LIT:\n>" . join ( result ) ) </s>
