Repository navigation
Expand file tree
/
Copy path_common.py
More file actions
244 lines (196 loc) · 5.7 KB
/
Copy path_common.py
File metadata and controls
244 lines (196 loc) · 5.7 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
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
import mtsound
import os
from PIL import Image
from concurrent.futures import ThreadPoolExecutor
from configparser import ConfigParser
from mtattr import Attr
from mtlogger import logger
from mtfs import write_text_file
from tqdm import tqdm
from _constants import (
DEFAULT_ICO_SIZES,
DESKTOP_INI_FILENAME,
FALLBACK_ENCODING,
HIDDEN_FILE_ATTRS,
HIDDEN_SYSTEM_FILE_ATTRS,
ICO_FILENAME,
INI_ICON_KEY,
INI_SHELL_SECTION,
MAX_ICO_SIZE,
PREFERRED_ENCODING,
)
from _settings import HIDE_GENERATED_ICONS
def process_parent_folder(
parent_folder_path: str,
depth: int,
image_filenames: list[str],
):
parent_folder_path = os.path.abspath(parent_folder_path)
parent_depth = parent_folder_path.rstrip(os.sep).count(os.sep)
folders_to_process = [parent_folder_path]
for root, dirs, _ in os.walk(parent_folder_path):
current_depth = root.rstrip(os.sep).count(os.sep) - parent_depth
if current_depth >= depth:
dirs.clear()
continue
for dir_name in dirs:
folders_to_process.append(os.path.join(root, dir_name))
with (
ThreadPoolExecutor() as executor,
tqdm(
total=len(folders_to_process), desc=f'Processing "{parent_folder_path}"'
) as progress,
):
for _ in executor.map(
lambda f: process_folder(f, image_filenames), folders_to_process
):
progress.update(1)
mtsound.notify()
logger.log(f'\nFinished setting icons for "{parent_folder_path}".')
def process_folder(
folder_path: str,
image_filenames: list[str],
):
image_path = None
ico_path = os.path.join(folder_path, ICO_FILENAME)
ico_exists = os.path.exists(ico_path)
skipped = False
for image_filename in image_filenames:
potential_path = os.path.join(folder_path, image_filename)
if os.path.exists(potential_path):
if not ico_exists or is_file_newer_than(potential_path, ico_path):
image_path = potential_path
else:
skipped = True
break
if image_path:
image_to_ico(image_path, ico_path)
set_folder_icon(folder_path, ICO_FILENAME)
elif skipped:
tqdm.write(
logger.format_warn(
f'No image found in "{folder_path}" is newer than the icon.'
)
)
else:
tqdm.write(
logger.format_warn(f'No suitable image found in "{folder_path}".')
)
def is_file_newer_than(
file_a: str,
file_b: str,
):
return os.path.getmtime(file_a) > os.path.getmtime(file_b)
def image_to_ico(
image_path: str,
ico_path: str,
icon_sizes=DEFAULT_ICO_SIZES,
):
try:
if os.path.exists(ico_path):
os.unlink(ico_path)
with Image.open(image_path) as img:
if img.width < MAX_ICO_SIZE:
img = img.resize(
(MAX_ICO_SIZE, int(MAX_ICO_SIZE * img.height / img.width)),
resample=Image.LANCZOS,
)
img.thumbnail((MAX_ICO_SIZE, MAX_ICO_SIZE), Image.LANCZOS)
background = Image.new('RGBA', (MAX_ICO_SIZE, MAX_ICO_SIZE), (0, 0, 0, 0))
offset = (
int((MAX_ICO_SIZE - img.size[0]) / 2),
int((MAX_ICO_SIZE - img.size[1]) / 2),
)
background.paste(img, offset)
background.save(
ico_path, format='ICO', sizes=[(s, s) for s in icon_sizes]
)
if HIDE_GENERATED_ICONS:
Attr.hide(ico_path)
except Exception as ex:
logger.error(f'Error converting "{image_path}" to ICO:\n{ex}')
def set_folder_icon(
folder_path: str,
ico_path: str,
overwrite_existing=False,
):
try:
desktop_ini_path = os.path.join(folder_path, DESKTOP_INI_FILENAME)
if os.path.exists(desktop_ini_path):
Attr.show(desktop_ini_path)
config, encoding = read_ini(desktop_ini_path)
if get_ini_icon(config) and not overwrite_existing:
logger.trace(f'Skipping "{folder_path}". A folder icon is already set.')
return
set_ini_icon(config, ico_path)
write_ini(desktop_ini_path, config, encoding)
Attr.hide(desktop_ini_path)
Attr.add(folder_path, ['s'])
except PermissionError:
tqdm.write(
logger.format_warn(
f'Permission denied: "{desktop_ini_path}". You may need to run the script as an administrator.'
)
)
except Exception as ex:
tqdm.write(
logger.format_error(
f'Error setting folder icon to "{folder_path}":\n{ex}'
)
)
def read_ini(
ini_path: str,
encoding=PREFERRED_ENCODING,
):
config = ConfigParser()
config.optionxform = str
try:
if os.path.exists(ini_path):
config.read(ini_path, encoding=encoding)
except Exception:
try:
encoding = FALLBACK_ENCODING
config.read(ini_path, encoding=encoding)
except Exception as ex:
logger.error(f'Failed to read "{ini_path}": {ex}')
return config, encoding
def write_file(
file_path: str,
content: str,
attrs: list[str],
encoding=PREFERRED_ENCODING,
):
if os.path.exists(file_path):
Attr.remove(file_path, attrs)
write_text_file(file_path, content, encoding)
logger.success(f'Saved "{file_path}".')
Attr.add(file_path, attrs)
def write_ini(
file_path: str,
config: ConfigParser,
encoding: str,
):
if os.path.exists(file_path):
Attr.remove(file_path, HIDDEN_SYSTEM_FILE_ATTRS)
with open(file_path, 'w', encoding=encoding) as ini:
config.write(ini)
logger.success(f'Saved "{file_path}".')
Attr.add(file_path, HIDDEN_SYSTEM_FILE_ATTRS)
def write_hidden_file(
file_path: str,
content: str,
encoding=PREFERRED_ENCODING,
):
write_file(file_path, content, HIDDEN_FILE_ATTRS, encoding)
def get_ini_icon(
config: ConfigParser,
):
return config.get(INI_SHELL_SECTION, INI_ICON_KEY, fallback=None)
def set_ini_icon(
config: ConfigParser,
ico_path: str,
ico_index=0,
):
if INI_SHELL_SECTION not in config:
config[INI_SHELL_SECTION] = {}
config[INI_SHELL_SECTION][INI_ICON_KEY] = f'{ico_path},{ico_index}'