<commit_before># Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    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.

"""Quotas for volumes."""


import datetime

from oslo.config import cfg

from cinder import context
from cinder import db
from cinder import exception
from cinder.openstack.common import importutils
from cinder.openstack.common import log as logging
from cinder.openstack.common import timeutils


LOG = logging.getLogger(__name__)

quota_opts = [
    cfg.IntOpt('quota_volumes',
               default=10,
               help='number of volumes allowed per project'),
    cfg.IntOpt('quota_snapshots',
               default=10,
               help='number of volume snapshots allowed per project'),
    cfg.IntOpt('quota_gigabytes',
               default=1000,
               help='number of volume gigabytes (snapshots are also included) '
                    'allowed per project'),
    cfg.IntOpt('reservation_expire',
               default=86400,
               help='number of seconds until a reservation expires'),
    cfg.IntOpt('until_refresh',
               default=0,
               help='count of reservations until usage is refreshed'),
    cfg.IntOpt('max_age',
               default=0,
               help='number of seconds between subsequent usage refreshes'),
    cfg.StrOpt('quota_driver',
               default='cinder.quota.DbQuotaDriver',
               help='default driver to use for quota checks'),
    cfg.BoolOpt('use_default_quota_class',
                default=True,
                help='whether to use default quota class for default quota'), ]

CONF = cfg.CONF
CONF.register_opts(quota_opts)


class DbQuotaDriver(object):

    """Driver to perform check to enforcement of quotas.

    Also allows to obtain quota information.
    The default driver utilizes the local database.
    """

    def get_by_project(self, context, project_id, resource_name):
        """Get a specific quota by project."""

        return db.quota_get(context, project_id, resource_name)

    def get_by_class(self, context, quota_class, resource_name):
        """Get a specific quota by quota class."""

        return db.quota_class_get(context, quota_class, resource_name)

    def get_default(self, context, resource):
        """Get a specific default quota for a resource."""

        default_quotas = db.quota_class_get_default(context)
        return default_quotas.get(resource.name, resource.default)

    def get_defaults(self, context, resources):
        """Given a list of resources, retrieve the default quotas.

        Use the class quotas named `_DEFAULT_QUOTA_NAME` as default quotas,
        if it exists.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        """

        quotas = {}
        default_quotas = {}
        if CONF.use_default_quota_class:
            default_quotas = db.quota_class_get_default(context)
        for resource in resources.values():
            if resource.name not in default_quotas:
                LOG.deprecated(_("Default quota for resource: %(res)s is set "
                                 "by the default quota flag: quota_%(res)s, "
                                 "it is now deprecated. Please use the "
                                 "the default quota class for default "
                                 "quota.") % {'res': resource.name})
            quotas[resource.name] = default_quotas.get(resource.name,
                                                       resource.default)

        return quotas

    def get_class_quotas(self, context, resources, quota_class,
                         defaults=True):
        """Given list of resources, retrieve the quotas for given quota class.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        :param quota_class: The name of the quota class to return
                            quotas for.
        :param defaults: If True, the default value will be reported
                         if there is no specific value for the
                         resource.
        """

        quotas = {}
        default_quotas = {}
        class_quotas = db.quota_class_get_all_by_name(context, quota_class)
        if defaults:
            default_quotas = db.quota_class_get_default(context)
        for resource in resources.values():
            if resource.name in class_quotas:
                quotas[resource.name] = class_quotas[resource.name]<commit_msg>Fix double "the" in Cinder quota warning<commit_after># Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    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.

"""Quotas for volumes."""


import datetime

from oslo.config import cfg

from cinder import context
from cinder import db
from cinder import exception
from cinder.openstack.common import importutils
from cinder.openstack.common import log as logging
from cinder.openstack.common import timeutils


LOG = logging.getLogger(__name__)

quota_opts = [
    cfg.IntOpt('quota_volumes',
               default=10,
               help='number of volumes allowed per project'),
    cfg.IntOpt('quota_snapshots',
               default=10,
               help='number of volume snapshots allowed per project'),
    cfg.IntOpt('quota_gigabytes',
               default=1000,
               help='number of volume gigabytes (snapshots are also included) '
                    'allowed per project'),
    cfg.IntOpt('reservation_expire',
               default=86400,
               help='number of seconds until a reservation expires'),
    cfg.IntOpt('until_refresh',
               default=0,
               help='count of reservations until usage is refreshed'),
    cfg.IntOpt('max_age',
               default=0,
               help='number of seconds between subsequent usage refreshes'),
    cfg.StrOpt('quota_driver',
               default='cinder.quota.DbQuotaDriver',
               help='default driver to use for quota checks'),
    cfg.BoolOpt('use_default_quota_class',
                default=True,
                help='whether to use default quota class for default quota'), ]

CONF = cfg.CONF
CONF.register_opts(quota_opts)


class DbQuotaDriver(object):

    """Driver to perform check to enforcement of quotas.

    Also allows to obtain quota information.
    The default driver utilizes the local database.
    """

    def get_by_project(self, context, project_id, resource_name):
        """Get a specific quota by project."""

        return db.quota_get(context, project_id, resource_name)

    def get_by_class(self, context, quota_class, resource_name):
        """Get a specific quota by quota class."""

        return db.quota_class_get(context, quota_class, resource_name)

    def get_default(self, context, resource):
        """Get a specific default quota for a resource."""

        default_quotas = db.quota_class_get_default(context)
        return default_quotas.get(resource.name, resource.default)

    def get_defaults(self, context, resources):
        """Given a list of resources, retrieve the default quotas.

        Use the class quotas named `_DEFAULT_QUOTA_NAME` as default quotas,
        if it exists.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        """

        quotas = {}
        default_quotas = {}
        if CONF.use_default_quota_class:
            default_quotas = db.quota_class_get_default(context)
        for resource in resources.values():
            if resource.name not in default_quotas:
                LOG.deprecated(_("Default quota for resource: %(res)s is set "
                                 "by the default quota flag: quota_%(res)s, "
                                 "it is now deprecated. Please use the "
                                 "default quota class for default "
                                 "quota.") % {'res': resource.name})
            quotas[resource.name] = default_quotas.get(resource.name,
                                                       resource.default)

        return quotas

    def get_class_quotas(self, context, resources, quota_class,
                         defaults=True):
        """Given list of resources, retrieve the quotas for given quota class.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        :param quota_class: The name of the quota class to return
                            quotas for.
        :param defaults: If True, the default value will be reported
                         if there is no specific value for the
                         resource.
        """

        quotas = {}
        default_quotas = {}
        class_quotas = db.quota_class_get_all_by_name(context, quota_class)
        if defaults:
            default_quotas = db.quota_class_get_default(context)
        for resource in resources.values():
            if resource.name in class_quotas:
                quotas[resource.name] = class_quotas[resource.name]
{'fraction_non_alphanumeric': 0.07452821151451289, 'fraction_numerical': 0.004406552351757831, 'mean_word_length': 2.2996207332490517, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4689920', 'n_tokens_mistral': 2709, 'n_tokens_neox': 2573, 'n_words': 926}