"""dovecot-acl-inherit - Adhoc command to inherit dovecot ACL files to children mailboxes"""

__version__ = '0.1.0'
__author__ = '<NAME> <<EMAIL>>'
__all__ = ['main']


import argparse
import glob
import os
import shutil
import sys
from itertools import chain


SEP = '.'


def delegate_acl(parent, exclude=None):
    parent = get_parent(parent)
    parent_acl_path = os.path.join(parent, 'dovecot-acl')
    children = get_children(parent, exclude=exclude)

    for child in children:
        shutil.copy(parent_acl_path, child)


def get_parent(name):
    parent_path = '.{}'.format(name)
    if not os.path.exists(parent_path):
        raise FileNotFoundError('Mailbox {!r} does not exist'.format(name))
    elif not os.path.isdir(parent_path):
        raise NotADirectoryError('Provided name {!r} is not a mailbox'.format(name))
    return parent_path


def get_children(parent, exclude=None):
    children_pattern = '{}{}**'.format(parent, SEP)
    children = glob.glob(children_pattern)
    if exclude is not None:
        exclude_patterns = ['{}*{}'.format(children_pattern, pattern)
                            for pattern in exclude]
        exclude_globs = (glob.glob(pattern) for pattern in exclude_patterns)
        exclude_children = list(chain(*exclude_globs))
        children = [child for child in children if child not in exclude_children]
    return children


def make_parser():
    parser = argparse.ArgumentParser(description='Inherit dovecot ACL files to children mailboxes')
    parser.add_argument('mailbox')
    parser.add_argument('-e', '--exclude', action='append',
                        help='exclude mathcing mailboxes, can be a glob pattern')
    return parser


def main():
    parser = make_parser()

    if len(sys.argv) == 1:
        parser.print_help()
        parser.exit(2)

    args = parser.parse_args()

    try:
        delegate_acl(args.mailbox, args.exclude)
    except FileNotFoundError as err:
        print(err, file=sys.stderr)
        sys.exit(1)

"""dovecot-acl-inherit - Adhoc command to inherit dovecot ACL files to children mailboxes"""

__version__ = '0.1.0'
__author__ = '<NAME> <<EMAIL>>'
__all__ = ['main']


import argparse
import glob
import os
import shutil
import sys
from itertools import chain


SEP = '.'


def delegate_acl(parent, exclude=None):
    parent = get_parent(parent)
    parent_acl_path = os.path.join(parent, 'dovecot-acl')
    children = get_children(parent, exclude=exclude)

    for child in children:
        shutil.copy(parent_acl_path, child)


def get_parent(name):
    parent_path = '.{}'.format(name)
    if not os.path.exists(parent_path):
        raise FileNotFoundError('Mailbox {!r} does not exist'.format(name))
    elif not os.path.isdir(parent_path):
        raise NotADirectoryError('Provided name {!r} is not a mailbox'.format(name))
    return parent_path


def get_children(parent, exclude=None):
    children_pattern = '{}{}**'.format(parent, SEP)
    children = glob.glob(children_pattern)
    if exclude is not None:
        exclude_patterns = ['{}*{}'.format(children_pattern, pattern)
                            for pattern in exclude]
        exclude_globs = (glob.glob(pattern) for pattern in exclude_patterns)
        exclude_children = list(chain(*exclude_globs))
        children = [child for child in children if child not in exclude_children]
    return children


def make_parser():
    parser = argparse.ArgumentParser(description='Inherit dovecot ACL files to children mailboxes')
    parser.add_argument('mailbox')
    parser.add_argument('-e', '--exclude', action='append',
                        help='exclude mathcing mailboxes, can be a glob pattern')
    return parser


def main():
    parser = make_parser()

    if len(sys.argv) == 1:
        parser.print_help()
        parser.exit(2)

    args = parser.parse_args()

    try:
        delegate_acl(args.mailbox, args.exclude)
    except FileNotFoundError as err:
        print(err, file=sys.stderr)
        sys.exit(1)

dovecot-acl-inherit - Adhoc command to inherit dovecot ACL files to children mailboxes