refactor: Cleaner solution for exportable dict

This commit is contained in:
moanos [he/him] 2023-01-26 12:05:12 +01:00
parent d4c754c103
commit 3dce62417e
2 changed files with 10 additions and 7 deletions

View File

@ -38,9 +38,6 @@ def load_blocklist_from_instance(server: str, token: str) -> [Instance]:
else:
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 cli():
parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server')
@ -67,8 +64,6 @@ 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:
@ -95,10 +90,10 @@ def cli():
Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete)
elif args.action == "export":
if not args.output:
print(toml.dumps({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]}))
print(toml.dumps({"instances": [b.exportable_dict for b in remote_blocklist]}))
else:
with open(args.output, "w") as f:
toml.dump({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]}, f)
toml.dump({"instances": [b.exportable_dict for b in remote_blocklist]}, f)
if __name__ == "__main__":

View File

@ -27,6 +27,14 @@ 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"]
exportable = {}
for key in keys:
exportable[key] = getattr(self, key)
return exportable
def parse_remote_block(self, instance_dict):
self.domain = instance_dict["domain"]
self.id = instance_dict["id"]