|
1 | 1 | import datetime
|
2 | 2 | import logging
|
3 | 3 | import os
|
| 4 | +import tempfile |
4 | 5 | from pathlib import Path
|
5 | 6 | from typing import Literal, Union, Optional
|
6 | 7 |
|
7 | 8 | from loguru import logger
|
8 |
| -from pydantic import BaseModel |
| 9 | +from pydantic import BaseModel, model_validator |
9 | 10 | from pydantic_settings import BaseSettings, SettingsConfigDict
|
10 | 11 |
|
11 | 12 | __all__ = [
|
@@ -72,6 +73,23 @@ class DownloaderConfiguration(BaseModel):
|
72 | 73 | use_bucket: bool = False
|
73 | 74 | bucket_path: Path = Path("./.ktoolbox/bucket_storage")
|
74 | 75 |
|
| 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 | + |
75 | 93 |
|
76 | 94 | class PostStructureConfiguration(BaseModel):
|
77 | 95 | # noinspection SpellCheckingInspection
|
@@ -164,23 +182,3 @@ class Configuration(BaseSettings):
|
164 | 182 |
|
165 | 183 |
|
166 | 184 | 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