From eac11c245556b44b8c88fe46872c79a444c47e56 Mon Sep 17 00:00:00 2001 From: "ph4ntome@gmail.com" Date: Fri, 31 Oct 2014 16:51:54 +0000 Subject: [PATCH] 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 --- callirhoe.py | 7 +++---- lib/plugin.py | 11 ++++------- make_pkg | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/callirhoe.py b/callirhoe.py index f6fcf1c..9ad58bb 100755 --- a/callirhoe.py +++ b/callirhoe.py @@ -84,13 +84,12 @@ def import_plugin(plugin_paths, cat, longcat, longcat2, listopt, preset): for path in plugin_paths: found += available_files(path, cat, preset) if len(found) == 0: raise IOError - if found[0][1] == "/": + if found[0][1] == "resource:": m = __import__("%s.%s" % (cat,preset), globals(), locals(), [ "*" ]) else: - old = sys.path[0] - sys.path[0] = found[0][1] + sys.path.insert(0, found[0][1]) m = __import__("%s.%s" % (cat,preset), globals(), locals(), [ "*" ]) - sys.path[0] = old + sys.path.pop(0) return m except IOError: sys.exit("callirhoe: %s definition '%s' not found, use %s to see available definitions" % (longcat, diff --git a/lib/plugin.py b/lib/plugin.py index 1f287c7..76768c2 100644 --- a/lib/plugin.py +++ b/lib/plugin.py @@ -29,7 +29,7 @@ import glob try: import resources except: - pass + resources = None def available_files(parent, dir, fmatch = None): """find parent/dir/*.py files to be used for plugins @@ -43,7 +43,7 @@ def available_files(parent, dir, fmatch = None): good = False res = [] 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) if basex == "__init__.py": good = True elif basex.startswith('_'): @@ -78,9 +78,6 @@ def get_plugin_paths(): @rtype: [str,str,..] """ result = [ os.path.expanduser("~/.callirhoe"), sys.path[0] if sys.path[0] else "." ] - try: - temp = resources.resource_list - result.append("/") - except: - pass + if resources: + result.append("resource:") return result diff --git a/make_pkg b/make_pkg index 49a29f3..9700886 100755 --- a/make_pkg +++ b/make_pkg @@ -2,7 +2,7 @@ # Create Callirhoe package mkdir _callirhoe -./make_resources_list > resources.py +./make_resources_list cp -R geom holidays lang layouts lib style _callirhoe cp callirhoe.py _callirhoe/__main__.py mv resources.py _callirhoe/lib