3 import sys, getopt, pathlib
4 import manga.lib, manga.reader, manga.profile, manga.local
5 from gi.repository import Gtk, Gdk, GObject
8 out.write("usage: automanga [-h] [-p PROFILE] {DIRECTORY|-l LIBRARY ID|-a ALIAS}\n")
9 out.write(" automanga -a ALIAS=LIBRARY:ID\n")
10 out.write(" automanga -t {DIRECTORY|-l LIBRARY ID|-a ALIAS} [-]TAG...\n")
11 out.write(" automanga -{s|S} LIBRARY NAME\n")
12 out.write(" automanga -L TAG [-o ORDER]\n")
21 opts, args = getopt.getopt(sys.argv[1:], "hl:sSp:a:tL:o:")
47 profile = manga.profile.profile.last()
49 profile = manga.profile.profile.byname("default")
50 elif profile is not None:
51 profile = manga.profile.profile.byname(profile)
61 lib = manga.lib.findlib(args[0])
63 sys.stderr.write("automanga: no such library: %s\n" % args[0])
67 results = lib.byname(args[1])
68 elif search == "match":
69 results = lib.search(args[1])
70 if hasattr(results, "__length_hint__"):
71 sys.stderr.write("%i results:\n" % results.__length_hint__())
72 except NotImplementedError:
73 sys.stderr.write("automanga: %s does not support searching\n" % args[0])
76 sys.stdout.write("\"%s\": %s\n" % (mng.name, mng.id))
79 if listtag is not None:
80 if profile is not None:
82 for mprof in profile.bytag(listtag):
86 sys.stderr.write("%s %s: no longer found\n" % (mprof.libnm, mprof.id))
88 except Exception as exc:
89 sys.stderr.write("%s %s: unexpected error: %s\n" % (mprof.libnm, mprof.id, str(exc)))
91 line = "%s %s: \"%s\"" % (mprof.libnm, mprof.id, mng.name)
93 sys.stdout.write("%s\n" % (line,))
95 if listorder == "mtime":
98 sys.stderr.write("automanga: undefined order: %s\n" % (listorder,))
100 results.append((line, key))
102 results.sort(key=lambda o: o[1])
103 for line, key in results:
104 sys.stdout.write("%s\n" % (line,))
107 if alias and (alias.find('=') > 0):
108 tgt, nm = alias.split('=', 1)
112 libname, mid = nm.split(':', 1)
114 lib = manga.lib.findlib(libname)
116 sys.stderr.write("automanga: no such library: %s\n" % libname)
120 except KeyError as e:
121 sys.stderr.write("automanga: no such manga: %s\n" % str(e))
123 profile.setalias(tgt, libname, mng.id)
128 libname, mid = profile.getalias(alias)
130 sys.stderr.write("automanga: no such alias: %s\n" % alias)
133 lib = manga.lib.findlib(libname)
135 sys.stderr.write("automanga: no such library: %s\n" % libname)
140 sys.stderr.write("automanga: manga no longer found: %s\n" % mid)
142 elif libname is not None:
147 lib = manga.lib.findlib(libname)
149 sys.stderr.write("automanga: no such library: %s\n" % libname)
152 mng = lib.byid(args.pop(0))
153 except KeyError as e:
154 sys.stderr.write("automanga: no such manga: %s\n" % str(e))
163 mng = manga.local.manga(pathlib.Path(mdir))
165 sys.stderr.write("automanga: not a valid manga directory: %s\n" % mdir)
168 mprof = None if profile is None else profile.getmanga(libname, mng.id, True)
169 if profile is not None:
173 if mprof is not None:
174 ntags = [tag for tag in args if tag[:1] != "-"]
175 rtags = [tag[1:] for tag in args if tag[:1] == "-"]
176 if ntags: mprof.tags.add(*ntags)
177 if rtags: mprof.tags.remove(*rtags)
180 reader = manga.reader.reader(mng, mprof)
186 except KeyboardInterrupt: