From 31ea855c3d572c75c6ad8737dda8df70502c76f9 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 12 May 2013 00:17:59 +0200 Subject: [PATCH] Chagned library importer interface. --- automanga | 8 ++++---- manga/lib.py | 27 +++++++++++++-------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/automanga b/automanga index e80db3f..39b86d7 100755 --- 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: diff --git a/manga/lib.py b/manga/lib.py index 3c83ab1..21edd3d 100644 --- a/manga/lib.py +++ b/manga/lib.py @@ -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] -- 2.11.0