<s> import logging <EOL> import re <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . utils . functional import lazy <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . html import conditional_escape <EOL> from django . contrib . contenttypes . models import ContentType <EOL> class NotSet ( object ) : <EOL> "<STR_LIT>" <EOL> def __str__ ( self ) : return "<STR_LIT>" <EOL> def __repr__ ( self ) : return self . __str__ ( ) <EOL> NotSet = NotSet ( ) <EOL> class Literal ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> class LazyList ( list ) : <EOL> """<STR_LIT>""" <EOL> def populate ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def __init__ ( self , populate_function = None ) : <EOL> if populate_function is not None : <EOL> self . populate = populate_function <EOL> self . _populated = False <EOL> def _populate ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _populated : <EOL> logging . debug ( "<STR_LIT>" % ( id ( self ) , self . __class__ . __name__ ) ) <EOL> try : <EOL> self . populate ( ) <EOL> self . _populated = True <EOL> except Exception , e : <EOL> logging . debug ( "<STR_LIT>" % e ) <EOL> def __len__ ( self ) : <EOL> self . _populate ( ) <EOL> return super ( LazyList , self ) . __len__ ( ) <EOL> def __getitem__ ( self , key ) : <EOL> self . _populate ( ) <EOL> return super ( LazyList , self ) . __getitem__ ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _populate ( ) <EOL> return super ( LazyList , self ) . __setitem__ ( key , value ) <EOL> def __delitem__ ( self , key ) : <EOL> self . _populate ( ) <EOL> return super ( LazyList , self ) . __delitem__ ( key ) <EOL> def __iter__ ( self ) : <EOL> self . _populate ( ) <EOL> return super ( LazyList , self ) . __iter__ ( ) <EOL> def __contains__ ( self , item ) : <EOL> self . _populate ( ) <EOL> return super ( LazyList , self ) . __contains__ ( item ) <EOL> class LazyChoices ( LazyList ) : <EOL> """<STR_LIT>""" <EOL> def __nonzero__ ( self ) : <EOL> if not self . _populated : <EOL> return True <EOL> else : <EOL> return bool ( len ( self ) ) <EOL> from django . core . urlresolvers import RegexURLResolver , RegexURLPattern , Resolver404 , get_resolver <EOL> def _pattern_resolve_to_name ( pattern , path ) : <EOL> match = pattern . regex . search ( path ) <EOL> if match : <EOL> name = "<STR_LIT>" <EOL> if pattern . name : <EOL> name = pattern . name <EOL> elif hasattr ( pattern , '<STR_LIT>' ) : <EOL> name = pattern . _callback_str <EOL> else : <EOL> name = "<STR_LIT>" % ( pattern . callback . __module__ , pattern . callback . func_name ) <EOL> return name <EOL> def _resolver_resolve_to_name ( resolver , path ) : <EOL> tried = [ ] <EOL> match = resolver . regex . search ( path ) <EOL> if match : <EOL> new_path = path [ match . end ( ) : ] <EOL> for pattern in resolver . url_patterns : <EOL> try : <EOL> if isinstance ( pattern , RegexURLPattern ) : <EOL> name = _pattern_resolve_to_name ( pattern , new_path ) <EOL> elif isinstance ( pattern , RegexURLResolver ) : <EOL> name = _resolver_resolve_to_name ( pattern , new_path ) <EOL> except Resolver404 , e : <EOL> tried . extend ( [ ( pattern . regex . pattern + '<STR_LIT:U+0020>' + t ) for t in e . args [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] ) <EOL> else : <EOL> if name : <EOL> return name <EOL> tried . append ( pattern . regex . pattern ) <EOL> raise Resolver404 , { '<STR_LIT>' : tried , '<STR_LIT:path>' : new_path } <EOL> def resolve_to_name ( path , urlconf = None ) : <EOL> try : <EOL> return _resolver_resolve_to_name ( get_resolver ( urlconf ) , path ) <EOL> except Resolver404 : <EOL> return None <EOL> def _replace_quot ( match ) : <EOL> unescape = lambda v : v . replace ( '<STR_LIT>' , '<STR_LIT:">' ) . replace ( '<STR_LIT>' , '<STR_LIT:&>' ) <EOL> return u'<STR_LIT>' % ( unescape ( match . group ( <NUM_LIT:1> ) ) , unescape ( match . group ( <NUM_LIT:3> ) ) ) <EOL> def escape_tags ( value , valid_tags ) : <EOL> """<STR_LIT>""" <EOL> value = conditional_escape ( value ) <EOL> if valid_tags : <EOL> tag_re = re . compile ( r'<STR_LIT>' % u'<STR_LIT:|>' . join ( re . escape ( tag ) for tag in valid_tags ) ) <EOL> value = tag_re . sub ( _replace_quot , value ) <EOL> value = value . replace ( "<STR_LIT>" , "<STR_LIT>" ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return mark_safe ( value ) <EOL> def _get_seo_content_types ( seo_models ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return [ ContentType . objects . get_for_model ( m ) . id for m in seo_models ] <EOL> except : <EOL> return [ ] <EOL> def get_seo_content_types ( seo_models ) : <EOL> return lazy ( _get_seo_content_types , list ) ( seo_models ) </s>
