Changed some stuff to make it zip-package friendly

git-svn-id: https://callirhoe.googlecode.com/svn/branches/phantome@165 81c8bb96-aa45-f2e2-0eef-c4fa4a15c6df
This commit is contained in:
ph4ntome@gmail.com 2014-10-30 21:55:10 +00:00
parent 8e8026e1a9
commit ec02bd6bc0
2 changed files with 28 additions and 25 deletions

View File

@ -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])

View File

@ -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 "." ]