From 8d5676d0b27fb2ce231bce1807cbe915505f1dfa Mon Sep 17 00:00:00 2001 From: Georg Krause Date: Thu, 12 Jan 2023 17:07:28 +0100 Subject: [PATCH] refactor: Avoid manually templating toml file entries --- mastodon_blocklist_deploy/cli.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 0341d24..89aa964 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -19,23 +19,6 @@ def load_blocklist_file(filename: str) -> [Instance]: return instances -def export_blocklist_toml(blocklist: [Instance], filname: str): - toml_str = "" - for instance in blocklist: - toml_str += f''' -[[instances]] -name = "{instance.domain}" -domain = "{instance.domain}" -severity = "{instance.severity}" -reject_media = {str(instance.reject_media).lower()} -reject_reports = {str(instance.reject_reports).lower()} -public_comment = "{instance.public_comment}" -private_comment = "{instance.private_comment}" - ''' - with open(filname, "w") as f: - f.write(toml_str) - - def blocklist_json_to_instances(blocklist_json: str) -> [Instance]: instances = [] for i in blocklist_json: @@ -109,9 +92,10 @@ def cli(): Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete) elif args.action == "export": if not args.output: - print("Export currently requires to pass --output as well") + print(toml.dumps({"instances": [b.__dict__ for b in remote_blocklist]})) else: - export_blocklist_toml(remote_blocklist, args.output) + with open(args.output, "w") as f: + toml.dump({"instances": [b.__dict__ for b in remote_blocklist]}, f) if __name__ == "__main__":