-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxies.py
More file actions
65 lines (47 loc) · 2.2 KB
/
Copy pathproxies.py
File metadata and controls
65 lines (47 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Three composable proxies: lazy, protection, metering.
Each forwards attribute access to a subject via ``__getattr__`` -- no shared
interface required -- and each adds exactly one kind of mediation. Because
every proxy is also a plain object, they stack:
``MeteringProxy(ProtectionProxy(LazyProxy(build), allow))``.
The disguise is skin-deep (this unit's standing caveat): ``isinstance``,
identity, and dunder lookups all see the proxy, not the subject.
"""
from __future__ import annotations
from collections import Counter
from collections.abc import Callable
from typing import Any
# Not-yet-built marker: ``None`` won't do, because a factory may legitimately
# return ``None`` and that result must still be cached exactly once.
_MISSING = object()
class LazyProxy:
"""Defer construction: the subject is built on first attribute access."""
def __init__(self, factory: Callable[[], object]) -> None:
# object.__setattr__-free here: plain attributes are fine because
# __getattr__ only fires for names *not* found on the proxy itself.
self._factory = factory
self._subject: object = _MISSING
@property
def is_built(self) -> bool:
"""Whether the expensive subject exists yet."""
return self._subject is not _MISSING
def __getattr__(self, name: str) -> Any:
if self._subject is _MISSING:
self._subject = self._factory()
return getattr(self._subject, name)
class ProtectionProxy:
"""Guard access: every attribute name passes ``allow`` or raises."""
def __init__(self, subject: object, allow: Callable[[str], bool]) -> None:
self._subject = subject
self._allow = allow
def __getattr__(self, name: str) -> Any:
if not self._allow(name):
raise PermissionError(f"access to {name!r} denied")
return getattr(self._subject, name)
class MeteringProxy:
"""Observe access: count every attribute lookup by name, then forward."""
def __init__(self, subject: object) -> None:
self._subject = subject
self.access_counts: Counter[str] = Counter()
def __getattr__(self, name: str) -> Any:
self.access_counts[name] += 1
return getattr(self._subject, name)