From d7569a84b94ab304a1b7f353ea71c15061ebd5d4 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 31 Jul 2008 18:57:10 +0000 Subject: 31: Renamed exceptions.py to errors.py --- ipalib/base.py | 16 +++--- ipalib/errors.py | 127 ++++++++++++++++++++++++++++++++++++++++++ ipalib/exceptions.py | 127 ------------------------------------------ ipalib/plugable.py | 10 ++-- ipalib/tests/test_base.py | 10 ++-- ipalib/tests/test_plugable.py | 10 ++-- 6 files changed, 150 insertions(+), 150 deletions(-) create mode 100644 ipalib/errors.py delete mode 100644 ipalib/exceptions.py diff --git a/ipalib/base.py b/ipalib/base.py index 63bb940b0..ae9dfae49 100644 --- a/ipalib/base.py +++ b/ipalib/base.py @@ -23,7 +23,7 @@ Base classes for plug-in architecture and generative API. import re import inspect -import exceptions +import errors class NameSpace(object): @@ -45,7 +45,7 @@ class NameSpace(object): For example, setting an attribute the normal way will raise an exception: >>> ns.my_message = 'some new value' - (raises exceptions.SetError) + (raises errors.SetError) But a programmer could still set the attribute like this: @@ -82,7 +82,7 @@ class NameSpace(object): NameSpace has been locked; otherwise calls object.__setattr__(). """ if self.__locked: - raise exceptions.SetError(name) + raise errors.SetError(name) super(NameSpace, self).__setattr__(name, value) def __getitem__(self, key): @@ -190,7 +190,7 @@ class Attribute(Named): return self.__obj def __set_obj(self, obj): if self.__obj is not None: - raise exceptions.TwiceSetError(self.__class__.__name__, 'obj') + raise errors.TwiceSetError(self.__class__.__name__, 'obj') assert isinstance(obj, Object) self.__obj = obj assert self.obj is obj @@ -227,7 +227,7 @@ class Object(Named): return self.__methods def __set_methods(self, methods): if self.__methods is not None: - raise exceptions.TwiceSetError( + raise errors.TwiceSetError( self.__class__.__name__, 'methods' ) assert type(methods) is NameSpace @@ -239,7 +239,7 @@ class Object(Named): return self.__properties def __set_properties(self, properties): if self.__properties is not None: - raise exceptions.TwiceSetError( + raise errors.TwiceSetError( self.__class__.__name__, 'properties' ) assert type(properties) is NameSpace @@ -338,11 +338,11 @@ class Registrar(object): def __findbase(self, cls): if not inspect.isclass(cls): - raise exceptions.RegistrationError('not a class', cls) + raise errors.RegistrationError('not a class', cls) for base in self.__allowed: if issubclass(cls, base): return base - raise exceptions.RegistrationError( + raise errors.RegistrationError( 'not subclass of an allowed base', cls, ) diff --git a/ipalib/errors.py b/ipalib/errors.py new file mode 100644 index 000000000..53a0870e6 --- /dev/null +++ b/ipalib/errors.py @@ -0,0 +1,127 @@ +# Authors: +# Jason Gerard DeRose +# +# Copyright (C) 2008 Red Hat +# see file 'COPYING' for use and warranty inmsgion +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2 only +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" +All custom errors raised by `ipalib` package. +""" + +class IPAError(Exception): + """ + Use this base class for your custom IPA errors unless there is a + specific reason to subclass from AttributeError, KeyError, etc. + """ + msg = None + + def __init__(self, *args, **kw): + self.args = args + self.kw = kw + + def __str__(self): + """ + Returns the string representation of this exception. + """ + if self.msg is None: + if len(self.args) == 1: + return unicode(self.args[0]) + return unicode(self.args) + if len(self.args) > 0: + return self.msg % self.args + return self.msg % self.kw + + + + + +class SetError(IPAError): + msg = 'setting %r, but NameSpace does not allow attribute setting' + + + + + + + +class RegistrationError(IPAError): + """ + Base class for errors that occur during plugin registration. + """ + + +class SubclassError(RegistrationError): + """ + Raised when registering a plugin that is not a subclass of one of the + allowed bases. + """ + msg = 'plugin %r not subclass of any base in %r' + + def __init__(self, cls, allowed): + self.cls = cls + self.allowed = allowed + + def __str__(self): + return self.msg % (self.cls, self.allowed) + + +class DuplicateError(RegistrationError): + """ + Raised when registering a plugin whose exact class has already been + registered. + """ + msg = '%r at %d was already registered' + + def __init__(self, cls): + self.cls = cls + + def __str__(self): + return self.msg % (self.cls, id(self.cls)) + + +class OverrideError(RegistrationError): + """ + Raised when override=False yet registering a plugin that overrides an + existing plugin in the same namespace. + """ + msg = 'unexpected override of %s.%s with %r (use override=True if intended)' + + def __init__(self, base, cls): + self.base = base + self.cls = cls + + def __str__(self): + return self.msg % (self.base.__name__, self.cls.__name__, self.cls) + + +class MissingOverrideError(RegistrationError): + """ + Raised when override=True yet no preexisting plugin with the same name + and base has been registered. + """ + msg = '%s.%s has not been registered, cannot override with %r' + + def __init__(self, base, cls): + self.base = base + self.cls = cls + + def __str__(self): + return self.msg % (self.base.__name__, self.cls.__name__, self.cls) + + + +class TwiceSetError(IPAError): + msg = '%s.%s cannot be set twice' diff --git a/ipalib/exceptions.py b/ipalib/exceptions.py deleted file mode 100644 index 376a7a567..000000000 --- a/ipalib/exceptions.py +++ /dev/null @@ -1,127 +0,0 @@ -# Authors: -# Jason Gerard DeRose -# -# Copyright (C) 2008 Red Hat -# see file 'COPYING' for use and warranty inmsgion -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2 only -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -""" -All custom exceptions raised by `ipalib` package. -""" - -class IPAError(Exception): - """ - Use this base class for your custom IPA exceptions unless there is a - specific reason to subclass from AttributeError, KeyError, etc. - """ - msg = None - - def __init__(self, *args, **kw): - self.args = args - self.kw = kw - - def __str__(self): - """ - Returns the string representation of this exception. - """ - if self.msg is None: - if len(self.args) == 1: - return unicode(self.args[0]) - return unicode(self.args) - if len(self.args) > 0: - return self.msg % self.args - return self.msg % self.kw - - - - - -class SetError(IPAError): - msg = 'setting %r, but NameSpace does not allow attribute setting' - - - - - - - -class RegistrationError(IPAError): - """ - Base class for errors that occur during plugin registration. - """ - - -class SubclassError(RegistrationError): - """ - Raised when registering a plugin that is not a subclass of one of the - allowed bases. - """ - msg = 'plugin %r not subclass of any base in %r' - - def __init__(self, cls, allowed): - self.cls = cls - self.allowed = allowed - - def __str__(self): - return self.msg % (self.cls, self.allowed) - - -class DuplicateError(RegistrationError): - """ - Raised when registering a plugin whose exact class has already been - registered. - """ - msg = '%r at %d was already registered' - - def __init__(self, cls): - self.cls = cls - - def __str__(self): - return self.msg % (self.cls, id(self.cls)) - - -class OverrideError(RegistrationError): - """ - Raised when override=False yet registering a plugin that overrides an - existing plugin in the same namespace. - """ - msg = 'unexpected override of %s.%s with %r (use override=True if intended)' - - def __init__(self, base, cls): - self.base = base - self.cls = cls - - def __str__(self): - return self.msg % (self.base.__name__, self.cls.__name__, self.cls) - - -class MissingOverrideError(RegistrationError): - """ - Raised when override=True yet no preexisting plugin with the same name - and base has been registered. - """ - msg = '%s.%s has not been registered, cannot override with %r' - - def __init__(self, base, cls): - self.base = base - self.cls = cls - - def __str__(self): - return self.msg % (self.base.__name__, self.cls.__name__, self.cls) - - - -class TwiceSetError(IPAError): - msg = '%s.%s cannot be set twice' diff --git a/ipalib/plugable.py b/ipalib/plugable.py index ba2241b99..0de31d820 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -22,7 +22,7 @@ Utility classes for registering plugins, base classe for writing plugins. """ import inspect -import exceptions +import errors @@ -50,7 +50,7 @@ class Registrar(object): for base in self.__allowed: if issubclass(cls, base): return base - raise exceptions.SubclassError(cls, self.__allowed) + raise errors.SubclassError(cls, self.__allowed) def __call__(self, cls, override=False): """ @@ -65,17 +65,17 @@ class Registrar(object): # Raise DuplicateError if this exact class was already registered: if cls in self.__registered: - raise exceptions.DuplicateError(cls) + raise errors.DuplicateError(cls) # Check override: if cls.__name__ in sub_d: # Must use override=True to override: if not override: - raise exceptions.OverrideError(base, cls) + raise errors.OverrideError(base, cls) else: # There was nothing already registered to override: if override: - raise exceptions.MissingOverrideError(base, cls) + raise errors.MissingOverrideError(base, cls) # The plugin is okay, add to __registered and sub_d: self.__registered.add(cls) diff --git a/ipalib/tests/test_base.py b/ipalib/tests/test_base.py index 8f6a0313c..da9de7a03 100644 --- a/ipalib/tests/test_base.py +++ b/ipalib/tests/test_base.py @@ -21,7 +21,7 @@ Unit tests for `ipalib.base` module. """ -from ipalib import base, exceptions, crud +from ipalib import base, errors, crud def read_only(obj, name): @@ -129,7 +129,7 @@ class test_NameSpace: raised = False try: setattr(ns, key, value) - except exceptions.SetError: + except errors.SetError: raised = True assert raised assert getattr(ns, key, None) != value @@ -206,7 +206,7 @@ def test_Attribute(): raised = False try: i.obj = u - except exceptions.TwiceSetError: + except errors.TwiceSetError: raised = True assert raised @@ -307,14 +307,14 @@ class test_Registrar(): raised = False try: r(user()) - except exceptions.RegistrationError: + except errors.RegistrationError: raised = True # Check that exception is raised trying to register class of wrong base: raised = False try: r(wrong_base) - except exceptions.RegistrationError: + except errors.RegistrationError: raised = True assert raised diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py index 1ba021134..f0bdeb4a0 100644 --- a/ipalib/tests/test_plugable.py +++ b/ipalib/tests/test_plugable.py @@ -21,7 +21,7 @@ Unit tests for `ipalib.plugable` module. """ -from ipalib import plugable, exceptions +from ipalib import plugable, errors def test_Registrar(): @@ -56,7 +56,7 @@ def test_Registrar(): raised = False try: r(plugin3) - except exceptions.SubclassError: + except errors.SubclassError: raised = True assert raised @@ -74,7 +74,7 @@ def test_Registrar(): raised = False try: r(plugin1) - except exceptions.DuplicateError: + except errors.DuplicateError: raised = True assert raised @@ -88,7 +88,7 @@ def test_Registrar(): raised = False try: r(plugin1) - except exceptions.OverrideError: + except errors.OverrideError: raised = True assert raised @@ -104,7 +104,7 @@ def test_Registrar(): raised = False try: r(plugin2, override=True) - except exceptions.MissingOverrideError: + except errors.MissingOverrideError: raised = True assert raised -- cgit