11import copy
22import io
3+ import logging
34from pathlib import Path
45from typing import Iterable
56from typing import List
@@ -43,13 +44,14 @@ def __call__(cls, *args, **kwargs):
4344 def get_instance (cls ):
4445 if cls ._instances :
4546 return cls ._instances [cls ]
47+ return cls ()
4648
4749
4850class BaseController (metaclass = SingleInstanceMetaClass ):
49- def __init__ (self , logger , config_path = constances .CONFIG_FILE_LOCATION ):
51+ def __init__ (self , config_path = constances .CONFIG_FILE_LOCATION ):
5052 self ._config_path = None
5153 self ._backend_client = None
52- self ._logger = logger
54+ self ._logger = logging . getLogger ( "root" )
5355 self ._s3_upload_auth_data = None
5456 self ._projects = None
5557 self ._folders = None
@@ -59,7 +61,6 @@ def __init__(self, logger, config_path=constances.CONFIG_FILE_LOCATION):
5961 self ._team_id = None
6062 self ._user_id = None
6163 self ._team_name = None
62- self ._team = None
6364 self .init (config_path )
6465
6566 def init (self , config_path ):
@@ -68,15 +69,15 @@ def init(self, config_path):
6869 self .configs .get_one ("token" ),
6970 self .configs .get_one ("main_endpoint" ),
7071 )
71- if token :
72- token = token .value
73- if main_endpoint :
74- main_endpoint = main_endpoint .value
72+ token = None if not token else token .value
73+ main_endpoint = None if not main_endpoint else main_endpoint .value
7574 if not main_endpoint :
7675 self .configs .insert (ConfigEntity ("main_endpoint" , constances .BACKEND_URL ))
76+ main_endpoint = constances .BACKEND_URL
7777 if not token :
7878 self .configs .insert (ConfigEntity ("token" , "" ))
79- self ._logger .warning ("Fill config.json" )
79+ # TODO check
80+ #self._logger.warning(f"Fill token in the {config_path}")
8081 return
8182 verify_ssl_entity = self .configs .get_one ("ssl_verify" )
8283 if not verify_ssl_entity :
@@ -94,8 +95,17 @@ def init(self, config_path):
9495 self ._backend_client .api_url = main_endpoint
9596 self ._backend_client ._auth_token = token
9697 self ._backend_client .get_session .cache_clear ()
97- self ._team_id = int (self .configs .get_one ("token" ).value .split ("=" )[- 1 ])
98- self ._team = None
98+ if self .is_valid_token (token ):
99+ self ._team_id = int (token .split ("=" )[- 1 ])
100+ self ._teams = None
101+
102+ @staticmethod
103+ def is_valid_token (token : str ):
104+ try :
105+ int (token .split ("=" )[- 1 ])
106+ return True
107+ except Exception :
108+ return False
99109
100110 @property
101111 def config_path (self ):
@@ -142,11 +152,10 @@ def folders(self):
142152 return self ._folders
143153
144154 def get_team (self ):
145- if not self ._team :
146- self ._team = usecases .GetTeamUseCase (
147- teams = self .teams , team_id = self .team_id
148- ).execute ()
149- return self ._team
155+ return usecases .GetTeamUseCase (
156+ teams = self .teams , team_id = self .team_id
157+ ).execute ()
158+
150159
151160 @property
152161 def ml_models (self ):
@@ -173,6 +182,8 @@ def configs(self):
173182 @property
174183 def team_id (self ) -> int :
175184 if not self ._team_id :
185+ if not self .is_valid_token (self .configs .get_one ("token" ).value ):
186+ raise AppException ("Invalid token." )
176187 self ._team_id = int (self .configs .get_one ("token" ).value .split ("=" )[- 1 ])
177188 return self ._team_id
178189
@@ -200,8 +211,8 @@ def s3_repo(self):
200211
201212
202213class Controller (BaseController ):
203- def __init__ (self , logger , config_path = constances .CONFIG_FILE_LOCATION ):
204- super ().__init__ (logger , config_path )
214+ def __init__ (self , config_path = constances .CONFIG_FILE_LOCATION ):
215+ super ().__init__ (config_path )
205216 self ._team = None
206217
207218 def _get_project (self , name : str ):
0 commit comments