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)
elif o == "-a":
alias = a
elif o == "-s":
- search = True
+ search = "name"
+ elif o == "-S":
+ search = "match"
elif o == "-p":
if a == "":
profile = None
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)
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.
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
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