<s> import types <EOL> import urllib <EOL> import locale <EOL> import datetime <EOL> import codecs <EOL> from decimal import Decimal <EOL> from django . utils . functional import Promise <EOL> class DjangoUnicodeDecodeError ( UnicodeDecodeError ) : <EOL> def __init__ ( self , obj , * args ) : <EOL> self . obj = obj <EOL> UnicodeDecodeError . __init__ ( self , * args ) <EOL> def __str__ ( self ) : <EOL> original = UnicodeDecodeError . __str__ ( self ) <EOL> return '<STR_LIT>' % ( original , self . obj , <EOL> type ( self . obj ) ) <EOL> class StrAndUnicode ( object ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def smart_unicode ( s , encoding = '<STR_LIT:utf-8>' , strings_only = False , errors = '<STR_LIT:strict>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , Promise ) : <EOL> return s <EOL> return force_unicode ( s , encoding , strings_only , errors ) <EOL> def is_protected_type ( obj ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( obj , ( <EOL> types . NoneType , <EOL> int , long , <EOL> datetime . datetime , datetime . date , datetime . time , <EOL> float , Decimal ) <EOL> ) <EOL> def force_unicode ( s , encoding = '<STR_LIT:utf-8>' , strings_only = False , errors = '<STR_LIT:strict>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , unicode ) : <EOL> return s <EOL> if strings_only and is_protected_type ( s ) : <EOL> return s <EOL> try : <EOL> if not isinstance ( s , basestring , ) : <EOL> if hasattr ( s , '<STR_LIT>' ) : <EOL> s = unicode ( s ) <EOL> else : <EOL> try : <EOL> s = unicode ( str ( s ) , encoding , errors ) <EOL> except UnicodeEncodeError : <EOL> if not isinstance ( s , Exception ) : <EOL> raise <EOL> s = u'<STR_LIT:U+0020>' . join ( [ force_unicode ( arg , encoding , strings_only , <EOL> errors ) for arg in s ] ) <EOL> elif not isinstance ( s , unicode ) : <EOL> s = s . decode ( encoding , errors ) <EOL> except UnicodeDecodeError , e : <EOL> if not isinstance ( s , Exception ) : <EOL> raise DjangoUnicodeDecodeError ( s , * e . args ) <EOL> else : <EOL> s = u'<STR_LIT:U+0020>' . join ( [ force_unicode ( arg , encoding , strings_only , <EOL> errors ) for arg in s ] ) <EOL> return s <EOL> def smart_str ( s , encoding = '<STR_LIT:utf-8>' , strings_only = False , errors = '<STR_LIT:strict>' ) : <EOL> """<STR_LIT>""" <EOL> if strings_only and isinstance ( s , ( types . NoneType , int ) ) : <EOL> return s <EOL> if isinstance ( s , Promise ) : <EOL> return unicode ( s ) . encode ( encoding , errors ) <EOL> elif not isinstance ( s , basestring ) : <EOL> try : <EOL> return str ( s ) <EOL> except UnicodeEncodeError : <EOL> if isinstance ( s , Exception ) : <EOL> return '<STR_LIT:U+0020>' . join ( [ smart_str ( arg , encoding , strings_only , <EOL> errors ) for arg in s ] ) <EOL> return unicode ( s ) . encode ( encoding , errors ) <EOL> elif isinstance ( s , unicode ) : <EOL> return s . encode ( encoding , errors ) <EOL> elif s and encoding != '<STR_LIT:utf-8>' : <EOL> return s . decode ( '<STR_LIT:utf-8>' , errors ) . encode ( encoding , errors ) <EOL> else : <EOL> return s <EOL> def iri_to_uri ( iri ) : <EOL> """<STR_LIT>""" <EOL> if iri is None : <EOL> return iri <EOL> return urllib . quote ( smart_str ( iri ) , safe = "<STR_LIT>" ) <EOL> def filepath_to_uri ( path ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> return path <EOL> return urllib . quote ( smart_str ( path ) . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) , safe = "<STR_LIT>" ) <EOL> try : <EOL> DEFAULT_LOCALE_ENCODING = locale . getdefaultlocale ( ) [ <NUM_LIT:1> ] or '<STR_LIT:ascii>' <EOL> codecs . lookup ( DEFAULT_LOCALE_ENCODING ) <EOL> except : <EOL> DEFAULT_LOCALE_ENCODING = '<STR_LIT:ascii>' <EOL> def python_2_unicode_compatible ( klass ) : <EOL> klass . __unicode__ = klass . __str__ <EOL> klass . __str__ = lambda self : self . __unicode__ ( ) . encode ( '<STR_LIT:utf-8>' ) <EOL> return klass <EOL> smart_text = smart_unicode <EOL> force_text = force_unicode <EOL> smart_bytes = smart_str </s>
