class Authors: def __init__(self, update_data=False): self.data_repo = DataRepo() if update_data: self.data_repo.pull() with open(self.data_repo.get_data_path().joinpath( "authors_mapping.json"), "r", encoding="utf-8") as json_file: self._mapping = json.load(json_file) self._authors: Dict[str, Author] = {} with open(self.data_repo.get_data_path().joinpath("authors.json"), "r", encoding="utf-8") as json_file: json_dict = json.load(json_file) for author in json_dict: self._authors[author] = Author(author, json_dict[author]) def __iter__(self) -> Generator[Author, None, None]: for author in sorted( self.authors_dict.values(), key=lambda item: f"{item.last_name}, {item.first_name}"): if not author.redirect: yield author @lru_cache(maxsize=1000) def get_author_by_mapping(self, name: str, issue: str) -> List[Author]: author_list = [] with contextlib.suppress(KeyError): mapping = self._mapping[name] if isinstance(mapping, dict): try: mapping = mapping[issue] except KeyError: mapping = mapping["*"] if isinstance(mapping, str): mapping = [mapping] for item in mapping: author_list.append(self.get_author(item)) return author_list def get_author(self, author_key: str) -> Author: author = self._authors[author_key.replace("|", "")] if author.redirect: author = self._authors[author.redirect] return author def set_mappings(self, mapping: Dict[str, str]): self._mapping.update(mapping) def set_author(self, mapping: Dict[str, AuthorDict]): for author_key in mapping: if author_key in self._authors: self._authors[author_key].update_internal_dict( mapping[author_key]) else: self._authors[author_key] = Author(author_key, mapping[author_key]) def _to_dict(self) -> Dict[str, AuthorDict]: author_dict = {} for dict_key in sorted(self._authors.keys()): author_dict[dict_key] = self._authors[dict_key].to_dict() return author_dict def persist(self): with open(self.data_repo.get_data_path().joinpath( "authors_mapping.json"), "w", encoding="utf-8") as json_file: json.dump(self._mapping, json_file, sort_keys=True, indent=2, ensure_ascii=False) with open(self.data_repo.get_data_path().joinpath("authors.json"), "w", encoding="utf-8") as json_file: json.dump(self._to_dict(), json_file, sort_keys=True, indent=2, ensure_ascii=False) @property def authors_dict(self) -> Dict[str, Author]: return self._authors @property def authors_mapping(self): return self._mapping
class Registers: def __init__(self, update_data=False): self.repo = DataRepo() if update_data: self.repo.pull() self._authors: Authors = Authors() self._registers: Dict[str, VolumeRegister] = OrderedDict() self._alphabetic_registers: Dict[str, AlphabeticRegister] = OrderedDict() for volume in Volumes().all_volumes: with contextlib.suppress(FileNotFoundError): self._registers[volume.name] = VolumeRegister( volume, self._authors) def __getitem__(self, item) -> VolumeRegister: return self._registers[item] def persist(self): for register in self._registers.values(): register.persist() @property def alphabetic(self) -> Generator[AlphabeticRegister, None, None]: for idx, start in enumerate(RE_ALPHABET): end = "zzzzzz" before_start = None after_next_start = None with contextlib.suppress(IndexError): end = RE_ALPHABET[idx + 1] with contextlib.suppress(IndexError): before_start = RE_ALPHABET[idx - 1] with contextlib.suppress(IndexError): after_next_start = RE_ALPHABET[idx + 2] yield AlphabeticRegister(start, end, before_start, after_next_start, self._registers) @property def author(self) -> Generator[AuthorRegister, None, None]: for author in self.authors: register = AuthorRegister(author, self.authors, self._registers) if len(register) > 0: yield register @property def short(self) -> Generator[ShortRegister, None, None]: for main_volume in Volumes().main_volumes: register = ShortRegister(main_volume, self._registers) yield register @property def pd(self) -> Generator[PublicDomainRegister, None, None]: current_year = datetime.now().year for year in range(current_year - 5, current_year + 5): register = PublicDomainRegister(year, self._authors, self._registers) yield register @property def volumes(self) -> Dict[str, VolumeRegister]: return self._registers @property def authors(self) -> Authors: return self._authors