Load modules from .zip file.

This commit is contained in:
Bastian Kleineidam 2012-12-12 23:22:36 +01:00
parent cfeba7a49f
commit fcbace28b4

View file

@ -4,17 +4,34 @@
Functions to load plugin modules.
"""
import os
import sys
import zipfile
import importlib
def is_frozen ():
"""Return True if running inside a py2exe-generated executable."""
return hasattr(sys, "frozen")
def get_modules(folder='plugins'):
"""Find all valid modules in the plugins subdirectory. A valid module
must have a .py extension, and is importable.
@return: all loaded valid modules
@rtype: iterator of module
"""
dirname = os.path.join(os.path.dirname(__file__), folder)
for modname in get_importable_modules(dirname):
if is_frozen():
# find modules in library.zip filename
zipname = os.path.dirname(os.path.dirname(__file__))
with zipfile.ZipFile(zipname, 'r') as f:
modnames = [os.path.splitext(n[17:])[0]
for n in f.namelist()
if n.startswith("dosagelib/%s" % folder)
and "__init__" not in n]
else:
dirname = os.path.join(os.path.dirname(__file__), folder)
modnames = get_importable_modules(dirname)
for modname in modnames:
try:
name ="..%s.%s" % (folder, modname)
yield importlib.import_module(name, __name__)