<commit_before># -*- coding: utf-8 -*-
'''
Apache Libcloud Storage State
=============================

Manage cloud storage using libcloud

    :codeauthor: :email:`<NAME> <<EMAIL>>`

Apache Libcloud Storage (object/blob) management for a full list
of supported clouds, see http://libcloud.readthedocs.io/en/latest/storage/supported_providers.html

Clouds include Amazon S3, Google Storage, Aliyun, Azure Blobs, Ceph, OpenStack swift

.. versionadded:: Oxygen

:configuration:
    This module uses a configuration profile for one or multiple Storage providers

    .. code-block:: yaml

        libcloud_storage:
            profile_test1:
              driver: google_storage
              key: <KEY>
              secret: mysecret
            profile_test2:
              driver: s3
              key: 12345
              secret: mysecret

Examples
--------

Creating a container and uploading a file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: yaml

    web_things:
      libcloud_storage.container_present:
        name: my_container_name  
        profile: profile1
      libcloud_storage.object_present:
        name: my_file.jpg
        container: my_container_name
        path: /path/to/local/file.jpg
        profile: profile1

Downloading a file
~~~~~~~~~~~~~~~~~~

This example will download the file from the remote cloud and keep it locally

.. code-block:: yaml

    web_things:
      libcloud_storage.file_present:
        name: my_file.jpg
        container: my_container_name
        path: /path/to/local/file.jpg
        profile: profile1

:depends: apache-libcloud
'''

# Import Python Libs
from __future__ import absolute_import
import logging

# Import salt libs
import salt.utils

log = logging.getLogger(__name__)


def __virtual__():
    return True


def __init__(opts):
    salt.utils.compat.pack_dunder(__name__)


def state_result(result, message):
    return {'result': result, 'comment': message}


def container_present(name, profile):
    '''
    Ensures a container is present.

    :param name: Container name
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    containers = __salt__['libcloud_storage.list_containers'](profile)
    
    match = [z for z in containers if z.name == name]
    if len(match) > 0:
        return state_result(True, "Container already exists")
    else:
        result = __salt__['libcloud_storage.create_container'](name, profile)
        return state_result(result, "Created new container")


def container_absent(name, profile):
    '''
    Ensures a container is absent.

    :param name: Container name
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    containers = __salt__['libcloud_storage.list_containers'](profile)
    
    match = [z for z in containers if z.name == name]
    if len(match) == 0:
        return state_result(True, "Container already absent")
    else:
        result = __salt__['libcloud_storage.delete_container'](name, profile)
        return state_result(result, "Deleted container")


