Merge branch 'rework_export'

This commit is contained in:
moanos [he/him] 2023-05-03 15:14:23 +02:00
commit e1d9fe04f9
3 changed files with 24 additions and 11 deletions

View File

@ -25,14 +25,15 @@ supports [Array of Tables](https://toml.io/en/v1.0.0#array-of-tables), I'd prefe
## ##
``` ```
usage: mastodon_blocklist_deploy [-h] [-s SERVER] [-t TOKEN] [-i INPUT_FILE] [-r REMOTE_BLOCKLIST] [-o OUTPUT] [-v] [-n] [--format FORMAT] [--private] usage: mastodon_blocklist_deploy [-h] [-s SERVER] [-t TOKEN] [-i INPUT_FILE] [-r REMOTE_BLOCKLIST] [-o OUTPUT] [-v] [-n]
[--format FORMAT] [--private]
{diff,deploy,export} {diff,deploy,export}
Deploy blocklist updates to a mastodon server Deploy blocklist updates to a mastodon server
positional arguments: positional arguments:
{diff,deploy,export} Either use 'diff' to check the difference between local blockĺist and the blocklist on the server, 'deploy' to apply the current local {diff,deploy,export} Either use 'diff' to check the difference between local blockĺist and the blocklist on the server, 'deploy'
blocklist or 'export' to export the remote blocklist into a local file. to apply the current local blocklist or 'export' to export the remote blocklist into a local file.
options: options:
-h, --help show this help message and exit -h, --help show this help message and exit
@ -48,8 +49,8 @@ options:
Filename where to export the blocklist Filename where to export the blocklist
-v, --verbose -v, --verbose
-n, --no-delete Do not delete existing blocks -n, --no-delete Do not delete existing blocks
--format FORMAT Export format: toml|markdown --format FORMAT Export format: toml|markdown|csv
--private When the flag is set private comment will also be exported. --private When the flag is set, private comment will also be exported.
``` ```
## Obtain a server token ## Obtain a server token

View File

@ -7,7 +7,7 @@ import os
import toml import toml
from mastodon_blocklist_deploy.models import Instance from mastodon_blocklist_deploy.models import Instance
from mastodon_blocklist_deploy.helpers import blocklist_to_markdown, blocklist_to_toml from mastodon_blocklist_deploy.helpers import blocklist_to_markdown, blocklist_to_toml, blocklist_to_csv
def load_blocklist_file(filename: str) -> [Instance]: def load_blocklist_file(filename: str) -> [Instance]:
@ -46,10 +46,12 @@ def remove_key_from_dict(dict, key):
def exporter(blocklist, output=None, format: str = "toml", private: bool = False): def exporter(blocklist, output=None, format: str = "toml", private: bool = False):
if format == "markdown":
exported_text = blocklist_to_markdown(blocklist, private)
if format == "toml": if format == "toml":
exported_text = blocklist_to_toml(blocklist, private) exported_text = blocklist_to_toml(blocklist, private)
if format == "csv":
exported_text = blocklist_to_csv(blocklist, private)
if format == "markdown":
exported_text = blocklist_to_markdown(blocklist, private)
# Output the text # Output the text
if output is not None: if output is not None:
@ -74,7 +76,7 @@ def cli():
parser.add_argument('-o', '--output', help="Filename where to export the blocklist") parser.add_argument('-o', '--output', help="Filename where to export the blocklist")
parser.add_argument('-v', '--verbose', action='store_true') 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('-n', '--no-delete', action='store_true', help="Do not delete existing blocks")
parser.add_argument('--format', help="Export format: toml|markdown") parser.add_argument('--format', help="Export format: toml|markdown|csv")
parser.add_argument('--private', action='store_true', help="When the flag is set, private comment will also be " parser.add_argument('--private', action='store_true', help="When the flag is set, private comment will also be "
"exported.") "exported.")
args = parser.parse_args() args = parser.parse_args()

View File

@ -1,6 +1,7 @@
from mastodon_blocklist_deploy.models import Instance from mastodon_blocklist_deploy.models import Instance
import toml import toml
import io
import csv
def blocklist_to_markdown(blocklist: [Instance], private: bool = False): def blocklist_to_markdown(blocklist: [Instance], private: bool = False):
if private: if private:
@ -19,3 +20,12 @@ def blocklist_to_markdown(blocklist: [Instance], private: bool = False):
def blocklist_to_toml(blocklist: [Instance], private: bool = False): def blocklist_to_toml(blocklist: [Instance], private: bool = False):
toml_string = toml.dumps({"instances": [b.as_dict(private) for b in blocklist]}) toml_string = toml.dumps({"instances": [b.as_dict(private) for b in blocklist]})
return toml_string return toml_string
def blocklist_to_csv(blocklist: [Instance], private: bool = False):
csv_string = io.StringIO()
blocklist_as_dict = [b.as_dict(private) for b in blocklist]
keys = blocklist_as_dict[0].keys()
w = csv.DictWriter(csv_string, keys)
w.writeheader()
w.writerows(blocklist_as_dict)
return csv_string.getvalue()