<s> import threading <EOL> import sys <EOL> import os <EOL> import json <EOL> import itertools <EOL> import time <EOL> from queue import Queue <EOL> import urllib . parse <EOL> import requests <EOL> from . common . utils import log <EOL> from . common import utils <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> sys_dir = os . path . dirname ( sys . executable ) if getattr ( sys , '<STR_LIT>' , False ) else os . path . dirname ( __file__ ) <EOL> res_dir = os . path . normpath ( os . path . join ( sys_dir , '<STR_LIT>' ) ) <EOL> def get_res_path ( res_name ) : <EOL> return os . path . join ( res_dir , res_name ) <EOL> json_header = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> def stackhut_api_call ( endpoint , msg , secure = True , return_json = True ) : <EOL> url = urllib . parse . urljoin ( utils . SERVER_URL , endpoint ) <EOL> log . debug ( "<STR_LIT>" . format ( url , json . dumps ( msg ) ) ) <EOL> r = requests . post ( url , data = json . dumps ( msg ) , headers = json_header ) <EOL> if r . status_code == requests . codes . ok : <EOL> return r . json ( ) if return_json else r . text <EOL> else : <EOL> log . error ( "<STR_LIT>" . format ( r . status_code ) ) <EOL> log . error ( r . text ) <EOL> r . raise_for_status ( ) <EOL> def stackhut_api_user_call ( endpoint , _msg , usercfg ) : <EOL> msg = _msg . copy ( ) <EOL> msg [ '<STR_LIT>' ] = dict ( username = usercfg . username , hash = usercfg [ '<STR_LIT>' ] ) <EOL> return stackhut_api_call ( endpoint , msg ) <EOL> class KeenClient ( threading . Thread ) : <EOL> project_id = '<STR_LIT>' <EOL> write_key = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> keen_url = "<STR_LIT>" "<STR_LIT>" . format ( project_id = project_id , write_key = write_key ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ) . __init__ ( * args , ** kwargs ) <EOL> self . send_analytics = False <EOL> self . analytics_ids = None <EOL> self . queue = Queue ( ) <EOL> def start ( self , usercfg ) : <EOL> self . send_analytics = usercfg . send_analytics <EOL> if self . send_analytics : <EOL> log . debug ( "<STR_LIT>" ) <EOL> self . analytics_ids = usercfg . analytics_ids <EOL> super ( ) . start ( ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" ) <EOL> def run ( self ) : <EOL> while True : <EOL> ( endpoint , msg ) = self . queue . get ( ) <EOL> msg . update ( self . analytics_ids ) <EOL> try : <EOL> log . debug ( "<STR_LIT>" . format ( endpoint ) ) <EOL> url = self . keen_url . format ( event_collection = endpoint ) <EOL> r = requests . post ( url , data = json . dumps ( msg ) , headers = json_header , timeout = <NUM_LIT:2> ) <EOL> if not ( r . status_code == requests . codes . created and r . json ( ) . get ( '<STR_LIT>' ) ) : <EOL> log . debug ( "<STR_LIT>" . format ( r . status_code , r . text ( ) ) ) <EOL> raise IOError ( ) <EOL> except : <EOL> log . debug ( "<STR_LIT>" . format ( endpoint ) ) <EOL> finally : <EOL> self . queue . task_done ( ) <EOL> def send ( self , endpoint , msg ) : <EOL> if self . send_analytics : <EOL> self . queue . put ( ( endpoint , msg ) ) <EOL> def shutdown ( self ) : <EOL> if self . send_analytics : <EOL> self . queue . join ( ) <EOL> keen_client = KeenClient ( daemon = True ) <EOL> class Spinner ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> spin_interval = <NUM_LIT:0.5> <EOL> dot_interval = <NUM_LIT:10> <EOL> dot_max = int ( dot_interval / spin_interval ) <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( daemon = True ) <EOL> self . spinning = threading . Event ( ) <EOL> self . spinner = itertools . cycle ( [ '<STR_LIT:->' , '<STR_LIT:\\>' , '<STR_LIT:|>' , '<STR_LIT:/>' ] ) <EOL> def __enter__ ( self ) : <EOL> self . spinning . set ( ) <EOL> self . start ( ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> self . spinning . clear ( ) <EOL> def run ( self ) : <EOL> dot_count = <NUM_LIT:0> <EOL> while self . spinning . is_set ( ) : <EOL> sys . stdout . write ( next ( self . spinner ) ) <EOL> sys . stdout . flush ( ) <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> time . sleep ( self . spin_interval ) <EOL> dot_count += <NUM_LIT:1> <EOL> if dot_count >= self . dot_max : <EOL> sys . stdout . write ( '<STR_LIT:.>' ) <EOL> dot_count = <NUM_LIT:0> <EOL> sys . stdout . write ( '<STR_LIT:\n>' ) <EOL> def stop ( self ) : <EOL> self . spinning . clear ( ) </s>
