<s> """<STR_LIT>""" <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:utf8>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> import re , sys , time , threading , itertools , traceback , os <EOL> try : <EOL> import subprocess <EOL> except ImportError : <EOL> subprocess = None <EOL> try : import datetime <EOL> except ImportError : pass <EOL> try : set <EOL> except NameError : <EOL> from sets import Set as set <EOL> class Storage ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __getattr__ ( self , key ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError , k : <EOL> raise AttributeError , k <EOL> def __setattr__ ( self , key , value ) : <EOL> self [ key ] = value <EOL> def __delattr__ ( self , key ) : <EOL> try : <EOL> del self [ key ] <EOL> except KeyError , k : <EOL> raise AttributeError , k <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' + dict . __repr__ ( self ) + '<STR_LIT:>>' <EOL> storage = Storage <EOL> def storify ( mapping , * requireds , ** defaults ) : <EOL> """<STR_LIT>""" <EOL> _unicode = defaults . pop ( '<STR_LIT>' , False ) <EOL> def unicodify ( s ) : <EOL> if _unicode and isinstance ( s , str ) : return safeunicode ( s ) <EOL> else : return s <EOL> def getvalue ( x ) : <EOL> if hasattr ( x , '<STR_LIT:file>' ) and hasattr ( x , '<STR_LIT:value>' ) : <EOL> return x . value <EOL> elif hasattr ( x , '<STR_LIT:value>' ) : <EOL> return unicodify ( x . value ) <EOL> else : <EOL> return unicodify ( x ) <EOL> stor = Storage ( ) <EOL> for key in requireds + tuple ( mapping . keys ( ) ) : <EOL> value = mapping [ key ] <EOL> if isinstance ( value , list ) : <EOL> if isinstance ( defaults . get ( key ) , list ) : <EOL> value = [ getvalue ( x ) for x in value ] <EOL> else : <EOL> value = value [ - <NUM_LIT:1> ] <EOL> if not isinstance ( defaults . get ( key ) , dict ) : <EOL> value = getvalue ( value ) <EOL> if isinstance ( defaults . get ( key ) , list ) and not isinstance ( value , list ) : <EOL> value = [ value ] <EOL> setattr ( stor , key , value ) <EOL> for ( key , value ) in defaults . iteritems ( ) : <EOL> result = value <EOL> if hasattr ( stor , key ) : <EOL> result = stor [ key ] <EOL> if value == ( ) and not isinstance ( result , tuple ) : <EOL> result = ( result , ) <EOL> setattr ( stor , key , result ) <EOL> return stor <EOL> class Counter ( storage ) : <EOL> """<STR_LIT>""" <EOL> def add ( self , n ) : <EOL> self . setdefault ( n , <NUM_LIT:0> ) <EOL> self [ n ] += <NUM_LIT:1> <EOL> def most ( self ) : <EOL> """<STR_LIT>""" <EOL> m = max ( self . itervalues ( ) ) <EOL> return [ k for k , v in self . iteritems ( ) if v == m ] <EOL> def least ( self ) : <EOL> """<STR_LIT>""" <EOL> m = min ( self . itervalues ( ) ) <EOL> return [ k for k , v in self . iteritems ( ) if v == m ] <EOL> def percent ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return float ( self [ key ] ) / sum ( self . values ( ) ) <EOL> def sorted_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return sorted ( self . keys ( ) , key = lambda k : self [ k ] , reverse = True ) <EOL> def sorted_values ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self [ k ] for k in self . sorted_keys ( ) ] <EOL> def sorted_items ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ( k , self [ k ] ) for k in self . sorted_keys ( ) ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' + dict . __repr__ ( self ) + '<STR_LIT:>>' <EOL> counter = Counter <EOL> iters = [ list , tuple ] <EOL> import __builtin__ <EOL> if hasattr ( __builtin__ , '<STR_LIT>' ) : <EOL> iters . append ( set ) <EOL> if hasattr ( __builtin__ , '<STR_LIT>' ) : <EOL> iters . append ( set ) <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:6> ) : <EOL> try : <EOL> from sets import Set <EOL> iters . append ( Set ) <EOL> except ImportError : <EOL> pass <EOL> class _hack ( tuple ) : pass <EOL> iters = _hack ( iters ) <EOL> iters . __doc__ = """<STR_LIT>""" <EOL> def _strips ( direction , text , remove ) : <EOL> if direction == '<STR_LIT:l>' : <EOL> if text . startswith ( remove ) : <EOL> return text [ len ( remove ) : ] <EOL> elif direction == '<STR_LIT:r>' : <EOL> if text . endswith ( remove ) : <EOL> return text [ : - len ( remove ) ] <EOL> else : <EOL> raise ValueError , "<STR_LIT>" <EOL> return text <EOL> def rstrips ( text , remove ) : <EOL> """<STR_LIT>""" <EOL> return _strips ( '<STR_LIT:r>' , text , remove ) <EOL> def lstrips ( text , remove ) : <EOL> """<STR_LIT>""" <EOL> return _strips ( '<STR_LIT:l>' , text , remove ) <EOL> def strips ( text , remove ) : <EOL> """<STR_LIT>""" <EOL> return rstrips ( lstrips ( text , remove ) , remove ) <EOL> def safeunicode ( obj , encoding = '<STR_LIT:utf-8>' ) : <EOL> r"""<STR_LIT>""" <EOL> t = type ( obj ) <EOL> if t is unicode : <EOL> return obj <EOL> elif t is str : <EOL> return obj . decode ( encoding ) <EOL> elif t in [ int , float , bool ] : <EOL> return unicode ( obj ) <EOL> else : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return unicode ( obj ) <EOL> else : <EOL> return str ( obj ) . decode ( encoding ) <EOL> def safestr ( obj , encoding = '<STR_LIT:utf-8>' ) : <EOL> r"""<STR_LIT>""" <EOL> if isinstance ( obj , unicode ) : <EOL> return obj . encode ( '<STR_LIT:utf-8>' ) <EOL> elif isinstance ( obj , str ) : <EOL> return obj <EOL> elif hasattr ( obj , '<STR_LIT>' ) and hasattr ( obj , '<STR_LIT>' ) : <EOL> return itertools . imap ( safestr , obj ) <EOL> else : <EOL> return str ( obj ) <EOL> utf8 = safestr <EOL> class TimeoutError ( Exception ) : pass <EOL> def timelimit ( timeout ) : <EOL> """<STR_LIT>""" <EOL> def _1 ( function ) : <EOL> def _2 ( * args , ** kw ) : <EOL> class Dispatch ( threading . Thread ) : <EOL> def __init__ ( self ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . result = None <EOL> self . error = None <EOL> self . setDaemon ( True ) <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> try : <EOL> self . result = function ( * args , ** kw ) <EOL> except : <EOL> self . error = sys . exc_info ( ) <EOL> c = Dispatch ( ) <EOL> c . join ( timeout ) <EOL> if c . isAlive ( ) : <EOL> raise TimeoutError , '<STR_LIT>' <EOL> if c . error : <EOL> raise c . error [ <NUM_LIT:0> ] , c . error [ <NUM_LIT:1> ] <EOL> return c . result <EOL> return _2 <EOL> return _1 <EOL> class Memoize : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , expires = None , background = True ) : <EOL> self . func = func <EOL> self . cache = { } <EOL> self . expires = expires <EOL> self . background = background <EOL> self . running = { } <EOL> def __call__ ( self , * args , ** keywords ) : <EOL> key = ( args , tuple ( keywords . items ( ) ) ) <EOL> if not self . running . get ( key ) : <EOL> self . running [ key ] = threading . Lock ( ) <EOL> def update ( block = False ) : <EOL> if self . running [ key ] . acquire ( block ) : <EOL> try : <EOL> self . cache [ key ] = ( self . func ( * args , ** keywords ) , time . time ( ) ) <EOL> finally : <EOL> self . running [ key ] . release ( ) <EOL> if key not in self . cache : <EOL> update ( block = True ) <EOL> elif self . expires and ( time . time ( ) - self . cache [ key ] [ <NUM_LIT:1> ] ) > self . expires : <EOL> if self . background : <EOL> threading . Thread ( target = update ) . start ( ) <EOL> else : <EOL> update ( ) <EOL> return self . cache [ key ] [ <NUM_LIT:0> ] <EOL> memoize = Memoize <EOL> re_compile = memoize ( re . compile ) <EOL> re_compile . __doc__ = """<STR_LIT>""" <EOL> class _re_subm_proxy : <EOL> def __init__ ( self ) : <EOL> self . match = None <EOL> def __call__ ( self , match ) : <EOL> self . match = match <EOL> return '<STR_LIT>' <EOL> def re_subm ( pat , repl , string ) : <EOL> """<STR_LIT>""" <EOL> compiled_pat = re_compile ( pat ) <EOL> proxy = _re_subm_proxy ( ) <EOL> compiled_pat . sub ( proxy . __call__ , string ) <EOL> return compiled_pat . sub ( repl , string ) , proxy . match <EOL> def group ( seq , size ) : <EOL> """<STR_LIT>""" <EOL> def take ( seq , n ) : <EOL> for i in xrange ( n ) : <EOL> yield seq . next ( ) <EOL> if not hasattr ( seq , '<STR_LIT>' ) : <EOL> seq = iter ( seq ) <EOL> while True : <EOL> x = list ( take ( seq , size ) ) <EOL> if x : <EOL> yield x <EOL> else : <EOL> break <EOL> def uniq ( seq ) : <EOL> """<STR_LIT>""" <EOL> seen = set ( ) <EOL> result = [ ] <EOL> for item in seq : <EOL> if item in seen : continue <EOL> seen . add ( item ) <EOL> result . append ( item ) <EOL> return result <EOL> def iterview ( x ) : <EOL> """<STR_LIT>""" <EOL> WIDTH = <NUM_LIT> <EOL> def plainformat ( n , lenx ) : <EOL> return '<STR_LIT>' % ( ( float ( n ) / lenx ) * <NUM_LIT:100> , len ( str ( lenx ) ) , n , lenx ) <EOL> def bars ( size , n , lenx ) : <EOL> val = int ( ( float ( n ) * size ) / lenx + <NUM_LIT:0.5> ) <EOL> if size - val : <EOL> spacing = "<STR_LIT:>>" + ( "<STR_LIT:U+0020>" * ( size - val ) ) [ <NUM_LIT:1> : ] <EOL> else : <EOL> spacing = "<STR_LIT>" <EOL> return "<STR_LIT>" % ( "<STR_LIT:=>" * val , spacing ) <EOL> def eta ( elapsed , n , lenx ) : <EOL> if n == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> if n == lenx : <EOL> secs = int ( elapsed ) <EOL> else : <EOL> secs = int ( ( elapsed / n ) * ( lenx - n ) ) <EOL> mins , secs = divmod ( secs , <NUM_LIT> ) <EOL> hrs , mins = divmod ( mins , <NUM_LIT> ) <EOL> return '<STR_LIT>' % ( hrs , mins , secs ) <EOL> def format ( starttime , n , lenx ) : <EOL> out = plainformat ( n , lenx ) + '<STR_LIT:U+0020>' <EOL> if n == lenx : <EOL> end = '<STR_LIT:U+0020>' <EOL> else : <EOL> end = '<STR_LIT>' <EOL> end += eta ( time . time ( ) - starttime , n , lenx ) <EOL> out += bars ( WIDTH - len ( out ) - len ( end ) , n , lenx ) <EOL> out += end <EOL> return out <EOL> starttime = time . time ( ) <EOL> lenx = len ( x ) <EOL> for n , y in enumerate ( x ) : <EOL> sys . stderr . write ( '<STR_LIT:\r>' + format ( starttime , n , lenx ) ) <EOL> yield y <EOL> sys . stderr . write ( '<STR_LIT:\r>' + format ( starttime , n + <NUM_LIT:1> , lenx ) + '<STR_LIT:\n>' ) <EOL> class IterBetter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterator ) : <EOL> self . i , self . c = iterator , <NUM_LIT:0> <EOL> def __iter__ ( self ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> yield self . _head <EOL> while <NUM_LIT:1> : <EOL> yield self . i . next ( ) <EOL> self . c += <NUM_LIT:1> <EOL> def __getitem__ ( self , i ) : <EOL> if i < self . c : <EOL> raise IndexError , "<STR_LIT>" + str ( i ) <EOL> try : <EOL> while i > self . c : <EOL> self . i . next ( ) <EOL> self . c += <NUM_LIT:1> <EOL> self . c += <NUM_LIT:1> <EOL> return self . i . next ( ) <EOL> except StopIteration : <EOL> raise IndexError , str ( i ) <EOL> def __nonzero__ ( self ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return len ( self ) != <NUM_LIT:0> <EOL> elif hasattr ( self , "<STR_LIT>" ) : <EOL> return True <EOL> else : <EOL> try : <EOL> self . _head = self . i . next ( ) <EOL> except StopIteration : <EOL> return False <EOL> else : <EOL> return True <EOL> iterbetter = IterBetter <EOL> def safeiter ( it , cleanup = None , ignore_errors = True ) : <EOL> """<STR_LIT>""" <EOL> def next ( ) : <EOL> while True : <EOL> try : <EOL> return it . next ( ) <EOL> except StopIteration : <EOL> raise <EOL> except : <EOL> traceback . print_exc ( ) <EOL> it = iter ( it ) <EOL> while True : <EOL> yield next ( ) <EOL> def safewrite ( filename , content ) : <EOL> """<STR_LIT>""" <EOL> f = file ( filename + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> f . write ( content ) <EOL> f . close ( ) <EOL> os . rename ( f . name , path ) <EOL> def dictreverse ( mapping ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( value , key ) for ( key , value ) in mapping . iteritems ( ) ] ) <EOL> def dictfind ( dictionary , element ) : <EOL> """<STR_LIT>""" <EOL> for ( key , value ) in dictionary . iteritems ( ) : <EOL> if element is value : <EOL> return key <EOL> def dictfindall ( dictionary , element ) : <EOL> """<STR_LIT>""" <EOL> res = [ ] <EOL> for ( key , value ) in dictionary . iteritems ( ) : <EOL> if element is value : <EOL> res . append ( key ) <EOL> return res <EOL> def dictincr ( dictionary , element ) : <EOL> """<STR_LIT>""" <EOL> dictionary . setdefault ( element , <NUM_LIT:0> ) <EOL> dictionary [ element ] += <NUM_LIT:1> <EOL> return dictionary [ element ] <EOL> def dictadd ( * dicts ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for dct in dicts : <EOL> result . update ( dct ) <EOL> return result <EOL> def requeue ( queue , index = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> x = queue . pop ( index ) <EOL> queue . insert ( <NUM_LIT:0> , x ) <EOL> return x <EOL> def restack ( stack , index = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> x = stack . pop ( index ) <EOL> stack . append ( x ) <EOL> return x <EOL> def listget ( lst , ind , default = None ) : <EOL> """<STR_LIT>""" <EOL> if len ( lst ) - <NUM_LIT:1> < ind : <EOL> return default <EOL> return lst [ ind ] <EOL> def intget ( integer , default = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( integer ) <EOL> except ( TypeError , ValueError ) : <EOL> return default <EOL> def datestr ( then , now = None ) : <EOL> """<STR_LIT>""" <EOL> def agohence ( n , what , divisor = None ) : <EOL> if divisor : n = n // divisor <EOL> out = str ( abs ( n ) ) + '<STR_LIT:U+0020>' + what <EOL> if abs ( n ) != <NUM_LIT:1> : out += '<STR_LIT:s>' <EOL> out += '<STR_LIT:U+0020>' <EOL> if n < <NUM_LIT:0> : <EOL> out += '<STR_LIT>' <EOL> else : <EOL> out += '<STR_LIT>' <EOL> return out <EOL> oneday = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> if not then : return "<STR_LIT>" <EOL> if not now : now = datetime . datetime . utcnow ( ) <EOL> if type ( now ) . __name__ == "<STR_LIT>" : <EOL> now = datetime . datetime . fromtimestamp ( now ) <EOL> if type ( then ) . __name__ == "<STR_LIT>" : <EOL> then = datetime . datetime . fromtimestamp ( then ) <EOL> elif type ( then ) . __name__ == "<STR_LIT:date>" : <EOL> then = datetime . datetime ( then . year , then . month , then . day ) <EOL> delta = now - then <EOL> deltaseconds = int ( delta . days * oneday + delta . seconds + delta . microseconds * <NUM_LIT> ) <EOL> deltadays = abs ( deltaseconds ) // oneday <EOL> if deltaseconds < <NUM_LIT:0> : deltadays *= - <NUM_LIT:1> <EOL> if deltadays : <EOL> if abs ( deltadays ) < <NUM_LIT:4> : <EOL> return agohence ( deltadays , '<STR_LIT>' ) <EOL> out = then . strftime ( '<STR_LIT>' ) <EOL> if then . year != now . year or deltadays < <NUM_LIT:0> : <EOL> out += '<STR_LIT>' % then . year <EOL> return out <EOL> if int ( deltaseconds ) : <EOL> if abs ( deltaseconds ) > ( <NUM_LIT> * <NUM_LIT> ) : <EOL> return agohence ( deltaseconds , '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> ) <EOL> elif abs ( deltaseconds ) > <NUM_LIT> : <EOL> return agohence ( deltaseconds , '<STR_LIT>' , <NUM_LIT> ) <EOL> else : <EOL> return agohence ( deltaseconds , '<STR_LIT>' ) <EOL> deltamicroseconds = delta . microseconds <EOL> if delta . days : deltamicroseconds = int ( delta . microseconds - <NUM_LIT> ) <EOL> if abs ( deltamicroseconds ) > <NUM_LIT:1000> : <EOL> return agohence ( deltamicroseconds , '<STR_LIT>' , <NUM_LIT:1000> ) <EOL> return agohence ( deltamicroseconds , '<STR_LIT>' ) <EOL> def numify ( string ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( [ c for c in str ( string ) if c . isdigit ( ) ] ) <EOL> def denumify ( string , pattern ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> for c in pattern : <EOL> if c == "<STR_LIT:X>" : <EOL> out . append ( string [ <NUM_LIT:0> ] ) <EOL> string = string [ <NUM_LIT:1> : ] <EOL> else : <EOL> out . append ( c ) <EOL> return '<STR_LIT>' . join ( out ) <EOL> def commify ( n ) : <EOL> """<STR_LIT>""" <EOL> if n is None : return None <EOL> n = str ( n ) <EOL> if '<STR_LIT:.>' in n : <EOL> dollars , cents = n . split ( '<STR_LIT:.>' ) <EOL> else : <EOL> dollars , cents = n , None <EOL> r = [ ] <EOL> for i , c in enumerate ( str ( dollars ) [ : : - <NUM_LIT:1> ] ) : <EOL> if i and ( not ( i % <NUM_LIT:3> ) ) : <EOL> r . insert ( <NUM_LIT:0> , '<STR_LIT:U+002C>' ) <EOL> r . insert ( <NUM_LIT:0> , c ) <EOL> out = '<STR_LIT>' . join ( r ) <EOL> if cents : <EOL> out += '<STR_LIT:.>' + cents <EOL> return out <EOL> def dateify ( datestring ) : <EOL> """<STR_LIT>""" <EOL> return denumify ( datestring , "<STR_LIT>" ) <EOL> def nthstr ( n ) : <EOL> """<STR_LIT>""" <EOL> assert n >= <NUM_LIT:0> <EOL> if n % <NUM_LIT:100> in [ <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT> ] : return '<STR_LIT>' % n <EOL> return { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' } . get ( n % <NUM_LIT:10> , '<STR_LIT>' ) % n <EOL> def cond ( predicate , consequence , alternative = None ) : <EOL> """<STR_LIT>""" <EOL> if predicate : <EOL> return consequence <EOL> else : <EOL> return alternative <EOL> class CaptureStdout : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> def __call__ ( self , * args , ** keywords ) : <EOL> from cStringIO import StringIO <EOL> out = StringIO ( ) <EOL> oldstdout = sys . stdout <EOL> sys . stdout = out <EOL> try : <EOL> self . func ( * args , ** keywords ) <EOL> finally : <EOL> sys . stdout = oldstdout <EOL> return out . getvalue ( ) <EOL> capturestdout = CaptureStdout <EOL> class Profile : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> def __call__ ( self , * args ) : <EOL> import hotshot , hotshot . stats , os , tempfile <EOL> f , filename = tempfile . mkstemp ( ) <EOL> os . close ( f ) <EOL> prof = hotshot . Profile ( filename ) <EOL> stime = time . time ( ) <EOL> result = prof . runcall ( self . func , * args ) <EOL> stime = time . time ( ) - stime <EOL> prof . close ( ) <EOL> import cStringIO <EOL> out = cStringIO . StringIO ( ) <EOL> stats = hotshot . stats . load ( filename ) <EOL> stats . stream = out <EOL> stats . strip_dirs ( ) <EOL> stats . sort_stats ( '<STR_LIT:time>' , '<STR_LIT>' ) <EOL> stats . print_stats ( <NUM_LIT> ) <EOL> stats . print_callers ( ) <EOL> x = '<STR_LIT>' + str ( stime ) + '<STR_LIT>' <EOL> x += out . getvalue ( ) <EOL> try : <EOL> os . remove ( filename ) <EOL> except IOError : <EOL> pass <EOL> return result , x <EOL> profile = Profile <EOL> import traceback <EOL> if not hasattr ( traceback , '<STR_LIT>' ) : <EOL> from cStringIO import StringIO <EOL> def format_exc ( limit = None ) : <EOL> strbuf = StringIO ( ) <EOL> traceback . print_exc ( limit , strbuf ) <EOL> return strbuf . getvalue ( ) <EOL> traceback . format_exc = format_exc <EOL> def tryall ( context , prefix = None ) : <EOL> """<STR_LIT>""" <EOL> context = context . copy ( ) <EOL> results = { } <EOL> for ( key , value ) in context . iteritems ( ) : <EOL> if not hasattr ( value , '<STR_LIT>' ) : <EOL> continue <EOL> if prefix and not key . startswith ( prefix ) : <EOL> continue <EOL> print key + '<STR_LIT::>' , <EOL> try : <EOL> r = value ( ) <EOL> dictincr ( results , r ) <EOL> print r <EOL> except : <EOL> print '<STR_LIT>' <EOL> dictincr ( results , '<STR_LIT>' ) <EOL> print '<STR_LIT:U+0020>' + '<STR_LIT>' . join ( traceback . format_exc ( ) . split ( '<STR_LIT:\n>' ) ) <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> print '<STR_LIT>' <EOL> for ( key , value ) in results . iteritems ( ) : <EOL> print '<STR_LIT:U+0020>' * <NUM_LIT:2> , str ( key ) + '<STR_LIT::>' , value <EOL> class ThreadedDict : <EOL> """<STR_LIT>""" <EOL> def __getattr__ ( self , key ) : <EOL> return getattr ( self . _getd ( ) , key ) <EOL> def __setattr__ ( self , key , value ) : <EOL> return setattr ( self . _getd ( ) , key , value ) <EOL> def __delattr__ ( self , key ) : <EOL> return delattr ( self . _getd ( ) , key ) <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> def _getd ( self ) : <EOL> t = threading . currentThread ( ) <EOL> if not hasattr ( t , '<STR_LIT>' ) : <EOL> t . _d = { } <EOL> if self not in t . _d : <EOL> t . _d [ self ] = storage ( ) <EOL> return t . _d [ self ] <EOL> threadeddict = ThreadedDict <EOL> def autoassign ( self , locals ) : <EOL> """<STR_LIT>""" <EOL> for ( key , value ) in locals . iteritems ( ) : <EOL> if key == '<STR_LIT>' : <EOL> continue <EOL> setattr ( self , key , value ) <EOL> def to36 ( q ) : <EOL> """<STR_LIT>""" <EOL> if q < <NUM_LIT:0> : raise ValueError , "<STR_LIT>" <EOL> letters = "<STR_LIT>" <EOL> converted = [ ] <EOL> while q != <NUM_LIT:0> : <EOL> q , r = divmod ( q , <NUM_LIT> ) <EOL> converted . insert ( <NUM_LIT:0> , letters [ r ] ) <EOL> return "<STR_LIT>" . join ( converted ) or '<STR_LIT:0>' <EOL> r_url = re_compile ( '<STR_LIT>' ) <EOL> def safemarkdown ( text ) : <EOL> """<STR_LIT>""" <EOL> from markdown import markdown <EOL> if text : <EOL> text = text . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) <EOL> text = r_url . sub ( r'<STR_LIT>' , text ) <EOL> text = markdown ( text ) <EOL> return text <EOL> def sendmail ( from_address , to_address , subject , message , headers = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> attachments = kw . pop ( "<STR_LIT>" , [ ] ) <EOL> mail = _EmailMessage ( from_address , to_address , subject , message , headers , ** kw ) <EOL> for a in attachments : <EOL> if isinstance ( a , dict ) : <EOL> mail . attach ( a [ '<STR_LIT:filename>' ] , a [ '<STR_LIT:content>' ] , a . get ( '<STR_LIT>' ) ) <EOL> elif hasattr ( a , '<STR_LIT>' ) : <EOL> filename = os . path . basename ( getattr ( a , "<STR_LIT:name>" , "<STR_LIT>" ) ) <EOL> content_type = getattr ( a , '<STR_LIT>' , None ) <EOL> mail . attach ( filename , a . read ( ) , content_type ) <EOL> elif isinstance ( a , basestring ) : <EOL> f = open ( a , '<STR_LIT:rb>' ) <EOL> content = f . read ( ) <EOL> f . close ( ) <EOL> filename = os . path . basename ( a ) <EOL> mail . attach ( filename , content , None ) <EOL> else : <EOL> raise ValueError , "<STR_LIT>" % repr ( a ) <EOL> mail . send ( ) <EOL> class _EmailMessage : <EOL> def __init__ ( self , from_address , to_address , subject , message , headers = None , ** kw ) : <EOL> def listify ( x ) : <EOL> if not isinstance ( x , list ) : <EOL> return [ safestr ( x ) ] <EOL> else : <EOL> return [ safestr ( a ) for a in x ] <EOL> subject = safestr ( subject ) <EOL> message = safestr ( message ) <EOL> from_address = safestr ( from_address ) <EOL> to_address = listify ( to_address ) <EOL> cc = listify ( kw . get ( '<STR_LIT>' , [ ] ) ) <EOL> bcc = listify ( kw . get ( '<STR_LIT>' , [ ] ) ) <EOL> recipients = to_address + cc + bcc <EOL> import email . Utils <EOL> self . from_address = email . Utils . parseaddr ( from_address ) [ <NUM_LIT:1> ] <EOL> self . recipients = [ email . Utils . parseaddr ( r ) [ <NUM_LIT:1> ] for r in recipients ] <EOL> self . headers = dictadd ( { <EOL> '<STR_LIT>' : from_address , <EOL> '<STR_LIT>' : "<STR_LIT:U+002CU+0020>" . join ( to_address ) , <EOL> '<STR_LIT>' : subject <EOL> } , headers or { } ) <EOL> if cc : <EOL> self . headers [ '<STR_LIT>' ] = "<STR_LIT:U+002CU+0020>" . join ( cc ) <EOL> self . message = self . new_message ( ) <EOL> self . message . add_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . message . add_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . message . add_header ( "<STR_LIT>" , "<STR_LIT:1.0>" ) <EOL> self . message . set_payload ( message , '<STR_LIT:utf-8>' ) <EOL> self . multipart = False <EOL> def new_message ( self ) : <EOL> from email . Message import Message <EOL> return Message ( ) <EOL> def attach ( self , filename , content , content_type = None ) : <EOL> if not self . multipart : <EOL> msg = self . new_message ( ) <EOL> msg . add_header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> msg . attach ( self . message ) <EOL> self . message = msg <EOL> self . multipart = True <EOL> import mimetypes <EOL> try : <EOL> from email import encoders <EOL> except : <EOL> from email import Encoders as encoders <EOL> content_type = content_type or mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] or "<STR_LIT>" <EOL> msg = self . new_message ( ) <EOL> msg . set_payload ( content ) <EOL> msg . add_header ( '<STR_LIT:Content-Type>' , content_type ) <EOL> msg . add_header ( '<STR_LIT>' , '<STR_LIT>' , filename = filename ) <EOL> if not content_type . startswith ( "<STR_LIT>" ) : <EOL> encoders . encode_base64 ( msg ) <EOL> self . message . attach ( msg ) <EOL> def send ( self ) : <EOL> try : <EOL> import webapi <EOL> except ImportError : <EOL> webapi = Storage ( config = Storage ( ) ) <EOL> for k , v in self . headers . iteritems ( ) : <EOL> self . message . add_header ( k , v ) <EOL> message_text = self . message . as_string ( ) <EOL> if webapi . config . get ( '<STR_LIT>' ) : <EOL> server = webapi . config . get ( '<STR_LIT>' ) <EOL> port = webapi . config . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> username = webapi . config . get ( '<STR_LIT>' ) <EOL> password = webapi . config . get ( '<STR_LIT>' ) <EOL> debug_level = webapi . config . get ( '<STR_LIT>' , None ) <EOL> starttls = webapi . config . get ( '<STR_LIT>' , False ) <EOL> import smtplib <EOL> smtpserver = smtplib . SMTP ( server , port ) <EOL> if debug_level : <EOL> smtpserver . set_debuglevel ( debug_level ) <EOL> if starttls : <EOL> smtpserver . ehlo ( ) <EOL> smtpserver . starttls ( ) <EOL> smtpserver . ehlo ( ) <EOL> if username and password : <EOL> smtpserver . login ( username , password ) <EOL> smtpserver . sendmail ( self . from_address , self . recipients , message_text ) <EOL> smtpserver . quit ( ) <EOL> else : <EOL> sendmail = webapi . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert not self . from_address . startswith ( '<STR_LIT:->' ) , '<STR_LIT>' <EOL> for r in self . recipients : <EOL> assert not r . startswith ( '<STR_LIT:->' ) , '<STR_LIT>' <EOL> cmd = [ sendmail , '<STR_LIT>' , self . from_address ] + self . recipients <EOL> if subprocess : <EOL> p = subprocess . Popen ( cmd , stdin = subprocess . PIPE ) <EOL> p . stdin . write ( message_text ) <EOL> p . stdin . close ( ) <EOL> p . wait ( ) <EOL> else : <EOL> i , o = os . popen2 ( cmd ) <EOL> i . write ( message ) <EOL> i . close ( ) <EOL> o . close ( ) <EOL> del i , o <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return self . message . as_string ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
