From 4f596dddb3422492c2b15a55a250a445e255bba0 Mon Sep 17 00:00:00 2001 From: Xiangce Liu Date: Wed, 8 Jan 2025 12:58:27 +0800 Subject: [PATCH] chore: sort out exceptions (#4327) - and remove the contrib.ConfigParser which only hold exceptions - N/A to "rhel_6" branch Signed-off-by: Xiangce Liu --- insights/combiners/crio_conf.py | 3 +- insights/contrib/ConfigParser.py | 14 ------ insights/core/__init__.py | 9 +++- insights/core/exceptions.py | 69 ++++++++++++++++++++-------- insights/tests/test_config_parser.py | 25 +++++----- 5 files changed, 72 insertions(+), 48 deletions(-) delete mode 100644 insights/contrib/ConfigParser.py diff --git a/insights/combiners/crio_conf.py b/insights/combiners/crio_conf.py index 252b395dcc..278c5308ed 100644 --- a/insights/combiners/crio_conf.py +++ b/insights/combiners/crio_conf.py @@ -4,7 +4,7 @@ The crio files are normally available to rules as a list of CrioConf objects. """ -from insights.contrib.ConfigParser import NoOptionError, NoSectionError +from insights.core.exceptions import NoOptionError, NoSectionError from insights.core.plugins import combiner from insights.parsers.crio_conf import CrioConf @@ -80,6 +80,7 @@ class AllCrioConf(object): Attributes: files (list): The list of configuration file names. """ + def __init__(self, crio_confs): self.data = {} self.files = [] diff --git a/insights/contrib/ConfigParser.py b/insights/contrib/ConfigParser.py deleted file mode 100644 index 485d8282c7..0000000000 --- a/insights/contrib/ConfigParser.py +++ /dev/null @@ -1,14 +0,0 @@ -from insights.parsr.iniparser import NoOptionError as NOE, NoSectionError as NSE - - -class NoOptionError(NOE): - """ - These classes are just here for backwards compatibility - until other projects can be updated to reference the new - location of the exceptions in iniparser. - """ - pass - - -class NoSectionError(NSE): - pass diff --git a/insights/core/__init__.py b/insights/core/__init__.py index ae070a9e03..5a994883cc 100644 --- a/insights/core/__init__.py +++ b/insights/core/__init__.py @@ -12,8 +12,13 @@ from collections import OrderedDict from fnmatch import fnmatch -from insights.contrib.ConfigParser import NoOptionError, NoSectionError -from insights.core.exceptions import ContentException, ParseException, SkipComponent # noqa: F401 +from insights.core.exceptions import ( + ContentException, + NoOptionError, + NoSectionError, + ParseException, + SkipComponent, +) # noqa: F401 from insights.core.serde import deserializer, serializer from insights.parsr import iniparser from insights.parsr.query import Directive, Entry, Result, Section, compile_queries diff --git a/insights/core/exceptions.py b/insights/core/exceptions.py index c9eb6d7d96..996f103965 100644 --- a/insights/core/exceptions.py +++ b/insights/core/exceptions.py @@ -1,8 +1,16 @@ +""" +Exceptions +========== +""" + +from insights.parsr.iniparser import NoOptionError, NoSectionError # noqa: F401 + class BlacklistedSpec(Exception): """ Exception to be thrown when a blacklisted spec is found. """ + pass @@ -31,20 +39,45 @@ def __unicode__(self): class InvalidArchive(Exception): + """ + Raised when archive cannot be identified or missing expected structure. + """ + def __init__(self, msg): super(InvalidArchive, self).__init__(msg) self.msg = msg +class InvalidContentType(InvalidArchive): + """ + Raised when invalid content_type is specified. + """ + + def __init__(self, content_type): + msg = 'Invalid content type: "%s"' % content_type + super(InvalidContentType, self).__init__(msg) + self.msg = msg + self.content_type = content_type + + class MissingRequirements(Exception): """ Raised during evaluation if a component's dependencies aren't met. """ + def __init__(self, requirements): self.requirements = requirements super(MissingRequirements, self).__init__(requirements) +class NoFilterException(Exception): + """ + Raised whenever no filters added to a `filterable` :class:`datasource`. + """ + + pass + + class ParseException(Exception): """ Exception that should be thrown from parsers that encounter @@ -52,6 +85,7 @@ class ParseException(Exception): is thrown, the exception message and data are logged and no parser output data is saved. """ + pass @@ -60,33 +94,32 @@ class SkipComponent(Exception): This class should be raised by components that want to be taken out of dependency resolution. """ + + pass + + +class ContentException(SkipComponent): + """ + Raised whenever a :class:`datasource` fails to get data. + """ + pass class TimeoutException(Exception): - """ Raised whenever a :class:`datasource` hits the set timeout value. """ + """ + Raised whenever a :class:`datasource` hits the set timeout value. + """ + pass class ValidationException(Exception): + """ + Raised when passes invalid arguments to Response. + """ + def __init__(self, msg, r=None): if r: msg = "%s: %s" % (msg, r) super(ValidationException, self).__init__(msg) - - -class ContentException(SkipComponent): - """ Raised whenever a :class:`datasource` fails to get data. """ - pass - - -class NoFilterException(ContentException): - """ Raised whenever no filters added to a `filterable` :class:`datasource`.""" - pass - - -class InvalidContentType(InvalidArchive): - def __init__(self, content_type): - self.msg = 'Invalid content type: "%s"' % content_type - super(InvalidContentType, self).__init__(self.msg) - self.content_type = content_type diff --git a/insights/tests/test_config_parser.py b/insights/tests/test_config_parser.py index ec3aee4d99..d29cdde8e9 100644 --- a/insights/tests/test_config_parser.py +++ b/insights/tests/test_config_parser.py @@ -1,8 +1,7 @@ import pytest -from insights.contrib.ConfigParser import NoOptionError from insights.core import ConfigParser, IniConfigFile -from insights.core.exceptions import SkipComponent +from insights.core.exceptions import NoOptionError, SkipComponent from insights.tests import context_wrap # An example config file with a few tricks and traps for the parser @@ -41,19 +40,19 @@ def test_ini_config_file_parser(): ini = IniConfigFile(context_wrap(CONFIG_FILE)) # sections() tests - assert list(ini.sections()) == \ - ['global', 'comment tricks', 'value overwriting', 'value checks'] + assert list(ini.sections()) == ['global', 'comment tricks', 'value overwriting', 'value checks'] # items() tests - here we return a dictionary - assert dict(ini.items('global')) == \ - {'keynospace': 'valuenospaces', - 'key with spaces': 'value with spaces', - 'key with continued value': "value1 value2"} - assert dict(ini.items('comment tricks')) == \ - {'comment # in key': 'value still found', - 'comment in value': 'value includes'} - assert dict(ini.items('value overwriting')) == \ - {'key': 'this one should be picked'} + assert dict(ini.items('global')) == { + 'keynospace': 'valuenospaces', + 'key with spaces': 'value with spaces', + 'key with continued value': "value1 value2", + } + assert dict(ini.items('comment tricks')) == { + 'comment # in key': 'value still found', + 'comment in value': 'value includes', + } + assert dict(ini.items('value overwriting')) == {'key': 'this one should be picked'} # get() tests on global section assert ini.get('global', 'keynospace') == 'valuenospaces'