# Path: cap_client/api/files_api.py
# class FilesAPI(CapAPI):
#     """Interface for CAP files methods."""
# 
#     def get(self, pid):
#         """Get list of files attached to analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :return: list of files
#         :rtype: list
#         """
#         return self._make_request(url='deposits/{}/files'.format(pid))
# 
#     def download(self, pid, filename, output_filepath=None):
#         """Download a file attached to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filename: filename
#         :type filename: str
#         :param output_filepath: save your file as..
#         :type output_filepath: str, optional
#         :return: None
#         """
#         if output_filepath:
#             dirpath = os.path.dirname(output_filepath)
#             if dirpath and not os.path.exists(dirpath):
#                 raise UsageError(
#                     'Directory {} does not exist.'.format(dirpath))
# 
#         bucket_url = self._get_bucket_link(pid)
#         data = self._make_request(
#             url=bucket_url + '/' + filename,
#             headers={},
#             stream=True,
#         )
# 
#         with open(output_filepath or filename, 'wb') as fp:
#             fp.write(data.content)
# 
#     def upload_directory(self, pid, filepath, output_filename=None):
#         """Upload a directory to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filepath: filepath to uploaded file
#         :type filepath: str
#         :param output_filename: save your file as..
#         :type output_filename: str
#         :return: None
#         """
#         bucket_url = self._get_bucket_link(pid)
#         fname = output_filename or os.path.basename(filepath)
#         fname = fname if fname.endswith('.tar.gz') else fname + '.tar.gz'
# 
#         with tempfile.TemporaryFile() as fp:
#             with tarfile.open(fileobj=fp, mode='w:gz') as tar:
#                 tar.add(filepath)
# 
#             fp.flush()
#             fp.seek(0)
# 
#             self._make_request(
#                 url=bucket_url + '/' + fname,
#                 method='put',
#                 headers={},
#                 data=fp,
#             )
# 
#     def upload_file(self, pid, filepath, output_filename=None):
#         """Upload a file to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filepath: filepath to uploaded file
#         :type filepath: str
#         :param output_filename: save your file as..
#         :type output_filename: str
#         :return: None
#         """
#         bucket_url = self._get_bucket_link(pid)
#         fname = output_filename or os.path.basename(filepath)
# 
#         with open(filepath, 'rb') as fp:
#             self._make_request(
#                 url=bucket_url + '/' + fname,
#                 method='put',
#                 headers={},
#                 data=fp,
#             )
# 
#     def remove(self, pid, filename):
#         """Remove a file attached to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filename: filename
#         :type filename: str
#         :return: None
#         """
#         bucket_url = self._get_bucket_link(pid)
# 
#         self._make_request(
#             url=bucket_url + '/' + filename,
#             method='delete',
#             expected_status_code=204,
#         )
# 
#     def _get_bucket_link(self, pid):
#         """Make request to server to fetch link to analysis bucket.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :return: url to analysis bucket
#         :rtype: str
#         """
#         ana = self._make_request(urljoin('deposits/', pid))
#         return ana['links']['bucket']
# 
# 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 os
import click
from cap_client.api import FilesAPI
from cap_client.utils import ColoredGroup, json_dumps, logger, pid_option
    help="Bypasses prompts..Say YES to everything",
)
@click.argument(
    'file',
    type=click.Path(exists=True),
)
@logger
@pass_api
def upload(api, pid, file, output_filename, yes_i_know):
    """Upload a file to your analysis."""
    if os.path.isdir(file):
        if yes_i_know or click.confirm(
                '{} is a directory. Do you want to upload a tarball?'.format(
                    file)):
            api.upload_directory(
                pid=pid,
                filepath=file,
                output_filename=output_filename,
            )
    else:
        api.upload_file(
            pid=pid,
            filepath=file,
            output_filename=output_filename,
        )

    click.echo("File uploaded successfully.")


@files.command()

