Skip to content

Commit 66f848f

Browse files
committed
refactor: use model_validator
Closes #74
1 parent 14c50fe commit 66f848f

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

ktoolbox/configuration.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import datetime
22
import logging
33
import os
4+
import tempfile
45
from pathlib import Path
56
from typing import Literal, Union, Optional
67

78
from loguru import logger
8-
from pydantic import BaseModel
9+
from pydantic import BaseModel, model_validator
910
from pydantic_settings import BaseSettings, SettingsConfigDict
1011

1112
__all__ = [
@@ -72,6 +73,23 @@ class DownloaderConfiguration(BaseModel):
7273
use_bucket: bool = False
7374
bucket_path: Path = Path("./.ktoolbox/bucket_storage")
7475

76+
@model_validator(mode="after")
77+
def check_bucket_path(self) -> "DownloaderConfiguration":
78+
if self.use_bucket:
79+
# noinspection PyBroadException
80+
try:
81+
bucket_path = Path(self.bucket_path)
82+
bucket_path.mkdir(parents=True, exist_ok=True)
83+
with tempfile.TemporaryFile(dir=bucket_path) as temp_file:
84+
temp_link_file_path = f"{bucket_path / temp_file.name}.hlink"
85+
os.link(temp_file.name, temp_link_file_path)
86+
os.remove(temp_link_file_path)
87+
except Exception:
88+
self.use_bucket = False
89+
logger.exception(f"`DownloaderConfiguration.bucket_path` is not available, "
90+
f"`DownloaderConfiguration.use_bucket` has been disabled.")
91+
return self
92+
7593

7694
class PostStructureConfiguration(BaseModel):
7795
# noinspection SpellCheckingInspection
@@ -164,23 +182,3 @@ class Configuration(BaseSettings):
164182

165183

166184
config = Configuration(_env_file='prod.env')
167-
168-
169-
def config_check_bucket():
170-
if config.downloader.use_bucket:
171-
import tempfile
172-
# noinspection PyBroadException
173-
try:
174-
bucket_path = Path(config.downloader.bucket_path)
175-
bucket_path.mkdir(parents=True, exist_ok=True)
176-
with tempfile.TemporaryFile(dir=bucket_path) as temp_file:
177-
temp_link_file_path = f"{bucket_path / temp_file.name}.hlink"
178-
os.link(temp_file.name, temp_link_file_path)
179-
os.remove(temp_link_file_path)
180-
except Exception:
181-
config.downloader.use_bucket = False
182-
logger.exception(f"`DownloaderConfiguration.bucket_path` is not available, "
183-
f"`DownloaderConfiguration.use_bucket` has been disabled.")
184-
185-
186-
config_check_bucket()

0 commit comments

Comments
 (0)