fbmessenger: add all.py, merge messages from different sources

followup for https://github.com/karlicoss/HPI/pull/179
This commit is contained in:
Dima Gerasimov 2022-02-05 16:19:53 +00:00 committed by karlicoss
parent 4626c1bba6
commit f8e73134b3
4 changed files with 98 additions and 4 deletions

48
my/fbmessenger/all.py Normal file
View file

@ -0,0 +1,48 @@
from typing import Iterator
from my.core import Res
from my.core.common import Stats
from my.core.source import import_source
from .common import Message, _merge_messages
src_export = import_source(module_name=f'my.fbmessenger.export')
src_android = import_source(module_name=f'my.fbmessenger.android')
@src_export
def _messages_export() -> Iterator[Res[Message]]:
from . import export
# ok, this one is a little tricky
# export.Message type is actually external (coming from fbmessengerexport module)
# so it's unclear how to make mypy believe/check that common.Message is a structural subtype of export.Message
# we could use runtime_checkable, but then it might also crash in runtime
# which feels somewhat mean if someone is only using fmbessenger.export module and needs its attributes only
# so perhaps it makes sense that the typecheck belongs here?
for m in export.messages():
# NOTE: just 'yield m' works and seems to type check properly
if isinstance(m, Exception):
yield m
else:
# however, this way it results in a nicer error (shows the missing Protocol attributes)
# https://github.com/python/mypy/issues/8235#issuecomment-570712356
m2: Message = m
yield m2
@src_android
def _messages_android() -> Iterator[Res[Message]]:
from . import android
yield from android.messages()
def messages() -> Iterator[Res[Message]]:
yield from _merge_messages(
_messages_export(),
_messages_android(),
)
def stats() -> Stats:
from my.core import stat
return stat(messages)