refactor: Avoid manually templating toml file entries

This commit is contained in:
Georg Krause 2023-01-12 17:07:28 +01:00
parent 6d2a4d82b4
commit 8d5676d0b2
No known key found for this signature in database
GPG Key ID: 2970D504B2183D22

View File

@ -19,23 +19,6 @@ def load_blocklist_file(filename: str) -> [Instance]:
return instances return instances
def export_blocklist_toml(blocklist: [Instance], filname: str):
toml_str = ""
for instance in blocklist:
toml_str += f'''
[[instances]]
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) -> [Instance]: def blocklist_json_to_instances(blocklist_json: str) -> [Instance]:
instances = [] instances = []
for i in blocklist_json: for i in blocklist_json:
@ -109,9 +92,10 @@ def cli():
Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete) Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete)
elif args.action == "export": elif args.action == "export":
if not args.output: if not args.output:
print("Export currently requires to pass --output as well") print(toml.dumps({"instances": [b.__dict__ for b in remote_blocklist]}))
else: else:
export_blocklist_toml(remote_blocklist, args.output) with open(args.output, "w") as f:
toml.dump({"instances": [b.__dict__ for b in remote_blocklist]}, f)
if __name__ == "__main__": if __name__ == "__main__":