Skip to content

Commit

Permalink
chore: sort out exceptions (#4327)
Browse files Browse the repository at this point in the history
- and remove the contrib.ConfigParser which only hold exceptions
- N/A to "rhel_6" branch

Signed-off-by: Xiangce Liu <[email protected]>
  • Loading branch information
xiangce authored Jan 8, 2025
1 parent 4c432b2 commit 4f596dd
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 48 deletions.
3 changes: 2 additions & 1 deletion insights/combiners/crio_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = []
Expand Down
14 changes: 0 additions & 14 deletions insights/contrib/ConfigParser.py

This file was deleted.

9 changes: 7 additions & 2 deletions insights/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 51 additions & 18 deletions insights/core/exceptions.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -31,27 +39,53 @@ 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
exceptions they recognize while parsing. When this exception
is thrown, the exception message and data are logged and no
parser output data is saved.
"""

pass


Expand All @@ -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
25 changes: 12 additions & 13 deletions insights/tests/test_config_parser.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 4f596dd

Please sign in to comment.