Mini Shell

Direktori : /opt/sharedrads/
Upload File :
Current File : //opt/sharedrads/exclude_sender

#!/opt/imh-python/bin/python3
import json
import argparse
from os import path
from datetime import datetime, timedelta

parser = argparse.ArgumentParser(
    description='Add or remove temporary exclusion from sender_volume crit.'
)

parser.add_argument(
    'Sender', metavar='sender', type=str, help='Email address to ignore.'
)

parser.add_argument(
    '-t',
    action='store',
    metavar='hours',
    type=int,
    default=1,
    help='Number of hours to exclude a sender from the crit, Default 1, Max 6.',
)

parser.add_argument(
    '--d',
    action='store_const',
    metavar='delete',
    default=False,
    const=True,
    help='Delete the exclusion for the specified user.',
)
args = parser.parse_args()


def get_exclusion_data():
    if path.exists("/tmp/sender_volume.json"):
        try:
            with open('/tmp/sender_volume.json') as exclude_json:
                data = json.load(exclude_json)
        except Exception:
            data = {}
    else:
        data = {}
    return data


def write_exclusion_file(data):
    with open('/tmp/sender_volume.json', 'w') as exclusion_file:
        json.dump(data, exclusion_file)


def main():
    data = get_exclusion_data()
    if args.d is False:
        if args.t > 6:
            expire_time = datetime.now() + timedelta(hours=1)
        else:
            expire_time = datetime.now() + timedelta(hours=args.t)
        data[args.Sender] = str(expire_time)
    if args.d is True:
        if args.Sender in data:
            del data[args.Sender]
    write_exclusion_file(data)


if __name__ == '__main__':
    main()

Zerion Mini Shell 1.0