From abfb91d87a57970315504508dd094b9f5cdba489 Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Mon, 27 Jan 2014 14:17:23 +0100 Subject: Move base exceptions and exit codes to separate error.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- command.py | 6 +++--- command_context.py | 2 +- command_manager.py | 6 +++--- error.py | 32 ++++++++++++++++++++++++++++++++ filter.py | 3 ++- format.py | 3 ++- main.py | 2 +- utils.py | 26 +------------------------- 8 files changed, 45 insertions(+), 35 deletions(-) create mode 100644 error.py diff --git a/command.py b/command.py index 9c5e946..fc890ba 100644 --- a/command.py +++ b/command.py @@ -8,10 +8,10 @@ __author__ = "Jan Pokorný " import logging from optparse import make_option, SUPPRESS_HELP +from .error import ClufterError, \ + EC from .plugin_registry import PluginRegistry -from .utils import ClufterError, \ - EC, \ - func_defaults_varnames, \ +from .utils import func_defaults_varnames, \ head_tail, \ hybridproperty diff --git a/command_context.py b/command_context.py index f251a44..fc8c7dd 100644 --- a/command_context.py +++ b/command_context.py @@ -7,7 +7,7 @@ __author__ = "Jan Pokorný " import logging -from .utils import ClufterError +from .error import ClufterError log = logging.getLogger(__name__) diff --git a/command_manager.py b/command_manager.py index 389c843..acbb730 100644 --- a/command_manager.py +++ b/command_manager.py @@ -8,10 +8,10 @@ __author__ = "Jan Pokorný " import logging from .command import commands +from .error import ClufterError, ClufterPlainError, \ + EC from .plugin_registry import PluginManager -from .utils import ClufterError, ClufterPlainError, \ - EC, \ - apply_preserving_depth, \ +from .utils import apply_preserving_depth, \ apply_aggregation_preserving_depth, \ apply_intercalate diff --git a/error.py b/error.py new file mode 100644 index 0000000..5aafec5 --- /dev/null +++ b/error.py @@ -0,0 +1,32 @@ +# -*- coding: UTF-8 -*- +# Copyright 2014 Red Hat, Inc. +# Part of clufter project +# Licensed under GPLv2 (a copy included | http://gnu.org/licenses/gpl-2.0.txt) +"""Base exception classes and exit code definitions""" +__author__ = "Jan Pokorný " + + +# name the exitcodes +ecodes = 'SUCCESS', 'FAILURE' +EC = type('EC', (), dict((n, v) for v, n + in enumerate('EXIT_' + i for i in ecodes))) + + +class ClufterError(Exception): + def __init__(self, ctx_self, msg, *args): + self.ctx_self = ctx_self + self.msg = msg + self.args = args + + def __str__(self): + ret = '' + if self.ctx_self: + ret = getattr(self.ctx_self, '__name__', + self.ctx_self.__class__.__name__) + ret += ': ' + return ret + self.msg.format(*self.args) + + +class ClufterPlainError(ClufterError): + def __init__(self, msg, *args): + super(ClufterPlainError, self).__init__(None, msg, *args) diff --git a/filter.py b/filter.py index 92d5799..2484d98 100644 --- a/filter.py +++ b/filter.py @@ -12,8 +12,9 @@ from collections import OrderedDict, defaultdict from lxml import etree +from .error import ClufterError from .plugin_registry import PluginRegistry -from .utils import ClufterError, head_tail, hybridproperty, filtervarspop +from .utils import head_tail, hybridproperty, filtervarspop from .command_context import CommandContext log = logging.getLogger(__name__) diff --git a/format.py b/format.py index d4e2ca4..ef3e211 100644 --- a/format.py +++ b/format.py @@ -16,8 +16,9 @@ from sys import modules from lxml import etree +from .error import ClufterError from .plugin_registry import PluginRegistry -from .utils import ClufterError, classproperty +from .utils import classproperty log = logging.getLogger(__name__) MAX_DEPTH = 1000 diff --git a/main.py b/main.py index 1079c7b..361e600 100644 --- a/main.py +++ b/main.py @@ -12,10 +12,10 @@ from optparse import make_option, \ OptionGroup, \ IndentedHelpFormatter +from .error import EC from .format_manager import FormatManager from .filter_manager import FilterManager from .command_manager import CommandManager -from .utils import EC from . import version_text, description_text diff --git a/utils.py b/utils.py index 2ef8bf5..785bdad 100644 --- a/utils.py +++ b/utils.py @@ -9,11 +9,7 @@ import os import sys from subprocess import Popen - -# name the exitcodes -ecodes = 'SUCCESS', 'FAILURE' -EC = type('EC', (), dict((n, v) for v, n - in enumerate('EXIT_' + i for i in ecodes))) +from .error import ClufterError head_tail = lambda x=None, *y: (x, x if x is None else y) @@ -76,26 +72,6 @@ def func_defaults_varnames(func): return func_defaults, func_varnames -class ClufterError(Exception): - def __init__(self, ctx_self, msg, *args): - self.ctx_self = ctx_self - self.msg = msg - self.args = args - - def __str__(self): - ret = '' - if self.ctx_self: - ret = getattr(self.ctx_self, '__name__', - self.ctx_self.__class__.__name__) - ret += ': ' - return ret + self.msg.format(*self.args) - - -class ClufterPlainError(ClufterError): - def __init__(self, msg, *args): - super(ClufterPlainError, self).__init__(None, msg, *args) - - class OneoffWrappedStdinPopen(object): """Singleton to watch for atmost one use of stdin in Popen context""" def __init__(self): -- cgit