2021-03-27 00:11:22 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from ruamel.yaml import YAML, scalarstring
|
|
|
|
|
2021-07-23 17:14:44 +02:00
|
|
|
from blimp.misc import local_file_present
|
2021-03-27 00:11:22 +01:00
|
|
|
|
2021-03-28 16:39:58 +02:00
|
|
|
|
2021-03-27 00:11:22 +01:00
|
|
|
class ProcessBlocklist:
|
2021-03-28 16:39:58 +02:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2021-03-27 00:11:22 +01:00
|
|
|
def process(self, blacklist, outfile, dryrun: bool):
|
|
|
|
"""
|
|
|
|
function to build and compare the local yaml file to the remote file
|
|
|
|
if the remote file is different, the local file gets overwritten
|
|
|
|
"""
|
|
|
|
# cheeky none catch
|
|
|
|
try:
|
|
|
|
# load local blacklist outfile
|
|
|
|
if local_file_present(outfile):
|
|
|
|
with open(outfile, "r", encoding="utf-8") as local_file:
|
|
|
|
local_blacklist = local_file.read()
|
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# no local copy use empty one instead
|
|
|
|
local_blacklist = YAML(typ="safe")
|
|
|
|
|
|
|
|
# blacklist frame
|
|
|
|
remote_file = {"acl": {"spamblacklist": {"server": []}}}
|
|
|
|
|
|
|
|
# build the blacklist with the given frame to compare to local blacklist
|
|
|
|
for entry in blacklist.split():
|
|
|
|
entry = scalarstring.DoubleQuotedScalarString(entry)
|
|
|
|
remote_file["acl"]["spamblacklist"]["server"].append(entry)
|
|
|
|
|
|
|
|
yml = YAML()
|
|
|
|
yml.indent(offset=2)
|
|
|
|
yml.default_flow_style = False
|
|
|
|
|
|
|
|
# if dry-run true print expected content
|
|
|
|
if dryrun:
|
|
|
|
yml.dump(remote_file, sys.stdout)
|
|
|
|
return
|
|
|
|
|
|
|
|
if local_blacklist == remote_file:
|
|
|
|
return
|
|
|
|
|
|
|
|
if outfile is None:
|
|
|
|
print("no outfile assigned", file=sys.stderr)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
# proceed to update the defined outfile
|
|
|
|
with open(outfile, "w", encoding="utf-8") as new_local_file:
|
|
|
|
yml.dump(remote_file, new_local_file)
|