<s> from datetime import datetime <EOL> from uuid import uuid4 <EOL> import logging <EOL> import numbers <EOL> from dateutil . tz import tzutc <EOL> from six import string_types <EOL> from analytics . utils import guess_timezone , clean <EOL> from analytics . consumer import Consumer <EOL> from analytics . version import VERSION <EOL> try : <EOL> import queue <EOL> except : <EOL> import Queue as queue <EOL> ID_TYPES = ( numbers . Number , string_types ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def __init__ ( self , write_key = None , debug = False , max_queue_size = <NUM_LIT> , <EOL> send = True , on_error = None ) : <EOL> require ( '<STR_LIT>' , write_key , string_types ) <EOL> self . queue = queue . Queue ( max_queue_size ) <EOL> self . consumer = Consumer ( self . queue , write_key , on_error = on_error ) <EOL> self . write_key = write_key <EOL> self . on_error = on_error <EOL> self . debug = debug <EOL> self . send = send <EOL> if debug : <EOL> self . log . setLevel ( logging . DEBUG ) <EOL> if send : <EOL> self . consumer . start ( ) <EOL> def identify ( self , user_id = None , traits = None , context = None , timestamp = None , <EOL> anonymous_id = None , integrations = None ) : <EOL> traits = traits or { } <EOL> context = context or { } <EOL> integrations = integrations or { } <EOL> require ( '<STR_LIT>' , user_id or anonymous_id , ID_TYPES ) <EOL> require ( '<STR_LIT>' , traits , dict ) <EOL> msg = { <EOL> '<STR_LIT>' : integrations , <EOL> '<STR_LIT>' : anonymous_id , <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : traits <EOL> } <EOL> return self . _enqueue ( msg ) <EOL> def track ( self , user_id = None , event = None , properties = None , context = None , <EOL> timestamp = None , anonymous_id = None , integrations = None ) : <EOL> properties = properties or { } <EOL> context = context or { } <EOL> integrations = integrations or { } <EOL> require ( '<STR_LIT>' , user_id or anonymous_id , ID_TYPES ) <EOL> require ( '<STR_LIT>' , properties , dict ) <EOL> require ( '<STR_LIT>' , event , string_types ) <EOL> msg = { <EOL> '<STR_LIT>' : integrations , <EOL> '<STR_LIT>' : anonymous_id , <EOL> '<STR_LIT>' : properties , <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : event <EOL> } <EOL> return self . _enqueue ( msg ) <EOL> def alias ( self , previous_id = None , user_id = None , context = None , <EOL> timestamp = None , integrations = None ) : <EOL> context = context or { } <EOL> integrations = integrations or { } <EOL> require ( '<STR_LIT>' , previous_id , ID_TYPES ) <EOL> require ( '<STR_LIT>' , user_id , ID_TYPES ) <EOL> msg = { <EOL> '<STR_LIT>' : integrations , <EOL> '<STR_LIT>' : previous_id , <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT:type>' : '<STR_LIT>' <EOL> } <EOL> return self . _enqueue ( msg ) <EOL> def group ( self , user_id = None , group_id = None , traits = None , context = None , <EOL> timestamp = None , anonymous_id = None , integrations = None ) : <EOL> traits = traits or { } <EOL> context = context or { } <EOL> integrations = integrations or { } <EOL> require ( '<STR_LIT>' , user_id or anonymous_id , ID_TYPES ) <EOL> require ( '<STR_LIT>' , group_id , ID_TYPES ) <EOL> require ( '<STR_LIT>' , traits , dict ) <EOL> msg = { <EOL> '<STR_LIT>' : integrations , <EOL> '<STR_LIT>' : anonymous_id , <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT>' : group_id , <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT>' : traits , <EOL> '<STR_LIT:type>' : '<STR_LIT>' <EOL> } <EOL> return self . _enqueue ( msg ) <EOL> def page ( self , user_id = None , category = None , name = None , properties = None , <EOL> context = None , timestamp = None , anonymous_id = None , <EOL> integrations = None ) : <EOL> properties = properties or { } <EOL> context = context or { } <EOL> integrations = integrations or { } <EOL> require ( '<STR_LIT>' , user_id or anonymous_id , ID_TYPES ) <EOL> require ( '<STR_LIT>' , properties , dict ) <EOL> if name : <EOL> require ( '<STR_LIT:name>' , name , string_types ) <EOL> if category : <EOL> require ( '<STR_LIT>' , category , string_types ) <EOL> msg = { <EOL> '<STR_LIT>' : integrations , <EOL> '<STR_LIT>' : anonymous_id , <EOL> '<STR_LIT>' : properties , <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT>' : category , <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : name , <EOL> } <EOL> return self . _enqueue ( msg ) <EOL> def screen ( self , user_id = None , category = None , name = None , properties = None , <EOL> context = None , timestamp = None , anonymous_id = None , <EOL> integrations = None ) : <EOL> properties = properties or { } <EOL> context = context or { } <EOL> integrations = integrations or { } <EOL> require ( '<STR_LIT>' , user_id or anonymous_id , ID_TYPES ) <EOL> require ( '<STR_LIT>' , properties , dict ) <EOL> if name : <EOL> require ( '<STR_LIT:name>' , name , string_types ) <EOL> if category : <EOL> require ( '<STR_LIT>' , category , string_types ) <EOL> msg = { <EOL> '<STR_LIT>' : integrations , <EOL> '<STR_LIT>' : anonymous_id , <EOL> '<STR_LIT>' : properties , <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT>' : category , <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : name , <EOL> } <EOL> return self . _enqueue ( msg ) <EOL> def _enqueue ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> timestamp = msg [ '<STR_LIT>' ] <EOL> if timestamp is None : <EOL> timestamp = datetime . utcnow ( ) . replace ( tzinfo = tzutc ( ) ) <EOL> require ( '<STR_LIT>' , msg [ '<STR_LIT>' ] , dict ) <EOL> require ( '<STR_LIT:type>' , msg [ '<STR_LIT:type>' ] , string_types ) <EOL> require ( '<STR_LIT>' , timestamp , datetime ) <EOL> require ( '<STR_LIT>' , msg [ '<STR_LIT>' ] , dict ) <EOL> timestamp = guess_timezone ( timestamp ) <EOL> msg [ '<STR_LIT>' ] = timestamp . isoformat ( ) <EOL> msg [ '<STR_LIT>' ] = str ( uuid4 ( ) ) <EOL> msg [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : VERSION <EOL> } <EOL> msg = clean ( msg ) <EOL> self . log . debug ( '<STR_LIT>' , msg ) <EOL> if self . queue . full ( ) : <EOL> self . log . warn ( '<STR_LIT>' ) <EOL> return False , msg <EOL> self . queue . put ( msg ) <EOL> self . log . debug ( '<STR_LIT>' , msg [ '<STR_LIT:type>' ] ) <EOL> return True , msg <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> queue = self . queue <EOL> size = queue . qsize ( ) <EOL> queue . join ( ) <EOL> self . log . debug ( '<STR_LIT>' , size ) <EOL> def join ( self ) : <EOL> """<STR_LIT>""" <EOL> self . consumer . pause ( ) <EOL> self . consumer . join ( ) <EOL> def require ( name , field , data_type ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( field , data_type ) : <EOL> msg = '<STR_LIT>' . format ( name , data_type , field ) <EOL> raise AssertionError ( msg ) </s>