def object_present(container, name, path, profile):
    '''
    Ensures a object is presnt.

    :param container: Container name
    :type  container: ``str``

    :param name: Object name in cloud
    :type  name: ``str``

    :param path: Local path to file
    :type  path: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    existing_object = __salt__['libcloud_storage.get_container_object'](container, name, profile)
    
    if existing_object is not None:
        return state_result(True, "Object already present")
    else:
        result = __salt__['libcloud_storage.upload_object'](path, container, name, profile
        return state_result(result, "Uploaded object")


def object_absent(container, name, profile):
    '''
    Ensures a object is absent.

    :param container: Container name
    :type  container: ``str``

    :param name: Object name in cloud
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    existing_object = __salt__['libcloud_storage.get_container_object'](container, name, profile)
    
    if existing_object is None:
        return state_result(True, "Object already absent")
    else:
        result = __salt__['libcloud_storage.delete_object'](container, name, profile
        return state_result(result, "Deleted object")


def file_present(container, name, path, profile, overwrite_existing=False):
    '''
    Ensures a object is downloaded locally.

    :param container: Container name
    :type  container: ``str``

    :param name: Object name in cloud
    :type  name: ``str``

    :param path: Local path to file
    :type  path: ``str``

    :param profile: The profile key
    :type  profile: ``str``

    :param overwrite_existing: Replace if already exists<commit_msg>added missing brackets<commit_after># -*- coding: utf-8 -*-
'''
Apache Libcloud Storage State
=============================

Manage cloud storage using libcloud

    :codeauthor: :email:`<NAME> <<EMAIL>>`

Apache Libcloud Storage (object/blob) management for a full list
of supported clouds, see http://libcloud.readthedocs.io/en/latest/storage/supported_providers.html

Clouds include Amazon S3, Google Storage, Aliyun, Azure Blobs, Ceph, OpenStack swift

.. versionadded:: Oxygen

:configuration:
    This module uses a configuration profile for one or multiple Storage providers

    .. code-block:: yaml

        libcloud_storage:
            profile_test1:
              driver: google_storage
              key: <KEY>
              secret: mysecret
            profile_test2:
              driver: s3
              key: 12345
              secret: mysecret

Examples
--------

Creating a container and uploading a file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: yaml

    web_things:
      libcloud_storage.container_present:
        name: my_container_name  
        profile: profile1
      libcloud_storage.object_present:
        name: my_file.jpg
        container: my_container_name
        path: /path/to/local/file.jpg
        profile: profile1

Downloading a file
~~~~~~~~~~~~~~~~~~

This example will download the file from the remote cloud and keep it locally

.. code-block:: yaml

    web_things:
      libcloud_storage.file_present:
        name: my_file.jpg
        container: my_container_name
        path: /path/to/local/file.jpg
        profile: profile1

:depends: apache-libcloud
'''

# Import Python Libs
from __future__ import absolute_import
import logging

# Import salt libs
import salt.utils

log = logging.getLogger(__name__)


def __virtual__():
    return True


def __init__(opts):
    salt.utils.compat.pack_dunder(__name__)


def state_result(result, message):
    return {'result': result, 'comment': message}


def container_present(name, profile):
    '''
    Ensures a container is present.

    :param name: Container name
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    containers = __salt__['libcloud_storage.list_containers'](profile)
    
    match = [z for z in containers if z.name == name]
    if len(match) > 0:
        return state_result(True, "Container already exists")
    else:
        result = __salt__['libcloud_storage.create_container'](name, profile)
        return state_result(result, "Created new container")


def container_absent(name, profile):
    '''
    Ensures a container is absent.

    :param name: Container name
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    containers = __salt__['libcloud_storage.list_containers'](profile)
    
    match = [z for z in containers if z.name == name]
    if len(match) == 0:
        return state_result(True, "Container already absent")
    else:
        result = __salt__['libcloud_storage.delete_container'](name, profile)
        return state_result(result, "Deleted container")


def object_present(container, name, path, profile):
    '''
    Ensures a object is presnt.

    :param container: Container name
    :type  container: ``str``

    :param name: Object name in cloud
    :type  name: ``str``

    :param path: Local path to file
    :type  path: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    existing_object = __salt__['libcloud_storage.get_container_object'](container, name, profile)
    
    if existing_object is not None:
        return state_result(True, "Object already present")
    else:
        result = __salt__['libcloud_storage.upload_object'](path, container, name, profile)
        return state_result(result, "Uploaded object")


def object_absent(container, name, profile):
    '''
    Ensures a object is absent.

    :param container: Container name
    :type  container: ``str``

    :param name: Object name in cloud
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    existing_object = __salt__['libcloud_storage.get_container_object'](container, name, profile)
    
    if existing_object is None:
        return state_result(True, "Object already absent")
    else:
        result = __salt__['libcloud_storage.delete_object'](container, name, profile)
        return state_result(result, "Deleted object")


def file_present(container, name, path, profile, overwrite_existing=False):
    '''
    Ensures a object is downloaded locally.

    :param container: Container name
    :type  container: ``str``

    :param name: Object name in cloud
    :type  name: ``str``

    :param path: Local path to file
    :type  path: ``str``

    :param profile: The profile key
    :type  profile: ``str``

    :param overwrite_existing: Replace if already exists
{'fraction_non_alphanumeric': 0.11462893209813703, 'fraction_numerical': 0.0030540568054565815, 'mean_word_length': 3.0611823067383215, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1662561', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2766, 'n_words': 869}