# Path: cap_client/api/files_api.py
# class FilesAPI(CapAPI):
#     """Interface for CAP files methods."""
# 
#     def get(self, pid):
#         """Get list of files attached to analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :return: list of files
#         :rtype: list
#         """
#         return self._make_request(url='deposits/{}/files'.format(pid))
# 
#     def download(self, pid, filename, output_filepath=None):
#         """Download a file attached to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filename: filename
#         :type filename: str
#         :param output_filepath: save your file as..
#         :type output_filepath: str, optional
#         :return: None
#         """
#         if output_filepath:
#             dirpath = os.path.dirname(output_filepath)
#             if dirpath and not os.path.exists(dirpath):
#                 raise UsageError(
#                     'Directory {} does not exist.'.format(dirpath))
# 
#         bucket_url = self._get_bucket_link(pid)
#         data = self._make_request(
#             url=bucket_url + '/' + filename,
#             headers={},
#             stream=True,
#         )
# 
#         with open(output_filepath or filename, 'wb') as fp:
#             fp.write(data.content)
# 
#     def upload_directory(self, pid, filepath, output_filename=None):
#         """Upload a directory to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filepath: filepath to uploaded file
#         :type filepath: str
#         :param output_filename: save your file as..
#         :type output_filename: str
#         :return: None
#         """
#         bucket_url = self._get_bucket_link(pid)
#         fname = output_filename or os.path.basename(filepath)
#         fname = fname if fname.endswith('.tar.gz') else fname + '.tar.gz'
# 
#         with tempfile.TemporaryFile() as fp:
#             with tarfile.open(fileobj=fp, mode='w:gz') as tar:
#                 tar.add(filepath)
# 
#             fp.flush()
#             fp.seek(0)
# 
#             self._make_request(
#                 url=bucket_url + '/' + fname,
#                 method='put',
#                 headers={},
#                 data=fp,
#             )
# 
#     def upload_file(self, pid, filepath, output_filename=None):
#         """Upload a file to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filepath: filepath to uploaded file
#         :type filepath: str
#         :param output_filename: save your file as..
#         :type output_filename: str
#         :return: None
#         """
#         bucket_url = self._get_bucket_link(pid)
#         fname = output_filename or os.path.basename(filepath)
# 
#         with open(filepath, 'rb') as fp:
#             self._make_request(
#                 url=bucket_url + '/' + fname,
#                 method='put',
#                 headers={},
#                 data=fp,
#             )
# 
#     def remove(self, pid, filename):
#         """Remove a file attached to your analysis.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param filename: filename
#         :type filename: str
#         :return: None
#         """
#         bucket_url = self._get_bucket_link(pid)
# 
#         self._make_request(
#             url=bucket_url + '/' + filename,
#             method='delete',
#             expected_status_code=204,
#         )
# 
#     def _get_bucket_link(self, pid):
#         """Make request to server to fetch link to analysis bucket.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :return: url to analysis bucket
#         :rtype: str
#         """
#         ana = self._make_request(urljoin('deposits/', pid))
#         return ana['links']['bucket']
# 
# 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/files_cli.py
import os
import click
from cap_client.api import FilesAPI
from cap_client.utils import ColoredGroup, json_dumps, logger, pid_option

    help="Bypasses prompts..Say YES to everything",
)
@click.argument(
    'file',
    type=click.Path(exists=True),
)
@logger
@pass_api
def upload(api, pid, file, output_filename, yes_i_know):
    """Upload a file to your analysis."""
    if os.path.isdir(file):
        if yes_i_know or click.confirm(
                '{} is a directory. Do you want to upload a tarball?'.format(
                    file)):
            api.upload_directory(
                pid=pid,
                filepath=file,
                output_filename=output_filename,
            )
    else:
        api.upload_file(
            pid=pid,
            filepath=file,
            output_filename=output_filename,
        )

    click.echo("File uploaded successfully.")


@files.command()

@pid_option(required=True)