Made sys.path manipulation in load_plugin a bit more pythonic.

Removed 2nd "try" inside plugin.py and made first work as George suggested.
Reverted to "resource:" to make it feel correct.


git-svn-id: https://callirhoe.googlecode.com/svn/branches/phantome@181 81c8bb96-aa45-f2e2-0eef-c4fa4a15c6df
This commit is contained in:
ph4ntome@gmail.com 2014-10-31 16:51:54 +00:00
parent 3a50df8572
commit eac11c2455
3 changed files with 8 additions and 12 deletions

View File

@ -84,13 +84,12 @@ def import_plugin(plugin_paths, cat, longcat, longcat2, listopt, preset):
for path in plugin_paths: for path in plugin_paths:
found += available_files(path, cat, preset) found += available_files(path, cat, preset)
if len(found) == 0: raise IOError if len(found) == 0: raise IOError
if found[0][1] == "/": if found[0][1] == "resource:":
m = __import__("%s.%s" % (cat,preset), globals(), locals(), [ "*" ]) m = __import__("%s.%s" % (cat,preset), globals(), locals(), [ "*" ])
else: else:
old = sys.path[0] sys.path.insert(0, found[0][1])
sys.path[0] = found[0][1]
m = __import__("%s.%s" % (cat,preset), globals(), locals(), [ "*" ]) m = __import__("%s.%s" % (cat,preset), globals(), locals(), [ "*" ])
sys.path[0] = old sys.path.pop(0)
return m return m
except IOError: except IOError:
sys.exit("callirhoe: %s definition '%s' not found, use %s to see available definitions" % (longcat, sys.exit("callirhoe: %s definition '%s' not found, use %s to see available definitions" % (longcat,

View File

@ -29,7 +29,7 @@ import glob
try: try:
import resources import resources
except: except:
pass resources = None
def available_files(parent, dir, fmatch = None): def available_files(parent, dir, fmatch = None):
"""find parent/dir/*.py files to be used for plugins """find parent/dir/*.py files to be used for plugins
@ -43,7 +43,7 @@ def available_files(parent, dir, fmatch = None):
good = False good = False
res = [] res = []
pattern = parent + "/" + dir + "/*.py" pattern = parent + "/" + dir + "/*.py"
for x in glob.glob(pattern) if parent != "/" else resources.resource_list[dir]: for x in glob.glob(pattern) if not parent.startswith('resource:') else resources.resource_list[dir]:
basex = os.path.basename(x) basex = os.path.basename(x)
if basex == "__init__.py": good = True if basex == "__init__.py": good = True
elif basex.startswith('_'): elif basex.startswith('_'):
@ -78,9 +78,6 @@ def get_plugin_paths():
@rtype: [str,str,..] @rtype: [str,str,..]
""" """
result = [ os.path.expanduser("~/.callirhoe"), sys.path[0] if sys.path[0] else "." ] result = [ os.path.expanduser("~/.callirhoe"), sys.path[0] if sys.path[0] else "." ]
try: if resources:
temp = resources.resource_list result.append("resource:")
result.append("/")
except:
pass
return result return result

View File

@ -2,7 +2,7 @@
# Create Callirhoe package # Create Callirhoe package
mkdir _callirhoe mkdir _callirhoe
./make_resources_list > resources.py ./make_resources_list
cp -R geom holidays lang layouts lib style _callirhoe cp -R geom holidays lang layouts lib style _callirhoe
cp callirhoe.py _callirhoe/__main__.py cp callirhoe.py _callirhoe/__main__.py
mv resources.py _callirhoe/lib mv resources.py _callirhoe/lib