Source code for bibble.model

  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
 20from uuid import UUID, uuid1
 21
 22# ##-- end stdlib imports
 23
 24# ##-- 3rd party imports
 25from bibtexparser import model
 26from jgdv import Maybe, Proto
 27
 28# ##-- end 3rd party imports
 29
 30# ##-- 1st party imports
 31import bibble._interface as API
 32# ##-- end 1st party imports
 33
 34# ##-- types
 35# isort: off
 36import abc
 37import collections.abc
 38from typing import TYPE_CHECKING, cast, assert_type, assert_never
 39from typing import Generic, NewType
 40# Protocols:
 41from typing import Protocol, runtime_checkable
 42# Typing Decorators:
 43from typing import no_type_check, final, override, overload
 44
 45if TYPE_CHECKING:
 46    from jgdv import Maybe
 47    from typing import Final
 48    from typing import ClassVar, Any, LiteralString
 49    from typing import Never, Self, Literal
 50    from typing import TypeGuard
 51    from collections.abc import Iterable, Iterator, Callable, Generator
 52    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 53
 54    from bibtexparser.libary import Library
 55##--|
 56
 57# isort: on
 58# ##-- end types
 59
 60##-- logging
 61logging = logmod.getLogger(__name__)
 62##-- end logging
 63
[docs] 64@Proto(API.CustomWriteBlock_p) 65class MetaBlock(model.Block): 66 """ A Metadata Block baseclass that does not get written out (typically), 67 But can hold information about the library 68 """ 69
[docs] 70 @classmethod 71 def find_in(cls, lib:Library) -> Maybe[Self]: 72 """ Find a block of this cls in a given library """ 73 for block in lib.blocks: 74 if isinstance(block, cls): 75 return block 76 else: 77 return None
78 79 def __init__(self, **kwargs): 80 super().__init__(0) 81 self.data = dict(kwargs) 82
[docs] 83 def visit(self, *args, **kwargs) -> list[str]: 84 return []
85 86
[docs] 87class FailedBlock(model.MiddlewareErrorBlock): 88 """ Records errors encountered by a middleware """ 89 90 def __init__(self, *, block:model.Block, error:Exception, source:type|str): 91 super().__init__(block, error) 92 self._block_type = type(block).__name__ 93 match source: 94 case API.Middleware_p() as mw: 95 self.source_middleware = type(source).__name__ 96 case type(): 97 self.source_middleware = source.__name__ 98 case str(): 99 self.source_middleware = source 100 case x: 101 raise TypeError(type(x)) 102 103 104 def __repr__(self): 105 key = self.ignore_error_block.key 106 return f"<{self.__class__.__name__}: {key}>" 107
[docs] 108 def report(self, *, i:int, total:int, source_file:Maybe[str|pl.Path]=None, **kwargs) -> list[str]: 109 match source_file: 110 case None: 111 report = f"({i}/{total}) [{self.source_middleware}] Bad <{self._block_type}>: {self.start_line} : {self.error}" 112 case str() | pl.Path(): 113 report = f"({i}/{total}) [{self.source_middleware}] Bad <{self._block_type}>: {source_file}:{self.start_line} : {self.error}" 114 case x: 115 raise TypeError(type(x)) 116 117 return [report]
118