add __slots__ to most classes

This commit is contained in:
Stefan Bühler 2023-10-02 09:18:32 +02:00
parent b8d9c6f2a1
commit 1f98e6627d

43
pqm
View File

@ -33,7 +33,7 @@ import yaml
T = typing.TypeVar('T')
@dataclasses.dataclass
@dataclasses.dataclass(slots=True)
class Config:
# if not remote_sources are configured -> use local queue
remote_sources: dict[str, str] = dataclasses.field(default_factory=dict)
@ -157,7 +157,7 @@ async def trio_parallel_ordered(
await tpo.close()
@dataclasses.dataclass
@dataclasses.dataclass(slots=True)
class Recipient:
"""Recipient in a postfix mail"""
address: str
@ -199,7 +199,7 @@ def json_decode_stream(data: str):
yield obj
@dataclasses.dataclass
@dataclasses.dataclass(slots=True)
class Mail:
"""Metadata for mail in postfix queue"""
queue_name: QueueName
@ -233,6 +233,8 @@ class Mail:
# abstract collection/cluster of postfix nodes (or just a single one)
class Source(abc.ABC):
__slots__ = ()
# list of server names in collection
@abc.abstractmethod
def server_list(self) -> list[str]:
@ -331,6 +333,8 @@ class Source(abc.ABC):
class LocalSource(Source):
__slots__ = ()
def server_list(self) -> list[str]:
return [socket.gethostname()]
@ -388,6 +392,8 @@ class LocalSource(Source):
class RemoteSource(Source):
__slots__ = ('remotes',)
def __init__(self, remotes: dict[str, str]) -> None:
super().__init__()
self.remotes = remotes
@ -520,6 +526,8 @@ class RemoteSource(Source):
class UserActivityStats:
__slots__ = ('count_mails', 'related_mails', 'cut_off')
# find most active address for `health` command
def __init__(self, cut_off: int = 10) -> None:
@ -588,6 +596,8 @@ class UserActivityStats:
class Filter(abc.ABC):
"""abstract base class for filter expressions"""
__slots__ = ()
def __init__(self) -> None:
pass
@ -650,11 +660,14 @@ class Filter(abc.ABC):
# abstract base class for filters that don't need (...) around in representations of members in combined filters
class SingleExprFilter(Filter):
pass
__slots__ = ()
class FalseFilter(SingleExprFilter):
"""constant false - matches no mail"""
__slots__ = ()
def __repr__(self) -> str:
return '0'
@ -680,6 +693,9 @@ class FalseFilter(SingleExprFilter):
class TrueFilter(SingleExprFilter):
"""constant true - matches all mail"""
__slots__ = ()
def __repr__(self) -> str:
return '1'
@ -708,6 +724,8 @@ TRUE_FILTER = TrueFilter()
class AndFilter(Filter):
__slots__ = ('expressions',)
def __init__(self, expressions: list[Filter] | tuple[()] = ()) -> None:
super().__init__()
self.expressions: list[Filter] = expressions or []
@ -761,6 +779,8 @@ class AndFilter(Filter):
class OrFilter(Filter):
__slots__ = ('expressions',)
def __init__(self, expressions: list[Filter] | tuple[()] = ()) -> None:
super().__init__()
self.expressions: list[Filter] = expressions or []
@ -814,6 +834,8 @@ class OrFilter(Filter):
class NotFilter(SingleExprFilter):
__slots__ = ('expression',)
def __init__(self, expression: Filter) -> None:
super().__init__()
self.expression = expression
@ -851,6 +873,9 @@ class NotFilter(SingleExprFilter):
class QueueFilter(SingleExprFilter):
"""match mails based on queue they are in"""
__slots__ = ('select',)
def __init__(self, select: list[QueueName]) -> None:
self.select = set(select)
@ -909,6 +934,8 @@ class AddressSelector(enum.Enum):
class BaseAddressPattern(abc.ABC):
__slots__ = ()
# abstract base class of patterns to use to match a mail address
# subclasses match either: full address, domain part, regex
def __init__(self) -> None:
@ -941,6 +968,8 @@ class BaseAddressPattern(abc.ABC):
class AddressPattern(BaseAddressPattern):
__slots__ = ('address',)
# match full address exactly
def __init__(self, address: str) -> None:
super().__init__()
@ -963,6 +992,8 @@ class AddressPattern(BaseAddressPattern):
class AddressDomainPattern(BaseAddressPattern):
__slots__ = ('domain',)
# match address by domain
def __init__(self, domain: str) -> None:
super().__init__()
@ -980,6 +1011,8 @@ class AddressDomainPattern(BaseAddressPattern):
class AddressRegexMatch(BaseAddressPattern):
__slots__ = ('address',)
# match address by regex
def __init__(self, address: str | re.Pattern[str]) -> None:
super().__init__()
@ -1001,6 +1034,8 @@ class AddressRegexMatch(BaseAddressPattern):
class AddressFilter(SingleExprFilter):
__slots__ = ('selector', 'patterns')
# match mails by address
def __init__(self, selector: AddressSelector, patterns: list[BaseAddressPattern]) -> None:
self.selector = selector