<commit_before># Copyright 2020 Nexenta by DDN, Inc. 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 hashlib
import json
import posixpath

from eventlet import greenthread
from oslo_log import log as logging
from pkg_resources import parse_version
import requests
import six

from cinder import exception
from cinder.i18n import _

LOG = logging.getLogger(__name__)

ASYNC_WAIT = 0.25
HTTPS_PORT = 8443
HTTP_PORT = 8080
HTTPS = 'https'
HTTP = 'http'
AUTO = 'auto'
NFS = 'nfs'
ISCSI = 'iscsi'


class NefException(exception.VolumeDriverException):
    def __init__(self, data=None, **kwargs):
        defaults = {
            'name': 'NexentaError',
            'code': 'EBADMSG',
            'source': 'CinderDriver',
            'message': 'Unknown error'
        }
        if isinstance(data, dict):
            for key in defaults:
                if key in kwargs:
                    continue
                if key in data:
                    kwargs[key] = data[key]
                else:
                    kwargs[key] = defaults[key]
        elif isinstance(data, six.string_types):
            if 'message' not in kwargs:
                kwargs['message'] = data
        for key in defaults:
            if key not in kwargs:
                kwargs[key] = defaults[key]
        message = ('%(message)s (source: %(source)s, '
                   'name: %(name)s, code: %(code)s)') % kwargs
        self.code = kwargs['code']
        del kwargs['message']
        super(NefException, self).__init__(message, **kwargs)


class NefRequest(object):
    def __init__(self, proxy, method):
        self.proxy = proxy
        self.method = method
        self.attempts = proxy.retries + 1
        self.payload = None
        self.error = None
        self.path = None
        self.time = 0
        self.wait = 0
        self.data = []
        self.stat = {}
        self.hooks = {
            'response': self.hook
        }
        self.kwargs = {
            'hooks': self.hooks,
            'timeout': self.proxy.timeout
        }

    def __call__(self, path, payload=None):
        info = '%(method)s %(url)s %(payload)s' % {
            'method': self.method,
            'url': self.proxy.url(path),
            'payload': payload
        }
        LOG.debug('Start request: %(info)s', {'info': info})
        self.path = path
        self.payload = payload
        for attempt in range(self.attempts):
            self.data = []
            if self.error:
                self.delay(attempt)
                if not self.find_host():
                    continue
                LOG.debug('Retry request: %(info)s '
                          '[%(attempt)s/%(attempts)s], '
                          'reason: %(error)s',
                          {'info': info, 'attempt': attempt,
                           'attempts': self.attempts,
                           'error': self.error})
            try:
                response = self.request(self.method, self.path, self.payload)
            except Exception as error:
                if isinstance(error, NefException):
                    self.error = error
                else:
                    message = six.text_type(error)
                    self.error = NefException(message=message)
                continue
            count = sum(self.stat.values())
            LOG.debug('Finish request: %(info)s, '
                      'total response time: %(time)s seconds, '
                      'total wait time: %(wait)s seconds, '
                      'total requests count: %(count)s, '
                      'requests statistics: %(stat)s, '
                      'response content: %(content)s',
                      {'info': info, 'time': self.time,
                       'wait': self.wait, 'count': count,
                       'stat': self.stat,
                       'content': response.content})
            content = None
            if response.content:
                content = json.loads(response.content)
            if not response.ok:
                LOG.error('Failed request: %(info)s, '
                          'response content: %(content)s',
                          {'info': info, 'content': content})
                raise NefException(content)
            if (response.status_code == requests.codes.created and
                    'location' in response.headers and not content):
                location = response.headers['location']
                name = posixpath.basename(location)
                data = six.moves.urllib.parse.unquote_plus(name)
                return data
            if isinstance(content, dict) and 'data' in content:
                return self.data
            return content
        LOG.error('Failed request: %(info)s, '
                  'reached maximum retry attempts: '
                  '%(attempts)s, reason: %(error)s',
                  {'info': info, 'attempts': self.attempts,
                   'error': self.error})
        raise self.error

    def request(self, method, path, payload):
        if self.method not in ['get', 'delete', 'put', 'post']:<commit_msg>Fixed error messages<commit_after># Copyright 2020 Nexenta by DDN, Inc. 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 hashlib
import json
import posixpath

from eventlet import greenthread
from oslo_log import log as logging
from pkg_resources import parse_version
import requests
import six

