2022-08-26 18:37:36 +03:00
|
|
|
from asyncio import gather
|
|
|
|
from configparser import ConfigParser
|
2022-11-01 12:55:23 +03:00
|
|
|
from logging import getLogger
|
2022-08-29 10:28:51 +03:00
|
|
|
from typing import Dict, List, Optional
|
|
|
|
from mastoposter.filters import run_filters
|
|
|
|
from mastoposter.filters.base import BaseFilter, FilterInstance
|
2022-08-26 18:37:36 +03:00
|
|
|
|
2022-08-29 10:28:51 +03:00
|
|
|
from mastoposter.integrations import (
|
|
|
|
DiscordIntegration,
|
|
|
|
FilteredIntegration,
|
|
|
|
TelegramIntegration,
|
|
|
|
)
|
2022-08-26 18:37:36 +03:00
|
|
|
from mastoposter.types import Status
|
|
|
|
|
2022-11-01 12:55:23 +03:00
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2022-08-26 18:37:36 +03:00
|
|
|
|
2022-08-29 10:28:51 +03:00
|
|
|
def load_integrations_from(config: ConfigParser) -> List[FilteredIntegration]:
|
|
|
|
modules: List[FilteredIntegration] = []
|
2022-08-26 18:37:36 +03:00
|
|
|
for module_name in config.get("main", "modules").split():
|
2022-08-27 14:27:42 +03:00
|
|
|
mod = config[f"module/{module_name}"]
|
2022-11-01 12:55:23 +03:00
|
|
|
logger.info(
|
|
|
|
"Configuring %s integration (type=%s)", module_name, mod["type"]
|
|
|
|
)
|
2022-08-29 10:28:51 +03:00
|
|
|
|
|
|
|
filters: Dict[str, FilterInstance] = {}
|
|
|
|
for filter_name in mod.get("filters", "").split():
|
|
|
|
filter_basename = filter_name.lstrip("~!")
|
|
|
|
|
2022-11-01 12:55:23 +03:00
|
|
|
logger.info(
|
|
|
|
"Loading filter %s for integration %s",
|
|
|
|
filter_basename,
|
|
|
|
module_name,
|
|
|
|
)
|
|
|
|
|
2022-08-29 10:28:51 +03:00
|
|
|
filters[filter_basename] = BaseFilter.new_instance(
|
|
|
|
filter_name, config[f"filter/{filter_basename}"]
|
|
|
|
)
|
|
|
|
|
|
|
|
for finst in list(filters.values()):
|
2022-11-01 12:55:23 +03:00
|
|
|
logger.info("Running post-initialization hook for %r", finst)
|
2022-08-29 10:28:51 +03:00
|
|
|
finst.filter.post_init(filters, config)
|
|
|
|
|
2022-11-01 12:55:23 +03:00
|
|
|
# TODO: make a registry of integrations
|
|
|
|
# INFO: mastoposter/integrations/base.py@__init__
|
2022-08-27 14:27:42 +03:00
|
|
|
if mod["type"] == "telegram":
|
2022-08-29 10:28:51 +03:00
|
|
|
modules.append(
|
|
|
|
FilteredIntegration(
|
2022-11-01 12:55:23 +03:00
|
|
|
TelegramIntegration.from_section(mod),
|
|
|
|
list(filters.values()),
|
2022-08-29 10:28:51 +03:00
|
|
|
)
|
|
|
|
)
|
2022-08-27 14:27:42 +03:00
|
|
|
elif mod["type"] == "discord":
|
2022-08-29 10:28:51 +03:00
|
|
|
modules.append(
|
|
|
|
FilteredIntegration(
|
2022-11-01 12:55:23 +03:00
|
|
|
DiscordIntegration.from_section(mod),
|
|
|
|
list(filters.values()),
|
2022-08-29 10:28:51 +03:00
|
|
|
)
|
|
|
|
)
|
2022-08-26 18:37:36 +03:00
|
|
|
else:
|
2022-08-27 14:27:42 +03:00
|
|
|
raise ValueError("Invalid module type %r" % mod["type"])
|
2022-08-26 18:37:36 +03:00
|
|
|
return modules
|
|
|
|
|
|
|
|
|
|
|
|
async def execute_integrations(
|
2022-08-29 10:28:51 +03:00
|
|
|
status: Status, sinks: List[FilteredIntegration]
|
2022-08-26 18:37:36 +03:00
|
|
|
) -> List[Optional[str]]:
|
2022-11-01 12:55:23 +03:00
|
|
|
logger.debug("Executing integrations...")
|
2022-08-29 10:28:51 +03:00
|
|
|
return await gather(
|
2022-08-29 10:34:23 +03:00
|
|
|
*[sink[0](status) for sink in sinks if run_filters(sink[1], status)],
|
2022-08-29 10:28:51 +03:00
|
|
|
return_exceptions=True,
|
|
|
|
)
|