From 8762112e379204e978d3a4de81cc172e8f230f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Sun, 29 Jan 2023 15:24:16 +0100 Subject: [PATCH] feat: Add Markdown export --- mastodon_blocklist_deploy/cli.py | 12 ++++++++++-- mastodon_blocklist_deploy/helpers.py | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 mastodon_blocklist_deploy/helpers.py diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 7bdb6e1..6f7b7c2 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -7,6 +7,7 @@ import os import toml from mastodon_blocklist_deploy.models import Instance +from mastodon_blocklist_deploy.helpers import blocklist_to_markdown def load_blocklist_file(filename: str) -> [Instance]: @@ -56,6 +57,7 @@ def cli(): parser.add_argument('-o', '--output', help="Filename where to export the blocklist") parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument('-n', '--no-delete', action='store_true', help="Do not delete existing blocks") + parser.add_argument('--markdown', action='store_true', help="Export as markdown table") args = parser.parse_args() if args.verbose: logging.basicConfig(level=logging.DEBUG) @@ -95,10 +97,16 @@ 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]})) + if args.markdown: + print(blocklist_to_markdown(remote_blocklist)) + else: + print(toml.dumps({"instances": [remove_key_from_dict(b.__dict__, 'id') 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) + if args.markdown: + f.write(blocklist_to_markdown(remote_blocklist)) + else: + toml.dump({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]}, f) if __name__ == "__main__": diff --git a/mastodon_blocklist_deploy/helpers.py b/mastodon_blocklist_deploy/helpers.py new file mode 100644 index 0000000..4e2bd61 --- /dev/null +++ b/mastodon_blocklist_deploy/helpers.py @@ -0,0 +1,6 @@ +from mastodon_blocklist_deploy.models import Instance +def blocklist_to_markdown(blocklist:[Instance]): + markdown_string = "| Instance | Status | Reason |\n | --- | --- | --- |\n" + for instance in blocklist: + markdown_string += f"| {instance.domain} | {instance.severity} | {instance.public_comment} |\n" + return markdown_string \ No newline at end of file