<s> import string <EOL> import random <EOL> import json <EOL> import traceback <EOL> import re <EOL> import unicodedata <EOL> import six <EOL> from giotto import get_config <EOL> from collections import defaultdict <EOL> def switchout_keyvalue ( engine ) : <EOL> from giotto import keyvalue <EOL> if engine == '<STR_LIT>' : <EOL> return keyvalue . DummyKeyValue <EOL> if engine == '<STR_LIT>' : <EOL> return keyvalue . LocMemKeyValue <EOL> if engine == '<STR_LIT>' : <EOL> return keyvalue . DatabaseKeyValue <EOL> if engine == '<STR_LIT>' : <EOL> return keyvalue . MemcacheKeyValue <EOL> if engine == '<STR_LIT>' : <EOL> return keyvalue . RedisKeyValue <EOL> class Mock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __getattribute__ ( self , item ) : <EOL> return Mock ( ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __bool__ ( self ) : <EOL> return False <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> def __iter__ ( self ) : <EOL> return iter ( [ ] ) <EOL> def parse_kwargs ( kwargs ) : <EOL> """<STR_LIT>""" <EOL> d = defaultdict ( list ) <EOL> for k , v in ( ( k . lstrip ( '<STR_LIT:->' ) , v ) for k , v in ( a . split ( '<STR_LIT:=>' ) for a in kwargs ) ) : <EOL> d [ k ] . append ( v ) <EOL> ret = { } <EOL> for k , v in d . items ( ) : <EOL> if len ( v ) == <NUM_LIT:1> and type ( v ) is list : <EOL> ret [ k ] = v [ <NUM_LIT:0> ] <EOL> else : <EOL> ret [ k ] = v <EOL> return ret <EOL> def super_accept_to_mimetype ( ext ) : <EOL> if not ext : <EOL> return <EOL> if ext . startswith ( '<STR_LIT:.>' ) : <EOL> ext = ext [ <NUM_LIT:1> : ] <EOL> if ext in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> if ext == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> if ext == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> if ext in ( '<STR_LIT:html>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> if ext == '<STR_LIT>' : <EOL> return '<STR_LIT:application/json>' <EOL> if ext == '<STR_LIT>' : <EOL> return "<STR_LIT>" <EOL> if ext == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> if ext == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> def random_string ( n ) : <EOL> return '<STR_LIT>' . join ( random . choice ( string . ascii_uppercase + string . digits ) for x in range ( n ) ) <EOL> def htmlize ( value ) : <EOL> """<STR_LIT>""" <EOL> return str ( value ) . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) <EOL> def htmlize_list ( items ) : <EOL> """<STR_LIT>""" <EOL> out = [ "<STR_LIT>" ] <EOL> for item in items : <EOL> out . append ( "<STR_LIT>" + htmlize ( item ) + "<STR_LIT>" ) <EOL> out . append ( "<STR_LIT>" ) <EOL> return "<STR_LIT:\n>" . join ( out ) <EOL> def pre_process_json ( obj ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) is dict : <EOL> new_dict = { } <EOL> for key , value in obj . items ( ) : <EOL> new_dict [ key ] = pre_process_json ( value ) <EOL> return new_dict <EOL> elif type ( obj ) is list : <EOL> new_list = [ ] <EOL> for item in obj : <EOL> new_list . append ( pre_process_json ( item ) ) <EOL> return new_list <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return dict ( obj . todict ( ) ) <EOL> else : <EOL> try : <EOL> json . dumps ( obj ) <EOL> except TypeError : <EOL> try : <EOL> json . dumps ( obj . __dict__ ) <EOL> except TypeError : <EOL> return str ( obj ) <EOL> else : <EOL> return obj . __dict__ <EOL> else : <EOL> return obj <EOL> def render_error_page ( code , exc , mimetype = '<STR_LIT>' , traceback = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> from giotto . views import get_jinja_template <EOL> if '<STR_LIT>' in mimetype : <EOL> return json . dumps ( { <EOL> '<STR_LIT:code>' : code , <EOL> '<STR_LIT>' : exc . __class__ . __name__ , <EOL> '<STR_LIT:message>' : str ( exc ) , <EOL> } ) <EOL> et = get_config ( '<STR_LIT>' ) <EOL> if not et : <EOL> return "<STR_LIT>" % ( code , str ( exc ) , traceback ) <EOL> template = get_jinja_template ( et ) <EOL> return template . render ( <EOL> code = code , <EOL> exception = exc . __class__ . __name__ , <EOL> message = str ( exc ) , <EOL> traceback = traceback <EOL> ) <EOL> to_remove = re . compile ( '<STR_LIT>' ) <EOL> remove_dup = re . compile ( '<STR_LIT>' ) <EOL> def slugify ( value ) : <EOL> """<STR_LIT>""" <EOL> if six . PY3 : <EOL> value = str ( value ) <EOL> else : <EOL> value = unicode ( value ) <EOL> value = unicodedata . normalize ( '<STR_LIT>' , value ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( '<STR_LIT:ascii>' ) <EOL> value = to_remove . sub ( '<STR_LIT>' , value ) . strip ( ) . lower ( ) <EOL> return remove_dup . sub ( '<STR_LIT:->' , value ) <EOL> def jsonify ( obj ) : <EOL> def handler ( obj ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . isoformat ( ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . todict ( ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % ( type ( obj ) , repr ( obj ) ) ) <EOL> return json . dumps ( obj , default = handler ) </s>
