Source code for bibble.util.selectors

  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 random import choices
 21from uuid import UUID, uuid1
 22
 23# ##-- end stdlib imports
 24
 25# ##-- 3rd party imports
 26import bibtexparser
 27import bibtexparser.model as model
 28from bibtexparser import middlewares as ms
 29from bibtexparser.middlewares.middleware import (BlockMiddleware,
 30                                                 LibraryMiddleware)
 31from bibtexparser.middlewares.names import (NameParts,
 32                                            parse_single_name_into_parts)
 33
 34# ##-- end 3rd party imports
 35
 36# ##-- types
 37# isort: off
 38import abc
 39import collections.abc
 40from typing import TYPE_CHECKING, cast, assert_type, assert_never
 41from typing import Generic, NewType
 42# Protocols:
 43from typing import Protocol, runtime_checkable
 44# Typing Decorators:
 45from typing import no_type_check, final, override, overload
 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 bibtexparser.libary import Library
 57
 58##--|
 59
 60# isort: on
 61# ##-- end types
 62
 63##-- logging
 64logging = logmod.getLogger(__name__)
 65##-- end logging
 66
[docs] 67class SelectN(LibraryMiddleware): 68 """ Select N random entries """ 69 70 def __init__(self, *, count:int=1): 71 super().__init__() 72 self._count = count 73
[docs] 74 def transform(self, library): 75 entries = library.entries 76 chosen = choices(entries, k=self._count) 77 return library
78
[docs] 79class SelectEntriesByType(LibraryMiddleware): 80 """ Select entries of a particular type """ 81 default_targets = ("article",) 82 _entry_targets : list[str] 83 84 def __init__(self, *, targets:Maybe[Iterable[str]]=None): 85 super().__init__() 86 self._entry_targets = [x.lower() for x in (targets or self.default_targets)] 87
[docs] 88 def transform(self, library): 89 chosen = [x for x in library.entries if x.entry_type.lower() in self._entry_targets] 90 return Library(chosen)
91
[docs] 92class SelectTags(LibraryMiddleware): 93 """ Select entries of with a particular tag """ 94 _targets : set[str] 95 96 def __init__(self, *, tags:Iterable[str]): 97 super().__init__() 98 self._targets = set(tags) 99
[docs] 100 def transform(self, library:Library) -> Library: 101 chosen = [x for x in library.entries if bool(x.fields_dict['tags'].value & self._targets)] 102 return Library(chosen)
103
[docs] 104class SelectAuthor(LibraryMiddleware): 105 """ TODO select entries by a set of authors 106 107 should run name split on them 108 """ 109 _targets : set[str] 110 111 def __init__(self, *, authors:Iterable[str]): 112 super().__init__() 113 self._targets = set(authors) 114
[docs] 115 def transform(self, library): 116 raise NotImplementedError()