-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy path__init__.py
More file actions
174 lines (134 loc) · 4.93 KB
/
Copy path__init__.py
File metadata and controls
174 lines (134 loc) · 4.93 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__license__ = "GPL v3"
__copyright__ = "2023-2025, Cirn09(古明地小板)"
from calibre.customize import InterfaceActionBase
from calibre.utils.filenames import sanitize_file_name
from calibre_plugins.notrans.config import prefs
class NoTransBase(InterfaceActionBase):
name = "NoTrans"
description = _("Do not translate my paths")
supported_platforms = ["windows", "osx", "linux"]
author = "Cirn09"
version = (3, 1, 0)
minimum_calibre_version = (5, 0, 0) # first version using Python 3
actual_plugin = "calibre_plugins.notrans.ui:NoTrans"
def is_customizable(self):
return True
def config_widget(self):
from calibre_plugins.notrans.ui import ConfigWidget
return ConfigWidget()
def save_settings(self, config_widget):
config_widget.save_settings()
hook.update(prefs)
class Hook(object):
def __init__(self):
# db
try:
from calibre.db import backend
self.db = backend
self.db_ori = backend.ascii_filename
except ImportError:
self.db = None
# usb
try:
from calibre.devices.usbms import device
self.usb = device
self.usb_ori = device.sanitize
except ImportError:
self.usb = None
# app
try:
from calibre.devices.smart_device_app import driver
self.app = driver
self.smart_ori = driver.sanitize
except ImportError:
self.app = None
# mtp
try:
from calibre.devices.mtp import driver
self.mtp = driver
self.mtp_ori = driver.MTP_DEVICE.create_upload_path
except ImportError:
self.mtp = None
# save_to_disk (实际上影响所有“发送到”)
# import calibre.library.save_to_disk
# old = calibre.library.save_to_disk.get_components
# def new(*args, **keywords):
# keywords['sanitize_func'] = sanitize_file_name
# return old(*args, **keywords)
# calibre.library.save_to_disk.get_components = new
try:
from calibre.library import save_to_disk
self.s2d = save_to_disk
self.s2d_ori = save_to_disk.get_components
except ImportError:
self.s2d = None
def update(self, config: dict):
if self.db:
if config.get("db", True):
self.db.ascii_filename = sanitize_file_name
print("NoTrans: db hooked")
else:
self.db.ascii_filename = self.db_ori
print("NoTrans: db unhooked")
send = config.get("send", True)
if self.usb:
if send:
self.usb.sanitize = sanitize_file_name
print("NoTrans: usb hooked")
else:
self.usb.sanitize = self.usb_ori
print("NoTrans: usb unhooked")
if self.app:
if send:
self.app.sanitize = sanitize_file_name
print("NoTrans: app hooked")
else:
self.app.sanitize = self.smart_ori
print("NoTrans: app unhooked")
if self.mtp:
if send:
self.mtp.MTP_DEVICE.create_upload_path = mtp_create_upload_path
print("NoTrans: mtp hooked")
else:
self.mtp.MTP_DEVICE.create_upload_path = self.mtp_ori
print("NoTrans: mtp unhooked")
if self.s2d:
if send:
self.s2d.get_components = gen_s2d_hook(self.s2d_ori)
print("NoTrans: save_to_disk hooked")
else:
self.s2d.get_components = self.s2d_ori
print("NoTrans: save_to_disk unhooked")
def mtp_create_upload_path(self, path, mdata, fname, routing):
from calibre.devices.utils import create_upload_path
import posixpath
ext = fname.rpartition(".")[-1].lower()
path = routing.get(ext, path)
filepath = create_upload_path(
mdata,
fname,
self.save_template,
sanitize_file_name,
prefix_path=path,
path_type=posixpath,
maxlen=self.MAX_PATH_LEN,
use_subdirs="/" in self.save_template,
news_in_folder=self.NEWS_IN_FOLDER,
)
return tuple(x for x in filepath.split("/"))
def gen_s2d_hook(ori_func):
def s2d_hook(*args, **keywords):
# def get_components(template, mi, id, timefmt='%b %Y', length=250,
# sanitize_func=ascii_filename, replace_whitespace=False,
# to_lowercase=False, safe_format=True, last_has_extension=True,
# single_dir=False):
if len(args) >= 6:
args = args[:5] + (sanitize_file_name,) + args[6:]
else:
keywords["sanitize_func"] = sanitize_file_name
return ori_func(*args, **keywords)
return s2d_hook
hook = Hook()
hook.update(prefs)