Skip to content

Commit

Permalink
Download klld file from MTP Kindle device
Browse files Browse the repository at this point in the history
  • Loading branch information
xxyzz committed Dec 26, 2023
1 parent 0ae1ed8 commit af5c2bb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
5 changes: 1 addition & 4 deletions custom_lemmas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
copy_klld_from_android,
copy_klld_from_kindle,
device_connected,
is_mtp_device,
)
from .utils import (
custom_lemmas_folder,
Expand Down Expand Up @@ -222,13 +221,11 @@ def check_empty_kindle_gloss(self) -> None:
if not package_name:
device_not_found_dialog(self)
return
if is_mtp_device(gui.device_manager.device):
return
custom_folder = custom_lemmas_folder(plugin_path, "en")
if isinstance(package_name, str):
copy_klld_from_android(package_name, custom_folder)
else:
copy_klld_from_kindle(gui, custom_folder)
copy_klld_from_kindle(gui.device_manager.device, custom_folder)

klld_path = get_kindle_klld_path(plugin_path)
if klld_path is None:
Expand Down
11 changes: 7 additions & 4 deletions epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ def extract_epub(self) -> Iterator[tuple[str, tuple[int, str, Path]]]:
):
for m in re.finditer(r">[^<]{2,}<", match_body.group(0)):
text = m.group(0)[1:-1]
yield unescape(text), (
match_body.start() + m.start() + 1,
text,
xhtml_path,
yield (
unescape(text),
(
match_body.start() + m.start() + 1,
text,
xhtml_path,
),
)

def add_entity(
Expand Down
42 changes: 32 additions & 10 deletions send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def move_files_to_kindle(self, device_manager: Any, device_book_path: Path) -> N
if use_mtp:
for file_path in (self.ll_path, self.x_ray_path):
dest_path = sidecar_folder.joinpath(file_path.name)
upload_file_to_kindle_mtp(device_manager, file_path, dest_path)
upload_file_to_mtp(device_manager, file_path, dest_path)
else:
sidecar_folder = device_mount_point.joinpath(sidecar_folder)
for file_path in (self.ll_path, self.x_ray_path):
Expand Down Expand Up @@ -239,11 +239,16 @@ def copy_klld_from_android(package_name: str, dest_path: Path) -> None:
dest_path.joinpath("wordwise").rmdir()


def copy_klld_from_kindle(gui: Any, dest_path: Path) -> None:
for klld_path in Path(f"{gui.device_manager.device._main_prefix}/system/kll").glob(
"*.en.klld"
):
shutil.copy(klld_path, dest_path)
def copy_klld_from_kindle(device_driver: Any, dest_path: Path) -> None:
if is_mtp_device(device_driver):
download_file_from_mtp(
device_driver, Path("system/kll/kll.en.en.klld"), dest_path
)
else:
for klld_path in Path(f"{device_driver._main_prefix}/system/kll").glob(
"*.en.klld"
):
shutil.copy(klld_path, dest_path)


def copy_klld_to_device(
Expand All @@ -264,7 +269,7 @@ def copy_klld_to_device(
if adb_path is not None:
run_subprocess([adb_path, "push", str(local_klld_path), str(device_klld_path)])
elif device_manager is not None:
upload_file_to_kindle_mtp(device_manager, local_klld_path, device_klld_path)
upload_file_to_mtp(device_manager, local_klld_path, device_klld_path)
else:
copy = False
if not device_klld_path.exists():
Expand All @@ -276,9 +281,7 @@ def copy_klld_to_device(
shutil.copy(local_klld_path, device_klld_path)


def upload_file_to_kindle_mtp(
device_manager: Any, source_path: Path, dest_path: Path
) -> None:
def upload_file_to_mtp(device_manager: Any, source_path: Path, dest_path: Path) -> None:
if not source_path.exists():
return
device_manager.create_job(
Expand All @@ -300,6 +303,25 @@ def mtp_upload_job(driver: Any, source_path: Path, dest_path: Path) -> None:
source_path.unlink()


def download_file_from_mtp(device_manager: Any, source_path: Path, dest_path: Path):
device_manager.create_job(
mtp_download_job,
None,
f"MTP downloading '{source_path}'",
args=[device_manager.device, source_path, dest_path],
)


def mtp_download_job(driver: Any, source_path: Path, dest_path: Path) -> None:
# https://github.com/kovidgoyal/calibre/blob/a1d86860ac83146e06fc398ed8c6d5422f8749ca/src/calibre/devices/mtp/driver.py#L171
storage = driver.filesystem_cache.storage(driver._main_id)
path = storage.find_path(source_path.parts)
if path is not None:
stream = driver.get_mtp_file(path)
with dest_path.open("wb") as dest_f:
shutil.copyfileobj(stream, dest_f)


def move_file_to_kindle_usbms(source_path: Path, dest_path: Path) -> None:
if not source_path.is_file():
return
Expand Down

0 comments on commit af5c2bb

Please sign in to comment.