pyblish / pyblish-base

Pyblish base library - see https://github.com/pyblish/pyblish for details.
Other
127 stars 59 forks source link

Implemented #343: Custom logic for filtering discovered plugins #344

Closed antirotor closed 5 years ago

antirotor commented 5 years ago

This implements #343

Adding ability to filter plugins and modify plugins during discovery. This is using same register_* logic as already existing api.

Adding filter function:

pyblish.api.register_discovery_filter(my_filter)

Filter function must return tuple (plugin, filtered) -> (class, bool) Plugin is plugin class processed by filter function, filtered is bool indicating wheter plugin should be filtered out or not.

So simple filter function can look like:

def my_filter_function(plugin):
    if plugin.__name__ == 'FilteredPlugin':
        return plugin, True
   return plugin, False

This can also modify plugin:

def my_filter_function(plugin):
    if plugin.__name__ == 'ModifiedPlugin':
        plugin.optional = False
   return plugin, False

This PR adds:

register_discovery_filter,
deregister_discovery_filter,
deregister_all_discovery_filters,
registered_discovery_filters
mottosso commented 5 years ago

Whop, you are fast. Commented here.

Let's continue implementation-specifics here.

antirotor commented 5 years ago

So now basic filter example looks like this:

def my_plugin_filter(plugins):
        for name, plugin in list(plugins.items()):
            if plugin.__name__ == "MyFilteredPlugin":
                del plugins[name]

        pass

Is like this because in python 3, you cannot delete from dictionary while iterating over it.

mottosso commented 5 years ago

@antirotor Thanks for this, would you mind if I made a few tweaks to your implementation?

mottosso commented 5 years ago

Let me know what you think about these changes, and whether they still apply to your usecase.

antirotor commented 5 years ago

Seems ok for me, looks cleaner. I have no problem with those changes.

mottosso commented 5 years ago

Great, then we're all set. :) Thanks @antirotor

mottosso commented 5 years ago

Released as 1.8.0