def test_active_publishers(active_publishers): for publisher in publisher_name_to_validators: # if the publisher is in the active_publisher param, I set it to True, otherwise False get_settings().update({ f"publisher.{publisher}.active": (publisher in active_publishers) }) publishers = list(get_active_publishers()) assert publishers == active_publishers
def select_event_to_publish( published_events: List[MobilizonEvent], unpublished_events: List[MobilizonEvent], ): strategy = STRATEGY_NAME_TO_STRATEGY_CLASS[get_settings()["selection"] ["strategy"]]() return strategy.select(published_events, unpublished_events)
def get_db_url(): """gets db url from settings Returns: str : db url """ settings = get_settings() db_path = Path(settings.db_path) db_url = f"sqlite:///{db_path}" return db_url
def conf(self) -> DynaBox: """ Retrieves class's settings. """ cls = type(self) try: t, n = cls._conf or tuple() return get_settings()[t][n] except (KeyError, ValueError): raise InvalidAttribute( f"Class {cls.__name__} has invalid ``_conf`` attribute" f" (should be 2-tuple)" )
def get_mobilizon_future_events( page: int = 1, from_date: Optional[arrow.Arrow] = None) -> List[MobilizonEvent]: url = get_settings()["source"]["mobilizon"]["url"] query = query_future_events.format( group=get_settings()["source"]["mobilizon"]["group"], page=page, afterDatetime=from_date or arrow.now().isoformat(), ) r = requests.post(url, json={"query": query}) if r.status_code != HTTPStatus.OK: raise MobilizonRequestFailed( f"Request for events failed with code:{r.status_code}") response_json = r.json() logger.debug(f"Response:\n{json.dumps(response_json, indent=4)}") if "errors" in response_json: raise MobilizonRequestFailed( f"Request for events failed because of the following errors: " f"{json.dumps(response_json['errors'],indent=4)}") return list( map(parse_event, response_json["data"]["group"]["organizedEvents"]["elements"]))
async def setup_db( mock_active_publishers_config, event_model_generator, publication_model_generator ): settings = get_settings() settings["publisher"]["zulip"][ "bot_email" ] = "*****@*****.**" settings["publisher"]["zulip"]["instance"] = "https://zulip.twc-italia.org" publisher = await Publisher.filter(name="zulip").first() event = event_model_generator() await event.save() publication = publication_model_generator( event_id=event.id, publisher_id=publisher.id ) await publication.save()
def _select( self, published_events: List[MobilizonEvent], unpublished_events: List[MobilizonEvent], ) -> Optional[List[MobilizonEvent]]: # if there are no unpublished events, there's nothing I can do if not unpublished_events: logger.debug("No event to publish.") return [] # if there's no published event (first execution) I return the next in queue if not published_events: logger.debug( "First Execution with an available event. Picking next event in the queue." ) return unpublished_events last_published_event = published_events[-1] now = arrow.now() last_published_event_most_recent_publication_time = max( last_published_event.publication_time.values()) assert last_published_event_most_recent_publication_time < now, ( f"Last published event has been published in the future\n" f"{last_published_event_most_recent_publication_time}\n" f"{now}") if (last_published_event_most_recent_publication_time.shift( minutes=get_settings() ["selection.strategy_options.break_between_events_in_minutes"]) > now): logger.debug( "Last event was published recently. No event is going to be published." ) return [] return unpublished_events
async def init(): settings = get_settings() dictConfig(settings["logging"]) db_path = Path(settings.db_path) db = MoReDB(db_path) await db.setup()
def set_strategy(strategy_name): get_settings().update({"selection.strategy": strategy_name})
def set_break_window_config(desired_break_window_days): get_settings().update({ "selection.strategy_options.break_between_events_in_minutes": desired_break_window_days * 24 * 60 })
def _fill_template(self, pattern: Template) -> str: config = get_settings() return pattern.render(locale=config["locale"], **asdict(self))
def set_locale(locale): settings = get_settings() old_locale = settings["locale"] yield get_settings().update({"locale": locale}) settings.update({"locale": old_locale})
def mobilizon_url(): return get_settings()["source"]["mobilizon"]["url"]
def get_active_notifiers(): return mobilizon_reshare.config.notifiers.get_active_notifiers(get_settings())
def get_active_publishers(): return mobilizon_reshare.config.publishers.get_active_publishers(get_settings())