Skip to content

Commit c2b93ba

Browse files
Vaghinak BasentsyanVaghinak Basentsyan
authored andcommitted
SDK new version 4.2.1b1
1 parent b07c5f6 commit c2b93ba

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

src/superannotate/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
from superannotate.lib.app.interface.sdk_interface import benchmark
5858
from superannotate.lib.app.interface.sdk_interface import clone_project
5959
from superannotate.lib.app.interface.sdk_interface import consensus
60+
from superannotate.lib.app.interface.sdk_interface import controller
6061
from superannotate.lib.app.interface.sdk_interface import copy_image
6162
from superannotate.lib.app.interface.sdk_interface import copy_images
6263
from superannotate.lib.app.interface.sdk_interface import create_annotation_class
@@ -162,6 +163,7 @@
162163

163164
__all__ = [
164165
"__version__",
166+
"controller",
165167
# Utils
166168
"AppException",
167169
#

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959

6060
@validate_arguments
61-
def init(path_to_config_json: str):
61+
def init(path_to_config_json: Optional[str] = None):
6262
"""
6363
Initializes and authenticates to SuperAnnotate platform using the config file.
6464
If not initialized then $HOME/.superannotate/config.json

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
mp = Mixpanel(TOKEN)
1212

13-
controller = Controller.get_instance()
14-
1513

1614
def get_default(team_name, user_id, project_name=None):
1715
return {
@@ -57,7 +55,7 @@ def track(self, *args, **kwargs):
5755

5856
def __call__(self, *args, **kwargs):
5957
try:
60-
self.__class__.TEAM_DATA = controller.get_team()
58+
self.__class__.TEAM_DATA = Controller.get_instance().get_team()
6159
ret = self.function(*args, **kwargs)
6260
self._success = True
6361
except Exception as e:

src/superannotate/lib/infrastructure/controller.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import io
33
import logging
4+
from os.path import expanduser
45
from pathlib import Path
56
from typing import Iterable
67
from 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)

src/superannotate/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "5.0.0b47"
1+
__version__ = "4.2.1b1"

tests/unit/test_controller_init.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
from io import StringIO
3+
from os.path import join
34
import json
45
from contextlib import contextmanager
56
import pkg_resources
@@ -8,6 +9,7 @@
89
from unittest.mock import mock_open
910
from unittest.mock import patch
1011

12+
1113
from src.superannotate.lib.app.interface.cli_interface import CLIFacade
1214
from src.superannotate.lib.core import CONFIG_FILE_LOCATION
1315

@@ -69,6 +71,14 @@ def test_init_create(self, input_mock):
6971

7072

7173
class SKDInitTest(TestCase):
74+
VALID_JSON = {
75+
"token": "a"*28 + "=1234"
76+
}
77+
INVALID_JSON ={
78+
"token": "a" * 28 + "=1234asd"
79+
}
80+
FILE_NAME = "config.json"
81+
FILE_NAME_2 = "config.json"
7282

7383
def test_init_flow(self):
7484
with tempfile.TemporaryDirectory() as temp_dir:
@@ -78,3 +88,12 @@ def test_init_flow(self):
7888
temp_config.close()
7989
import src.superannotate as sa
8090
sa.init(token_path)
91+
92+
def test_init(self):
93+
with tempfile.TemporaryDirectory() as temp_dir:
94+
path = join(temp_dir, self.FILE_NAME)
95+
with open(path, "w") as config:
96+
json.dump(self.VALID_JSON, config)
97+
import src.superannotate as sa
98+
sa.init(path)
99+
self.assertEqual(sa.controller.team_id, 1234)

0 commit comments

Comments
 (0)