<s> import cStringIO <EOL> import csv <EOL> import codecs <EOL> import decimal <EOL> import datetime <EOL> import json <EOL> import random <EOL> import re <EOL> import hashlib <EOL> import pytz <EOL> import pystache <EOL> from funcy import distinct <EOL> from . human_time import parse_human_time <EOL> from redash import settings <EOL> COMMENTS_REGEX = re . compile ( "<STR_LIT>" ) <EOL> def utcnow ( ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . now ( pytz . utc ) <EOL> def slugify ( s ) : <EOL> return re . sub ( '<STR_LIT>' , '<STR_LIT:->' , s . lower ( ) ) <EOL> def gen_query_hash ( sql ) : <EOL> """<STR_LIT>""" <EOL> sql = COMMENTS_REGEX . sub ( "<STR_LIT>" , sql ) <EOL> sql = "<STR_LIT>" . join ( sql . split ( ) ) . lower ( ) <EOL> return hashlib . md5 ( sql . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> def generate_token ( length ) : <EOL> chars = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> rand = random . SystemRandom ( ) <EOL> return '<STR_LIT>' . join ( rand . choice ( chars ) for x in range ( length ) ) <EOL> class JSONEncoder ( json . JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def default ( self , o ) : <EOL> if isinstance ( o , decimal . Decimal ) : <EOL> return float ( o ) <EOL> if isinstance ( o , ( datetime . date , datetime . time ) ) : <EOL> return o . isoformat ( ) <EOL> if isinstance ( o , datetime . timedelta ) : <EOL> return str ( o ) <EOL> super ( JSONEncoder , self ) . default ( o ) <EOL> def json_dumps ( data ) : <EOL> return json . dumps ( data , cls = JSONEncoder ) <EOL> def build_url ( request , host , path ) : <EOL> parts = request . host . split ( '<STR_LIT::>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> port = parts [ <NUM_LIT:1> ] <EOL> if ( port , request . scheme ) not in ( ( '<STR_LIT>' , '<STR_LIT:http>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> host = '<STR_LIT>' . format ( host , port ) <EOL> return "<STR_LIT>" . format ( request . scheme , host , path ) <EOL> class UnicodeWriter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , f , dialect = csv . excel , encoding = "<STR_LIT:utf-8>" , ** kwds ) : <EOL> self . queue = cStringIO . StringIO ( ) <EOL> self . writer = csv . writer ( self . queue , dialect = dialect , ** kwds ) <EOL> self . stream = f <EOL> self . encoder = codecs . getincrementalencoder ( encoding ) ( ) <EOL> def _encode_utf8 ( self , val ) : <EOL> if isinstance ( val , ( unicode , str ) ) : <EOL> return val . encode ( '<STR_LIT:utf-8>' ) <EOL> return val <EOL> def writerow ( self , row ) : <EOL> self . writer . writerow ( [ self . _encode_utf8 ( s ) for s in row ] ) <EOL> data = self . queue . getvalue ( ) <EOL> data = data . decode ( "<STR_LIT:utf-8>" ) <EOL> data = self . encoder . encode ( data ) <EOL> self . stream . write ( data ) <EOL> self . queue . truncate ( <NUM_LIT:0> ) <EOL> def writerows ( self , rows ) : <EOL> for row in rows : <EOL> self . writerow ( row ) <EOL> def _collect_key_names ( nodes ) : <EOL> keys = [ ] <EOL> for node in nodes . _parse_tree : <EOL> if isinstance ( node , pystache . parser . _EscapeNode ) : <EOL> keys . append ( node . key ) <EOL> elif isinstance ( node , pystache . parser . _SectionNode ) : <EOL> keys . append ( node . key ) <EOL> keys . extend ( _collect_key_names ( node . parsed ) ) <EOL> return distinct ( keys ) <EOL> def collect_query_parameters ( query ) : <EOL> nodes = pystache . parse ( query ) <EOL> keys = _collect_key_names ( nodes ) <EOL> return keys <EOL> def collect_parameters_from_request ( args ) : <EOL> parameters = { } <EOL> for k , v in args . iteritems ( ) : <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> parameters [ k [ <NUM_LIT:2> : ] ] = v <EOL> return parameters <EOL> def base_url ( org ) : <EOL> if settings . MULTI_ORG : <EOL> return "<STR_LIT>" . format ( settings . HOST , org . slug ) <EOL> return settings . HOST </s>
