Tiny Python Reflections and Plugin Library
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs
pynsive
tests
tools
.gitignore
LICENSE
MANIFEST.in
README.md
README.rst
VERSION
setup.py
tox.ini

README.md

Build Status

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()

Discovering Python Modules

Pynsive allows you to search a given directory tree for potential module names to aid in discovery of interesting code. These functions will search any directories found under the directory specified for python modules and will recurse as specified by their names.

import pynsive

# Non-recursive search
found_modules = pynsive.discover_modules('/some/path')
print('Discovered {1} modules.', len(found_modules))

# Recursive search
found_modules = pynsive.rdiscover_modules('/some/path')
print('Discovered {1} modules.', len(found_modules))

Dynamically Listing Submodules

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()

Dynamically Finding Classes in a Module

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()

Dynamically Finding Classes in a Module and its Submodules

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.