from cinder import exception
from cinder.i18n import _

LOG = logging.getLogger(__name__)

ASYNC_WAIT = 0.25
HTTPS_PORT = 8443
HTTP_PORT = 8080
HTTPS = 'https'
HTTP = 'http'
AUTO = 'auto'
NFS = 'nfs'
ISCSI = 'iscsi'


class NefException(exception.VolumeDriverException):
    def __init__(self, data=None, **kwargs):
        defaults = {
            'name': 'NexentaError',
            'code': 'EBADMSG',
            'source': 'CinderDriver',
            'message': 'Unknown error'
        }
        if isinstance(data, dict):
            for key in defaults:
                if key in kwargs:
                    continue
                if key in data:
                    kwargs[key] = data[key]
                else:
                    kwargs[key] = defaults[key]
        elif isinstance(data, six.string_types):
            if 'message' not in kwargs:
                kwargs['message'] = data
        for key in defaults:
            if key not in kwargs:
                kwargs[key] = defaults[key]
        message = ('%(message)s (source: %(source)s, '
                   'name: %(name)s, code: %(code)s)') % kwargs
        self.code = kwargs['code']
        del kwargs['message']
        super(NefException, self).__init__(message, **kwargs)


class NefRequest(object):
    def __init__(self, proxy, method):
        self.proxy = proxy
        self.method = method
        self.attempts = proxy.retries + 1
        self.payload = None
        self.error = None
        self.path = None
        self.time = 0
        self.wait = 0
        self.data = []
        self.stat = {}
        self.hooks = {
            'response': self.hook
        }
        self.kwargs = {
            'hooks': self.hooks,
            'timeout': self.proxy.timeout
        }

    def __call__(self, path, payload=None):
        info = '%(method)s %(url)s %(payload)s' % {
            'method': self.method,
            'url': self.proxy.url(path),
            'payload': payload
        }
        LOG.debug('Start request: %(info)s', {'info': info})
        self.path = path
        self.payload = payload
        for attempt in range(self.attempts):
            if self.error:
                self.delay(attempt)
                if not self.find_host():
                    continue
                LOG.debug('Retry request %(info)s after %(attempt)s '
                          'failed attempts, maximum retry attempts '
                          '%(attempts)s, reason: %(error)s',
                          {'info': info, 'attempt': attempt,
                           'attempts': self.attempts,
                           'error': self.error})
            self.data = []
            try:
                response = self.request(self.method, self.path, self.payload)
            except Exception as error:
                if isinstance(error, NefException):
                    self.error = error
                else:
                    code = 'EAGAIN'
                    message = six.text_type(error)
                    self.error = NefException(code=code, message=message)
                LOG.error('Failed request %(info)s: %(error)s',
                          {'info': info, 'error': self.error})
                continue
            count = sum(self.stat.values())
            LOG.debug('Finish request %(info)s, '
                      'response time: %(time)s seconds, '
                      'wait time: %(wait)s seconds, '
                      'requests count: %(count)s, '
                      'requests statistics: %(stat)s, '
                      'response content: %(content)s',
                      {'info': info, 'time': self.time,
                       'wait': self.wait, 'count': count,
                       'stat': self.stat,
                       'content': response.content})
            content = None
            if response.content:
                content = json.loads(response.content)
            if not response.ok:
                LOG.error('Failed request %(info)s, '
                          'response content: %(content)s',
                          {'info': info, 'content': content})
                raise NefException(content)
            if (response.status_code == requests.codes.created and
                    'location' in response.headers and not content):
                location = response.headers['location']
                name = posixpath.basename(location)
                data = six.moves.urllib.parse.unquote_plus(name)
                return data
            if isinstance(content, dict) and 'data' in content:
                return self.data
            return content
        LOG.error('Failed request %(info)s, '
                  'reached maximum retry attempts: '
                  '%(attempts)s, reason: %(error)s',
                  {'info': info, 'attempts': self.attempts,
                   'error': self.error})
        raise self.error

    def request(self, method, path, payload):
        if self.method not in ['get', 'delete', 'put', 'post']:
{'fraction_non_alphanumeric': 0.1072637334027597, 'fraction_numerical': 0.004165581879718824, 'mean_word_length': 1.604882459312839, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '963', 'n_tokens_mistral': 3126, 'n_tokens_neox': 3003, 'n_words': 878}