Skip to content

Commit

Permalink
Modified app.strict implementation. Update of strict during runtime i…
Browse files Browse the repository at this point in the history
…s possible now.
  • Loading branch information
danwos committed Dec 20, 2016
1 parent e1c7803 commit e7077fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
22 changes: 6 additions & 16 deletions groundwork/groundwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ class App(object):
:param strict: If true, Exceptions are thrown, if a plugin can not be initialised or activated.
"""
def __init__(self, config_files=None, plugins=None, strict=False):
if config_files is None:
config_files = []

self.strict = strict

#: logging object for sending log messages. Example::
#:
#: from groundwork import App
#: my_app = App()
#: my_app.log.debug("Send debug message")
#: my_app.log.error("Send error....")

if config_files is None:
config_files = []

self.log = logging.getLogger("groundwork")

self._configure_logging()
Expand Down Expand Up @@ -75,22 +76,11 @@ def __init__(self, config_files=None, plugins=None, strict=False):

#: Instance of :class:`~groundwork.pluginmanager.PluginManager`- Provides functions to load, activate and
# deactivate plugins.
self.plugins = PluginManager(app=self, strict=strict)
self.plugins = PluginManager(app=self)

if plugins is not None:
self.plugins.classes.register(plugins)

@property
def strict(self):
return self.plugins._strict

@strict.setter
def strict(self, value):
if not isinstance(value, bool):
raise TypeError("strict must be bool")
self.plugins._strict = value
self.plugins.classes._strict = value

def _configure_logging(self, logger_dict=None):
"""
Configures the logging module with a given dictionary, which in most cases was loaded from a configuration
Expand Down
22 changes: 10 additions & 12 deletions groundwork/pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PluginManager:
PluginManager for searching, initialising, activating and deactivating groundwork plugins.
"""

def __init__(self, app, strict=False):
def __init__(self, app):
"""
Initialises the plugin manager.
Expand All @@ -34,13 +34,12 @@ def __init__(self, app, strict=False):
:param strict: If True, problems during plugin registration/initialisation or activation will throw an exception
"""
self._log = logging.getLogger(__name__)
self._strict = strict
self._app = app
self._plugins = {}

#: Instance of :class:`~groundwork.pluginmanager.PluginClassManager`.
#: Handles the registration of plugin classes, which can be used to create new plugins during runtime.
self.classes = PluginClassManager(self._app, self._strict)
self.classes = PluginClassManager(self._app)

def initialise_by_names(self, plugins=None):
"""
Expand Down Expand Up @@ -90,7 +89,7 @@ def initialise(self, clazz, name=None):
plugin_instance.name = name
except Exception as e:
self._log.warning("Plugin class %s could not be initialised" % clazz.__name__)
if self._strict:
if self._app.strict:
raise PluginNotInitialisableException("Plugin class %s could not be initialised" % clazz.__name__) \
from e

Expand All @@ -114,7 +113,7 @@ def initialise(self, clazz, name=None):

if not issubclass(clazz, GwBasePattern):
self._log.warn("Can not load %s. Plugin is not based on groundwork.Plugin." % clazz.__name__)
if self._strict:
if self._app.strict:
raise Exception("Can not load %s. Plugin is not based on groundwork.Plugin." % clazz.__name__)
return None

Expand Down Expand Up @@ -154,7 +153,7 @@ def activate(self, plugins=[]):
self.initialise_by_names([plugin_name])
except Exception as e:
self._log.error("Couldn't initialise plugin %s" % plugin_name)
if self._strict:
if self._app.strict:
raise Exception("Couldn't initialise plugin %s" % plugin_name) from e
else:
continue
Expand All @@ -171,7 +170,7 @@ def activate(self, plugins=[]):
plugins_activated.append(plugin_name)
else:
self._log.warning("Plugin %s got already activated." % plugin_name)
if self._strict:
if self._app.strict:
raise PluginNotInitialisableException()

self._log.info("Plugins activated: %s" % ", ".join(plugins_activated))
Expand Down Expand Up @@ -266,10 +265,9 @@ class PluginClassManager:
Provides functions to register new plugin classes during runtime.
"""

def __init__(self, app, strict=False):
def __init__(self, app):
self._log = logging.getLogger(__name__)
self._app = app
self._strict = strict
self._classes = {}
self._get_plugins_by_entry_points()

Expand Down Expand Up @@ -355,17 +353,17 @@ def register_class(self, clazz, name=None, entrypoint_name=None, distribution_pa

if not inspect.isclass(clazz) or not issubclass(clazz, GwBasePattern):
self._log.error("Given plugin is not a subclass of groundworkPlugin.")
if self._strict:
if self._app.strict:
raise AttributeError("Given plugin is not a subclass of groundworkPlugin.")

if isinstance(clazz, GwBasePattern):
self._log.error("Given plugin %s is already initialised. Please provide a class not an instance.")
if self._strict:
if self._app.strict:
raise Exception("Given plugin %s is already initialised. Please provide a class not an instance.")

if name in self._classes.keys():
self._log.warning("Plugin %s already registered" % name)
if self._strict:
if self._app.strict:
raise PluginRegistrationException("Plugin %s already registered" % name)

self._classes[name] = PluginClass(name, clazz, entrypoint_name, distribution_path,
Expand Down

0 comments on commit e7077fa

Please sign in to comment.