From 46b0fe6b50cf7510ad2169afff2ca91a66fdc20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 3 May 2023 11:30:28 +0200 Subject: [PATCH 1/5] Rework exporter --- mastodon_blocklist_deploy/cli.py | 38 +++++++++++++++++++++++----- mastodon_blocklist_deploy/helpers.py | 21 +++++++++++++++ mastodon_blocklist_deploy/models.py | 9 ++++--- 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 mastodon_blocklist_deploy/helpers.py diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 68ea688..a8faa5d 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, blocklist_to_toml def load_blocklist_file(filename: str) -> [Instance]: @@ -39,6 +40,31 @@ def load_blocklist_from_instance(server: str, token: str) -> [Instance]: raise ConnectionError(f"Could not connect to the server ({response.status_code}: {response.reason})") +def remove_key_from_dict(dict, key): + del dict[key] + return dict + + +def exporter(blocklist, output=None, format: str = "toml", private: bool = False): + if format == "markdown": + exported_text = blocklist_to_markdown(blocklist, private) + if output is not None: + with open(output, "w") as f: + f.write(exported_text) + else: + print(exported_text) + if format == "toml": + exported_text = blocklist_to_toml(blocklist, private) + + # Output the text + if output is not None: + with open(output, "w") as f: + f.write(exported_text) + else: + print(exported_text) + + + def cli(): parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server') parser.add_argument('action', choices=['diff', 'deploy', 'export'], @@ -53,6 +79,9 @@ 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") + parser.add_argument('--private', action='store_true', help="When the flag is set private comment will also be " + "exported.") args = parser.parse_args() if args.verbose: logging.basicConfig(level=logging.DEBUG) @@ -64,6 +93,8 @@ def cli(): else: token = os.getenv('MBD_TOKEN') + + """if there is a remote blocklist provided load this instead of fetching it from a server (for debugging reasons)""" if args.remote_blocklist: with open(args.remote_blocklist) as f: @@ -89,12 +120,7 @@ def cli(): diffs = Instance.list_diffs(local_blocklist, remote_blocklist) Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete) elif args.action == "export": - if not args.output: - print(toml.dumps({"instances": [b.exportable_dict for b in remote_blocklist]})) - else: - with open(args.output, "w") as f: - toml.dump({"instances": [b.exportable_dict for b in remote_blocklist]}, f) - + exporter(remote_blocklist, args.output, args.format, args.private) if __name__ == "__main__": cli() diff --git a/mastodon_blocklist_deploy/helpers.py b/mastodon_blocklist_deploy/helpers.py new file mode 100644 index 0000000..93d7b30 --- /dev/null +++ b/mastodon_blocklist_deploy/helpers.py @@ -0,0 +1,21 @@ +from mastodon_blocklist_deploy.models import Instance +import toml + + +def blocklist_to_markdown(blocklist: [Instance], private: bool = False): + if private: + markdown_string = "| Instance | Status | Reason | Private Comment |\n | --- | --- | --- |\n" + else: + markdown_string = "| Instance | Status | Reason |\n | --- | --- | --- |\n" + for instance in blocklist: + + if private: + markdown_string += f"| {instance.domain} | {instance.severity} | {instance.public_comment} | {instance.private_comment} |\n" + else: + markdown_string += f"| {instance.domain} | {instance.severity} | {instance.public_comment} |\n" + + return markdown_string + +def blocklist_to_toml(blocklist: [Instance], private: bool = False): + toml_string = toml.dumps({"instances": [b.as_dict(private) for b in blocklist]}) + return toml_string diff --git a/mastodon_blocklist_deploy/models.py b/mastodon_blocklist_deploy/models.py index bf650d3..7b360a1 100644 --- a/mastodon_blocklist_deploy/models.py +++ b/mastodon_blocklist_deploy/models.py @@ -27,9 +27,10 @@ class Instance: def status_str(self): return f"{self.severity}\nReject reports: {self.reject_reports}\nReject media: {self.reject_media}\nObfuscate: {self.obfuscate}" - @property - def exportable_dict(self): - keys = ["domain", "severity", "public_comment", "private_comment", "obfuscate", "reject_media", "reject_reports"] + def as_dict(self, private=False): + keys = ["domain", "severity", "public_comment", "obfuscate", "reject_media", "reject_reports"] + if private: + keys.append("private_comment") exportable = {} for key in keys: exportable[key] = getattr(self, key) @@ -86,7 +87,7 @@ class Instance: response = requests.put(f'https://{server}/api/v1/admin/domain_blocks/{block_id}', data=data, headers=headers) if response.status_code != 200: - raise ConnectionError(f"Could not apply block ({response.status_code}: {response.reason})") + raise ConnectionError(f"Could not apply block for {self.domain} ({response.status_code}: {response.reason})") def delete(self, server: str, token: str): """Deletes the instance from the blocklist on the remote server""" From 7d986a7072385c0c272c2fe9d6aa66c5db2459a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 3 May 2023 11:53:41 +0200 Subject: [PATCH 2/5] Update help --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d1ebc2e..e0b5d66 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,14 @@ supports [Array of Tables](https://toml.io/en/v1.0.0#array-of-tables), I'd prefe ## ``` -$ mastodon_blocklist_deploy -h -usage: mastodon_blocklist_deploy [-h] [-s SERVER] [-t TOKEN] [-i INPUT_FILE] [-r REMOTE_BLOCKLIST] [-o OUTPUT] [-v] [-n] {diff,deploy,export} +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} Deploy blocklist updates to a mastodon server 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 blocklist or 'export' to export the remote blocklist into a local file. + {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 + blocklist or 'export' to export the remote blocklist into a local file. options: -h, --help show this help message and exit @@ -47,6 +48,8 @@ options: Filename where to export the blocklist -v, --verbose -n, --no-delete Do not delete existing blocks + --format FORMAT Export format: toml|markdown + --private When the flag is set private comment will also be exported. ``` ## Obtain a server token From fe12631ee48b224b9c59a112791280c23877bae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 3 May 2023 14:45:53 +0200 Subject: [PATCH 3/5] Remove double output, typo --- mastodon_blocklist_deploy/cli.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index a8faa5d..55abf97 100644 --- a/mastodon_blocklist_deploy/cli.py +++ b/mastodon_blocklist_deploy/cli.py @@ -48,11 +48,6 @@ def remove_key_from_dict(dict, key): def exporter(blocklist, output=None, format: str = "toml", private: bool = False): if format == "markdown": exported_text = blocklist_to_markdown(blocklist, private) - if output is not None: - with open(output, "w") as f: - f.write(exported_text) - else: - print(exported_text) if format == "toml": exported_text = blocklist_to_toml(blocklist, private) @@ -80,7 +75,7 @@ def cli(): 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") - 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.") args = parser.parse_args() if args.verbose: From 58429a39f0ab590b654e42e276a58bbfe6d95651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 3 May 2023 15:09:12 +0200 Subject: [PATCH 4/5] Add csv format option --- mastodon_blocklist_deploy/cli.py | 10 ++++++---- mastodon_blocklist_deploy/helpers.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/mastodon_blocklist_deploy/cli.py b/mastodon_blocklist_deploy/cli.py index 55abf97..4b4b7e4 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 +from mastodon_blocklist_deploy.helpers import blocklist_to_markdown, blocklist_to_toml, blocklist_to_csv 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): - if format == "markdown": - exported_text = blocklist_to_markdown(blocklist, private) if format == "toml": 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 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('-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") + 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 " "exported.") args = parser.parse_args() diff --git a/mastodon_blocklist_deploy/helpers.py b/mastodon_blocklist_deploy/helpers.py index 93d7b30..ed35b21 100644 --- a/mastodon_blocklist_deploy/helpers.py +++ b/mastodon_blocklist_deploy/helpers.py @@ -1,6 +1,7 @@ from mastodon_blocklist_deploy.models import Instance import toml - +import io +import csv def blocklist_to_markdown(blocklist: [Instance], private: bool = False): if private: @@ -19,3 +20,12 @@ def blocklist_to_markdown(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]}) 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() From 2984729841acb51f03d8fece34bdb83696d61e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Wed, 3 May 2023 15:11:56 +0200 Subject: [PATCH 5/5] Upadte reference in readme --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e0b5d66..4f857bf 100644 --- a/README.md +++ b/README.md @@ -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} Deploy blocklist updates to a mastodon server 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 - blocklist or 'export' to export the remote blocklist into a local file. + {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 blocklist or 'export' to export the remote blocklist into a local file. options: -h, --help show this help message and exit @@ -48,8 +49,8 @@ options: Filename where to export the blocklist -v, --verbose -n, --no-delete Do not delete existing blocks - --format FORMAT Export format: toml|markdown - --private When the flag is set private comment will also be exported. + --format FORMAT Export format: toml|markdown|csv + --private When the flag is set, private comment will also be exported. ``` ## Obtain a server token