From 3dce62417eb7c9136a3c881031d9739673f5252f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Thu, 26 Jan 2023 12:05:12 +0100 Subject: [PATCH] refactor: Cleaner solution for exportable dict --- mastodon_blocklist_deploy/cli.py | 9 ++------- mastodon_blocklist_deploy/models.py | 8 ++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 7bdb6e1..68ea688 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -38,9 +38,6 @@ def load_blocklist_from_instance(server: str, token: str) -> [Instance]: else: raise ConnectionError(f"Could not connect to the server ({response.status_code}: {response.reason})") -def remove_key_from_dict(dict, key): - del dict[key] - return dict def cli(): parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server') @@ -67,8 +64,6 @@ def cli(): else: token = os.getenv('MBD_TOKEN') - - """if there is a remote blocklist provided load this instead of fetching it from a server (for debugging reasons)""" if args.remote_blocklist: with open(args.remote_blocklist) as f: @@ -95,10 +90,10 @@ def cli(): Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete) elif args.action == "export": if not args.output: - print(toml.dumps({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]})) + print(toml.dumps({"instances": [b.exportable_dict for b in remote_blocklist]})) else: with open(args.output, "w") as f: - toml.dump({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]}, f) + toml.dump({"instances": [b.exportable_dict for b in remote_blocklist]}, f) if __name__ == "__main__": diff --git a/mastodon_blocklist_deploy/models.py b/mastodon_blocklist_deploy/models.py index 1fad913..bf650d3 100644 --- a/mastodon_blocklist_deploy/models.py +++ b/mastodon_blocklist_deploy/models.py @@ -27,6 +27,14 @@ class Instance: def status_str(self): return f"{self.severity}\nReject reports: {self.reject_reports}\nReject media: {self.reject_media}\nObfuscate: {self.obfuscate}" + @property + def exportable_dict(self): + keys = ["domain", "severity", "public_comment", "private_comment", "obfuscate", "reject_media", "reject_reports"] + exportable = {} + for key in keys: + exportable[key] = getattr(self, key) + return exportable + def parse_remote_block(self, instance_dict): self.domain = instance_dict["domain"] self.id = instance_dict["id"]