Skip to content

Commit 2e60048

Browse files
authored
Merge branch 'friday' into assests_provider
2 parents 01c2f00 + f48a5d1 commit 2e60048

27 files changed

+567
-1312
lines changed

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
minversion = 3.7
33
log_cli=true
44
python_files = test_*.py
5-
addopts = -n auto --dist=loadscope
5+
;addopts = -n auto --dist=loadscope

src/superannotate/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import logging.config # noqa
77
import requests # noqa
88
from packaging.version import parse # noqa
9+
from superannotate.lib.app.input_converters import convert_json_version # noqa
10+
from superannotate.lib.app.input_converters import convert_project_type # noqa
911
from superannotate.lib.app.analytics.class_analytics import class_distribution # noqa
1012
from superannotate.lib.app.exceptions import AppException # noqa
1113
from superannotate.lib.app.input_converters import convert_json_version # noqa
@@ -18,14 +20,15 @@
1820
from superannotate.lib.core import PACKAGE_VERSION_UPGRADE # noqa
1921
from superannotate.logger import get_default_logger # noqa
2022
from superannotate.version import __version__ # noqa
21-
23+
import superannotate.lib.core.enums as enums # noqa
2224

2325
SESSIONS = {}
2426

2527
__all__ = [
2628
"__version__",
2729
"SAClient",
2830
# Utils
31+
"enums",
2932
"AppException",
3033
# analytics
3134
"class_distribution",
Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import functools
22
import os
33
import sys
4-
from abc import abstractmethod
54
from inspect import signature
65
from pathlib import Path
76
from types import FunctionType
87
from typing import Iterable
98
from typing import Sized
9+
from typing import Tuple
1010

1111
import lib.core as constants
1212
from lib.app.helpers import extract_project_folder
1313
from lib.app.interface.types import validate_arguments
14+
from lib.core import CONFIG
1415
from lib.core.exceptions import AppException
15-
from lib.core.reporter import Session
1616
from lib.infrastructure.controller import Controller
1717
from lib.infrastructure.repositories import ConfigRepository
1818
from mixpanel import Mixpanel
@@ -22,67 +22,56 @@
2222
class BaseInterfaceFacade:
2323
REGISTRY = []
2424

25-
def __init__(
26-
self,
27-
token: str = None,
28-
config_path: str = constants.CONFIG_PATH,
29-
):
30-
host = constants.BACKEND_URL
31-
env_token = os.environ.get("SA_TOKEN")
25+
def __init__(self, token: str = None, config_path: str = None):
3226
version = os.environ.get("SA_VERSION", "v1")
33-
ssl_verify = bool(os.environ.get("SA_SSL", True))
27+
_token, _config_path = None, None
28+
_host = os.environ.get("SA_URL", constants.BACKEND_URL)
29+
_ssl_verify = bool(os.environ.get("SA_SSL", True))
3430
if token:
35-
token = Controller.validate_token(token=token)
36-
elif env_token:
37-
host = os.environ.get("SA_URL", constants.BACKEND_URL)
38-
39-
token = Controller.valdate_token(env_token)
31+
_token = Controller.validate_token(token=token)
32+
elif config_path:
33+
_token, _host, _ssl_verify = self._retrieve_configs(config_path)
4034
else:
41-
config_path = os.path.expanduser(str(config_path))
42-
if not Path(config_path).is_file() or not os.access(config_path, os.R_OK):
43-
raise AppException(
44-
f"SuperAnnotate config file {str(config_path)} not found."
45-
f" Please provide correct config file location to sa.init(<path>) or use "
46-
f"CLI's superannotate init to generate default location config file."
35+
_token = os.environ.get("SA_TOKEN")
36+
if not _token:
37+
_token, _host, _ssl_verify = self._retrieve_configs(
38+
constants.CONFIG_PATH
4739
)
48-
config_repo = ConfigRepository(config_path)
49-
token, host, ssl_verify = (
50-
Controller.validate_token(config_repo.get_one("token").value),
51-
config_repo.get_one("main_endpoint").value,
52-
config_repo.get_one("ssl_verify").value,
53-
)
54-
self._host = host
55-
self._token = token
56-
self.controller = Controller(token, host, ssl_verify, version)
40+
self._token, self._host = _token, _host
41+
self.controller = Controller(_token, _host, _ssl_verify, version)
42+
BaseInterfaceFacade.REGISTRY.append(self)
5743

58-
def __new__(cls, *args, **kwargs):
59-
obj = super().__new__(cls, *args, **kwargs)
60-
cls.REGISTRY.append(obj)
61-
return obj
44+
@staticmethod
45+
def _retrieve_configs(path) -> Tuple[str, str, str]:
46+
config_path = os.path.expanduser(str(path))
47+
if not Path(config_path).is_file() or not os.access(config_path, os.R_OK):
48+
raise AppException(
49+
f"SuperAnnotate config file {str(config_path)} not found."
50+
)
51+
config_repo = ConfigRepository(config_path)
52+
return (
53+
Controller.validate_token(config_repo.get_one("token").value),
54+
config_repo.get_one("main_endpoint").value,
55+
config_repo.get_one("ssl_verify").value,
56+
)
6257

6358
@property
64-
@abstractmethod
6559
def host(self):
66-
raise NotImplementedError
60+
return self._host
6761

6862
@property
69-
@abstractmethod
7063
def token(self):
71-
raise NotImplementedError
72-
73-
@property
74-
@abstractmethod
75-
def logger(self):
76-
raise NotImplementedError
64+
return self._token
7765

7866

7967
class Tracker:
8068
def get_mp_instance(self) -> Mixpanel:
81-
if self.client:
82-
if self.client.host == constants.BACKEND_URL:
83-
return Mixpanel("ca95ed96f80e8ec3be791e2d3097cf51")
84-
else:
85-
return Mixpanel("e741d4863e7e05b1a45833d01865ef0d")
69+
client = self.get_client()
70+
mp_token = "ca95ed96f80e8ec3be791e2d3097cf51"
71+
if client:
72+
if client.host != constants.BACKEND_URL:
73+
mp_token = "e741d4863e7e05b1a45833d01865ef0d"
74+
return Mixpanel(mp_token)
8675

8776
@staticmethod
8877
def get_default_payload(team_name, user_id):
@@ -98,16 +87,19 @@ def __init__(self, function):
9887
self._client = None
9988
functools.update_wrapper(self, function)
10089

101-
@property
102-
def client(self):
90+
def get_client(self):
10391
if not self._client:
10492
if BaseInterfaceFacade.REGISTRY:
105-
self._client = BaseInterfaceFacade.REGISTRY[-1]
93+
return BaseInterfaceFacade.REGISTRY[-1]
10694
else:
10795
from lib.app.interface.sdk_interface import SAClient
10896

109-
self._client = SAClient()
110-
return self._client
97+
try:
98+
return SAClient()
99+
except Exception:
100+
pass
101+
elif hasattr(self._client, "controller"):
102+
return self._client
111103

112104
@staticmethod
113105
def extract_arguments(function, *args, **kwargs) -> dict:
@@ -144,19 +136,21 @@ def _track(self, user_id: str, event_name: str, data: dict):
144136
self.get_mp_instance().track(user_id, event_name, data)
145137

146138
def _track_method(self, args, kwargs, success: bool):
139+
client = self.get_client()
140+
if not client:
141+
return
147142
function_name = self.function.__name__ if self.function else ""
148143
arguments = self.extract_arguments(self.function, *args, **kwargs)
149144
event_name, properties = self.default_parser(function_name, arguments)
150-
151-
user_id = self.client.controller.team_data.creator_id
152-
team_name = self.client.controller.team_data.name
145+
user_id = client.controller.team_data.creator_id
146+
team_name = client.controller.team_data.name
153147

154148
properties["Success"] = success
155149
default = self.get_default_payload(team_name=team_name, user_id=user_id)
156150
self._track(
157151
user_id,
158152
event_name,
159-
{**default, **properties, **Session.get_current_session().data},
153+
{**default, **properties, **CONFIG.get_current_session().data},
160154
)
161155

162156
def __get__(self, obj, owner=None):
@@ -183,7 +177,9 @@ def __call__(self, *args, **kwargs):
183177
class TrackableMeta(type):
184178
def __new__(mcs, name, bases, attrs):
185179
for attr_name, attr_value in attrs.items():
186-
if isinstance(attr_value, FunctionType):
180+
if isinstance(
181+
attr_value, FunctionType
182+
) and not attr_value.__name__.startswith("_"):
187183
attrs[attr_name] = Tracker(validate_arguments(attr_value))
188184
tmp = super().__new__(mcs, name, bases, attrs)
189185
return tmp

0 commit comments

Comments
 (0)