Example #1
0
def add_error_class(klass, code):
    """
    Maps an exception class to a string code. Used to map remoting C{onStatus}
    objects to an exception class so that an exception can be built to
    represent that error::

        class AuthenticationError(Exception):
            pass

    An example: C{add_error_class(AuthenticationError, 'Auth.Failed')}

    @param klass:
    @type klass:
    @param code:
    @type code: C{str}

    @raise TypeError: C{klass} must be a C{class} type.
    @raise TypeError: Error classes must subclass the C{__builtin__.Exception} class.
    @raise ValueError: Code is already registered.
    """
    if not isinstance(code, basestring):
        code = str(code)

    if not isinstance(klass, (type, types.ClassType)):
        raise TypeError, "klass must be a class type"

    mro = util.get_mro(klass)

    if not Exception in util.get_mro(klass):
        raise TypeError, 'error classes must subclass the __builtin__.Exception class'

    if code in ERROR_CLASS_MAP.keys():
        raise ValueError, 'Code %s is already registered' % code

    ERROR_CLASS_MAP[code] = klass
Example #2
0
def add_error_class(klass, code):
    """
    Maps an exception class to a string code. Used to map remoting C{onStatus}
    objects to an exception class so that an exception can be built to
    represent that error::

        class AuthenticationError(Exception):
            pass

    An example: C{add_error_class(AuthenticationError, 'Auth.Failed')}

    @type code: C{str}

    @raise TypeError: C{klass} must be a C{class} type.
    @raise TypeError: Error classes must subclass the C{__builtin__.Exception} class.
    @raise ValueError: Code is already registered.
    """
    if not isinstance(code, basestring):
        code = str(code)

    if not isinstance(klass, (type, types.ClassType)):
        raise TypeError, "klass must be a class type"

    mro = util.get_mro(klass)

    if not Exception in util.get_mro(klass):
        raise TypeError, 'error classes must subclass the __builtin__.Exception class'

    if code in ERROR_CLASS_MAP.keys():
        raise ValueError, 'Code %s is already registered' % code

    ERROR_CLASS_MAP[code] = klass
Example #3
0
    def _getAttrs(self,
                  obj,
                  static_attrs=None,
                  dynamic_attrs=None,
                  traverse=True):
        if static_attrs is None:
            static_attrs = []

        if dynamic_attrs is None:
            dynamic_attrs = []

        modified_attrs = False

        if self.attrs is not None:
            modified_attrs = True
            static_attrs.extend(self.attrs)
        elif traverse is True and hasattr(obj, '__slots__'):
            modified_attrs = True
            static_attrs.extend(obj.__slots__)

        if self.attr_func is not None:
            modified_attrs = True
            extra_attrs = self.attr_func(obj)

            dynamic_attrs.extend(
                [key for key in extra_attrs if key not in static_attrs])

        if traverse is True:
            for base in util.get_mro(obj.__class__):
                try:
                    alias = get_class_alias(base)
                except UnknownClassAlias:
                    continue

                x, y = alias._getAttrs(obj, static_attrs, dynamic_attrs, False)

                if x is not None:
                    static_attrs.extend(x)
                    modified_attrs = True

                if y is not None:
                    dynamic_attrs.extend(y)
                    modified_attrs = True

        if modified_attrs is False:
            return None, None

        sa = []
        da = []

        for x in static_attrs:
            if x not in sa:
                sa.append(x)

        for x in dynamic_attrs:
            if x not in da:
                da.append(x)

        return (sa, da)
Example #4
0
    def _getAttrs(self, obj, static_attrs=None, dynamic_attrs=None, traverse=True):
        if static_attrs is None:
            static_attrs = []

        if dynamic_attrs is None:
            dynamic_attrs = []

        modified_attrs = False

        if self.attrs is not None:
            modified_attrs = True
            static_attrs.extend(self.attrs)
        elif traverse is True and hasattr(obj, '__slots__'):
            modified_attrs = True
            static_attrs.extend(obj.__slots__)

        if self.attr_func is not None:
            modified_attrs = True
            extra_attrs = self.attr_func(obj)

            dynamic_attrs.extend([key for key in extra_attrs if key not in static_attrs])

        if traverse is True:
            for base in util.get_mro(obj.__class__):
                try:
                    alias = get_class_alias(base)
                except UnknownClassAlias:
                    continue

                x, y = alias._getAttrs(obj, static_attrs, dynamic_attrs, False)

                if x is not None:
                    static_attrs.extend(x)
                    modified_attrs = True

                if y is not None:
                    dynamic_attrs.extend(y)
                    modified_attrs = True

        if modified_attrs is False:
            return None, None

        sa = []
        da = []

        for x in static_attrs:
            if x not in sa:
                sa.append(x)

        for x in dynamic_attrs:
            if x not in da:
                da.append(x)

        return (sa, da)
Example #5
0
    def getAttrs(self, obj, attrs=None, traverse=True):
        if attrs is None:
            attrs = []

        did_something = False

        if self.attrs is not None:
            did_something = True
            attrs += self.attrs

        if 'dynamic' in self.metadata and self.attr_func is not None:
            did_something = True
            extra_attrs = self.attr_func(obj)

            for key in extra_attrs:
                if key not in attrs:
                    attrs.append(key)

        if traverse is True:
            for base in util.get_mro(obj.__class__):
                try:
                    alias = get_class_alias(base)
                except UnknownClassAlias:
                    continue

                x = alias.getAttrs(obj, attrs, False)

                if x is not None:
                    attrs += x
                    did_something = True

        if did_something is False:
            return None

        a = []

        for x in attrs:
            if x not in a:
                a.append(x)

        return a
Example #6
0
    def getAttrs(self, obj, attrs=None, traverse=True):
        if attrs is None:
            attrs = []

        did_something = False

        if self.attrs is not None:
            did_something = True
            attrs += self.attrs

        if 'dynamic' in self.metadata and self.attr_func is not None:
            did_something = True
            extra_attrs = self.attr_func(obj)

            attrs += [key for key in extra_attrs if key not in attrs]

        if traverse is True:
            for base in util.get_mro(obj.__class__):
                try:
                    alias = get_class_alias(base)
                except UnknownClassAlias:
                    continue

                x = alias.getAttrs(obj, attrs, False)

                if x is not None:
                    attrs += x
                    did_something = True

        if did_something is False:
            return None

        a = []

        for x in attrs:
            if x not in a:
                a.append(x)

        return a