Files
vaiola/modelspace/Repository.py

54 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import uuid
from dataclasses import dataclass
from pathlib import Path
from modelspace.ModelPackage import ModelPackage
from modelspace.ModelPackageSubRepository import ModelPackageSubRepository
from pythonapp.Libs.ConfigDataClass import Config
@dataclass
class RepoConfig(Config):
"""Конфигурация репозитория с сидом"""
seed: str = "" # UUID сида
class Repository:
def __init__(self, path: str):
self.path = Path(path)
# Создаем директорию если она не существует
self.path.mkdir(parents=True, exist_ok=True)
self.config_file = self.path / "repo.json"
# Создаем конфигурацию
self.config = RepoConfig(
filename=str(self.config_file),
autosave=True
)
# Проверяем и устанавливаем сид
if not self.config.seed:
self._generate_and_save_seed()
# Создаем поддиректорию model-packages если она не существует
self.model_sub_repo = ModelPackageSubRepository(self.path / "model-packages", self.seed)
# Completed
def _generate_and_save_seed(self) -> None:
"""Генерирует новый UUID и сохраняет его в конфиг"""
self.config.seed = str(uuid.uuid4())
self.config.save() # Сохраняем сразу после генерации
# Completed
@property
def seed(self) -> str:
"""Возвращает текущий сид"""
return self.config.seed
def add_model_package_interactive(self) -> ModelPackage:
return self.model_sub_repo.add_package_interactive()
global_repo = Repository(str(Path('..') / 'repo'))