diff --git a/main_application.py b/main_application.py index 10dfcdb..e4d87ae 100644 --- a/main_application.py +++ b/main_application.py @@ -1,4 +1,4 @@ -"""Main applicatoin that demonstrates the functionality of +"""Main application that demonstrates the functionality of the dynamic plugins and the PluginCollection class """ diff --git a/plugin_collection.py b/plugin_collection.py index a91a356..ac37aa3 100644 --- a/plugin_collection.py +++ b/plugin_collection.py @@ -1,12 +1,16 @@ +""" Simple Plugin implementation in Python +""" + import inspect import os import pkgutil -class Plugin(object): - """Base class that each plugin must inherit from. within this class +class Plugin(): + """Base class that each plugin must inherit from. Within this class you must define the methods that all of your plugins must implement """ + # pylint: disable=too-few-public-methods def __init__(self): self.description = 'UNKNOWN' @@ -20,7 +24,7 @@ def perform_operation(self, argument): -class PluginCollection(object): +class PluginCollection(): """Upon creation, this class will read the plugins package for modules that contain a class definition that is inheriting from the Plugin class """ @@ -50,22 +54,25 @@ def apply_all_plugins_on_value(self, argument): print() print(f'Applying all plugins on value {argument}:') for plugin in self.plugins: - print(f' Applying {plugin.description} on value {argument} yields value {plugin.perform_operation(argument)}') + print(f' Applying {plugin.description} on value {argument}'\ + f' yields value {plugin.perform_operation(argument)}') def walk_package(self, package): """Recursively walk the supplied package to retrieve all plugins """ imported_package = __import__(package, fromlist=['blah']) - for _, pluginname, ispkg in pkgutil.iter_modules(imported_package.__path__, imported_package.__name__ + '.'): + for _, pluginname, ispkg in pkgutil.iter_modules( + imported_package.__path__, imported_package.__name__ + '.' + ): if not ispkg: plugin_module = __import__(pluginname, fromlist=['blah']) clsmembers = inspect.getmembers(plugin_module, inspect.isclass) - for (_, c) in clsmembers: + for (_, cls) in clsmembers: # Only add classes that are a sub class of Plugin, but NOT Plugin itself - if issubclass(c, Plugin) & (c is not Plugin): - print(f' Found plugin class: {c.__module__}.{c.__name__}') - self.plugins.append(c()) + if issubclass(cls, Plugin) & (cls is not Plugin): + print(f' Found plugin class: {cls.__module__}.{cls.__name__}') + self.plugins.append(cls()) # Now that we have looked at all the modules in the current package, start looking @@ -74,14 +81,18 @@ def walk_package(self, package): if isinstance(imported_package.__path__, str): all_current_paths.append(imported_package.__path__) else: - all_current_paths.extend([x for x in imported_package.__path__]) + all_current_paths.extend(imported_package.__path__) for pkg_path in all_current_paths: if pkg_path not in self.seen_paths: self.seen_paths.append(pkg_path) # Get all sub directory of the current package path directory - child_pkgs = [p for p in os.listdir(pkg_path) if os.path.isdir(os.path.join(pkg_path, p))] + child_pkgs = [ + p for p in os.listdir(pkg_path) + if os.path.isdir(os.path.join(pkg_path, p)) + ] + # For each sub directory, apply the walk_package method recursively for child_pkg in child_pkgs: diff --git a/plugins/double/double_negative.py b/plugins/double/double_negative.py index 24c97b1..dbd3fff 100644 --- a/plugins/double/double_negative.py +++ b/plugins/double/double_negative.py @@ -1,8 +1,13 @@ -import plugin_collection +"""Negative double function +""" -class DoubleNegative(plugin_collection.Plugin): +from plugin_collection import Plugin + +class DoubleNegative(Plugin): """This plugin will just multiply the argument with the value -2 """ + # pylint: disable=too-few-public-methods + def __init__(self): super().__init__() self.description = 'Negative double function' diff --git a/plugins/double/double_positive.py b/plugins/double/double_positive.py index f045687..3dab19a 100644 --- a/plugins/double/double_positive.py +++ b/plugins/double/double_positive.py @@ -1,8 +1,13 @@ +"""Double function +""" + from plugin_collection import Plugin class DoublePositive(Plugin): """This plugin will just multiply the argument with the value 2 """ + # pylint: disable=too-few-public-methods + def __init__(self): super().__init__() self.description = 'Double function' diff --git a/plugins/identity.py b/plugins/identity.py index a2c87d6..78f0840 100644 --- a/plugins/identity.py +++ b/plugins/identity.py @@ -1,8 +1,13 @@ -import plugin_collection +"""Identity function +""" -class Identity(plugin_collection.Plugin): +from plugin_collection import Plugin + +class Identity(Plugin): """This plugin is just the identity function: it returns the argument """ + # pylint: disable=too-few-public-methods + def __init__(self): super().__init__() self.description = 'Identity function'