pytex/PyTeX/build/versioning/version_info/version_info.py

127 lines
3.8 KiB
Python

from typing import Optional, List, Dict
from PyTeX.format.repo_status_info import RepoStatusInfo
from .constants import *
from ....format.config import Config
from ...build.enums import *
class FileVersionInfo(Config):
def __init__(self):
self._relative_name: Optional[str] = None
self._file_hash: Optional[str] = None
self._sources_hash: Optional[str] = None
# Meta properties actually not needed for build itself
self._repo_status_info: Optional[RepoStatusInfo] = None
self._build_time: Optional[str] = None
def set_from_json(self, content: Optional[Dict]):
content = self._fill_keys(content)
self._relative_name = content[JSON_RELATIVE_NAME]
self._file_hash = content[JSON_FILE_HASH]
self._sources_hash = content[JSON_SOURCES_HASH]
self._build_time = content[JSON_BUILD_TIME]
self._repo_status_info = RepoStatusInfo.from_json(
content[JSON_REPO_STATUS_INFO]
)
def to_json(self) -> Dict:
return {
JSON_RELATIVE_NAME: self._relative_name,
JSON_FILE_HASH: self._file_hash,
JSON_SOURCES_HASH: self._sources_hash,
JSON_BUILD_TIME: self._build_time,
JSON_REPO_STATUS_INFO: self._repo_status_info.to_json()
}
@property
def file_hash(self) -> str:
if self._file_hash is None:
return DEFAULT_HASH
else:
return self._file_hash
@file_hash.setter
def file_hash(self, file_hash: str):
self._file_hash = file_hash
@property
def sources_hash(self) -> str:
if self._sources_hash is None:
return DEFAULT_HASH
else:
return self._sources_hash
@sources_hash.setter
def sources_hash(self, sources_hash: str):
self._sources_hash = sources_hash
@property
def relative_name(self) -> str:
if self._relative_name is None:
return NO_RELATIVE_NAME
else:
return self._relative_name
@relative_name.setter
def relative_name(self, relative_name: str):
self._relative_name = relative_name
@property
def repo_status_info(self) -> RepoStatusInfo:
if self._repo_status_info is None:
return RepoStatusInfo()
else:
return self._repo_status_info
@repo_status_info.setter
def repo_status_info(self, repo_status_info: RepoStatusInfo):
self._repo_status_info = repo_status_info
@property
def build_time(self) -> str:
if self._build_time is None:
return NO_BUILD_TIME
else:
return self._build_time
@build_time.setter
def build_time(self, build_time: str):
self._build_time = build_time
class VersionInfo(Config):
def __init__(self):
self._pytex_dir_type: Optional[PyTeXRootDirType] = None
self._file_versions: Optional[List[FileVersionInfo]] = None
def set_from_json(self, content: Optional[Dict]):
content = self._fill_keys(content)
self._pytex_dir_type = None # TODO
self._file_versions = [
FileVersionInfo.from_json(entry)
for entry in content[JSON_FILE_VERSIONS]
]
def to_json(self) -> Dict:
return {
JSON_FILE_VERSIONS: [
file_version_info.to_json()
for file_version_info in self._file_versions
]
}
@property
def pytex_dir_type(self) -> PyTeXRootDirType:
if self._pytex_dir_type is None:
return PyTeXRootDirType.PYTEX_SOURCE
else:
return self._pytex_dir_type
@property
def file_versions(self) -> List[FileVersionInfo]:
if self._file_versions is None:
return []
else:
return self._file_versions