Issue #77 resolved

Allow dynamic loading of libraries

tobes_
created an issue

It is nice if applications can load libraries dynamically this patch adds this ability. Does this seem to be the sort of thing wanted?

patch attached as file

{{{ diff -r 4fa0fe482eaa fanstatic/init.py --- a/fanstatic/init.py Sat May 05 18:17:04 2012 +0100 +++ b/fanstatic/init.py Tue May 08 17:11:18 2012 +0100 @@ -25,7 +25,8 @@ set_resource_file_existence_checking, UnknownResourceError)

-from fanstatic.registry import get_library_registry, LibraryRegistry +from fanstatic.registry import (get_library_registry, LibraryRegistry, + add_library)

from fanstatic.codegen import generate_code

diff -r 4fa0fe482eaa fanstatic/registry.py --- a/fanstatic/registry.py Sat May 05 18:17:04 2012 +0100 +++ b/fanstatic/registry.py Tue May 08 17:11:18 2012 +0100 @@ -1,4 +1,5 @@ import pkg_resources +from core import Library

ENTRY_POINT = 'fanstatic.libraries'

@@ -26,9 +27,19 @@ """ self[library.name] = library

+_added_libraries = [] + +def add_library(library): + ''' used to add libraries dynamically ''' + assert isinstance(library, Library) + _added_libraries.append(library) + # force libraries to be reloaded + global _library_registry + _library_registry = None +

def get_libraries_from_entry_points(): - libraries = [] + libraries = _added_libraries[:] for entry_point in pkg_resources.iter_entry_points(ENTRY_POINT): library = entry_point.load() # If the distribution is in development mode we don't use its version. }}}

Comments (4)

  1. Jan-Jaap Driessen

    You can dynamically add libraries to the registry like this:

    library = fanstatic.Library(...)
    registry = fanstatic.get_library_registry()
    registry.add(library)
    

    Would that work in your situation? This way, you don't need to force libraries to be reloaded.

  2. tobes_ reporter

    Sorry I've not been able to test this yet, it looks like it would meet my needs. I'll try to add it to my code and report back by the end of this week.

    It saves adding extra code too.

  3. tobes_ reporter

    Sorry for the delay in getting back on this.

    It works perfectly for my needs thanks - I'm happy for this ticket to be closed

  4. Log in to comment