forked from xxyzz/WordDumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_file.py
64 lines (56 loc) · 2.52 KB
/
send_file.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
#!/usr/bin/env python3
import shutil
from pathlib import Path
from calibre.gui2 import FunctionDispatcher
from calibre_plugins.worddumb.database import get_ll_path, get_x_ray_path
class SendFile:
def __init__(self, gui, data, notif):
self.gui = gui
self.device_manager = gui.device_manager
self.notif = notif
self.book_id, self.asin, self.book_path, self.mi, _ = data
self.ll_path = get_ll_path(self.asin, self.book_path)
self.x_ray_path = get_x_ray_path(self.asin, self.book_path)
# use some code from calibre.gui2.device:DeviceMixin.upload_books
def send_files(self, job):
if job is not None:
if job.failed:
self.gui.job_exception(job, dialog_title='Upload book failed')
return
self.gui.books_uploaded(job)
[has_book, _, _, _, paths] = self.gui.book_on_device(self.book_id)
# /Volumes/Kindle
device_prefix = self.device_manager.device._main_prefix
if has_book:
device_book_path = Path(device_prefix).joinpath(next(iter(paths)))
self.move_file_to_device(self.ll_path, device_book_path)
self.move_file_to_device(self.x_ray_path, device_book_path)
self.gui.status_bar.show_message(self.notif)
elif job is None:
# upload book and cover to device
self.gui.update_thumbnail(self.mi)
job = self.device_manager.upload_books(
FunctionDispatcher(self.send_files), [self.book_path],
[Path(self.book_path).name], on_card=None, metadata=[self.mi],
titles=[i.title for i in [self.mi]],
plugboards=self.gui.current_db.new_api.pref('plugboards', {}))
self.gui.upload_memory[job] = (
[self.mi], None, None, [self.book_path])
def move_file_to_device(self, file_path, device_book_path):
if not file_path.is_file():
return
sidecar_folder = device_book_path.parent.joinpath(
f'{device_book_path.stem}.sdr')
if not sidecar_folder.is_dir():
sidecar_folder.mkdir()
dst_path = sidecar_folder.joinpath(file_path.name)
if dst_path.is_file():
dst_path.unlink()
# Python 3.9 accepts path-like object, calibre uses 3.8
shutil.move(str(file_path), str(dst_path))
def kindle_connected(gui):
if not gui.device_manager.is_device_present:
return False
if gui.device_manager.device.VENDOR_NAME != 'KINDLE':
return False
return True