# Path: cap_client/api/permissions_api.py
# class PermissionsAPI(CapAPI):
#     """Interface for CAP permissions methods."""
# 
#     def get(self, pid):
#         """List permissions for analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :return: list of users/egroups with access to analysis
#         :rtype: dict
#         """
#         res = self._make_request(
#             url=urljoin('deposits/', pid),
#             headers={'Accept': 'application/permissions+json'})
# 
#         return res.get('permissions')
# 
#     def add(self, pid, email, rights, is_egroup=False):
#         """Give user permissions to analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param email: user|egroup email
#         :type email: str
#         :param rights: list of rights (read|update|admin)
#         :type rights: list(str)
#         :param is_egroup: permissions for egroup or user
#         :type is_egroup: bool
#         :return: updated permissions
#         :rtype: dict
#         """
#         data = self._construct_permission(email,
#                                           "add",
#                                           rights,
#                                           is_egroup=is_egroup)
#         res = self._make_request(
#             url='deposits/{}/actions/permissions'.format(pid),
#             method='post',
#             expected_status_code=201,
#             headers={
#                 'Content-type': 'application/json',
#                 'Accept': 'application/permissions+json'
#             },
#             data=json.dumps(data),
#         )
# 
#         return res.get('permissions')
# 
#     def remove(self, pid, email, rights, is_egroup=False):
#         """Revoke user permissions to analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param email: user|egroup email
#         :type email: str
#         :param rights: list of rights (read|update|admin)
#         :type rights: list(str)
#         :param is_egroup: permissions for egroup or user
#         :type is_egroup: bool
#         :return: updated permissions
#         :rtype: dict
#         """
#         data = self._construct_permission(email,
#                                           "remove",
#                                           rights,
#                                           is_egroup=is_egroup)
#         res = self._make_request(
#             url='deposits/{}/actions/permissions'.format(pid),
#             method='post',
#             expected_status_code=201,
#             headers={
#                 'Content-type': 'application/json',
#                 'Accept': 'application/permissions+json'
#             },
#             data=json.dumps(data),
#         )
# 
#         return res.get('permissions')
# 
#     def _construct_permission(self, email, operation, rights, is_egroup=False):
#         return [{
#             "email": email,
#             "type": "egroup" if is_egroup else "user",
#             "op": operation,
#             "action": "deposit-{}".format(right)
#         } for right in rights]
# 
# Path: cap_client/utils.py
# class ColoredGroup(HelpColorsGroup):
# class NotRequiredIf(click.Option):
# class MultipleMutuallyExclusiveOptions(click.Option):
#     def __init__(self, *args, **kwargs):
# def make_tarfile(output_filename, source_dir):
# def validate_version(ctx, param, version_value):
# def load_json_from_file(ctx, param, value):
# def load_json(ctx, param, value):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
# def logger(fun):
#     def wrapper(*args, **kwargs):
# def pid_option(required):
#     def inner_pid_option(fun):
#         def wrapper(*args, **kwargs):


import click
from cap_client.api import PermissionsAPI
from cap_client.utils import (
    ColoredGroup, NotRequiredIf,
    json_dumps, logger, pid_option
)
    cls=NotRequiredIf,
    not_required_if="user",
    help='Egroup mail.',
)
@click.option(
    '--rights',
    '-r',
    type=click.Choice(['read', 'update', 'admin']),
    required=True,
    multiple=True,
)
@logger
@pass_api
def add(api, pid, rights, user, egroup):
    """Add user/egroup permissions for your analysis."""
    res = api.add(
        pid=pid,
        email=user or egroup,
        rights=rights,
        is_egroup=egroup and True,
    )

    click.echo(json_dumps(res))


@permissions.command()
@pid_option(required=True)
@click.option(
    '--user',
    '-u',

# Path: cap_client/api/permissions_api.py
# class PermissionsAPI(CapAPI):
#     """Interface for CAP permissions methods."""
# 
#     def get(self, pid):
#         """List permissions for analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :return: list of users/egroups with access to analysis
#         :rtype: dict
#         """
#         res = self._make_request(
#             url=urljoin('deposits/', pid),
#             headers={'Accept': 'application/permissions+json'})
# 
#         return res.get('permissions')
# 
#     def add(self, pid, email, rights, is_egroup=False):
#         """Give user permissions to analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param email: user|egroup email
#         :type email: str
#         :param rights: list of rights (read|update|admin)
#         :type rights: list(str)
#         :param is_egroup: permissions for egroup or user
#         :type is_egroup: bool
#         :return: updated permissions
#         :rtype: dict
#         """
#         data = self._construct_permission(email,
#                                           "add",
#                                           rights,
#                                           is_egroup=is_egroup)
#         res = self._make_request(
#             url='deposits/{}/actions/permissions'.format(pid),
#             method='post',
#             expected_status_code=201,
#             headers={
#                 'Content-type': 'application/json',
#                 'Accept': 'application/permissions+json'
#             },
#             data=json.dumps(data),
#         )
# 
#         return res.get('permissions')
# 
#     def remove(self, pid, email, rights, is_egroup=False):
#         """Revoke user permissions to analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param email: user|egroup email
#         :type email: str
#         :param rights: list of rights (read|update|admin)
#         :type rights: list(str)
#         :param is_egroup: permissions for egroup or user
#         :type is_egroup: bool
#         :return: updated permissions
#         :rtype: dict
#         """
#         data = self._construct_permission(email,
#                                           "remove",
#                                           rights,
#                                           is_egroup=is_egroup)
#         res = self._make_request(
#             url='deposits/{}/actions/permissions'.format(pid),
#             method='post',
#             expected_status_code=201,
#             headers={
#                 'Content-type': 'application/json',
#                 'Accept': 'application/permissions+json'
#             },
#             data=json.dumps(data),
#         )
# 
#         return res.get('permissions')
# 
#     def _construct_permission(self, email, operation, rights, is_egroup=False):
#         return [{
#             "email": email,
#             "type": "egroup" if is_egroup else "user",
#             "op": operation,
#             "action": "deposit-{}".format(right)
#         } for right in rights]
# 
# Path: cap_client/utils.py
# class ColoredGroup(HelpColorsGroup):
# class NotRequiredIf(click.Option):
# class MultipleMutuallyExclusiveOptions(click.Option):
#     def __init__(self, *args, **kwargs):
# def make_tarfile(output_filename, source_dir):
# def validate_version(ctx, param, version_value):
# def load_json_from_file(ctx, param, value):
# def load_json(ctx, param, value):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
# def logger(fun):
#     def wrapper(*args, **kwargs):
# def pid_option(required):
#     def inner_pid_option(fun):
#         def wrapper(*args, **kwargs):


# Path: cap_client/cli/permissions_cli.py
import click
from cap_client.api import PermissionsAPI
from cap_client.utils import (
    ColoredGroup, NotRequiredIf,
    json_dumps, logger, pid_option
)

    cls=NotRequiredIf,
    not_required_if="user",
    help='Egroup mail.',
)
@click.option(
    '--rights',
    '-r',
    type=click.Choice(['read', 'update', 'admin']),
    required=True,
    multiple=True,
)
@logger
@pass_api
def add(api, pid, rights, user, egroup):
    """Add user/egroup permissions for your analysis."""
    res = api.add(
        pid=pid,
        email=user or egroup,
        rights=rights,
        is_egroup=egroup and True,
    )

    click.echo(json_dumps(res))


@permissions.command()
@pid_option(required=True)
@click.option(
    '--user',
    '-u',

    cls=NotRequiredIf,