Use metaclass to automate exc error_type; Remove needless inheritance

master
voussoir 2017-03-17 02:30:02 -07:00
parent 85d91310bc
commit 019c0d727d
1 changed files with 30 additions and 55 deletions

View File

@ -6,155 +6,130 @@ def pascal_to_loudsnakes(text):
text = text.upper() text = text.upper()
return text return text
def with_error_type(cls):
cls.error_type = pascal_to_loudsnakes(cls.__name__)
return cls
class EtiquetteException(Exception): class ErrorTypeAdder(type):
'''
Thanks Unutbu
http://stackoverflow.com/a/18126678
'''
def __init__(cls, name, bases, clsdict):
type.__init__(cls, name, bases, clsdict)
cls.error_type = pascal_to_loudsnakes(name)
class EtiquetteException(Exception, metaclass=ErrorTypeAdder):
error_message = '' error_message = ''
class WithFormat(EtiquetteException):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.given_args = args self.given_args = args
self.given_kwargs = kwargs self.given_kwargs = kwargs
self.error_message = self.error_message.format(*args, **kwargs) self.error_message = self.error_message.format(*args, **kwargs)
EtiquetteException.__init__(self, self.error_message)
# NO SUCH # NO SUCH
class NoSuch(WithFormat): class NoSuch(EtiquetteException):
pass pass
@with_error_type
class NoSuchAlbum(NoSuch): class NoSuchAlbum(NoSuch):
error_message = 'Album "{}" does not exist.' error_message = 'Album "{}" does not exist.'
@with_error_type
class NoSuchBookmark(NoSuch): class NoSuchBookmark(NoSuch):
error_message = 'Bookmark "{}" does not exist.' error_message = 'Bookmark "{}" does not exist.'
@with_error_type
class NoSuchGroup(NoSuch): class NoSuchGroup(NoSuch):
error_message = 'Group "{}" does not exist.' error_message = 'Group "{}" does not exist.'
@with_error_type
class NoSuchPhoto(NoSuch): class NoSuchPhoto(NoSuch):
error_message = 'Photo "{}" does not exist.' error_message = 'Photo "{}" does not exist.'
@with_error_type
class NoSuchSynonym(NoSuch): class NoSuchSynonym(NoSuch):
error_message = 'Synonym "{}" does not exist.' error_message = 'Synonym "{}" does not exist.'
@with_error_type
class NoSuchTag(NoSuch): class NoSuchTag(NoSuch):
error_message = 'Tag "{}" does not exist.' error_message = 'Tag "{}" does not exist.'
@with_error_type
class NoSuchUser(NoSuch): class NoSuchUser(NoSuch):
error_message = 'User "{}" does not exist.' error_message = 'User "{}" does not exist.'
# EXISTS # EXISTS
@with_error_type class AlbumExists(EtiquetteException):
class AlbumExists(WithFormat):
error_message = 'Album "{}" already exists.' error_message = 'Album "{}" already exists.'
def __init__(self, album): def __init__(self, album):
self.album = album self.album = album
WithFormat.__init__(self, album.id) EtiquetteException.__init__(self, album.id)
@with_error_type class GroupExists(EtiquetteException):
class GroupExists(WithFormat):
error_message = '{member} already in group {group}' error_message = '{member} already in group {group}'
@with_error_type class PhotoExists(EtiquetteException):
class PhotoExists(WithFormat):
error_message = 'Photo "{}" already exists.' error_message = 'Photo "{}" already exists.'
def __init__(self, photo): def __init__(self, photo):
self.photo = photo self.photo = photo
WithFormat.__init__(self, photo.id) EtiquetteException.__init__(self, photo.id)
@with_error_type class TagExists(EtiquetteException):
class TagExists(WithFormat):
error_message = 'Tag "{}" already exists.' error_message = 'Tag "{}" already exists.'
def __init__(self, tag): def __init__(self, tag):
self.tag = tag self.tag = tag
WithFormat.__init__(self, tag.name) EtiquetteException.__init__(self, tag.name)
@with_error_type class UserExists(EtiquetteException):
class UserExists(WithFormat):
error_message = 'User "{}" already exists.' error_message = 'User "{}" already exists.'
def __init__(self, user): def __init__(self, user):
self.user = user self.user = user
WithFormat.__init__(self, user.username) EtiquetteException.__init__(self, user.username)
# TAG ERRORS # TAG ERRORS
@with_error_type
class CantSynonymSelf(EtiquetteException): class CantSynonymSelf(EtiquetteException):
error_message = 'Cannot apply synonym to self.' error_message = 'Cannot apply synonym to self.'
@with_error_type
class EasyBakeError(EtiquetteException): class EasyBakeError(EtiquetteException):
error_message = '' error_message = '{}'
def __init__(self, message):
self.error_message = message
EtiquetteException.__init__(self)
@with_error_type class RecursiveGrouping(EtiquetteException):
class RecursiveGrouping(WithFormat):
error_message = '{group} is an ancestor of {member}.' error_message = '{group} is an ancestor of {member}.'
@with_error_type class TagTooLong(EtiquetteException):
class TagTooLong(WithFormat):
error_message = 'Tag "{}" is too long.' error_message = 'Tag "{}" is too long.'
@with_error_type class TagTooShort(EtiquetteException):
class TagTooShort(WithFormat):
error_message = 'Tag "{}" has too few valid characters.' error_message = 'Tag "{}" has too few valid characters.'
# USER ERRORS # USER ERRORS
@with_error_type
class AlreadySignedIn(EtiquetteException): class AlreadySignedIn(EtiquetteException):
error_message = 'You\'re already signed in.' error_message = 'You\'re already signed in.'
@with_error_type class InvalidUsernameChars(EtiquetteException):
class InvalidUsernameChars(WithFormat):
error_message = 'Username "{username}" contains invalid characters: {badchars}.' error_message = 'Username "{username}" contains invalid characters: {badchars}.'
@with_error_type class PasswordTooShort(EtiquetteException):
class PasswordTooShort(WithFormat):
error_message = 'Password is shorter than the minimum of {min_length}.' error_message = 'Password is shorter than the minimum of {min_length}.'
@with_error_type class UsernameTooLong(EtiquetteException):
class UsernameTooLong(WithFormat):
error_message = 'Username "{username}" is longer than maximum of {max_length}.' error_message = 'Username "{username}" is longer than maximum of {max_length}.'
@with_error_type class UsernameTooShort(EtiquetteException):
class UsernameTooShort(WithFormat):
error_message = 'Username "{username}" is shorter than minimum of {min_length}.' error_message = 'Username "{username}" is shorter than minimum of {min_length}.'
@with_error_type
class WrongLogin(EtiquetteException): class WrongLogin(EtiquetteException):
error_message = 'Wrong username-password combination.' error_message = 'Wrong username-password combination.'
# GENERAL ERRORS # GENERAL ERRORS
@with_error_type
class FeatureDisabled(EtiquetteException): class FeatureDisabled(EtiquetteException):
''' '''
For when features of the system have been disabled by the configuration. For when features of the system have been disabled by the configuration.
''' '''
error_message = 'This feature has been disabled.' error_message = 'This feature has been disabled.'
@with_error_type
class NotExclusive(EtiquetteException): class NotExclusive(EtiquetteException):
''' '''
For when two or more mutually exclusive actions have been requested. For when two or more mutually exclusive actions have been requested.
''' '''
pass pass
@with_error_type class OutOfOrder(EtiquetteException):
class OutOfOrder(WithFormat):
''' '''
For when a requested minmax range (a, b) has b > a For when a requested minmax range (a, b) has b > a
''' '''