Source code for jgdv._abstract.protocols.pre_processable

  1#!/usr/bin/env python3
  2"""
  3
  4"""
  5# Imports:
  6from __future__ import annotations
  7
  8# ##-- stdlib imports
  9import datetime
 10import enum
 11import functools as ftz
 12import itertools as itz
 13import logging as logmod
 14import pathlib as pl
 15import re
 16import time
 17import collections
 18import contextlib
 19import hashlib
 20from copy import deepcopy
 21from uuid import UUID, uuid1
 22from weakref import ref
 23import atexit # for @atexit.register
 24import faulthandler
 25# ##-- end stdlib imports
 26
 27# ##-- types
 28# isort: off
 29# General
 30import abc
 31import collections.abc
 32import typing
 33import types
 34from typing import cast, assert_type, assert_never
 35from typing import Generic, NewType, Never
 36from typing import no_type_check, final, override, overload
 37# Protocols and Interfaces:
 38from typing import Protocol, runtime_checkable
 39if typing.TYPE_CHECKING:
 40    from typing import Final, ClassVar, Any, Self
 41    from typing import Literal, LiteralString
 42    from typing import TypeGuard
 43    from collections.abc import Iterable, Iterator, Callable, Generator
 44    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 45
 46    from jgdv import Maybe, MaybeT
 47    type InstanceData      = dict
 48    type PostInstanceData  = dict
 49
 50# isort: on
 51# ##-- end types
 52
 53# ##-- Generated Exports
 54__all__ = (
 55# -- Types
 56"HookOverride", "PreProcessResult",
 57# -- Classes
 58"PreProcessor_p", "ProcessorHooks",
 59
 60)
 61# ##-- end Generated Exports
 62
 63##-- logging
 64logging = logmod.getLogger(__name__)
 65##-- end logging
 66
 67# Vars:
 68
 69type HookOverride         = bool
 70type PreProcessResult[T]  = tuple[str, InstanceData, PostInstanceData, Maybe[type[T]]]
 71# Body:
 72
[docs] 73class PreProcessor_p[T](Protocol): 74 """ Protocol for things like Strang, 75 whose metaclass preprocess the initialisation data before even __new__ is called. 76 77 Is used in a metatype.__call__ as:: 78 79 cls._pre_process(...) 80 obj = cls.__new__(...) 81 obj.__init__(...) 82 obj._process() 83 obj._post_process() 84 return obj 85 86 """ 87
[docs] 88 def pre_process(self, cls:type[T], input:Any, *args:Any, strict:bool=False, **kwargs:Any) -> PreProcessResult[T]: ... # noqa: A002, ANN401
89
[docs] 90 def process(self, obj:T, *, data:PostInstanceData) -> Maybe[T]: ...
91
[docs] 92 def post_process(self, obj:T, *, data:PostInstanceData) -> Maybe[T]: ...
93
[docs] 94class ProcessorHooks[T](Protocol): 95 """ Hooks a PreProcessor_p recognizes. 96 97 returning True as the first result value means the processor's logic is *not* to be used. 98 otherwise it *is* used, with the results from the hook 99 """ 100
[docs] 101 @classmethod 102 def _pre_process_h(cls:type[T], input:Any, *args:Any, strict:bool=False, **kwargs:Any) -> Maybe[tuple[bool, *PreProcessResult[T]]]: ... # noqa: A002, ANN401
103
[docs] 104 def _process_h(self, *, data:PostInstanceData) -> Maybe[tuple[HookOverride, Maybe[T]]]: ...
105
[docs] 106 def _post_process_h(self, *, data:PostInstanceData) -> Maybe[tuple[HookOverride, Maybe[T]]]: ...