social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1from dataclasses import dataclass, field
2from typing import TypeVar
3
4from cross.attachments import Attachment
5from cross.tokens import Token
6
7T = TypeVar("T", bound=Attachment)
8
9
10class AttachmentKeeper:
11 def __init__(self) -> None:
12 self._map: dict[type, Attachment] = {}
13
14 def put(self, attachment: Attachment) -> None:
15 self._map[attachment.__class__] = attachment
16
17 def get(self, cls: type[T]) -> T | None:
18 instance = self._map.get(cls)
19 if instance is None:
20 return None
21 if not isinstance(instance, cls):
22 raise TypeError(f"Expected {cls.__name__}, got {type(instance).__name__}")
23 return instance
24
25 def __repr__(self) -> str:
26 return f"AttachmentKeeper(_map={self._map.values()})"
27
28
29@dataclass
30class Post:
31 id: str
32 parent_id: str | None
33 tokens: list[Token]
34 attachments: AttachmentKeeper = field(default_factory=AttachmentKeeper)