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
25import bibtexparser
26import bibtexparser.model as model
27from bibtexparser import middlewares as ms
28from bibtexparser.middlewares.middleware import (BlockMiddleware,
29 LibraryMiddleware)
30from bibtexparser.middlewares.names import NameParts
31from jgdv import Proto, Mixin
32from jgdv.files.tags import SubstitutionFile
33
34# ##-- end 3rd party imports
35
36# ##-- 1st party imports
37import bibble._interface as API
38from . import _interface as API_N
39from bibble.util.mixins import ErrorRaiser_m, FieldMatcher_m
40from bibble.fields.field_substitutor import FieldSubstitutor
41from bibble.util.name_parts import NameParts_d
42
43# ##-- end 1st party imports
44
45##-- logging
46logging = logmod.getLogger(__name__)
47##-- end logging
48
49##--|
50
[docs]
51@Proto(API.ReadTime_p)
52class NameSubstitutor(FieldSubstitutor):
53 """ replaces names in author and editor fields as necessary """
54
55 def __init__(self, *, subs:SubstitutionFile, **kwargs):
56 super().__init__(fields=[API_N.AUTHOR_K, API_N.EDITOR_K],
57 subs=subs,
58 **kwargs)
59
[docs]
60 def on_read(self):
61 Never()
62
[docs]
63 def field_h(self, field, entry):
64 match field.value:
65 case str():
66 return ValueError("Name parts should already be combined, but authors shouldn't be merged yet")
67 case [*xs] if any(isinstance(x, NameParts|NameParts_d) for x in xs):
68 return ValueError("Name parts should already be combined, but authors shouldn't be merged yet")
69 case []:
70 return [field]
71 case [*xs]:
72 clean_names = []
73 for name in xs:
74 match self._subs.sub(name):
75 case None:
76 clean_names.append(name)
77 case set() as val:
78 head, *_ = val
79 clean_names.append(head)
80 else:
81 return [model.Field(field.key, clean_names)]
82 case value:
83 return ValueError("Unsupported replacement field value type(%s): %s", entry.key, type(value))