Source code for bibble.metadata.isbn_writer

  1#!/usr/bin/env python3
  2"""
  3
  4"""
  5
  6# Imports:
  7from __future__ import annotations
  8
  9# ##-- stdlib imports
 10import datetime
 11import enum
 12import functools as ftz
 13import itertools as itz
 14import logging as logmod
 15import pathlib as pl
 16import re
 17import time
 18import types
 19import weakref
 20import warnings
 21from uuid import UUID, uuid1
 22
 23# ##-- end stdlib imports
 24
 25# ##-- 3rd party imports
 26from jgdv import Proto, Mixin
 27import bibtexparser
 28import bibtexparser.model as model
 29import pyisbn
 30from bibtexparser import middlewares as ms
 31from bibtexparser.middlewares.middleware import (BlockMiddleware,
 32                                                 LibraryMiddleware)
 33
 34# ##-- end 3rd party imports
 35
 36with warnings.catch_warnings():
 37    warnings.simplefilter("ignore", category=SyntaxWarning)
 38    import isbn_hyphenate
 39
 40# ##-- 1st party imports
 41import bibble._interface as API
 42from . import _interface as MAPI
 43from bibble.util.middlecore import IdenBlockMiddleware
 44from bibble.util.mixins import ErrorRaiser_m
 45# ##-- end 1st party imports
 46
 47# ##-- types
 48# isort: off
 49import abc
 50import collections.abc
 51from typing import TYPE_CHECKING, cast, assert_type, assert_never
 52from typing import Generic, NewType
 53# Protocols:
 54from typing import Protocol, runtime_checkable
 55# Typing Decorators:
 56from typing import no_type_check, final, override, overload
 57
 58if TYPE_CHECKING:
 59    from jgdv import Maybe
 60    from typing import Final
 61    from typing import ClassVar, Any, LiteralString
 62    from typing import Never, Self, Literal
 63    from typing import TypeGuard
 64    from collections.abc import Iterable, Iterator, Callable, Generator
 65    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 66
 67##--|
 68
 69# isort: on
 70# ##-- end types
 71
 72##-- logging
 73logging = logmod.getLogger(__name__)
 74##-- end logging
 75
 76##--|
 77
[docs] 78@Proto(API.WriteTime_p) 79@Mixin(ErrorRaiser_m) 80class IsbnWriter(IdenBlockMiddleware): 81 """ 82 format the isbn for writing 83 """ 84
[docs] 85 def on_write(self): 86 Never()
87
[docs] 88 def transform_Entry(self, entry, library) -> list: 89 f_dict = entry.fields_dict 90 if MAPI.ISBN_K not in f_dict: 91 return [entry] 92 if MAPI.INVALID_ISBN_K in f_dict: 93 return [entry] 94 if not bool(f_dict[MAPI.ISBN_K].value): 95 return [entry] 96 97 try: 98 isbn = isbn_hyphenate.hyphenate(f_dict[MAPI.ISBN_K].value) 99 entry.set_field(model.Field(MAPI.ISBN_K, isbn)) 100 return [entry] 101 except isbn_hyphenate.IsbnError as err: 102 self._logger.warning("Writing ISBN failed: %s : %s : %s", entry.key, f_dict[MAPI.ISBN_K].value, err) 103 entry.set_field(model.Field(MAPI.INVALID_ISBN_K, f_dict[MAPI.ISBN_K].value)) 104 entry.set_field(model.Field(MAPI.ISBN_K, "")) 105 return [entry]