From: Simon Tatham Date: Fri, 8 Dec 2023 08:55:14 +0000 (+0000) Subject: Make the dry-run posting mode a standard option. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=8f618c4f39730621b22ac92fc159a709adc520ef;p=mastodonochrome.git Make the dry-run posting mode a standard option. It's silly to keep hacking it out in the source code every time I want to test something! --- diff --git a/client.py b/client.py index 9511f02..155ec48 100644 --- 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): diff --git a/cursesclient.py b/cursesclient.py index 19fb358..8bfad55 100644 --- a/cursesclient.py +++ b/cursesclient.py @@ -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): diff --git a/mastodonochrome b/mastodonochrome index 8e62161..9c1330a 100755 --- a/mastodonochrome +++ b/mastodonochrome @@ -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__':