From: Jakob Cornell Date: Sun, 25 Sep 2022 18:01:18 +0000 (-0500) Subject: Expose read-only mode as a config file entry X-Git-Tag: strikebot-0.0.7~23 X-Git-Url: https://jcornell.net/gitweb/gitweb.cgi?a=commitdiff_plain;h=ea74774d65f3f8070a95c301b16faa74b883921a;p=counting.git Expose read-only mode as a config file entry --- diff --git a/strikebot/docs/sample_config.ini b/strikebot/docs/sample_config.ini index 7efebdd..423f220 100644 --- a/strikebot/docs/sample_config.ini +++ b/strikebot/docs/sample_config.ini @@ -8,12 +8,17 @@ auth IDs = 13 15 bot user = count_better + +# if false, don't strike or delete updates or report incorrectly stricken updates (optional, default true) enforcing = true # log messages at or above this severity to standard out; level names and numbers are supported (optional, default # WARNING); see https://docs.python.org/3/library/logging.html#levels log level = WARNING +# if true, don't modify the live thread at all (implies enforcing = false) (optional, default false) +read-only = false + thread ID = abc123 diff --git a/strikebot/src/strikebot/__init__.py b/strikebot/src/strikebot/__init__.py index 4c7c496..d9e4302 100644 --- a/strikebot/src/strikebot/__init__.py +++ b/strikebot/src/strikebot/__init__.py @@ -19,9 +19,6 @@ from strikebot.updates import Command, parse_update __version__ = importlib.metadata.version(__package__) -_READ_ONLY: bool = False # suppress any API requests that modify the thread? - - @dataclass class _Update: id: str @@ -99,12 +96,13 @@ async def count_tracker_impl( thread_id: str, bot_user: str, enforcing: bool, + read_only: bool, update_retention: dt.timedelta, logger: logging.Logger, ) -> None: from strikebot.reddit_api import CorrectionUpdateRequest, DeleteRequest, ReportUpdateRequest, StrikeRequest - enforcing = enforcing and not _READ_ONLY + enforcing = enforcing and not read_only buffer_ = [] timeline = [] @@ -199,7 +197,7 @@ async def count_tracker_impl( if parts: parts.append(_format_curr_count(last_valid)) - if not _READ_ONLY: + if not read_only: api_pool.enqueue_request(CorrectionUpdateRequest(thread_id, "\n\n".join(parts))) for invalid_tu in newly_invalid: @@ -212,7 +210,7 @@ async def count_tracker_impl( api_pool.enqueue_request(StrikeRequest(thread_id, update.name, update.ts)) update.stricken = True - if not _READ_ONLY and update.command is Command.REPORT: + if not read_only and update.command is Command.REPORT: api_pool.enqueue_request(ReportUpdateRequest(thread_id, body = _format_curr_count(last_valid))) async with message_rx: @@ -268,7 +266,7 @@ async def count_tracker_impl( slot.update.stricken = True if isinstance(slot, _TimelineUpdate) and slot.accepted: logger.info(f"bad strike of {slot.update.id}") - if not _READ_ONLY: + if not read_only: body = _format_bad_strike_alert(slot.update, thread_id) api_pool.enqueue_request(CorrectionUpdateRequest(thread_id, body)) else: diff --git a/strikebot/src/strikebot/__main__.py b/strikebot/src/strikebot/__main__.py index e725eeb..a655692 100644 --- a/strikebot/src/strikebot/__main__.py +++ b/strikebot/src/strikebot/__main__.py @@ -120,7 +120,7 @@ api_pool_error_delay = dt.timedelta(seconds = main_cfg.getfloat("API pool error api_pool_error_window = dt.timedelta(seconds = main_cfg.getfloat("API pool error window")) auth_ids = set(map(int, main_cfg["auth IDs"].split())) bot_user = main_cfg["bot user"] -enforcing = main_cfg.getboolean("enforcing") +enforcing = main_cfg.getboolean("enforcing", fallback = True) raw_log_level = main_cfg.get("log level", "WARNING") try: @@ -128,6 +128,7 @@ try: except ValueError: log_level = raw_log_level +read_only = main_cfg.getboolean("read-only", fallback = False) reorder_buffer_time = dt.timedelta(seconds = main_cfg.getfloat("reorder buffer time")) request_queue_limit = main_cfg.getint("request queue limit") thread_id = main_cfg["thread ID"] @@ -144,6 +145,10 @@ getters = { db_connect_params = {k: getters.get(k, db_cfg.get)(k) for k in db_cfg} +if read_only and enforcing: + raise RuntimeError("can't use read-only mode with enforcing on") + + logger = getLogger(__package__) logger.setLevel(logging.NOTSET - 1) # NOTSET means inherit from parent; we use handlers to filter @@ -195,7 +200,7 @@ async def main(): nursery_c.start_soon(ws_pool.conn_refresher_impl) nursery_c.start_soon( - count_tracker_impl, message_rx, api_pool, reorder_buffer_time, thread_id, bot_user, enforcing, + count_tracker_impl, message_rx, api_pool, reorder_buffer_time, thread_id, bot_user, enforcing, read_only, update_retention, logger.getChild("track") ) await ws_pool.init_workers()