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

137 lines
4 KiB
Python
Raw Normal View History

2022-02-07 22:24:14 +01:00
from typing import Optional, List, Dict
2022-02-04 11:39:15 +01:00
2022-02-07 22:24:14 +01:00
from PyTeX.format.repo_status_info import RepoStatusInfo
2022-02-06 15:09:49 +01:00
from .constants import *
2022-02-07 22:24:14 +01:00
from ....format.config import Config
2022-02-08 00:33:29 +01:00
from ...build.enums import *
2022-02-04 11:39:15 +01:00
2022-02-08 00:33:45 +01:00
2022-02-07 22:24:14 +01:00
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]):
2022-02-08 17:40:08 +01:00
filled_content = self._fill_keys(content)
self._relative_name = filled_content[JSON_RELATIVE_NAME]
self._file_hash = filled_content[JSON_FILE_HASH]
self._sources_hash = filled_content[JSON_SOURCES_HASH]
self._build_time = filled_content[JSON_BUILD_TIME]
2022-02-07 22:24:14 +01:00
self._repo_status_info = RepoStatusInfo.from_json(
2022-02-08 17:40:08 +01:00
filled_content[JSON_REPO_STATUS_INFO]
2022-02-07 22:24:14 +01:00
)
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,
2022-02-08 17:40:08 +01:00
JSON_REPO_STATUS_INFO: self.repo_status_info.to_json()
2022-02-07 22:24:14 +01:00
}
@property
def file_hash(self) -> str:
if self._file_hash is None:
return DEFAULT_HASH
2022-02-04 11:39:15 +01:00
else:
2022-02-07 22:24:14 +01:00
return self._file_hash
2022-02-04 11:39:15 +01:00
2022-02-07 22:24:14 +01:00
@file_hash.setter
def file_hash(self, file_hash: str):
self._file_hash = file_hash
2022-02-04 11:39:15 +01:00
@property
2022-02-07 22:24:14 +01:00
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
2022-02-04 11:39:15 +01:00
@property
2022-02-07 22:24:14 +01:00
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
2022-02-04 11:39:15 +01:00
@property
2022-02-07 22:24:14 +01:00
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
2022-02-08 00:33:29 +01:00
class VersionInfo(Config):
def __init__(self):
self._pytex_dir_type: Optional[PyTeXRootDirType] = None
2022-02-08 19:24:48 +01:00
self._files: Optional[List[FileVersionInfo]] = None
2022-02-08 00:33:29 +01:00
def set_from_json(self, content: Optional[Dict]):
2022-02-08 17:40:08 +01:00
filled_content: Dict = self._fill_keys(content)
2022-02-08 00:33:29 +01:00
self._pytex_dir_type = None # TODO
2022-02-08 19:24:48 +01:00
self._files = [
2022-02-08 00:33:29 +01:00
FileVersionInfo.from_json(entry)
2022-02-08 17:40:08 +01:00
for entry in filled_content[JSON_FILE_VERSIONS]
2022-02-08 00:33:29 +01:00
]
def to_json(self) -> Dict:
return {
JSON_FILE_VERSIONS: [
file_version_info.to_json()
2022-02-08 19:24:48 +01:00
for file_version_info in self.files
2022-02-08 00:33:29 +01:00
]
}
@property
def pytex_dir_type(self) -> PyTeXRootDirType:
if self._pytex_dir_type is None:
return PyTeXRootDirType.PYTEX_SOURCE
else:
return self._pytex_dir_type
2022-02-08 19:24:48 +01:00
@pytex_dir_type.setter
def pytex_dir_type(self, pytex_dir_type: PyTeXRootDirType):
self._pytex_dir_type = pytex_dir_type
2022-02-08 00:33:29 +01:00
@property
2022-02-08 19:24:48 +01:00
def files(self) -> List[FileVersionInfo]:
if self._files is None:
2022-02-08 00:33:29 +01:00
return []
else:
2022-02-08 19:24:48 +01:00
return self._files
@files.setter
def files(self, files: List[FileVersionInfo]):
self._files = files