Add json
continuous-integration/drone/push Build is passing Details

pull/12/head
Julian-Samuel Gebühr 2023-05-03 15:40:27 +02:00
parent e1d9fe04f9
commit 569cff0957
3 changed files with 10 additions and 3 deletions

View File

@ -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.
```

View File

@ -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()

View File

@ -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