<commit_before># Copyright 2017 Datera
# 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.

import functools
import re
import time
import types
import uuid

import dfs_sdk

from oslo_log import log as logging

from cinder import context
from cinder import exception
from cinder.i18n import _
from cinder.image import glance
from cinder.volume import qos_specs
from cinder.volume import volume_types

from glanceclient import exc as glance_exc

LOG = logging.getLogger(__name__)
OS_PREFIX = "OS"
UNMANAGE_PREFIX = "UNMANAGED"

# Taken from this SO post :
# http://stackoverflow.com/a/18516125
# Using old-style string formatting because of the nature of the regex
# conflicting with new-style curly braces
UUID4_STR_RE = ("%s.*([a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab]"
                "[a-f0-9]{3}-?[a-f0-9]{12})")
UUID4_RE = re.compile(UUID4_STR_RE % OS_PREFIX)
SNAP_RE = re.compile(r"\d{10,}\.\d+")

# Recursive dict to assemble basic url structure for the most common
# API URL endpoints. Most others are constructed from these
DEFAULT_SI_SLEEP = 1
DEFAULT_SI_SLEEP_API_2 = 5
DEFAULT_SNAP_SLEEP = 1
API_VERSIONS = ["2.1", "2.2"]
API_TIMEOUT = 20

###############
# METADATA KEYS
###############

M_TYPE = 'cinder_volume_type'
M_CALL = 'cinder_calls'
M_CLONE = 'cinder_clone_from'
M_MANAGED = 'cinder_managed'

M_KEYS = [M_TYPE, M_CALL, M_CLONE, M_MANAGED]


def get_name(resource):
    dn = resource.get('display_name')
    cid = resource.get('id')
    if dn:
        # Check to ensure the name is short enough to fit.  Prioritize
        # the prefix and Cinder ID
        nl = len(OS_PREFIX) + len(dn) + len(cid) + 2
        if nl >= 64:
            dn = dn[:-(nl - 63)]
        return "-".join((OS_PREFIX, dn, cid))
    return "-".join((OS_PREFIX, cid))


def get_unmanaged(name):
    return "-".join((UNMANAGE_PREFIX, name))


def lookup(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        obj = args[0]
        name = "_" + func.func_name + "_" + obj.apiv.replace(".", "_")
        LOG.debug("Trying method: %s", name)
        call_id = uuid.uuid4()
        if obj.do_profile:
            LOG.debug("Profiling method: %s, id %s", name, call_id)
            t1 = time.time()
        obj.thread_local.trace_id = call_id
        result = getattr(obj, name)(*args[1:], **kwargs)
        if obj.do_profile:
            t2 = time.time()
            timedelta = round(t2 - t1, 3)<commit_msg>Bugfix for incorrectly handled unicode characters in display name<commit_after># Copyright 2017 Datera
# 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.

import functools
import re
import time
import types
import string
import uuid

import dfs_sdk

from oslo_log import log as logging

from cinder import context
from cinder import exception
from cinder.i18n import _
from cinder.image import glance
from cinder.volume import qos_specs
from cinder.volume import volume_types

from glanceclient import exc as glance_exc

LOG = logging.getLogger(__name__)
OS_PREFIX = "OS"
UNMANAGE_PREFIX = "UNMANAGED"

# Taken from this SO post :
# http://stackoverflow.com/a/18516125
# Using old-style string formatting because of the nature of the regex
# conflicting with new-style curly braces
UUID4_STR_RE = ("%s.*([a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab]"
                "[a-f0-9]{3}-?[a-f0-9]{12})")
UUID4_RE = re.compile(UUID4_STR_RE % OS_PREFIX)
SNAP_RE = re.compile(r"\d{10,}\.\d+")

# Recursive dict to assemble basic url structure for the most common
# API URL endpoints. Most others are constructed from these
DEFAULT_SI_SLEEP = 1
DEFAULT_SI_SLEEP_API_2 = 5
DEFAULT_SNAP_SLEEP = 1
API_VERSIONS = ["2.1", "2.2"]
API_TIMEOUT = 20

VALID_CHARS = set(string.ascii_letters + string.digits + "-_.")

###############
# METADATA KEYS
###############

M_TYPE = 'cinder_volume_type'
M_CALL = 'cinder_calls'
M_CLONE = 'cinder_clone_from'
M_MANAGED = 'cinder_managed'

M_KEYS = [M_TYPE, M_CALL, M_CLONE, M_MANAGED]


def get_name(resource):
    dn = resource.get('display_name')
    cid = resource.get('id')
    if dn:
        dn = ''.join([c for c in dn if c in VALID_CHARS])
        # Check to ensure the name is short enough to fit.  Prioritize
        # the prefix and Cinder ID, strip all invalid characters
        nl = len(OS_PREFIX) + len(dn) + len(cid) + 2
        if nl >= 64:
            dn = dn[:-(nl - 63)]
        return "-".join((OS_PREFIX, dn, cid))
    return "-".join((OS_PREFIX, cid))


def get_unmanaged(name):
    return "-".join((UNMANAGE_PREFIX, name))


def lookup(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        obj = args[0]
        name = "_" + func.func_name + "_" + obj.apiv.replace(".", "_")
        LOG.debug("Trying method: %s", name)
        call_id = uuid.uuid4()
        if obj.do_profile:
            LOG.debug("Profiling method: %s, id %s", name, call_id)
            t1 = time.time()
        obj.thread_local.trace_id = call_id
        result = getattr(obj, name)(*args[1:], **kwargs)
        if obj.do_profile:
            t2 = time.time()
            timedelta = round(t2 - t1, 3)
{'fraction_non_alphanumeric': 0.12319193889159759, 'fraction_numerical': 0.021127905086949457, 'mean_word_length': 3.2412129565816676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3192257', 'n_tokens_mistral': 2140, 'n_tokens_neox': 2092, 'n_words': 632}