Add export options, use toml

This commit is contained in:
moanos [he/him] 2023-01-09 13:10:40 +01:00
parent 7f3154f515
commit fb85a35828
3 changed files with 35 additions and 10 deletions

41
cli.py
View File

@ -4,14 +4,14 @@ import json
import logging import logging
import requests import requests
import tomli import toml
from models import Instance from models import Instance
def load_local_blocklist(filename: str) -> [Instance]: def load_local_blocklist(filename: str) -> [Instance]:
with open(filename, "rb") as f: with open(filename, "rb") as f:
data = tomli.load(f) data = toml.load(f)
instances = [] instances = []
for instance_dict in data["instances"]: for instance_dict in data["instances"]:
instance = Instance(instance_dict) instance = Instance(instance_dict)
@ -21,6 +21,23 @@ def load_local_blocklist(filename: str) -> [Instance]:
return instances return instances
def export_blocklist_toml(blocklist: [Instance], filname: str):
toml_str = ""
for instance in blocklist:
toml_str += f'''
[instance]
name = "{instance.domain}"
domain = "{instance.domain}"
severity = "{instance.severity}"
reject_media = {str(instance.reject_media).lower()}
reject_reports = {str(instance.reject_reports).lower()}
public_comment = "{instance.public_comment}"
private_comment = "{instance.private_comment}"
'''
with open(filname, "w") as f:
f.write(toml_str)
def blocklist_json_to_instances(blocklist_json: str): def blocklist_json_to_instances(blocklist_json: str):
instances = [] instances = []
for i in blocklist_json: for i in blocklist_json:
@ -43,30 +60,38 @@ def load_remote_blocklist(server: str, token: str):
def cli(): def cli():
parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server') parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server')
parser.add_argument('action', choices=['diff', 'deploy'], parser.add_argument('action', choices=['diff', 'deploy', 'export'],
help="Either use 'diff' to check the difference between current blocks and future blocks or 'deploy'.") help="Either use 'diff' to check the difference between current blocks and future blocks or 'deploy'.")
parser.add_argument('-s', '--server', help="The address of the server where you want to deploy (e.g. " parser.add_argument('-s', '--server', help="The address of the server where you want to deploy (e.g. "
"mastodon.social)") "mastodon.social)")
parser.add_argument('-t', '--token', help="Authorization token") parser.add_argument('-t', '--token', help="Authorization token")
parser.add_argument('-i', '--input-file', help="The blocklist to use") parser.add_argument('-i', '--input-file', help="The blocklist to use")
parser.add_argument('-r', '--remote-blocklist', help="The remote blocklist as json for debugging reasons") parser.add_argument('-r', '--remote-blocklist', help="The remote blocklist as json for debugging reasons")
parser.add_argument('-o', '--output', help="Filename where to export the blocklist")
args = parser.parse_args() args = parser.parse_args()
logging.basicConfig(level=logging.WARN) logging.basicConfig(level=logging.WARN)
if args.input_file:
blocklist_filename = args.input_file
else:
blocklist_filename = "blocklist.toml"
local_blocklist = load_local_blocklist(blocklist_filename)
if args.remote_blocklist: if args.remote_blocklist:
with open(args.remote_blocklist) as f: with open(args.remote_blocklist) as f:
remote_blocklist = blocklist_json_to_instances(json.load(f)) remote_blocklist = blocklist_json_to_instances(json.load(f))
else: else:
remote_blocklist = load_remote_blocklist(server=args.server, token=args.token) remote_blocklist = load_remote_blocklist(server=args.server, token=args.token)
"""Load local blocklist only when needed"""
if args.action in ["diff", "deploy"]:
if args.input_file:
blocklist_filename = args.input_file
else:
blocklist_filename = "blocklist.toml"
local_blocklist = load_local_blocklist(blocklist_filename)
if args.action == "diff": if args.action == "diff":
Instance.show_diffs(local_blocklist, remote_blocklist) Instance.show_diffs(local_blocklist, remote_blocklist)
elif args.action == "deploy": elif args.action == "deploy":
diffs = Instance.list_diffs(local_blocklist, remote_blocklist) diffs = Instance.list_diffs(local_blocklist, remote_blocklist)
Instance.apply_blocks_from_diff(diffs, args.server, args.token) Instance.apply_blocks_from_diff(diffs, args.server, args.token)
elif args.action == "export":
export_blocklist_toml(remote_blocklist, args.output)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -17,7 +17,7 @@ class Instance:
self.parse_local_block(instance_dict) self.parse_local_block(instance_dict)
def __str__(self): def __str__(self):
return f"{self.name}: {self.severity}" return f"{self.domain}: {self.severity}"
def __eq__(self, other): def __eq__(self, other):
return self.domain == other.domain and self.severity == other.severity and self.reject_media == other.reject_media and self.reject_reports == other.reject_reports and self.obfuscate == other.obfuscate return self.domain == other.domain and self.severity == other.severity and self.reject_media == other.reject_media and self.reject_reports == other.reject_reports and self.obfuscate == other.obfuscate

View File

@ -8,9 +8,9 @@ packages = [{include = "mastodon_blocklist_deploy"}]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.10" python = "^3.10"
tomli = "^2.0.1"
requests = "^2.28.1" requests = "^2.28.1"
rich = "^13.0.1" rich = "^13.0.1"
toml = "^0.10.2"
[build-system] [build-system]