11import copy
22import io
33import logging
4+ from os .path import expanduser
45from pathlib import Path
56from typing import Iterable
67from typing import List
@@ -61,18 +62,21 @@ def __init__(self, config_path=constances.CONFIG_FILE_LOCATION):
6162 self ._team_id = None
6263 self ._user_id = None
6364 self ._team_name = None
64- self ._config_path = Path (config_path )
65- if str (self ._config_path ) == constances .CONFIG_FILE_LOCATION :
66- if not self ._config_path .is_file ():
67- self .configs .insert (ConfigEntity ("main_endpoint" , constances .BACKEND_URL ))
68- self .configs .insert (ConfigEntity ("token" , "" ))
65+ self ._config_path = expanduser (config_path )
6966 try :
7067 self .init (config_path )
7168 except AppException :
7269 pass
7370
74- def init (self , config_path = constances .CONFIG_FILE_LOCATION ):
75- config_path = Path (config_path )
71+ def init (self , config_path = None ):
72+ if not config_path :
73+ config_path = constances .CONFIG_FILE_LOCATION
74+ config_path = Path (expanduser (config_path ))
75+ if str (config_path ) == constances .CONFIG_FILE_LOCATION :
76+ if not Path (self ._config_path ).is_file ():
77+ self .configs .insert (ConfigEntity ("main_endpoint" , constances .BACKEND_URL ))
78+ self .configs .insert (ConfigEntity ("token" , "" ))
79+ return
7680 if not config_path .is_file ():
7781 raise AppException (
7882 f"SuperAnnotate config file { str (config_path )} not found."
@@ -97,26 +101,27 @@ def init(self, config_path=constances.CONFIG_FILE_LOCATION):
97101 verify_ssl = verify_ssl_entity .value
98102 if not self ._backend_client :
99103 self ._backend_client = SuperannotateBackendService (
100- api_url = self . configs . get_one ( " main_endpoint" ). value ,
101- auth_token = self . configs . get_one ( " token" ). value ,
104+ api_url = main_endpoint ,
105+ auth_token = token ,
102106 logger = self ._logger ,
103107 verify_ssl = verify_ssl ,
104108 )
105109 else :
106110 self ._backend_client .api_url = main_endpoint
107111 self ._backend_client ._auth_token = token
108112 self ._backend_client .get_session .cache_clear ()
109- if self .is_valid_token (token ):
110- self ._team_id = int (token .split ("=" )[- 1 ])
113+ self ._team_id = None
114+ self .validate_token (token )
115+ self ._team_id = int (token .split ("=" )[- 1 ])
111116 self ._teams = None
112117
113118 @staticmethod
114- def is_valid_token (token : str ):
119+ def validate_token (token : str ):
115120 try :
116121 int (token .split ("=" )[- 1 ])
117122 return True
118123 except Exception :
119- return False
124+ raise AppException ( "Invalid token." ) from None
120125
121126 @property
122127 def config_path (self ):
@@ -167,7 +172,6 @@ def get_team(self):
167172 teams = self .teams , team_id = self .team_id
168173 ).execute ()
169174
170-
171175 @property
172176 def ml_models (self ):
173177 if not self ._ml_models :
@@ -176,7 +180,7 @@ def ml_models(self):
176180
177181 @property
178182 def teams (self ):
179- if not self ._teams :
183+ if self . team_id and not self ._teams :
180184 self ._teams = TeamRepository (self ._backend_client )
181185 return self ._teams
182186
@@ -193,9 +197,7 @@ def configs(self):
193197 @property
194198 def team_id (self ) -> int :
195199 if not self ._team_id :
196- if not self .is_valid_token (self .configs .get_one ("token" ).value ):
197- raise AppException ("Invalid token." )
198- self ._team_id = int (self .configs .get_one ("token" ).value .split ("=" )[- 1 ])
200+ raise AppException (f"Invalid credentials provided in the { self ._config_path } ." )
199201 return self ._team_id
200202
201203 @timed_lru_cache (seconds = 3600 )
0 commit comments