Source code for jgdv.structs.metalord.core

  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 collections
 20import contextlib
 21import hashlib
 22from copy import deepcopy
 23from uuid import UUID, uuid1
 24from weakref import ref
 25import atexit # for @atexit.register
 26import faulthandler
 27# ##-- end stdlib imports
 28
 29# ##-- types
 30# isort: off
 31import abc
 32import collections.abc
 33from typing import TYPE_CHECKING, cast, assert_type, assert_never
 34from typing import Generic, NewType
 35# Protocols:
 36from typing import Protocol, runtime_checkable
 37# Typing Decorators:
 38from typing import no_type_check, final, override, overload
 39
 40if TYPE_CHECKING:
 41    from jgdv import Maybe
 42    from typing import Final
 43    from typing import ClassVar, Any, LiteralString
 44    from typing import Never, Self, Literal
 45    from typing import TypeGuard
 46    from collections.abc import Iterable, Iterator, Callable, Generator
 47    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 48
 49##--|
 50
 51# isort: on
 52# ##-- end types
 53
 54##-- logging
 55logging = logmod.getLogger(__name__)
 56logging.disabled = True
 57##-- end logging
 58
 59# Vars:
 60
 61# Body:
 62
[docs] 63class MetalordCore(type): 64 """ 65 The Core of Metalord. 66 67 By default classes are constructed using type(). 68 https://docs.python.org/3/reference/datamodel.html#metaclasses 69 70 Execution order:: 71 72 - Metaclass Definition 73 - Metaclass Creation 74 75 - Class Definition: 76 1) MRO resolution : __mro_entries__(self, bases) 77 2) metaclass selection : (most dervied of (type | metaclasses)) 78 3) namespace preparation : metacls.__prepare__(metacls, name, bases, **kwargs) -> dict 79 4) class body execution : exec(body, globals, namespace) 80 5) class object creation : metaclass.__new__(name, bases, namespace, **kwargs) -> classobj 81 6) subclass init : parent.__init_subclass__(cls, **kwargs) 82 7) class object init : metaclass.__init__(cls, na,e, bases, namespace, **kwargs) 83 84 - Instance Creation:: 85 1) metaclass.__call__(cls, ...) -> instance 86 1.b) cls.__new__(cls, ...) -> instance 87 1.c) cls.__init__(instance, ...) 88 89 """ 90 91 @classmethod 92 def __prepare__(mcls:type, name:str, bases:tuple, **kwargs) -> dict: 93 """ Class Definition preparation of namespace """ 94 logging.debug("Metalord Prepare: %s %s %s", name, bases, kwargs) 95 return super().__prepare__(name, bases, **kwargs) 96 97 def __new__(mcls:type, name:str, bases:tuple, namespace:dict, **kwargs) -> type: 98 """ Class Definition Object creation 99 Equivalent to type(name, bases, namespace) 100 """ 101 logging.debug("Metalord new: %s %s %s %s %s", mcls, name, bases, namespace, kwargs) 102 return super().__new__(mcls, name, bases, dict(namespace), **kwargs) 103 104 def __init__(cls, name, bases:tuple, namespace:dict, **kwargs): 105 """ Class Definition initialisation """ 106 logging.debug("Metalord init: %s %s %s %s %s", cls, name, bases, namespace, kwargs) 107 super().__init__(name, bases, namespace, **kwargs) 108 109 def __call__(cls, *args, **kwargs): 110 """ Class Instance creation 111 """ 112 logging.debug("Metalord Call: %s %s %s", cls, args, kwargs) 113 obj = cls.__new__(cls, *args, **kwargs) 114 obj.__init__(*args, **kwargs) 115 return obj 116 117 def __instancecheck__(self, instance) -> bool: 118 logging.debug("Metalord instance check: %s %s", self, instance) 119 return super().__instancecheck__(instance) 120 121 def __subclasscheck__(self, subclass) -> bool: 122 logging.debug("Metalord subclass check: %s %s", self, subclass) 123 return super().__subclasscheck__(subclass)