Esempio n. 1
0
 def __init__(self, expected, value):
     super(KeyTypeError, self).__init__(
         _('Key %(key)s must be of type %(expected)s not %(actual)s') % {
             'key': repr(value),
             'expected': expected.__name__,
             'actual': value.__class__.__name__,
         })
Esempio n. 2
0
 def coerce(self, obj, attr, value):
     if not isinstance(value, list):
         raise ValueError(_('A list is required here'))
     for index, element in enumerate(list(value)):
         value[index] = self._element_type.coerce(obj,
                                                  '%s[%i]' % (attr, index),
                                                  element)
     return value
Esempio n. 3
0
 def coerce(self, obj, attr, value):
     try:
         obj_name = value.obj_name()
     except AttributeError:
         obj_name = ""
     if obj_name != self._obj_name:
         raise ValueError(
             _('An object of type %s is required here') % self._obj_name)
     return value
Esempio n. 4
0
 def __init__(self, expected, key, value):
     super(ElementTypeError, self).__init__(
         _('Element %(key)s:%(val)s must be of type %(expected)s'
           ' not %(actual)s') % {
               'key': key,
               'val': repr(value),
               'expected': expected,
               'actual': value.__class__.__name__,
           })
Esempio n. 5
0
 def _null(self, obj, attr):
     if self.nullable:
         return None
     elif self._default != UnspecifiedDefault:
         # NOTE(danms): We coerce the default value each time the field
         # is set to None as our contract states that we'll let the type
         # examine the object and attribute name at that time.
         return self._type.coerce(obj, attr, self._default)
     else:
         raise ValueError(_("Field `%s' cannot be None") % attr)
Esempio n. 6
0
    def obj_attr_is_set(self, attrname):
        """Test object to see if attrname is present.

        Returns True if the named attribute has a value set, or
        False if not. Raises AttributeError if attrname is not
        a valid attribute for this object.
        """
        if attrname not in self.obj_fields:
            raise AttributeError(
                _("%(objname)s object has no attribute '%(attrname)s'") % {
                    'objname': self.obj_name(),
                    'attrname': attrname
                })
        return hasattr(self, get_attrname(attrname))
Esempio n. 7
0
 def coerce(self, obj, attr, value):
     if not isinstance(value, dict):
         raise ValueError(_('A dict is required here'))
     for key, element in value.items():
         if not isinstance(key, six.string_types):
             # NOTE(guohliu) In order to keep compatibility with python3
             # we need to use six.string_types rather than basestring here,
             # since six.string_types is a tuple, so we need to pass the
             # real type in.
             raise KeyTypeError(six.string_types[0], key)
         value[key] = self._element_type.coerce(obj,
                                                '%s["%s"]' % (attr, key),
                                                element)
     return value
Esempio n. 8
0
 def obj_load_attr(self, attrname):
     """Load an additional attribute from the real object."""
     raise NotImplementedError(
         _("Cannot load '%s' in the base class") % attrname)
