From 569cff09576240d7ee6e674e2a0b438031771f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 3 May 2023 15:40:27 +0200 Subject: [PATCH] Add json --- README.md | 2 +- mastodon_blocklist_deploy/cli.py | 6 ++++-- mastodon_blocklist_deploy/helpers.py | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f2f4f96..e93c597 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ options: Filename where to export the blocklist -v, --verbose -n, --no-delete Do not delete existing blocks - --format FORMAT Export format: toml|markdown|csv + --format FORMAT Export format: toml|markdown|csv|json --private When the flag is set, private comment will also be exported. ``` diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 4b4b7e4..31946d0 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -7,7 +7,7 @@ import os import toml from mastodon_blocklist_deploy.models import Instance -from mastodon_blocklist_deploy.helpers import blocklist_to_markdown, blocklist_to_toml, blocklist_to_csv +from mastodon_blocklist_deploy.helpers import blocklist_to_markdown, blocklist_to_toml, blocklist_to_csv, blocklist_to_json def load_blocklist_file(filename: str) -> [Instance]: @@ -52,6 +52,8 @@ def exporter(blocklist, output=None, format: str = "toml", private: bool = False exported_text = blocklist_to_csv(blocklist, private) if format == "markdown": exported_text = blocklist_to_markdown(blocklist, private) + if format == "json": + exported_text = blocklist_to_json(blocklist, private) # Output the text if output is not None: @@ -76,7 +78,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('--format', help="Export format: toml|markdown|csv") + parser.add_argument('--format', help="Export format: toml|markdown|csv|json") parser.add_argument('--private', action='store_true', help="When the flag is set, private comment will also be " "exported.") args = parser.parse_args() diff --git a/mastodon_blocklist_deploy/helpers.py b/mastodon_blocklist_deploy/helpers.py index ed35b21..09393a5 100644 --- a/mastodon_blocklist_deploy/helpers.py +++ b/mastodon_blocklist_deploy/helpers.py @@ -2,6 +2,7 @@ from mastodon_blocklist_deploy.models import Instance import toml import io import csv +import json def blocklist_to_markdown(blocklist: [Instance], private: bool = False): if private: @@ -29,3 +30,7 @@ def blocklist_to_csv(blocklist: [Instance], private: bool = False): w.writeheader() w.writerows(blocklist_as_dict) return csv_string.getvalue() + +def blocklist_to_json(blocklist: [Instance], private: bool = False): + json_string = json.dumps([b.as_dict(private) for b in blocklist]) + return json_string