Skip to content

chadlung/pynsive

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pynsive

Pronounced, "Pensive"

This is a simple plugin library that uses the sys.meta_path list along with custom finder and loader definitions to hook into the Python import process.

For more information on the import process hooks, please see:

Documentation

Examples

Creating and Using a Plugin Context

The plugin context is a nice way of managing what directories you've plugged into the sys.meta_path variable. Managers may be destroyed when no longer needed. Destroying a manager removes all directories that the manager plugged into from the sys.meta_path variable.

import pynsive

plugin_manager = pynsive.PluginManager()
plugin_manager.plug_into('/some/path')

try:
    import myplugins.module.plugin_a as plugin
    print('Imported plugin module: {1}', plugin)
finally:
    plugin_manager.destroy()

Note: The list functions in Pynsive will not descend into the submodules that may exist under the specified module. In order to recursively search use the rlist functions.

import pynsive
import test_module

plugin_manager = pynsive.PluginManager()
plugin_manager.plug_into('/some/path')

try:
    found_modules = pynsive.list_modules('ext.plugins')
    print('Discovered {1} modules.', len(found_modules))
finally:
    plugin_manager.destroy()

Note: The list functions in Pynsive will not descend into the submodules that may exist under the specified module. In order to recursively search use the rlist functions.

import pynsive
import test_module

plugin_manager = pynsive.PluginManager()
plugin_manager.plug_into('/some/path')

try:
    def subclasses_only(type_to_test):
        same = type_to_test is not test_module.MyClass
        is_subclass = issubclass(type_to_test, test_module.MyClass)
        return not same and is_subclass

    classes = pynsive.list_classes('ext.plugins', subclasses_only)
    print('Discovered {1} classes.', len(classes))
finally:
    plugin_manager.destroy()

Note: The rlist functions in Pynsive will descend into the submodules that may exist under the specified module. In order to perform a non-recursive listing use the list functions.

import pynsive
import test_module

plugin_manager = pynsive.PluginManager()
plugin_manager.plug_into('/some/path')

try:
    def subclasses_only(type_to_test):
        same = type_to_test is not test_module.MyClass
        is_subclass = issubclass(type_to_test, test_module.MyClass)
        return not same and is_subclass

    classes = pynsive.rlist_classes('ext.plugins', subclasses_only)
    print('Discovered {1} classes.', len(classes))
finally:
    plugin_manager.destroy()

##That Legal Thing...

This software library is released to you under the MIT License. See LICENSE for more information.

About

Python Reflections

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%