@@ -52,7 +52,7 @@ def get_instance(cls):
5252
5353
5454class BaseController (metaclass = SingleInstanceMetaClass ):
55- def __init__ (self , config_path = constances . CONFIG_FILE_LOCATION , token : str = None ):
55+ def __init__ (self , config_path : str , token : str = None ):
5656 self ._team_data = None
5757 self ._token = None
5858 self ._backend_url = None
@@ -69,36 +69,36 @@ def __init__(self, config_path=constances.CONFIG_FILE_LOCATION, token: str = Non
6969 self ._user_id = None
7070 self ._team_name = None
7171 self ._reporter = None
72- self ._config_path = expanduser (config_path )
73- self ._token , self ._backend_url = (
74- os .environ .get ("SA_TOKEN" ),
75- os .environ .get ("SA_URL" ),
76- )
72+ self ._ssl_verify = not os .environ .get ("SA_TESTING" , False )
73+ self ._config_path = expanduser (config_path ) if config_path else constances .CONFIG_FILE_LOCATION
74+
75+ self ._backend_url = os .environ .get ("SA_URL" , constances .BACKEND_URL )
76+ if not token and not config_path :
77+ env_token = os .environ .get ("SA_TOKEN" )
78+ if env_token :
79+ self ._token = self ._validate_token (os .environ .get ("SA_TOKEN" ))
80+ if token :
81+ self ._token = self ._validate_token (token )
7782
78- if not self ._token and token :
79- if self ._validate_token (token ):
80- self ._token = token
81- else :
82- raise AppException ("Invalid token." )
83- if not self ._token and config_path :
84- self ._token , self ._backend_url , ssl_verify = self .retrieve_configs (
85- Path (config_path ), raise_exception = False
83+ if not self ._token :
84+ self ._token , self ._backend_url , self ._ssl_verify = self .retrieve_configs (
85+ Path (self ._config_path ), raise_exception = False
8686 )
8787
8888 def retrieve_configs (
8989 self , path : Path , raise_exception = True
9090 ) -> Tuple [Optional [str ], Optional [str ], Optional [str ]]:
91- if not path .is_file ():
91+ if not path .is_file () or not os . access ( path , os . R_OK ) :
9292 if raise_exception :
9393 raise AppException (
9494 f"SuperAnnotate config file { str (path )} not found."
9595 f" Please provide correct config file location to sa.init(<path>) or use "
9696 f"CLI's superannotate init to generate default location config file."
9797 )
9898 try :
99- config_repo = ConfigRepository (self . _config_path )
99+ config_repo = ConfigRepository (str ( path ) )
100100 return (
101- config_repo .get_one ("token" ).value ,
101+ self . _validate_token ( config_repo .get_one ("token" ).value ) ,
102102 config_repo .get_one ("main_endpoint" ).value ,
103103 config_repo .get_one ("ssl_verify" ).value ,
104104 )
@@ -110,23 +110,32 @@ def retrieve_configs(
110110 return None , None , None
111111
112112 def init (
113- self , config_path : str = constances . CONFIG_FILE_LOCATION , token : str = None
113+ self , token : str = None , backend_url : str = None , config_path : str = None ,
114114 ):
115+ if backend_url :
116+ self ._backend_url = backend_url
115117 if token :
116118 if self ._validate_token (token ):
117119 self ._token = token
120+ return self
118121 else :
119122 raise AppException ("Invalid token." )
120- if not token and config_path :
121- self ._config_path = config_path
122- self ._token , self ._backend_url , ssl_verify = self .retrieve_configs (
123- Path (config_path ), raise_exception = True
123+ if not config_path :
124+ raise AppException (
125+ " Please provide correct config file location to sa.init(<path>)."
124126 )
125- self ._ssl_verify = False # TODO fix if ssl_verify is False else True
127+ self ._config_path = config_path
128+ self ._token , self ._backend_url , ssl_verify = self .retrieve_configs (
129+ Path (config_path ), raise_exception = True
130+ )
131+ self ._ssl_verify = ssl_verify
132+ return self
126133
127134 @property
128135 def backend_client (self ):
129136 if not self ._backend_client :
137+ if not self ._token :
138+ raise AppException ("Team token not provided" )
130139 self ._backend_client = SuperannotateBackendService (
131140 api_url = self ._backend_url ,
132141 auth_token = self ._token ,
@@ -164,14 +173,12 @@ def _validate_token(token: str):
164173 int (token .split ("=" )[- 1 ])
165174 except ValueError :
166175 raise AppException ("Invalid token." )
176+ return token
167177
168178 def set_token (self , token : str , backend_url : str = constances .BACKEND_URL ):
169- self ._validate_token (token )
170- self ._token = token
179+ self ._token = self . _validate_token (token )
180+ self ._backend_url = backend_url
171181 self ._backend_client = self .backend_client
172- self ._backend_client ._api_url = backend_url
173- self ._backend_client ._auth_token = token
174- self ._backend_client .get_session .cache_clear ()
175182
176183 @property
177184 def projects (self ):
@@ -254,7 +261,8 @@ def annotation_validators(self) -> AnnotationValidators:
254261
255262
256263class Controller (BaseController ):
257- def __init__ (self , config_path = constances .CONFIG_FILE_LOCATION ):
264+
265+ def __init__ (self , config_path : str = None ):
258266 super ().__init__ (config_path )
259267 self ._team = None
260268
0 commit comments