Source code for bibble.library

  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 collections import defaultdict
 21from uuid import UUID, uuid1
 22
 23# ##-- end stdlib imports
 24
 25# ##-- 3rd party imports
 26from jgdv import Proto
 27from bibtexparser.library import Library
 28from bibtexparser.middlewares import BlockMiddleware
 29
 30# ##-- end 3rd party imports
 31
 32import bibble._interface as API
 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# from dataclasses import InitVar, dataclass, field
 45# from pydantic import BaseModel, Field, model_validator, field_validator, ValidationError
 46
 47if TYPE_CHECKING:
 48    from jgdv import Maybe
 49    from typing import Final
 50    from typing import ClassVar, Any, LiteralString
 51    from typing import Never, Self, Literal
 52    from typing import TypeGuard
 53    from collections.abc import Iterable, Iterator, Callable, Generator
 54    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 55
 56    from API import Middleware
 57##--|
 58
 59# isort: on
 60# ##-- end types
 61
 62##-- logging
 63logging = logmod.getLogger(__name__)
 64##-- end logging
 65
[docs] 66@Proto(API.Library_p) 67class BibbleLib(Library): 68 """ A library with a key value store for extra info 69 Also tracks the individual files used as source 70 """ 71 72 def __init__(self, *args, **kwargs): 73 super().__init__(*args, **kwargs) 74 self._kv_store = defaultdict(set) 75 self.source_files = set() 76
[docs] 77 def add_sublibrary(self, lib:Library, source:Maybe[str|pl.Path]=None) -> Self: 78 """ Merge entries, kv_store and source files into this library 79 will *overwrite* existing kv_store keys 80 """ 81 if source: 82 self.source_files.add(source) 83 84 match lib: 85 case BibbleLib(): 86 self.add(lib.entries) 87 self.source_files.update(lib.source_files) 88 for k,v in lib._kv_store.items(): 89 self.store_meta_value(k, v) 90 case Library(): 91 self.add(lib.entries) 92 case _: 93 raise TypeError("Bad update sublibrary") 94 95 return self
96
[docs] 97 def store_meta_value(self, key:str|Middleware, value:Any): 98 raise DeprecationWarning("Use a MetaBlock")
99
[docs] 100 def get_meta_value(self, key) -> set|Any: 101 raise DeprecationWarning("Use a MetaBlock")