diff --git a/callirhoe.py b/callirhoe.py index 735b8c1..3c6ea0d 100755 --- a/callirhoe.py +++ b/callirhoe.py @@ -21,7 +21,6 @@ # TODO: - # fix auto-measure rendering (cairo) # fix plugin loading (without global vars) # week markers selectable @@ -63,7 +62,7 @@ class Abort(Exception): from lib.plugin import * # TODO: SEE IF IT CAN BE MOVED INTO lib.plugin ... -def import_plugin(plugin_paths, cat, longcat, longcat2, listopt, preset): +def import_plugin(plugin_path, cat, longcat, longcat2, listopt, preset): """import a plugin making it visible I{Example:} @@ -81,12 +80,9 @@ def import_plugin(plugin_paths, cat, longcat, longcat2, listopt, preset): @note: Aimed for internal use with I{lang}, I{style}, I{geom}, I{layouts}. """ try: - found = available_files(plugin_paths[0], cat, preset) + available_files(plugin_paths[1], cat, preset) + found = available_files(plugin_path, cat, preset) if len(found) == 0: raise IOError - old = sys.path[0]; - sys.path[0] = found[0][1] m = __import__("%s.%s" % (cat,preset), globals(), locals(), [ "*" ]) - sys.path[0] = old return m except IOError: sys.exit("callirhoe: %s definition '%s' not found, use %s to see available definitions" % (longcat, @@ -312,11 +308,11 @@ def main_program(): list_and_exit = True if list_and_exit: return - plugin_paths = get_plugin_paths() - Language = import_plugin(plugin_paths, "lang", "language", "languages", "--list-languages", options.lang) - Style = import_plugin(plugin_paths, "style", "style", "styles", "--list-styles", options.style) - Geometry = import_plugin(plugin_paths, "geom", "geometry", "geometries", "--list-geometries", options.geom) - Layout = import_plugin(plugin_paths, "layouts", "layout", "layouts", "--list-layouts", options.layout) + plugin_path = "/" + Language = import_plugin(plugin_path, "lang", "language", "languages", "--list-languages", options.lang) + Style = import_plugin(plugin_path, "style", "style", "styles", "--list-styles", options.style) + Geometry = import_plugin(plugin_path, "geom", "geometry", "geometries", "--list-geometries", options.geom) + Layout = import_plugin(plugin_path, "layouts", "layout", "layouts", "--list-layouts", options.layout) for x in argv2: if '=' in x: x = x[0:x.find('=')] @@ -401,6 +397,8 @@ def main_program(): if __name__ == "__main__": try: +# import pkg_resources +# print pkg_resources.resource_listdir(__name__,"/") main_program() except Abort as e: sys.exit(e.args[0]) diff --git a/lib/plugin.py b/lib/plugin.py index 911cc98..db58a13 100644 --- a/lib/plugin.py +++ b/lib/plugin.py @@ -24,7 +24,21 @@ import sys import os.path -import glob +import pkg_resources + +def glob(path, pattern): + tmplist = pkg_resources.resource_listdir("__main__", "/"+path) + filelist = [] + for i in tmplist: + if i == "": + continue + if pkg_resources.resource_isdir("__main__", "/"+path+"/"+i): + filelist.extend(glob(path+"/"+i, pattern)) + else: + if i.lower()[-3:] == pattern.lower(): + name = path+"/"+i + filelist.append(name) + return filelist def available_files(parent, dir, fmatch = None): """find parent/dir/*.py files to be used for plugins @@ -37,17 +51,16 @@ def available_files(parent, dir, fmatch = None): """ good = False res = [] - pattern = parent + "/" + dir + "/*.py" - for x in glob.glob(pattern): + for x in glob(dir,".py"): basex = os.path.basename(x) if basex == "__init__.py": good = True elif basex.startswith('_'): # ignore files aimed for internal use # safer than [a-z]-style matching... continue - else: + else: base = os.path.splitext(basex)[0] - if base and ((not fmatch) or (fmatch == base)): res.append((base,parent)) + if base and ((not fmatch) or (fmatch == base)): res.append((base,"/"+dir+parent)) return res if good else [] def plugin_list(cat): @@ -55,18 +68,10 @@ def plugin_list(cat): @rtype: [str,...] """ - plugin_paths = get_plugin_paths() - return available_files(plugin_paths[0], cat) + available_files(plugin_paths[1], cat) + return available_files("../", cat) # cat = lang (category) # longcat = language # longcat2 = languages # listopt = --list-lang # preset = "EN" - -def get_plugin_paths(): - """return the plugin search paths - - @rtype: [str,str] - """ - return [ os.path.expanduser("~/.callirhoe"), sys.path[0] if sys.path[0] else "." ]