Chagned library importer interface.
authorFredrik Tolf <fredrik@dolda2000.com>
Sat, 11 May 2013 22:17:59 +0000 (00:17 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Sat, 11 May 2013 22:17:59 +0000 (00:17 +0200)
automanga
manga/lib.py

index e80db3f..39b86d7 100755 (executable)
--- a/automanga
+++ b/automanga
@@ -29,8 +29,8 @@ if search:
         usage(sys.stderr)
         sys.exit(1)
     try:
-        lib = manga.lib.libraries[args[0]]
-    except KeyError:
+        lib = manga.lib.findlib(args[0])
+    except ImportError:
         sys.stderr.write("automanga: no such library: %s\n" % args[0])
         sys.exit(1)
     for manga in lib.byname(args[1]):
@@ -42,8 +42,8 @@ if libname is not None:
         usage(sys.stderr)
         sys.exit(1)
     try:
-        lib = manga.lib.libraries[libname]
-    except KeyError:
+        lib = manga.lib.findlib(libname)
+    except ImportError:
         sys.stderr.write("automanga: no such library: %s\n" % libname)
         sys.exit(1)
     try:
index 3c83ab1..21edd3d 100644 (file)
@@ -172,17 +172,16 @@ class cursor(object):
     def __iter__(self):
         return self
 
-def _lazymod(name):
-    return __import__(name, fromlist=["dummy"])
-class _lazydict(object):
-    def __init__(self):
-        self.bk = {}
-    def __setitem__(self, key, val):
-        self.bk[key] = "u", val
-    def __getitem__(self, key):
-        st, v = self.bk[key]
-        if st == "u":
-            v = self.bk[key] = v()
-        return v
-libraries = _lazydict()
-libraries["mf"] = lambda: _lazymod("manga.mangafox").library()
+loaded = {}
+def findlib(name):
+    def load(name):
+        mod = __import__(name, fromlist=["dummy"])
+        if not hasattr(mod, "library"):
+            raise ImportError("module " + name + " is not a manga library")
+        return mod.library()
+    if name not in loaded:
+        try:
+            loaded[name] = load("manga." + name)
+        except ImportError:
+            loaded[name] = load(name)
+    return loaded[name]