Skip to content

Commit 63c9d2a

Browse files
Vaghinak BasentsyanVaghinak Basentsyan
authored andcommitted
Fix config path issue
1 parent c2e1229 commit 63c9d2a

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/superannotate/lib/infrastructure/controller.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,24 @@ def __init__(self, config_path=constances.CONFIG_FILE_LOCATION):
6161
self._team_id = None
6262
self._user_id = None
6363
self._team_name = None
64-
self.init(config_path)
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", ""))
69+
try:
70+
self.init(config_path)
71+
except AppException:
72+
pass
6573

66-
def init(self, config_path):
74+
def init(self, config_path=constances.CONFIG_FILE_LOCATION):
75+
config_path = Path(config_path)
76+
if not config_path.is_file():
77+
raise AppException(
78+
f"SuperAnnotate config file {str(config_path)} not found."
79+
f" Please provide correct config file location to sa.init(<path>) or use "
80+
f"CLI's superannotate init to generate default location config file."
81+
)
6782
self._config_path = config_path
6883
token, main_endpoint = (
6984
self.configs.get_one("token"),
@@ -72,13 +87,9 @@ def init(self, config_path):
7287
token = None if not token else token.value
7388
main_endpoint = None if not main_endpoint else main_endpoint.value
7489
if not main_endpoint:
75-
self.configs.insert(ConfigEntity("main_endpoint", constances.BACKEND_URL))
7690
main_endpoint = constances.BACKEND_URL
7791
if not token:
78-
self.configs.insert(ConfigEntity("token", ""))
79-
# TODO check
80-
#self._logger.warning(f"Fill token in the {config_path}")
81-
return
92+
raise AppException(f"Incorrect config file: token is not present in the config file {config_path}")
8293
verify_ssl_entity = self.configs.get_one("ssl_verify")
8394
if not verify_ssl_entity:
8495
verify_ssl = True

src/superannotate/lib/infrastructure/repositories.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import io
22
import json
33
import os
4+
from os.path import expanduser
45
from typing import List
56
from typing import Optional
67

@@ -32,20 +33,20 @@ def __init__(self, config_path: str = constance.CONFIG_FILE_LOCATION):
3233

3334
@property
3435
def config_path(self):
35-
return self._config_path
36+
return expanduser(self._config_path)
3637

3738
def _create_config(self):
3839
"""
3940
Create a config file
4041
"""
41-
os.makedirs(os.path.dirname(self._config_path), exist_ok=True)
42-
open(self._config_path, "w").close()
42+
os.makedirs(os.path.dirname(self.config_path), exist_ok=True)
43+
open(self.config_path, "w").close()
4344
return {}
4445

4546
def _get_config(self) -> Optional[dict]:
46-
if not os.path.exists(self._config_path):
47+
if not os.path.exists(self.config_path):
4748
return
48-
with open(self._config_path) as config:
49+
with open(self.config_path) as config:
4950
return json.load(config)
5051

5152
def get_one(self, uuid: str) -> Optional[ConfigEntity]:

0 commit comments

Comments
 (0)