forked from kfix/calibre_plugin_djvumaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
403 lines (357 loc) · 17.5 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
utils module for Calibre plugin djvumaker - CLI setup, pdf2djvu installation, and utility methods
References:
(#NODOC)
--- CLI ---
create_cli_parser(self_DJVUmaker, PLUGINNAME, PLUGINVER_DOT, REGISTERED_BACKENDS_KEYS)
ask_yesno_input(question, prints=print)
printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█',
prints=print)
--- pdf2djvu installation ---
version_str_to_intlist(verstr)
version_intlist_to_str(verintlist)
version_from_output(output)
check_version_executable(executable_path)
create_backend_link(backend_name, version)
install_pdf2djvu(PLUGINNAME, preferences, log=print)
get_url_basename(url)
download_pdf2djvu(web_version, log)
unpack_zip_or_tar(PLUGINNAME, fpath, log)
--- utility functions ---
EmptyClass()
empty_function(*args, **kwargs)
add_method_dec(method, method_name)
discover_backend(backend_name, preferences, folder)
"""
from __future__ import unicode_literals, division, absolute_import, print_function
import argparse
import os
import urllib
import urllib2
import urlparse
import subprocess
from calibre.constants import isosx, iswindows, islinux, isbsd
from calibre.utils.config import config_dir
def create_cli_parser(self_DJVUmaker, PLUGINNAME, PLUGINVER_DOT, REGISTERED_BACKENDS_KEYS):
"""Creates CLI for plugin."""
# TODO: add message before and after printing help
# TODO: add information in cli about current installation and setting and overriding by
# customization help
parser = argparse.ArgumentParser(prog="calibre-debug -r {} -- ".format(PLUGINNAME))
parser.add_argument('-V', '--version', action='version', version='v{}'.format(PLUGINVER_DOT),
help="show plugin's version number and exit")
subparsers = parser.add_subparsers(metavar='command')
# DEBUG COMMENT
# parser_test = subparsers.add_parser('test')
# parser_test.set_defaults(func=self_DJVUmaker.cli_test)
parser_backend = subparsers.add_parser('backend', help='Backends handling. See '
'`{}backend --help`'.format(parser.prog))
parser_backend.set_defaults(func=self_DJVUmaker.cli_backend)
parser_backend.add_argument('command', choices=['install', 'set'],
help='installs or sets backend')
parser_backend.add_argument('backend', choices=REGISTERED_BACKENDS_KEYS,
help='choosed backend', nargs="?")
parser_convert = subparsers.add_parser('convert', help='Convert file to djvu')
parser_convert.set_defaults(func=self_DJVUmaker.cli_convert)
group_convert = parser_convert.add_mutually_exclusive_group(required=True)
group_convert.add_argument('-p', "--path",
help="convert file under PATH to djvu using default settings",
action="store", type=str)
group_convert.add_argument('-i', "--id",
help="convert file with ID to djvu using default settings",
action="store", type=int)
group_convert.add_argument("--all", help=("convert all pdf files in calibre's library, you have to"
" turn on postimport conversion first (`calibre-debug -r"
" djvumaker -- postimport -y`)"),
action="store_true")
parser_postimport = subparsers.add_parser('postimport', help='change postimport settings')
parser_postimport.set_defaults(func=self_DJVUmaker.cli_set_postimport)
group_postimport = parser_postimport.add_mutually_exclusive_group(required=False)
group_postimport.add_argument('-y', "--yes",
help=("sets plugin to convert PDF files after import"
" (sometimes do not work for pdf2djvu)"),
action="store_true")
group_postimport.add_argument('-n', "--no",
help="sets plugin to do not convert PDF files after import (default)",
action="store_true")
parser_install_deps = subparsers.add_parser('install_deps',
help='(depreciated) alias for `{}backend install djvudigital`'.format(parser.prog))
parser_install_deps.set_defaults(func=self_DJVUmaker.cli_backend, command='install',
backend='djvudigital')
parser_convert_all = subparsers.add_parser('convert_all',
help='(depreciated) alias for `{}convert --all`'.format(parser.prog))
parser_convert_all.set_defaults(func=self_DJVUmaker.cli_convert, all=True)
return parser
def version_str_to_intlist(verstr):
"""Conversion from 'x.y.z' to [x, y, z] version format."""
return [int(x) for x in verstr.split('.')]
def version_intlist_to_str(verintlist):
"""Conversion from [x, y, z] to 'x.y.z' version format."""
return '.'.join(map(str,verintlist))
def version_from_output(output):
"""Extracts version number from typical pdf2djvu --version output."""
return output.splitlines()[0].split()[1]
def check_version_executable(executable_path):
#NODOC
return version_from_output(subprocess.check_output([executable_path, '--version'],
stderr= subprocess.STDOUT))
def discover_backend(backend_name, preferences, folder):
"""
Discovers backend locations and versions. Currently works only for pdf2djvu.
Assumes folder structure and existence of backend cli flag --version,
with output in specific format.
Side effect:
If value of version under preferences does not reference to existing installed backend
it's updates preferences to recognize this issue (set's value of version to None).
Checks:
1. Under CALIBRE's_config_dir/plugin/djvumaker/{backend_name}-{saved_installed_version}/{backend_name}
2. Under CALIBRE's_config_dir/plugin/djvumaker/{backend_name}-{other_versions}/{backend_name}
3. Under {backend_name} (it works if backend_name is on PATH ENV)
Return values:
(backend_path, saved_version, best_installed_version, version_under_path)
Return values description:
1. backend_path - path to found backend executable
2. saved_version - version saved in plugin JSON file
3. best_installed_version - best version in coresponding folder
4. version_under_path - version of executable under PATH
backend_path links to first found version during checks. If it's not found it takes None value.
If there is no valid version under value 2, 3 or 4, this value is None.
"""
# Check 1:
backend_path = None
saved_version = preferences[backend_name]['version']
if saved_version is not None:
try:
saved_version = check_version_executable(create_backend_link(backend_name, saved_version))
backend_path = create_backend_link(backend_name, saved_version)
except OSError:
saved_version = None
# Side effect:
preferences[backend_name]['version'] = None
preferences.commit()
# Check 2:
try:
folders_list = [filename.split('-') for filename in os.listdir(folder) if os.path.isdir(
os.path.join(folder, filename))]
installed_versions = [folder_name[1] for folder_name in folders_list if
folder_name[0] == backend_name]
best_installed_version = version_intlist_to_str(sorted(
[version_str_to_intlist(version) for version in installed_versions])[-1])
if backend_path is None:
backend_path = create_backend_link(backend_name, best_installed_version)
except OSError, IndexError:
best_installed_version = None
# Check 3:
try:
version_under_path = check_version_executable(backend_name)
if backend_path is None:
backend_path = backend_name
except OSError:
version_under_path = None
return backend_path, saved_version, best_installed_version, version_under_path
def create_backend_link(backend_name, version):
#NODOC
return os.path.join(os.path.join(plugin_dir('djvumaker'), '{}-{}'.format(backend_name, version), backend_name))
# TODO:
# class Installer_pdf2djvu(Installer):
# pass
def install_pdf2djvu(PLUGINNAME, preferences, log=print):
#NODOC
backend_path, saved_version, installed_version, path_version = discover_backend('pdf2djvu',
preferences, plugin_dir(PLUGINNAME))
# DEBUG COMMENT
# log("DEBUG: ", (backend_path, saved_version, installed_version, path_version))
local_version = None
if saved_version is None and installed_version is None:
log('pdf2djvu was not found in plugin directory.')
if path_version is not None:
log('Version {} of pdf2djvu was found on your local path env.'.format(path_version))
local_version = path_version
else:
log('pdf2djvu was not found on your local path env.')
else:
if saved_version is None or (installed_version is not None and saved_version is not None
and version_str_to_intlist(installed_version) > version_str_to_intlist(saved_version)):
saved_version = installed_version
preferences['pdf2djvu']['version'] = installed_version
preferences.commit()
log('Version {} of pdf2djvu was found in plugin directory.'.format(saved_version))
local_version = saved_version
log("Checking pdf2djvu's author page for current relase...")
github_latest_url = r'https://github.com/jwilk/pdf2djvu/releases/latest'
# DEBUG UNCOMMENT
github_page = urllib2.urlopen(github_latest_url)
web_version = get_url_basename(github_page.geturl())
# DEBUG COMMENT
# web_version = '0.9.5'
# local_version = None
log('Version {} of pdf2djvu is available on program\'s GitHub page.'.format(web_version))
def check_extracted_archive_pdf2djvu(exec_path, asked_version):
dir_path = os.path.dirname(exec_path)
output_version = check_version_executable(exec_path)
if output_version != asked_version:
raise Exception('Extracted file has wrong version.')
def download_and_unpack():
try:
fpath = download_pdf2djvu(web_version, log)
except:
msg = ('Error occured during downloading new relase, you can try manually download current'
' relase from {} and extract it inside calibre{sep}plugins{sep}djvumaker'
).format(github_latest_url, os.getcwd(), sep=os.pathsep)
log(msg)
raise
try:
unpack_zip_or_tar(PLUGINNAME, fpath, log)
check_extracted_archive_pdf2djvu(create_backend_link('pdf2djvu', web_version), web_version)
log('Extracting verified.')
except:
msg = ('Error occured during unpacking, check {}{sep}djvumaker folder for archive and try'
' extract it manually inside calibre{sep}plugins{sep}djvumaker'
).format(os.getcwd(), sep=os.pathsep)
log(msg)
raise
if local_version is None:
if not ask_yesno_input('Do you want to download current version of pdf2djvu?', log):
return False, None
download_and_unpack()
return True, web_version
local_ver_intlist = version_str_to_intlist(local_version)
new_ver_intlist = version_str_to_intlist(web_version)
if new_ver_intlist == local_ver_intlist:
log('You already have locally current version of pdf2djvu.')
if saved_version is None and installed_version is None:
if ask_yesno_input("Do you want to redownload current version of pdf2djvu to plugin\'s"
" directory? (it isn't necessary)"):
download_and_unpack()
return True, local_version
else:
return False, None
return True, local_version
elif new_ver_intlist > local_ver_intlist:
if not ask_yesno_input(
'Do you want to download newer version of pdf2djvu to plugin\'s directory?', log):
return False, None
download_and_unpack()
return True, web_version
else: #new_ver_intlist < local_ver_intlist
raise Exception("Newer local version than current pdf2djvu found.")
def get_url_basename(url):
#NODOC
return os.path.basename(urlparse.urlsplit(url).path)
def download_pdf2djvu(web_version, log):
#NODOC
def gen_zip_url(code):
#NODOC
return r'https://github.com/jwilk/pdf2djvu/releases/download/{}/pdf2djvu-win32-{}.zip'.format(code, code)
def gen_tar_url(code):
#NODOC
return r'https://github.com/jwilk/pdf2djvu/releases/download/{}/pdf2djvu-{}.tar.xz'.format(code, code)
# TODO: what with fallback!?! new argument
# TODO: cross import
PLUGINNAME = 'djvumaker'
fallback_version = '0.9.5'
if iswindows:
fallback_arch_url = gen_zip_url(fallback_version)
arch_url = gen_zip_url(web_version)
else:
fallback_arch_url = gen_tar_url(fallback_version)
arch_url = gen_tar_url(web_version)
def download_progress_bar(i, chunk, full):
"""'args: a count of blocks transferred so far,
a block size in bytes, and the total size of the file"""
printProgressBar(i*chunk, full, prefix = '\tProgress:', suffix = 'Complete',
length=50, prints=print)
def check_msg(fpath, msg):
return (
'Content-Length' in msg and int(msg['Content-Length']) > 0
and 'Content-Type' in msg and msg['Content-Type'].split('/')[0] == 'application'
and 'Content-Disposition' in msg
and msg['Content-Disposition'].split(';')[0] == 'attachment'
and msg['Content-Disposition'].split(';')[1].strip() == 'filename={}'.format(
os.path.basename(fpath))
)
log('Downloading current version of pdf2djvu...')
if not os.path.isdir(plugin_dir(PLUGINNAME)):
os.mkdir(plugin_dir(PLUGINNAME))
fpath, msg = urllib.urlretrieve(arch_url, os.path.join(plugin_dir(PLUGINNAME), get_url_basename(arch_url)),
download_progress_bar)
# print() # should progess bar function handle this TODO:
if not check_msg(fpath, msg):
log('Cannot download current version {} from GitHub.'.format(web_version))
if web_version != fallback_version:
log('Trying download version {}...'.format(fallback_version), download_progress_bar)
fpath, msg_fallback = urllib.urlretrieve(fallback_arch_url, os.path.join(plugin_dir(PLUGINNAME),
get_url_basename(fallback_arch_url)),
download_progress_bar)
# print() # should progess bar function handle this TODO:
if not check_msg(fpath, msg_fallback):
raise Exception('Cannot download pdf2djvu.')
else:
log('Dowloaded {} file'.format(os.path.abspath(fpath)))
return fpath
def unpack_zip_or_tar(PLUGINNAME, fpath, log):
#NODOC
# DEBUG COMMENT
# log(fpath)
log('Extracting now...')
if iswindows:
from zipfile import ZipFile
with ZipFile(fpath, 'r') as myzip:
myzip.extractall(os.path.dirname(fpath))
else:
raise NotImplementedError('Python 2.7 Standard Library cannot unpack tar.xz archives, do this manually or through shell.')
# it may not not work for macOS:
# subprocess.call(['tar', 'xf', fpath, '-C', os.path.dirname(fpath)])
# TODO: you have to make it still..., with sth like:
# subprocess.call(['make'])
# doesn't work for linux or mac then
log('Extracted downloaded archive')
os.remove(fpath)
log('Removed downloaded archive')
# Print iterations progress
def printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '=',
prints=print):
"""
source: http://stackoverflow.com/a/34325723/2351523
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
prints('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')
# Print New Line on Complete
if iteration >= total:
prints()
def ask_yesno_input(question, prints=print):
"""Ask user for yes/no input. Loops if other answer."""
while True:
prints('\n\t'+ question + ' (y/n)\n')
user_input = raw_input().strip().lower()
if user_input == 'y':
return True
elif user_input == 'n':
return False
else:
prints("Your input is not 'y' or 'n'.")
class EmptyClass():
pass
def empty_function(*args, **kwargs):
pass
def add_method_dec(obj, name):
"""Decorate `fun`, add `obj` as attribute with name `name` to `fun`"""
def inner(fun):
setattr(fun, name, obj)
return fun
return inner
def plugin_dir(plugin_name):
return os.path.join(config_dir, 'plugins', plugin_name)