<commit_before># -*- coding: utf-8 -*-

# Copyright 2014,  Digital Reasoning
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import logging

import yaml
from rest_framework import generics, status
from rest_framework.compat import OrderedDict
from rest_framework.filters import DjangoFilterBackend, DjangoObjectPermissionsFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.views import APIView

from stackdio.api.blueprints.serializers import BlueprintSerializer
from stackdio.api.formulas.models import FormulaVersion
from stackdio.api.formulas.serializers import FormulaVersionSerializer
from stackdio.core.exceptions import BadRequest, ResourceConflict
from stackdio.core.permissions import StackdioModelPermissions, StackdioObjectPermissions
from stackdio.core.viewsets import (
    StackdioModelUserPermissionsViewSet,
    StackdioModelGroupPermissionsViewSet,
    StackdioObjectUserPermissionsViewSet,
    StackdioObjectGroupPermissionsViewSet,
)
from . import filters, mixins, models, serializers, permissions

logger = logging.getLogger(__name__)


class CloudRootView(APIView):
    """
    Root of the cloud API. Below are all of the cloud API endpoints that
    are currently accessible. Each API will have its own documentation
    and particular parameters that may discoverable by browsing directly
    to them.
    """
    permission_classes = (IsAuthenticated,)

    def get(self, request, format=None):
        api = OrderedDict((
            ('providers', reverse('cloudprovider-list',
                                  request=request,
                                  format=format)),
            ('accounts', reverse('cloudaccount-list',
                                 request=request,
                                 format=format)),
            ('profiles', reverse('cloudprofile-list',
                                 request=request,
                                 format=format)),
            ('snapshots', reverse('snapshot-list',
                                  request=request,
                                  format=format)),
            ('security_groups', reverse('securitygroup-list',
                                        request=request,
                                        format=format)),
        ))

        return Response(api)


class CloudProviderListAPIView(generics.ListAPIView):
    queryset = models.CloudProvider.objects.all()
    serializer_class = serializers.CloudProviderSerializer
    permission_classes = (StackdioModelPermissions,)
    filter_backends = (DjangoObjectPermissionsFilter, DjangoFilterBackend)
    lookup_field = 'name'


class CloudProviderDetailAPIView(generics.RetrieveAPIView):
    queryset = models.CloudProvider.objects.all()
    serializer_class = serializers.CloudProviderSerializer
    permission_classes = (StackdioObjectPermissions,)
    lookup_field = 'name'


class CloudProviderObjectUserPermissionsViewSet(mixins.CloudProviderRelatedMixin,
                                                    StackdioObjectUserPermissionsViewSet):
    permission_classes = (permissions.CloudProviderPermissionsObjectPermissions,)
    parent_lookup_field = 'name'


class CloudProviderObjectGroupPermissionsViewSet(mixins.CloudProviderRelatedMixin,
                                                     StackdioObjectGroupPermissionsViewSet):
    permission_classes = (permissions.CloudProviderPermissionsObjectPermissions,)
    parent_lookup_field = 'name'


class CloudAccountListAPIView(generics.ListCreateAPIView):
    queryset = models.CloudAccount.objects.all()
    serializer_class = serializers.CloudAccountSerializer
    permission_classes = (StackdioModelPermissions,)
    filter_backends = (DjangoObjectPermissionsFilter, DjangoFilterBackend)
    filter_class = filters.CloudAccountFilter

    def perform_create(self, serializer):

        data = self.request.DATA
        files = self.request.FILES

        # Lookup provider type
        try:
            obj = serializer.save()

            logger.debug(obj.slug)

            driver = obj.get_driver()

            # Leverage the driver to generate its required data that
            # will be serialized down to yaml and stored in both the database
            # and the salt cloud providers file
            provider_data = driver.get_provider_data(data, files)

            # Generate the yaml and store in the database
            yaml_data = {
                obj.slug: provider_data
            }
            obj.yaml = yaml.safe_dump(yaml_data, default_flow_style=False)
            obj.save()

            # Update the salt cloud providers file
            obj.update_config()

        except models.CloudProvider.DoesNotExist:
            err_msg = 'Given provider type does not exist.'
            logger.exception(err_msg)
            raise BadRequest(err_msg)<commit_msg>Forgot to remove a logging statement<commit_after># -*- coding: utf-8 -*-

