Skip to content

Commit

Permalink
pylint cleanups (including bugfixes)
Browse files Browse the repository at this point in the history
  • Loading branch information
abadger committed Sep 12, 2024
1 parent d61eb01 commit 77bc4a7
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions leapp/actors/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

import abc
import glob
import importlib
import logging
import os.path
import pkgutil
from collections import defaultdict

import six
Expand Down Expand Up @@ -52,6 +50,9 @@ class ValidationError(Exception):
"""


# pylint: disable=deprecated-decorator
# @abc.abstractproperty is deprecated in newer Python3 versions but it's
# necessary for Python <= 3.3 (including 2.7)
@six.add_metaclass(abc.ABCMeta)
class Config:
"""
Expand Down Expand Up @@ -96,10 +97,12 @@ def to_dict(cls):
'{0}_description__'.format(cls.name): cls.description
}
}
### TODO: Retrieve the default values from the type field.
# representation[cls.section][cls.name] = cls.type_.get_default()
# TODO: Retrieve the default values from the type field.
# Something like this maybe:
# representation[cls.section][cls.name] = cls.type_.get_default()

return representation
# pylint: enable=deprecated-decorator


def _merge_config(configuration, new_config):
Expand Down Expand Up @@ -132,7 +135,7 @@ def _get_config(config_dir='/etc/leapp/actor_conf.d'):
parsed_config = yaml.load(raw_cfg, SafeLoader)
except Exception as e:
log.warning("Warning: unparsable yaml file %s in the config directory."
" Error: %s", filename, str(e))
" Error: %s", config_file, str(e))
raise

_merge_config(configuration, parsed_config)
Expand All @@ -158,7 +161,7 @@ def normalize_schemas(schemas):
message = ('Two actors added incompatible configuration items'
' with the same name for Section: {section},'
' Field: {field}'.format(section=field.section,
name=field.name))
field=field.name))
log.error(message)
raise SchemaError(message)

Expand All @@ -180,7 +183,9 @@ def _validate_field_type(field_type, field_value):
# with. This might not work right or there might be a much better way.
try:
field_type.create(field_value)
except Exception:
except Exception as e: # pylint: disable=broad-exception-caught
# Any problems mean that the field did not validate.
log.info("Configuration value failed to validate with: %(exception)".format(str(e)))
return False
return True

Expand All @@ -196,6 +201,12 @@ def _normalize_config(actor_config, schema):
continue

for field_name in actor_config:
# Any field names which end in "__" are reserved for LEAPP to use
# for its purposes. In particular, it places documentation of
# a field's value into these reserved field names.
if field_name.endswith("__"):
continue

if field_name not in schema[section_name]:
# TODO: Also have information about which config file contains the unknown field.
message = ("A config file contained an unknown field: (Section:"
Expand Down Expand Up @@ -259,26 +270,24 @@ def load(config_dir, schemas):
return _ACTOR_CONFIG

config = _get_config(config_dir)
config = _normalize_config(config, schema)
config = _normalize_config(config, schemas)

_ACTOR_CONFIG = config
return _ACTOR_CONFIG


def retrieve_config(schema):
"""Called by the actor to retrieve the actor configuration specific to this actor."""
# TODO: This isn't good API. Since this function is called by the Actors,
# we *know* that this is okay to do (as the configuration will have already
# been loaded.) However, there's nothing in the API that ensures that this
# is the case. Need to redesign this. Can't think of how it should look
# right now because loading requires information that the Actor doesn't
# know.
global _ACTOR_CONFIG
all_actor_config = _ACTOR_CONFIG
# TODO: The use of _ACTOR_CONFIG isn't good API. Since this function is
# called by the Actors, we *know* that this is okay to do (as the
# configuration will have already been loaded.) However, there's nothing in
# the API that ensures that this is the case. Need to redesign this.
# Can't think of how it should look right now because loading requires
# information that the Actor doesn't know.

configuration = defaultdict(dict)
for field in schema:
configuration[field.section][field.name] = all_actor_config[field.section][field.name]
configuration[field.section][field.name] = _ACTOR_CONFIG[field.section][field.name]

return configuration

Expand Down Expand Up @@ -312,4 +321,6 @@ def format_config():
- leapp-repository
- snactor
"""
return SafeDumper(yaml.dump(parse_config_files(), dumper=SafeDumper))
# TODO: This is just a placeholder. We need to do some additional
# formatting that includes the documentation, not just return it as is.
return yaml.dump(_ACTOR_CONFIG, SafeDumper)

0 comments on commit 77bc4a7

Please sign in to comment.