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