Esempio n. 9
0
class deprecated(object):
    """A decorator to mark callables as deprecated.

    This decorator logs a deprecation message when the callable it decorates is
    used. The message will include the release where the callable was
    deprecated, the release where it may be removed and possibly an optional
    replacement.

    Examples:

    1. Specifying the required deprecated release

    >>> @deprecated(as_of=deprecated.ICEHOUSE)
    ... def a(): pass

    2. Specifying a replacement:

    >>> @deprecated(as_of=deprecated.ICEHOUSE, in_favor_of='f()')
    ... def b(): pass

    3. Specifying the release where the functionality may be removed:

    >>> @deprecated(as_of=deprecated.ICEHOUSE, remove_in=+1)
    ... def c(): pass

    4. Specifying the deprecated functionality will not be removed:
    >>> @deprecated(as_of=deprecated.ICEHOUSE, remove_in=0)
    ... def d(): pass

    5. Specifying a replacement, deprecated functionality will not be removed:
    >>> @deprecated(as_of=deprecated.ICEHOUSE, in_favor_of='f()', remove_in=0)
    ... def e(): pass

    """

    # NOTE(morganfainberg): Bexar is used for unit test purposes, it is
    # expected we maintain a gap between Bexar and Folsom in this list.
    BEXAR = 'B'
    FOLSOM = 'F'
    GRIZZLY = 'G'
    HAVANA = 'H'
    ICEHOUSE = 'I'
    JUNO = 'J'
    KILO = 'K'

    _RELEASES = {
        # NOTE(morganfainberg): Bexar is used for unit test purposes, it is
        # expected we maintain a gap between Bexar and Folsom in this list.
        'B': 'Bexar',
        'F': 'Folsom',
        'G': 'Grizzly',
        'H': 'Havana',
        'I': 'Icehouse',
        'J': 'Juno',
        'K': 'Kilo',
    }

    _deprecated_msg_with_alternative = _(
        '%(what)s is deprecated as of %(as_of)s in favor of '
        '%(in_favor_of)s and may be removed in %(remove_in)s.')

    _deprecated_msg_no_alternative = _(
        '%(what)s is deprecated as of %(as_of)s and may be '
        'removed in %(remove_in)s. It will not be superseded.')

    _deprecated_msg_with_alternative_no_removal = _(
        '%(what)s is deprecated as of %(as_of)s in favor of %(in_favor_of)s.')

    _deprecated_msg_with_no_alternative_no_removal = _(
        '%(what)s is deprecated as of %(as_of)s. It will not be superseded.')

    def __init__(self, as_of, in_favor_of=None, remove_in=2, what=None):
        """Initialize decorator

        :param as_of: the release deprecating the callable. Constants
            are define in this class for convenience.
        :param in_favor_of: the replacement for the callable (optional)
        :param remove_in: an integer specifying how many releases to wait
            before removing (default: 2)
        :param what: name of the thing being deprecated (default: the
            callable's name)

        """
        self.as_of = as_of
        self.in_favor_of = in_favor_of
        self.remove_in = remove_in
        self.what = what

    def __call__(self, func_or_cls):
        if not self.what:
            self.what = func_or_cls.__name__ + '()'
        msg, details = self._build_message()

        if inspect.isfunction(func_or_cls):

            @six.wraps(func_or_cls)
            def wrapped(*args, **kwargs):
                LOG.deprecated(msg, details)
                return func_or_cls(*args, **kwargs)

            return wrapped
        elif inspect.isclass(func_or_cls):
            orig_init = func_or_cls.__init__

            # TODO(tsufiev): change `functools` module to `six` as
            # soon as six 1.7.4 (with fix for passing `assigned`
            # argument to underlying `functools.wraps`) is released
            # and added to the oslo-incubator requrements
            @functools.wraps(orig_init, assigned=('__name__', '__doc__'))
            def new_init(self, *args, **kwargs):
                LOG.deprecated(msg, details)
                orig_init(self, *args, **kwargs)

            func_or_cls.__init__ = new_init
            return func_or_cls
        else:
            raise TypeError('deprecated can be used only with functions or '
                            'classes')

    def _get_safe_to_remove_release(self, release):
        # TODO(dstanek): this method will have to be reimplemented once
        #    when we get to the X release because once we get to the Y
        #    release, what is Y+2?
        new_release = chr(ord(release) + self.remove_in)
        if new_release in self._RELEASES:
            return self._RELEASES[new_release]
        else:
            return new_release

    def _build_message(self):
        details = dict(what=self.what,
                       as_of=self._RELEASES[self.as_of],
                       remove_in=self._get_safe_to_remove_release(self.as_of))

        if self.in_favor_of:
            details['in_favor_of'] = self.in_favor_of
            if self.remove_in > 0:
                msg = self._deprecated_msg_with_alternative
            else:
                # There are no plans to remove this function, but it is
                # now deprecated.
                msg = self._deprecated_msg_with_alternative_no_removal
        else:
            if self.remove_in > 0:
                msg = self._deprecated_msg_no_alternative
            else:
                # There are no plans to remove this function, but it is
                # now deprecated.
                msg = self._deprecated_msg_with_no_alternative_no_removal
        return msg, details