Compare commits
9 Commits
b39a2c4b4e
...
229608a090
Author | SHA1 | Date |
---|---|---|
Georg Krause | 229608a090 | |
Julian-Samuel Gebühr | 0b49740e83 | |
Julian-Samuel Gebühr | c7872201ea | |
Julian-Samuel Gebühr | ce5c1ae39d | |
Julian-Samuel Gebühr | da984d80e4 | |
Julian-Samuel Gebühr | eaccce8c6e | |
Julian-Samuel Gebühr | 066e77d493 | |
moanos | b4ef4b9199 | |
Georg Krause | 0ecc925373 |
|
@ -3,13 +3,13 @@ import argparse
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
import os
|
||||||
import toml
|
import toml
|
||||||
|
|
||||||
from mastodon_blocklist_deploy.models import Instance
|
from mastodon_blocklist_deploy.models import Instance
|
||||||
|
|
||||||
|
|
||||||
def load_local_blocklist(filename: str) -> [Instance]:
|
def load_blocklist_file(filename: str) -> [Instance]:
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r") as f:
|
||||||
data = toml.load(f)
|
data = toml.load(f)
|
||||||
instances = []
|
instances = []
|
||||||
|
@ -19,14 +19,14 @@ def load_local_blocklist(filename: str) -> [Instance]:
|
||||||
return instances
|
return instances
|
||||||
|
|
||||||
|
|
||||||
def blocklist_json_to_instances(blocklist_json: str):
|
def blocklist_json_to_instances(blocklist_json: str) -> [Instance]:
|
||||||
instances = []
|
instances = []
|
||||||
for i in blocklist_json:
|
for i in blocklist_json:
|
||||||
instances.append(Instance(i))
|
instances.append(Instance(i))
|
||||||
return instances
|
return instances
|
||||||
|
|
||||||
|
|
||||||
def load_remote_blocklist(server: str, token: str):
|
def load_blocklist_from_instance(server: str, token: str) -> [Instance]:
|
||||||
headers = {
|
headers = {
|
||||||
f'Authorization': f'Bearer {token}',
|
f'Authorization': f'Bearer {token}',
|
||||||
}
|
}
|
||||||
|
@ -59,12 +59,19 @@ def cli():
|
||||||
else:
|
else:
|
||||||
logging.basicConfig(level=logging.WARN)
|
logging.basicConfig(level=logging.WARN)
|
||||||
|
|
||||||
|
if args.token:
|
||||||
|
token = args.token
|
||||||
|
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 there is a remote blocklist provided load this instead of fetching it from a server (for debugging reasons)"""
|
||||||
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_blocklist_from_instance(server=args.server, token=token)
|
||||||
|
|
||||||
"""Load local blocklist only when needed"""
|
"""Load local blocklist only when needed"""
|
||||||
if args.action in ["diff", "deploy"]:
|
if args.action in ["diff", "deploy"]:
|
||||||
|
@ -72,16 +79,19 @@ def cli():
|
||||||
blocklist_filename = args.input_file
|
blocklist_filename = args.input_file
|
||||||
else:
|
else:
|
||||||
blocklist_filename = "../blocklist.toml"
|
blocklist_filename = "../blocklist.toml"
|
||||||
local_blocklist = load_local_blocklist(blocklist_filename)
|
local_blocklist = load_blocklist_file(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, args.no_delete)
|
Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete)
|
||||||
elif args.action == "export":
|
elif args.action == "export":
|
||||||
with open(args.output, "w") as f:
|
if not args.output:
|
||||||
toml.dump({"instances": [b.__dict__ for b in remote_blocklist]}, f)
|
print(toml.dumps({"instances": [b.__dict__ for b in remote_blocklist]}))
|
||||||
|
else:
|
||||||
|
with open(args.output, "w") as f:
|
||||||
|
toml.dump({"instances": [b.__dict__ for b in remote_blocklist]}, f)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue