Expose read-only mode as a config file entry
authorJakob Cornell <jakob+gpg@jcornell.net>
Sun, 25 Sep 2022 18:01:18 +0000 (13:01 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Sun, 25 Sep 2022 18:01:18 +0000 (13:01 -0500)
strikebot/docs/sample_config.ini
strikebot/src/strikebot/__init__.py
strikebot/src/strikebot/__main__.py

index 7efebdd32e81ecc882f52144255786a0c9bc3281..423f22023b15df587f46c47fc3c43759e2e35f6f 100644 (file)
@@ -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
 
 
index 4c7c49611934f8c62abf4631ce7deb9a009356cf..d9e43023c2739d325e34c4f4cff47333f32e812c 100644 (file)
@@ -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:
index e725eeb94f4d49a85183a9901acbf23636a7f077..a655692a5dee2b8ec8f68c98ca5db427f260ff07 100644 (file)
@@ -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()