Skip to content

Commit e602db3

Browse files
authored
Merge pull request #239 from superannotateai/sdk_config_fix
Fixed wrong imports
2 parents 6a421a1 + ac90d07 commit e602db3

File tree

9 files changed

+62
-43
lines changed

9 files changed

+62
-43
lines changed
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import logging
2-
31
from lib.infrastructure.controller import Controller
42
from lib.infrastructure.repositories import ConfigRepository
53

64

7-
logger = logging.getLogger()
8-
9-
105
class BaseInterfaceFacade:
116
def __init__(self):
127
self._config_path = None
@@ -15,6 +10,7 @@ def __init__(self):
1510
def controller(self):
1611
if not ConfigRepository().get_one("token"):
1712
raise Exception("Config does not exists!")
13+
controller = Controller.get_instance()
1814
if self._config_path:
19-
return Controller(logger, self._config_path)
20-
return Controller(logger)
15+
controller.init(self._config_path)
16+
return controller

src/superannotate/lib/app/interface/cli_interface.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import logging
32
import os
43
import sys
54
import tempfile
@@ -24,8 +23,8 @@
2423
from lib.infrastructure.controller import Controller
2524
from lib.infrastructure.repositories import ConfigRepository
2625

27-
logger = logging.getLogger()
28-
controller = Controller(logger)
26+
27+
controller = Controller.get_instance()
2928

3029

3130
class CLIFacade(BaseInterfaceFacade):

src/superannotate/lib/app/interface/sdk_interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
from tqdm import tqdm
5454

5555

56+
controller = Controller.get_instance()
5657
logger = logging.getLogger("root")
57-
controller = Controller(logger)
5858

5959

6060
@validate_arguments
@@ -67,7 +67,7 @@ def init(path_to_config_json: str):
6767
:param path_to_config_json: Location to config JSON file
6868
:type path_to_config_json: str or Path
6969
"""
70-
# global controller
70+
global controller
7171
controller.init(path_to_config_json)
7272

7373

src/superannotate/lib/app/interface/types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Union
44

55
from lib.core.enums import AnnotationStatus
6-
from lib.core.exceptions import AppException
76
from pydantic import constr
87
from pydantic import StrictStr
98
from pydantic import validate_arguments as pydantic_validate_arguments

src/superannotate/lib/app/mixp/decorators.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import functools
22
import sys
33

4+
from lib.infrastructure.controller import Controller
45
from mixpanel import Mixpanel
5-
from superannotate.lib.infrastructure.controller import Controller
6-
from superannotate.version import __version__
6+
from version import __version__
77

88
from .config import TOKEN
99
from .utils import parsers
@@ -38,17 +38,20 @@ def track(self, *args, **kwargs):
3838
data = getattr(parsers, self.function.__name__)(*args, **kwargs)
3939
event_name = data["event_name"]
4040
properties = data["properties"]
41+
team_data = self.__class__.TEAM_DATA.data
42+
user_id = team_data.creator_id
43+
team_name = team_data.name
4144
properties["Success"] = self._success
4245
default = get_default(
43-
team_name=self.__class__.TEAM_DATA[1],
44-
user_id=self.__class__.TEAM_DATA[0],
46+
team_name=team_name,
47+
user_id=user_id,
4548
project_name=properties.get("project_name", None),
4649
)
4750
properties.pop("project_name", None)
4851
properties = {**default, **properties}
4952

5053
if "pytest" not in sys.modules:
51-
mp.track(controller.user_id, event_name, properties)
54+
mp.track(user_id, event_name, properties)
5255
except Exception as _:
5356
pass
5457

src/superannotate/lib/app/mixp/utils/parsers.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import logging
1+
import lib.core as constances
2+
from lib.app.helpers import extract_project_folder
3+
from lib.core.enums import ProjectType
4+
from lib.infrastructure.controller import Controller
25

3-
import superannotate.lib.core as constances
4-
from superannotate.lib.app.helpers import extract_project_folder
5-
from superannotate.lib.core.enums import ProjectType
6-
from superannotate.lib.infrastructure.controller import Controller
7-
8-
controller = Controller(logger=logging.getLogger())
6+
controller = Controller.get_instance()
97

108

119
def get_project_name(project):

src/superannotate/lib/core/usecases/projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ def execute(self):
833833
try:
834834
self._response.data = self._teams.get_one(self._team_id)
835835
except Exception:
836-
raise AppException("Can't get team data.")
836+
raise AppException("Can't get team data.") from None
837837
return self._response
838838

839839

src/superannotate/lib/infrastructure/controller.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import io
3+
import logging
34
from pathlib import Path
45
from typing import Iterable
56
from 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

4850
class 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

202213
class 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):

tests/unit/test_controller_init.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
from contextlib import contextmanager
55
import pkg_resources
6+
import tempfile
67
from unittest import TestCase
78
from unittest.mock import mock_open
89
from unittest.mock import patch
@@ -65,3 +66,15 @@ def test_init_create(self, input_mock):
6566
)
6667
)
6768
self.assertEqual(out.getvalue().strip(), "Configuration file successfully created.")
69+
70+
71+
class SKDInitTest(TestCase):
72+
73+
def test_init_flow(self):
74+
with tempfile.TemporaryDirectory() as temp_dir:
75+
token_path = f"{temp_dir}/config.json"
76+
with open(token_path, "w") as temp_config:
77+
json.dump({"token": "token=1234"}, temp_config)
78+
temp_config.close()
79+
import src.superannotate as sa
80+
sa.init(token_path)

0 commit comments

Comments
 (0)