Skip to content

Commit 8898179

Browse files
Vaghinak BasentsyanVaghinak Basentsyan
authored andcommitted
Fixed wrong imports
1 parent 6a421a1 commit 8898179

File tree

9 files changed

+50
-33
lines changed

9 files changed

+50
-33
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: 2 additions & 2 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

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: 22 additions & 10 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
@@ -68,15 +70,14 @@ 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", ""))
79-
self._logger.warning("Fill config.json")
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,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

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

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)