<s> import unicodedata <EOL> def _matches ( val1 , val2 , exactMath = True ) : <EOL> """<STR_LIT>""" <EOL> return ( val1 . lower ( ) == val2 . lower ( ) ) if ( <EOL> exactMath ) else val1 . lower ( ) . startswith ( val2 . lower ( ) ) <EOL> def filter_out ( d , prefix , exactMatch = False ) : <EOL> """<STR_LIT>""" <EOL> ret = dict ( ) <EOL> for key , val in d . items ( ) : <EOL> if not _matches ( key , prefix , exactMath = exactMatch ) : <EOL> ret [ key ] = val <EOL> return ret <EOL> def filter_in ( d , prefix , exactMatch = False ) : <EOL> """<STR_LIT>""" <EOL> ret = dict ( ) <EOL> for key , val in d . items ( ) : <EOL> if _matches ( key , prefix , exactMath = exactMatch ) : <EOL> ret [ key ] = val <EOL> return ret <EOL> def path4url ( * args ) : <EOL> """<STR_LIT>""" <EOL> r = '<STR_LIT:/>' . join ( [ '<STR_LIT>' ] + [ arg . decode ( '<STR_LIT:utf-8>' ) if ( <EOL> isinstance ( arg , str ) ) else '<STR_LIT:%s>' % arg for arg in args ] ) <EOL> while '<STR_LIT>' in r : <EOL> r = r . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> return ( '<STR_LIT>' % r . strip ( '<STR_LIT:/>' ) ) if r else '<STR_LIT>' <EOL> def readall ( openfile , size , retries = <NUM_LIT:7> ) : <EOL> """<STR_LIT>""" <EOL> remains = size if size > <NUM_LIT:0> else <NUM_LIT:0> <EOL> buf = '<STR_LIT>' <EOL> for i in range ( retries ) : <EOL> tmp_buf = openfile . read ( remains ) <EOL> if tmp_buf : <EOL> buf += tmp_buf <EOL> remains -= len ( tmp_buf ) <EOL> if remains > <NUM_LIT:0> : <EOL> continue <EOL> return buf <EOL> raise IOError ( '<STR_LIT>' % size ) <EOL> def escape_ctrl_chars ( s ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , unicode ) : <EOL> return "<STR_LIT>" . join ( ch . encode ( "<STR_LIT>" ) if ( <EOL> unicodedata . category ( ch ) [ <NUM_LIT:0> ] ) == "<STR_LIT:C>" else ch for ch in s ) <EOL> if isinstance ( s , basestring ) : <EOL> return "<STR_LIT>" . join ( <EOL> [ c if <NUM_LIT> < ord ( c ) < <NUM_LIT> else c . encode ( "<STR_LIT>" ) for c in s ] ) <EOL> return s </s>
