Commit | Line | Data |
---|---|---|
3cc7937c | 1 | #!/usr/bin/python3 |
e26514bb | 2 | |
f72650a7 | 3 | import sys, getopt, pathlib |
012c4cae | 4 | import manga.lib, manga.reader, manga.profile, manga.local |
9f1721f1 | 5 | from gi.repository import Gtk, Gdk, GObject |
e26514bb | 6 | |
e26514bb | 7 | def usage(out): |
271d68da FT |
8 | out.write("usage: automanga [-h] [-p PROFILE] {DIRECTORY|-l LIBRARY ID|-a ALIAS}\n") |
9 | out.write(" automanga -a ALIAS=LIBRARY:ID\n") | |
5c000565 | 10 | out.write(" automanga -t {DIRECTORY|-l LIBRARY ID|-a ALIAS} [-]TAG...\n") |
ffd12e71 | 11 | out.write(" automanga -{s|S} LIBRARY NAME\n") |
5c000565 | 12 | out.write(" automanga -L TAG\n") |
e26514bb FT |
13 | |
14 | libname = None | |
ffd12e71 | 15 | search = None |
012c4cae | 16 | profile = "" |
271d68da | 17 | alias = None |
5c000565 FT |
18 | settags = False |
19 | listtag = None | |
20 | opts, args = getopt.getopt(sys.argv[1:], "hl:sSp:a:tL:") | |
e26514bb FT |
21 | for o, a in opts: |
22 | if o == "-h": | |
23 | usage(sys.stdout) | |
24 | sys.exit(0) | |
25 | elif o == "-l": | |
26 | libname = a | |
271d68da FT |
27 | elif o == "-a": |
28 | alias = a | |
e26514bb | 29 | elif o == "-s": |
ffd12e71 FT |
30 | search = "name" |
31 | elif o == "-S": | |
32 | search = "match" | |
012c4cae FT |
33 | elif o == "-p": |
34 | if a == "": | |
35 | profile = None | |
36 | else: | |
37 | profile = a | |
5c000565 FT |
38 | elif o == "-t": |
39 | settags = True | |
40 | elif o == "-L": | |
41 | listtag = a | |
012c4cae FT |
42 | if profile == "": |
43 | try: | |
44 | profile = manga.profile.profile.last() | |
45 | except KeyError: | |
46 | profile = manga.profile.profile.byname("default") | |
47 | elif profile is not None: | |
48 | profile = manga.profile.profile.byname(profile) | |
e26514bb | 49 | |
299d3f50 | 50 | def main(): |
41a034f6 FT |
51 | global libname |
52 | ||
299d3f50 FT |
53 | if search: |
54 | if len(args) < 2: | |
55 | usage(sys.stderr) | |
56 | sys.exit(1) | |
57 | try: | |
58 | lib = manga.lib.findlib(args[0]) | |
59 | except ImportError: | |
60 | sys.stderr.write("automanga: no such library: %s\n" % args[0]) | |
61 | sys.exit(1) | |
62 | try: | |
63 | if search == "name": | |
64 | results = lib.byname(args[1]) | |
65 | elif search == "match": | |
66 | results = lib.search(args[1]) | |
67 | if hasattr(results, "__length_hint__"): | |
68 | sys.stderr.write("%i results:\n" % results.__length_hint__()) | |
69 | except NotImplementedError: | |
70 | sys.stderr.write("automanga: %s does not support searching\n" % args[0]) | |
71 | sys.exit(1) | |
72 | for mng in results: | |
73 | sys.stdout.write("\"%s\": %s\n" % (mng.name, mng.id)) | |
74 | return | |
e26514bb | 75 | |
5c000565 FT |
76 | if listtag is not None: |
77 | if profile is not None: | |
78 | for mprof in profile.bytag(listtag): | |
1e28222b FT |
79 | try: |
80 | mng = mprof.open() | |
81 | except KeyError: | |
82 | sys.stderr.write("%s %s: no longer found\n" % (mprof.libnm, mprof.id)) | |
83 | continue | |
5c000565 FT |
84 | sys.stdout.write("%s %s: \"%s\"\n" % (mprof.libnm, mprof.id, mng.name)) |
85 | return | |
86 | ||
299d3f50 | 87 | if alias and (alias.find('=') > 0): |
41a034f6 | 88 | tgt, nm = alias.split('=', 1) |
299d3f50 FT |
89 | if not ':' in nm: |
90 | usage(sys.stderr) | |
91 | sys.exit(1) | |
92 | libname, mid = nm.split(':', 1) | |
93 | try: | |
94 | lib = manga.lib.findlib(libname) | |
95 | except ImportError: | |
96 | sys.stderr.write("automanga: no such library: %s\n" % libname) | |
97 | sys.exit(1) | |
98 | try: | |
99 | mng = lib.byid(mid) | |
d39faa3f FT |
100 | except KeyError as e: |
101 | sys.stderr.write("automanga: no such manga: %s\n" % str(e)) | |
299d3f50 | 102 | sys.exit(1) |
41a034f6 | 103 | profile.setalias(tgt, libname, mng.id) |
299d3f50 | 104 | return |
271d68da | 105 | |
299d3f50 FT |
106 | if alias: |
107 | try: | |
108 | libname, mid = profile.getalias(alias) | |
109 | except KeyError: | |
43bddc57 | 110 | sys.stderr.write("automanga: no such alias: %s\n" % alias) |
299d3f50 FT |
111 | sys.exit(1) |
112 | try: | |
113 | lib = manga.lib.findlib(libname) | |
114 | except ImportError: | |
115 | sys.stderr.write("automanga: no such library: %s\n" % libname) | |
116 | sys.exit(1) | |
117 | try: | |
118 | mng = lib.byid(mid) | |
119 | except KeyError: | |
120 | sys.stderr.write("automanga: manga no longer found: %s\n" % mid) | |
121 | sys.exit(1) | |
122 | elif libname is not None: | |
123 | if len(args) < 1: | |
124 | usage(sys.stderr) | |
125 | sys.exit(1) | |
126 | try: | |
127 | lib = manga.lib.findlib(libname) | |
128 | except ImportError: | |
129 | sys.stderr.write("automanga: no such library: %s\n" % libname) | |
130 | sys.exit(1) | |
131 | try: | |
5c000565 | 132 | mng = lib.byid(args.pop(0)) |
d39faa3f FT |
133 | except KeyError as e: |
134 | sys.stderr.write("automanga: no such manga: %s\n" % str(e)) | |
299d3f50 FT |
135 | sys.exit(1) |
136 | else: | |
137 | libname = "local" | |
138 | try: | |
9bdfc968 FT |
139 | if len(args) < 1: |
140 | usage(sys.stderr) | |
141 | sys.exit(1) | |
142 | mdir = args.pop(0) | |
f72650a7 | 143 | mng = manga.local.manga(pathlib.Path(mdir)) |
299d3f50 FT |
144 | except TypeError: |
145 | sys.stderr.write("automanga: not a valid manga directory: %s\n" % mdir) | |
146 | sys.exit(1) | |
147 | ||
148 | mprof = None if profile is None else profile.getmanga(libname, mng.id, True) | |
149 | if profile is not None: | |
150 | profile.setlast() | |
e26514bb | 151 | |
5c000565 FT |
152 | if settags: |
153 | if mprof is not None: | |
154 | ntags = [tag for tag in args if tag[:1] != "-"] | |
155 | rtags = [tag[1:] for tag in args if tag[:1] == "-"] | |
5c000565 FT |
156 | if ntags: mprof.tags.add(*ntags) |
157 | if rtags: mprof.tags.remove(*rtags) | |
158 | return | |
159 | ||
299d3f50 FT |
160 | reader = manga.reader.reader(mng, mprof) |
161 | reader.show() | |
9f1721f1 | 162 | Gtk.main() |
012c4cae | 163 | |
299d3f50 FT |
164 | try: |
165 | main() | |
166 | except KeyboardInterrupt: | |
167 | pass |