Skip to content

Commit

Permalink
added plugin class contextmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Oct 16, 2017
1 parent b4568b8 commit c54b3e2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion jsonextended/__init__.py
Expand Up @@ -91,7 +91,7 @@
"""

__version__ = '0.7.2'
__version__ = '0.7.3'

from jsonextended import ejson, units, utils, edict, plugins

Expand Down
60 changes: 58 additions & 2 deletions jsonextended/plugins.py
Expand Up @@ -8,6 +8,7 @@
import uuid
import warnings
from fnmatch import fnmatch
from contextlib import contextmanager

# py 2/3 compatibility
try:
Expand Down Expand Up @@ -164,7 +165,7 @@ def unload_all_plugins(category=None):
_all_plugins[category] = {}


def unload_plugin(name, category):
def unload_plugin(name, category=None):
""" remove single plugin
Parameters
Expand Down Expand Up @@ -198,14 +199,21 @@ def unload_plugin(name, category):
{'decoders': {}, 'encoders': {}, 'parsers': {}}
"""
_all_plugins[category].pop(name)
if category is not None:
_all_plugins[category].pop(name)
else:
for cat in _all_plugins:
if name in _all_plugins[cat]:
_all_plugins[cat].pop(name)


def load_plugin_classes(classes, category=None, overwrite=False):
""" load plugins from class objects
Parameters
----------
classes: list
list of classes
category : None or str
if str, apply for single plugin category
overwrite : bool
Expand Down Expand Up @@ -249,6 +257,53 @@ def load_plugin_classes(classes, category=None, overwrite=False):
return load_errors


@contextmanager
def plugins_context(classes, category=None):
""" context manager to load plugin class(es) then unload on exit
Parameters
----------
classes: list
list of classes
category : None or str
if str, apply for single plugin category
Examples
--------
>>> from pprint import pprint
>>> class DecoderPlugin(object):
... plugin_name = 'example'
... plugin_descript = 'a decoder for dicts containing _example_ key'
... dict_signature = ('_example_',)
...
>>> with plugins_context([DecoderPlugin]):
... pprint(view_plugins())
{'decoders': {'example': 'a decoder for dicts containing _example_ key'},
'encoders': {},
'parsers': {}}
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
"""
original = {cat: list(_all_plugins[cat].keys()) for cat in _all_plugins}
errors = load_plugin_classes(classes, category, overwrite=True)
# if errors:
# for cat in _all_plugins:
# for name, kls in list(_all_plugins[cat].items()):
# if name not in original[cat]:
# _all_plugins[cat].pop(name)
# raise RuntimeError("errors occurred while loading plugins: {}".format(errors))
yield
for cat in _all_plugins:
for name, kls in list(_all_plugins[cat].items()):
if name not in original[cat]:
_all_plugins[cat].pop(name)


def load_plugins_dir(path, category=None, overwrite=False):
""" load plugins from a directory
Expand Down Expand Up @@ -303,6 +358,7 @@ def load_builtin_plugins(category=None, overwrite=False):
Parameters
----------
name: None or str
category : None or str
if str, apply for single plugin category
Expand Down

0 comments on commit c54b3e2

Please sign in to comment.