# Copyright 2014,  Digital Reasoning
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import logging

import yaml
from rest_framework import generics, status
from rest_framework.compat import OrderedDict
from rest_framework.filters import DjangoFilterBackend, DjangoObjectPermissionsFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.views import APIView

from stackdio.api.blueprints.serializers import BlueprintSerializer
from stackdio.api.formulas.models import FormulaVersion
from stackdio.api.formulas.serializers import FormulaVersionSerializer
from stackdio.core.exceptions import BadRequest, ResourceConflict
from stackdio.core.permissions import StackdioModelPermissions, StackdioObjectPermissions
from stackdio.core.viewsets import (
    StackdioModelUserPermissionsViewSet,
    StackdioModelGroupPermissionsViewSet,
    StackdioObjectUserPermissionsViewSet,
    StackdioObjectGroupPermissionsViewSet,
)
from . import filters, mixins, models, serializers, permissions

logger = logging.getLogger(__name__)


class CloudRootView(APIView):
    """
    Root of the cloud API. Below are all of the cloud API endpoints that
    are currently accessible. Each API will have its own documentation
    and particular parameters that may discoverable by browsing directly
    to them.
    """
    permission_classes = (IsAuthenticated,)

    def get(self, request, format=None):
        api = OrderedDict((
            ('providers', reverse('cloudprovider-list',
                                  request=request,
                                  format=format)),
            ('accounts', reverse('cloudaccount-list',
                                 request=request,
                                 format=format)),
            ('profiles', reverse('cloudprofile-list',
                                 request=request,
                                 format=format)),
            ('snapshots', reverse('snapshot-list',
                                  request=request,
                                  format=format)),
            ('security_groups', reverse('securitygroup-list',
                                        request=request,
                                        format=format)),
        ))

        return Response(api)


class CloudProviderListAPIView(generics.ListAPIView):
    queryset = models.CloudProvider.objects.all()
    serializer_class = serializers.CloudProviderSerializer
    permission_classes = (StackdioModelPermissions,)
    filter_backends = (DjangoObjectPermissionsFilter, DjangoFilterBackend)
    lookup_field = 'name'


class CloudProviderDetailAPIView(generics.RetrieveAPIView):
    queryset = models.CloudProvider.objects.all()
    serializer_class = serializers.CloudProviderSerializer
    permission_classes = (StackdioObjectPermissions,)
    lookup_field = 'name'


class CloudProviderObjectUserPermissionsViewSet(mixins.CloudProviderRelatedMixin,
                                                    StackdioObjectUserPermissionsViewSet):
    permission_classes = (permissions.CloudProviderPermissionsObjectPermissions,)
    parent_lookup_field = 'name'


class CloudProviderObjectGroupPermissionsViewSet(mixins.CloudProviderRelatedMixin,
                                                     StackdioObjectGroupPermissionsViewSet):
    permission_classes = (permissions.CloudProviderPermissionsObjectPermissions,)
    parent_lookup_field = 'name'


class CloudAccountListAPIView(generics.ListCreateAPIView):
    queryset = models.CloudAccount.objects.all()
    serializer_class = serializers.CloudAccountSerializer
    permission_classes = (StackdioModelPermissions,)
    filter_backends = (DjangoObjectPermissionsFilter, DjangoFilterBackend)
    filter_class = filters.CloudAccountFilter

    def perform_create(self, serializer):

        data = self.request.DATA
        files = self.request.FILES

        # Lookup provider type
        try:
            obj = serializer.save()

            driver = obj.get_driver()

            # Leverage the driver to generate its required data that
            # will be serialized down to yaml and stored in both the database
            # and the salt cloud providers file
            provider_data = driver.get_provider_data(data, files)

            # Generate the yaml and store in the database
            yaml_data = {
                obj.slug: provider_data
            }
            obj.yaml = yaml.safe_dump(yaml_data, default_flow_style=False)
            obj.save()

            # Update the salt cloud providers file
            obj.update_config()

        except models.CloudProvider.DoesNotExist:
            err_msg = 'Given provider type does not exist.'
            logger.exception(err_msg)
            raise BadRequest(err_msg)
{'fraction_non_alphanumeric': 0.06376918129192319, 'fraction_numerical': 0.0016539557107415235, 'mean_word_length': 2.7582872928176796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4791777', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2552, 'n_words': 711}