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
@@ -68,12 +70,11 @@ def init(self, config_path):
6870 self .configs .get_one ("token" ),
6971 self .configs .get_one ("main_endpoint" ),
7072 )
71- if token :
72- token = token .value
73- if main_endpoint :
74- main_endpoint = main_endpoint .value
73+ token = None if not token else token .value
74+ main_endpoint = None if not main_endpoint else main_endpoint .value
7575 if not main_endpoint :
7676 self .configs .insert (ConfigEntity ("main_endpoint" , constances .BACKEND_URL ))
77+ main_endpoint = constances .BACKEND_URL
7778 if not token :
7879 self .configs .insert (ConfigEntity ("token" , "" ))
7980 self ._logger .warning ("Fill config.json" )
@@ -94,9 +95,18 @@ 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+ if self .is_valid_token (token ):
99+ self ._team_id = int (token .split ("=" )[- 1 ])
98100 self ._team = None
99101
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
109+
100110 @property
101111 def config_path (self ):
102112 return self ._config_path
@@ -173,6 +183,8 @@ def configs(self):
173183 @property
174184 def team_id (self ) -> int :
175185 if not self ._team_id :
186+ if not self .is_valid_token (self .configs .get_one ("token" ).value ):
187+ raise AppException ("Invalid token." )
176188 self ._team_id = int (self .configs .get_one ("token" ).value .split ("=" )[- 1 ])
177189 return self ._team_id
178190
@@ -200,8 +212,8 @@ def s3_repo(self):
200212
201213
202214class Controller (BaseController ):
203- def __init__ (self , logger , config_path = constances .CONFIG_FILE_LOCATION ):
204- super ().__init__ (logger , config_path )
215+ def __init__ (self , config_path = constances .CONFIG_FILE_LOCATION ):
216+ super ().__init__ (config_path )
205217 self ._team = None
206218
207219 def _get_project (self , name : str ):
0 commit comments