From 271d68da6cb2a273fd3ce29e95c4a021cc6da125 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 12 May 2013 17:12:20 +0200 Subject: [PATCH] Added manga aliases. --- automanga | 47 +++++++++++++++++++++++++++++++++++++++++++---- manga/profile.py | 24 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/automanga b/automanga index 36bd1cf..81d602d 100755 --- a/automanga +++ b/automanga @@ -8,19 +8,23 @@ gobject.threads_init() gtk.gdk.threads_init() def usage(out): - out.write("usage: automanga [-h] [-p PROFILE] [-l LIBRARY] [DIR-OR-ID]\n") - out.write(" automanga [-h] -s LIBRARY NAME\n") + out.write("usage: automanga [-h] [-p PROFILE] {DIRECTORY|-l LIBRARY ID|-a ALIAS}\n") + out.write(" automanga -a ALIAS=LIBRARY:ID\n") + out.write(" automanga -s LIBRARY NAME\n") libname = None search = False profile = "" -opts, args = getopt.getopt(sys.argv[1:], "hl:sp:") +alias = None +opts, args = getopt.getopt(sys.argv[1:], "hl:sp:a:") for o, a in opts: if o == "-h": usage(sys.stdout) sys.exit(0) elif o == "-l": libname = a + elif o == "-a": + alias = a elif o == "-s": search = True elif o == "-p": @@ -49,7 +53,42 @@ if search: sys.stdout.write("\"%s\": %s\n" % (manga.name, manga.id)) sys.exit(0) -if libname is not None: +if alias and (alias.find('=') > 0): + alias, nm = alias.split('=', 1) + if not ':' in nm: + usage(sys.stderr) + sys.exit(1) + libname, mid = nm.split(':', 1) + try: + lib = manga.lib.findlib(libname) + except ImportError: + sys.stderr.write("automanga: no such library: %s\n" % libname) + sys.exit(1) + try: + mng = lib.byid(mid) + except KeyError: + sys.stderr.write("automanga: no such manga: %s\n" % mid) + sys.exit(1) + profile.setalias(alias, libname, mid) + sys.exit(0) + +if alias: + try: + libname, mid = profile.getalias(alias) + except KeyError: + sys.stderr("automanga: no such alias: %s\n" % alias) + sys.exit(1) + try: + lib = manga.lib.findlib(libname) + except ImportError: + sys.stderr.write("automanga: no such library: %s\n" % libname) + sys.exit(1) + try: + mng = lib.byid(mid) + except KeyError: + sys.stderr.write("automanga: manga no longer found: %s\n" % mid) + sys.exit(1) +elif libname is not None: if len(args) < 1: usage(sys.stderr) sys.exit(1) diff --git a/manga/profile.py b/manga/profile.py index 87f02a9..2ff6be5 100644 --- a/manga/profile.py +++ b/manga/profile.py @@ -182,6 +182,30 @@ class profile(object): with openwdir(pj(basedir, "last"), "w") as f: f.write(self.name + "\n") + def getaliases(self): + ret = {} + if os.path.exists(pj(self.dir, "alias")): + with openwdir(pj(self.dir, "alias")) as f: + for ln in f: + ln = splitline(ln) + if len(ln) < 1: continue + if ln[0] == "alias" and len(ln) > 3: + ret[ln[1]] = ln[2], ln[3] + return ret + + def savealiases(self, map): + with openwdir(pj(self.dir, "alias"), "w") as f: + for nm, (libnm, id) in map.iteritems(): + f.write(consline("alias", nm, libnm, id) + "\n") + + def getalias(self, nm): + return self.getaliases()[nm] + + def setalias(self, nm, libnm, id): + aliases = self.getaliases() + aliases[nm] = libnm, id + self.savealiases(aliases) + @classmethod def byname(cls, name): if not name or name == "last" or name[0] == '.': -- 2.11.0