Skip to content

Commit 6e286f5

Browse files
committed
[Ex oracle] Improve handling oracle mode(dev/prod/test)
1 parent 93284c9 commit 6e286f5

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

packages/examples/cvat/exchange-oracle/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from src.core.config import Config
55

66
if __name__ == "__main__":
7-
is_dev = Config.environment == "development"
7+
is_dev = Config.is_development_mode()
88
Config.validate()
99
register_in_kvstore()
1010

packages/examples/cvat/exchange-oracle/src/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ async def startup_event():
3232
logger.info("Exchange Oracle is up and running!")
3333

3434

35-
is_test = Config.environment == "test"
36-
if not is_test:
35+
if not Config.is_test_mode():
3736
setup_cron_jobs(app)

packages/examples/cvat/exchange-oracle/src/core/config.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import inspect
55
import os
66
from collections.abc import Iterable
7-
from typing import ClassVar
7+
from enum import Enum
8+
from typing import ClassVar, Optional
89

910
from attrs.converters import to_bool
1011
from dotenv import load_dotenv
@@ -295,12 +296,25 @@ def validate(cls) -> None:
295296
raise Exception(" ".join([ex_prefix, str(ex)]))
296297

297298

298-
class Config:
299-
_DEVELOPMENT_MODE = "development"
299+
class Environment(str, Enum):
300+
PRODUCTION = "production"
301+
DEVELOPMENT = "development"
302+
TEST = "test"
303+
304+
@classmethod
305+
def _missing_(cls, value: str) -> Optional["Environment"]:
306+
value = value.lower()
307+
for member in cls:
308+
if member.value == value:
309+
return member
310+
311+
return None
312+
300313

314+
class Config:
301315
debug = to_bool(os.environ.get("DEBUG", "false"))
302316
port = int(os.environ.get("PORT", 8000))
303-
environment = os.environ.get("ENVIRONMENT", _DEVELOPMENT_MODE)
317+
environment = Environment(os.environ.get("ENVIRONMENT", Environment.DEVELOPMENT.value))
304318
workers_amount = int(os.environ.get("WORKERS_AMOUNT", 1))
305319
webhook_max_retries = int(os.environ.get("WEBHOOK_MAX_RETRIES", 5))
306320
webhook_delay_if_failed = int(os.environ.get("WEBHOOK_DELAY_IF_FAILED", 60))
@@ -323,8 +337,19 @@ class Config:
323337
encryption_config = EncryptionConfig
324338

325339
@classmethod
326-
def is_development(cls) -> bool:
327-
return cls.environment.lower() == cls._DEVELOPMENT_MODE
340+
def is_development_mode(cls) -> bool:
341+
"""Returns whether the oracle is running in development mode or not"""
342+
return cls.environment == Environment.DEVELOPMENT
343+
344+
@classmethod
345+
def is_test_mode(cls) -> bool:
346+
"""Returns whether the oracle is running in testing mode or not"""
347+
return cls.environment == Environment.TEST
348+
349+
@classmethod
350+
def is_production_mode(cls) -> bool:
351+
"""Returns whether the oracle is running in production mode or not"""
352+
return cls.environment == Environment.PRODUCTION
328353

329354
@classmethod
330355
def validate(cls) -> None:

packages/examples/cvat/exchange-oracle/src/handlers/error_handlers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ async def http_exception_handler(_, exc):
3232
return JSONResponse(content=exc.detail.to_dict(), status_code=exc.status_code)
3333
if isinstance(error_detail, str):
3434
return JSONResponse(content={"message": error_detail}, status_code=status_code)
35-
if Config.environment == "development":
35+
if Config.is_development_mode():
3636
return JSONResponse(content={"message": str(error_detail)}, status_code=status_code)
3737

3838
return JSONResponse(content={"message": "Something went wrong"}, status_code=status_code)
3939

4040
@app.exception_handler(Exception)
4141
async def generic_exception_handler(_, exc: Exception):
4242
message = (
43-
"Something went wrong"
44-
if Config.environment != "development"
45-
else ".".join(map(str, exc.args))
43+
"Something went wrong" if Config.is_production_mode() else ".".join(map(str, exc.args))
4644
)
4745

4846
return JSONResponse(content={"message": message}, status_code=500)

packages/examples/cvat/exchange-oracle/src/handlers/job_creation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def _setup_gt_job_for_cvat_task(
210210
with annotations_archive_path.open("wb") as annotations_archive:
211211
write_dir_to_zip_archive(export_dir, annotations_archive)
212212

213-
if Config.is_development():
213+
if Config.is_development_mode():
214214
self._save_cvat_gt_dataset_to_oracle_bucket(
215215
gt_dataset_path=annotations_archive_path, file_suffix=f"for_task_{task_id}"
216216
)

packages/examples/cvat/exchange-oracle/src/log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_logger_name(module_name: str) -> str:
1616

1717
def setup_logging():
1818
log_level_name = logging.getLevelName(
19-
Config.loglevel or (logging.DEBUG if Config.environment == "development" else logging.INFO)
19+
Config.loglevel or (logging.DEBUG if Config.is_development_mode() else logging.INFO)
2020
)
2121

2222
log_config = {

0 commit comments

Comments
 (0)