From ffd12e718bf1915abde0455fdf05a34d0632ed4d Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Tue, 23 Jul 2013 21:47:34 +0200 Subject: [PATCH] Added simplistic keyword searching to mrnet and local libraries. --- automanga | 20 +++++++++++++++----- manga/lib.py | 13 +++++++++++++ manga/local.py | 5 +++++ manga/mrnet.py | 8 ++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/automanga b/automanga index fd9ce02..53f8de6 100755 --- a/automanga +++ b/automanga @@ -10,13 +10,13 @@ gtk.gdk.threads_init() def usage(out): 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") + out.write(" automanga -{s|S} LIBRARY NAME\n") libname = None -search = False +search = None profile = "" alias = None -opts, args = getopt.getopt(sys.argv[1:], "hl:sp:a:") +opts, args = getopt.getopt(sys.argv[1:], "hl:sSp:a:") for o, a in opts: if o == "-h": usage(sys.stdout) @@ -26,7 +26,9 @@ for o, a in opts: elif o == "-a": alias = a elif o == "-s": - search = True + search = "name" + elif o == "-S": + search = "match" elif o == "-p": if a == "": profile = None @@ -49,7 +51,15 @@ if search: except ImportError: sys.stderr.write("automanga: no such library: %s\n" % args[0]) sys.exit(1) - for manga in lib.byname(args[1]): + try: + if search == "name": + results = lib.byname(args[1]) + elif search == "match": + results = lib.search(args[1]) + except NotImplementedError: + sys.stderr.write("automanga: %s does not support searching\n" % args[0]) + sys.exit(1) + for manga in results: sys.stdout.write("\"%s\": %s\n" % (manga.name, manga.id)) sys.exit(0) diff --git a/manga/lib.py b/manga/lib.py index 21edd3d..353d7ad 100644 --- a/manga/lib.py +++ b/manga/lib.py @@ -9,6 +9,19 @@ class library(object): All libraries should implement this.""" raise NotImplementedError() + def search(self, string): + """Returns an iterable object of mangas in this library that + matches the search string in a library-dependent manner. While + each library is at liberty to define its own matching + criteria, it is probably likely to involve something akin to + searching for keywords in the titles of the library. + + Searching may return very many results and may be slow to + iterate. + + Not all libraries need implement this.""" + raise NotImplementedError() + def byid(self, id): """Returns a previously known manga by its string ID, or raises KeyError if no such manga could be found. diff --git a/manga/local.py b/manga/local.py index fe699a4..2d79b20 100644 --- a/manga/local.py +++ b/manga/local.py @@ -185,8 +185,13 @@ class directory(dumb): ret.append(manga(pj(self.path, dent))) return ret + def search(self, expr): + expr = expr.lower() + return [manga(pj(self.path, dent)) for dent in os.listdir(self.path) if expr in dent.lower()] + def __iter__(self): for dent in os.listdir(self.path): yield manga(pj(self.path, dent)) + library = dumb diff --git a/manga/mrnet.py b/manga/mrnet.py index c509755..48338a3 100644 --- a/manga/mrnet.py +++ b/manga/mrnet.py @@ -164,3 +164,11 @@ class library(lib.library): for manga in self: if manga.name.lower()[:len(prefix)] == prefix: yield manga + + def search(self, expr): + if not isinstance(expr, unicode): + expr = expr.decode("utf8") + expr = expr.lower() + for manga in self: + if expr in manga.name.lower(): + yield manga -- 2.11.0