chiark / gitweb /
Make the dry-run posting mode a standard option.
authorSimon Tatham <anakin@pobox.com>
Fri, 8 Dec 2023 08:55:14 +0000 (08:55 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 8 Dec 2023 08:55:14 +0000 (08:55 +0000)
It's silly to keep hacking it out in the source code every time I want
to test something!

client.py
cursesclient.py
mastodonochrome

index 9511f020020c28c29f67cd0c247a6cc201533641..155ec48edbe08c0aaf31918cd24744bf9944c2c2 100644 (file)
--- a/client.py
+++ b/client.py
@@ -31,6 +31,8 @@ class Client:
         # that we can look up the text of one to show in a 'Re:' header
         self.status_cache = {}
 
+        self.readonly = False
+
     def set_instance_url(self, instance_url):
         self.instance_url = instance_url
         self.urls = {
@@ -70,6 +72,9 @@ class Client:
 
         self.log_response = log_response
 
+    def set_readonly(self):
+        self.readonly = True
+
     def method_start(self, method, path, base, params, stream, links={}):
         headers = {}
         if self.bearer_token is not None:
@@ -95,6 +100,7 @@ class Client:
     def get(self, path, base='api', **params):
         return self.method(requests.get, path, base, params)
     def post(self, path, base='api', **params):
+        assert not self.readonly, "HTTP POST got through in readonly mode"
         return self.method(requests.post, path, base, params)
 
     def get_incremental(self, path, base='api', **params):
index 19fb35890e9b9eb8f032455a3896c5a0ced5c47b..8bfad554e184a028772fcabf8359bc4209572479 100644 (file)
@@ -652,6 +652,9 @@ class ObjectFile(File):
         target = self.statuses[self.select_target]
         reply_id = target.get_reply_id()
         verb = "favourite" if direction > 0 else "unfavourite"
+        if self.cc.readonly:
+            print(verb, reply_id, file=sys.stderr)
+            return
         data = self.cc.post(f"statuses/{reply_id}/{verb}")
         target.update_fave_boost(data)
 
@@ -660,6 +663,9 @@ class ObjectFile(File):
         target = self.statuses[self.select_target]
         reply_id = target.get_reply_id()
         verb = "reblog" if direction > 0 else "unreblog"
+        if self.cc.readonly:
+            print(verb, reply_id, file=sys.stderr)
+            return
         data = self.cc.post(f"statuses/{reply_id}/{verb}")
         target.update_fave_boost(data)
 
@@ -994,6 +1000,11 @@ class Composer:
         }
         if self.reply_id is not None:
             params["in_reply_to_id"] = self.reply_id
+        if self.cc.readonly:
+            print("post", file=sys.stderr)
+            for key, value in params.items():
+                print(f"    {key}: {value!r}", file=sys.stderr)
+            return
         self.cc.post("statuses", **params)
 
 class testComposerLayout(unittest.TestCase):
index 8e62161686ec5b4bfd70cfaa41eed856d802efb1..9c1330a7ba122922f6144d5382995ae41b437e31 100755 (executable)
@@ -86,6 +86,10 @@ def main():
     parser.add_argument("--log", help="File to log debug information to.")
     parser.add_argument("--test", nargs=argparse.REMAINDER,
                         help="Run unit tests.")
+    parser.add_argument("--readonly", action="store_true",
+                        help="Disable write operations (posting, favouriting "
+                        "etc), and just log on stderr what would have been "
+                        "done.")
     parser.add_argument("--combined", action="store_const", dest="action",
                         const=CombinedUI, help="Temporary mode to fetch "
                         "the user's timeline and mentions, interleave them, "
@@ -106,6 +110,8 @@ def main():
     login.setup_client(client)
     if args.log is not None:
         client.enable_debug(args.log)
+    if args.readonly:
+        client.set_readonly()
     client.run()
 
 if __name__ == '__main__':