11import functools
22import os
33import sys
4- from abc import abstractmethod
54from inspect import signature
65from pathlib import Path
76from types import FunctionType
87from typing import Iterable
98from typing import Sized
9+ from typing import Tuple
1010
1111import lib .core as constants
1212from lib .app .helpers import extract_project_folder
1313from lib .app .interface .types import validate_arguments
14+ from lib .core import CONFIG
1415from lib .core .exceptions import AppException
15- from lib .core .reporter import Session
1616from lib .infrastructure .controller import Controller
1717from lib .infrastructure .repositories import ConfigRepository
1818from mixpanel import Mixpanel
2222class BaseInterfaceFacade :
2323 REGISTRY = []
2424
25- def __init__ (
26- self ,
27- token : str = None ,
28- config_path : str = constants .CONFIG_PATH ,
29- ):
30- host = constants .BACKEND_URL
31- env_token = os .environ .get ("SA_TOKEN" )
25+ def __init__ (self , token : str = None , config_path : str = None ):
3226 version = os .environ .get ("SA_VERSION" , "v1" )
33- ssl_verify = bool (os .environ .get ("SA_SSL" , True ))
27+ _token , _config_path = None , None
28+ _host = os .environ .get ("SA_URL" , constants .BACKEND_URL )
29+ _ssl_verify = bool (os .environ .get ("SA_SSL" , True ))
3430 if token :
35- token = Controller .validate_token (token = token )
36- elif env_token :
37- host = os .environ .get ("SA_URL" , constants .BACKEND_URL )
38-
39- token = Controller .valdate_token (env_token )
31+ _token = Controller .validate_token (token = token )
32+ elif config_path :
33+ _token , _host , _ssl_verify = self ._retrieve_configs (config_path )
4034 else :
41- config_path = os .path .expanduser (str (config_path ))
42- if not Path (config_path ).is_file () or not os .access (config_path , os .R_OK ):
43- raise AppException (
44- f"SuperAnnotate config file { str (config_path )} not found."
45- f" Please provide correct config file location to sa.init(<path>) or use "
46- f"CLI's superannotate init to generate default location config file."
35+ _token = os .environ .get ("SA_TOKEN" )
36+ if not _token :
37+ _token , _host , _ssl_verify = self ._retrieve_configs (
38+ constants .CONFIG_PATH
4739 )
48- config_repo = ConfigRepository (config_path )
49- token , host , ssl_verify = (
50- Controller .validate_token (config_repo .get_one ("token" ).value ),
51- config_repo .get_one ("main_endpoint" ).value ,
52- config_repo .get_one ("ssl_verify" ).value ,
53- )
54- self ._host = host
55- self ._token = token
56- self .controller = Controller (token , host , ssl_verify , version )
40+ self ._token , self ._host = _token , _host
41+ self .controller = Controller (_token , _host , _ssl_verify , version )
42+ BaseInterfaceFacade .REGISTRY .append (self )
5743
58- def __new__ (cls , * args , ** kwargs ):
59- obj = super ().__new__ (cls , * args , ** kwargs )
60- cls .REGISTRY .append (obj )
61- return obj
44+ @staticmethod
45+ def _retrieve_configs (path ) -> Tuple [str , str , str ]:
46+ config_path = os .path .expanduser (str (path ))
47+ if not Path (config_path ).is_file () or not os .access (config_path , os .R_OK ):
48+ raise AppException (
49+ f"SuperAnnotate config file { str (config_path )} not found."
50+ )
51+ config_repo = ConfigRepository (config_path )
52+ return (
53+ Controller .validate_token (config_repo .get_one ("token" ).value ),
54+ config_repo .get_one ("main_endpoint" ).value ,
55+ config_repo .get_one ("ssl_verify" ).value ,
56+ )
6257
6358 @property
64- @abstractmethod
6559 def host (self ):
66- raise NotImplementedError
60+ return self . _host
6761
6862 @property
69- @abstractmethod
7063 def token (self ):
71- raise NotImplementedError
72-
73- @property
74- @abstractmethod
75- def logger (self ):
76- raise NotImplementedError
64+ return self ._token
7765
7866
7967class Tracker :
8068 def get_mp_instance (self ) -> Mixpanel :
81- if self .client :
82- if self .client .host == constants .BACKEND_URL :
83- return Mixpanel ("ca95ed96f80e8ec3be791e2d3097cf51" )
84- else :
85- return Mixpanel ("e741d4863e7e05b1a45833d01865ef0d" )
69+ client = self .get_client ()
70+ mp_token = "ca95ed96f80e8ec3be791e2d3097cf51"
71+ if client :
72+ if client .host != constants .BACKEND_URL :
73+ mp_token = "e741d4863e7e05b1a45833d01865ef0d"
74+ return Mixpanel (mp_token )
8675
8776 @staticmethod
8877 def get_default_payload (team_name , user_id ):
@@ -98,16 +87,19 @@ def __init__(self, function):
9887 self ._client = None
9988 functools .update_wrapper (self , function )
10089
101- @property
102- def client (self ):
90+ def get_client (self ):
10391 if not self ._client :
10492 if BaseInterfaceFacade .REGISTRY :
105- self . _client = BaseInterfaceFacade .REGISTRY [- 1 ]
93+ return BaseInterfaceFacade .REGISTRY [- 1 ]
10694 else :
10795 from lib .app .interface .sdk_interface import SAClient
10896
109- self ._client = SAClient ()
110- return self ._client
97+ try :
98+ return SAClient ()
99+ except Exception :
100+ pass
101+ elif hasattr (self ._client , "controller" ):
102+ return self ._client
111103
112104 @staticmethod
113105 def extract_arguments (function , * args , ** kwargs ) -> dict :
@@ -144,19 +136,21 @@ def _track(self, user_id: str, event_name: str, data: dict):
144136 self .get_mp_instance ().track (user_id , event_name , data )
145137
146138 def _track_method (self , args , kwargs , success : bool ):
139+ client = self .get_client ()
140+ if not client :
141+ return
147142 function_name = self .function .__name__ if self .function else ""
148143 arguments = self .extract_arguments (self .function , * args , ** kwargs )
149144 event_name , properties = self .default_parser (function_name , arguments )
150-
151- user_id = self .client .controller .team_data .creator_id
152- team_name = self .client .controller .team_data .name
145+ user_id = client .controller .team_data .creator_id
146+ team_name = client .controller .team_data .name
153147
154148 properties ["Success" ] = success
155149 default = self .get_default_payload (team_name = team_name , user_id = user_id )
156150 self ._track (
157151 user_id ,
158152 event_name ,
159- {** default , ** properties , ** Session .get_current_session ().data },
153+ {** default , ** properties , ** CONFIG .get_current_session ().data },
160154 )
161155
162156 def __get__ (self , obj , owner = None ):
@@ -183,7 +177,9 @@ def __call__(self, *args, **kwargs):
183177class TrackableMeta (type ):
184178 def __new__ (mcs , name , bases , attrs ):
185179 for attr_name , attr_value in attrs .items ():
186- if isinstance (attr_value , FunctionType ):
180+ if isinstance (
181+ attr_value , FunctionType
182+ ) and not attr_value .__name__ .startswith ("_" ):
187183 attrs [attr_name ] = Tracker (validate_arguments (attr_value ))
188184 tmp = super ().__new__ (mcs , name , bases , attrs )
189185 return tmp
0 commit comments