Source code for jgdv.structs.locator.processor

  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
 27from jgdv._abstract.protocols.pre_processable import PreProcessor_p
 28from jgdv.structs.strang import _interface as StrangAPI  # noqa: N812
 29from jgdv.structs.strang.processor import StrangBasicProcessor
 30from . import _interface as API # noqa: N812
 31
 32# ##-- types
 33# isort: off
 34# General
 35import abc
 36import collections.abc
 37import typing
 38import types
 39from typing import cast, assert_type, assert_never
 40from typing import Generic, NewType, Never
 41from typing import no_type_check, final, override, overload
 42# Protocols and Interfaces:
 43from typing import Protocol, runtime_checkable
 44if typing.TYPE_CHECKING:
 45    from jgdv._abstract.protocols.pre_processable import InstanceData, PostInstanceData
 46    from typing import Final, ClassVar, Any, Self
 47    from typing import Literal, LiteralString
 48    from typing import TypeGuard
 49    from collections.abc import Iterable, Iterator, Callable, Generator
 50    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 51
 52    from jgdv._abstract.protocols.pre_processable import PreProcessResult
 53    from jgdv import Maybe, Ctor
 54
 55# isort: on
 56# ##-- end types
 57
 58##-- logging
 59logging = logmod.getLogger(__name__)
 60##-- end logging
 61
 62# Vars:
 63
 64# Body:
 65
[docs] 66class LocationProcessor[T:API.Location_p](StrangBasicProcessor): 67
[docs] 68 @override 69 def pre_process(self, cls:type[T], input:Any, *args:Any, strict:bool=False, **kwargs:Any) -> PreProcessResult[T]: # type: ignore[override] # noqa: PLR0912, PLR0915 70 assert(cls.section(0).case is not None) 71 assert(cls.section(1).case is not None) 72 x : Any 73 y : Any 74 default_mark : Maybe[StrangAPI.StrangMarkAbstract_e] 75 head_end : str 76 text : str 77 head_case : str = cast("str", cls.section(0).case) 78 body_case : str = cast("str", cls.section(1).case) 79 head_text : set[str] = set() 80 body_text : list[str] = [] 81 inst_data : InstanceData = {} 82 post_data : PostInstanceData = {} 83 marks : StrangAPI.StrangMarkAbstract_e = cls.Marks 84 ctor : Ctor[T] = cls 85 match cls.section(0).end: 86 case None: 87 raise ValueError() 88 case str() as x: 89 head_end = x 90 91 match marks.default(): 92 case None: 93 raise ValueError() 94 case x: 95 default_mark = marks.default() 96 ##--| clean the input 97 match input: 98 case StrangAPI.Strang_p() as val: 99 x, m, y = val[:,:].partition(head_end) 100 if bool(m): 101 head_text.update(x.split(head_case)) 102 body_text.append(y) 103 else: 104 body_text.append(x) 105 case pl.Path() as val if bool(val.suffix): 106 head_text.add(marks.file) # type: ignore[attr-defined] 107 body_text.append(str(val)) 108 case str() as val: 109 x, m, y = val.partition(head_end) 110 if bool(m): 111 head_text.update(x.split(head_case)) 112 body_text.append(y) 113 else: 114 body_text.append(x) 115 case val: 116 x, m, y = str(val).partition(head_end) 117 if bool(m): 118 head_text.update(x.split(head_case)) 119 body_text.append(y) 120 else: 121 body_text.append(x) 122 123 124 body : str = body_case.join(body_text) 125 s1_marks = cls.section(1).marks 126 assert(s1_marks is not None) 127 if any(x.value in body for x in s1_marks): 128 head_text.add(marks.abstract.value) # type: ignore[attr-defined] 129 if "." in body: 130 head_text.add(marks.file.value) # type: ignore[attr-defined] 131 132 if not bool(head_text) and default_mark is not None: 133 head_text.add(default_mark) 134 assert(bool(body_text)) 135 text = head_end.join([head_case.join(head_text), body]) 136 return text, inst_data, post_data, ctor
137