-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_engine.py
More file actions
514 lines (428 loc) · 18.5 KB
/
Copy pathsync_engine.py
File metadata and controls
514 lines (428 loc) · 18.5 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Core synchronization engine for the Link Farm plugin.
Handles symlink creation, deletion, and state management.
"""
import os
from datetime import datetime
from typing import Dict, List, Optional, Tuple, Set
from calibre_plugins.linkfarm.common import (
sanitize_filename, ensure_dir_exists, get_relative_path,
CUSTOM_DATA_KEY, STATUS_SYNCED, STATUS_ERROR, STATUS_PENDING
)
from calibre_plugins.linkfarm.profile_manager import prefs, SyncProfile
class SyncResult:
"""Represents the result of a sync operation."""
def __init__(self):
self.created: List[int] = []
self.updated: List[int] = []
self.removed: List[int] = []
self.errors: List[Tuple[Optional[int], str]] = []
self.skipped: List[int] = []
def total_changes(self) -> int:
"""Get total number of changes made."""
return len(self.created) + len(self.updated) + len(self.removed)
def has_errors(self) -> bool:
"""Check if any errors occurred."""
return len(self.errors) > 0
def summary(self) -> str:
"""Get a human-readable summary of the sync."""
parts = []
if self.created:
parts.append(f"{len(self.created)} created")
if self.updated:
parts.append(f"{len(self.updated)} updated")
if self.removed:
parts.append(f"{len(self.removed)} removed")
if self.skipped:
parts.append(f"{len(self.skipped)} skipped")
if self.errors:
parts.append(f"{len(self.errors)} errors")
return ", ".join(parts) if parts else "No changes"
class SyncEngine:
"""Handles synchronization of books to external directories via symlinks."""
def __init__(self, gui):
"""
Initialize the sync engine.
Args:
gui: Calibre GUI instance
"""
self.gui = gui
self.db = gui.current_db.new_api
def sync_profile(self, profile: SyncProfile) -> SyncResult:
"""
Synchronize books for a specific profile.
Args:
profile: The profile to sync
Returns:
SyncResult with details of the operation
"""
result = SyncResult()
# Validate target directory
if not ensure_dir_exists(profile.target_directory):
result.errors.append((None, f"Cannot create/access directory: {profile.target_directory}"))
return result
# Get books selected for syncing
try:
selected_books = self._get_selected_books(profile)
print(f"[Link Farm] Profile '{profile.name}': Found {len(selected_books)} selected books: {selected_books}")
except Exception as e:
result.errors.append((None, f"Error getting selected books: {str(e)}"))
return result
# Build map of desired symlinks
desired_links = {}
for book_id in selected_books:
try:
source, target = self._compute_symlink(book_id, profile)
if source and target:
desired_links[book_id] = (source, target)
else:
result.skipped.append(book_id)
except Exception as e:
result.errors.append((book_id, f"Error computing symlink: {str(e)}"))
# Get currently synced books for this profile
current_synced = self._get_currently_synced_books(profile.id)
# Determine what to create, update, and remove
desired_set = set(desired_links.keys())
current_set = set(current_synced.keys())
to_create = desired_set - current_set
to_remove = current_set - desired_set
to_check = desired_set & current_set # Check if these need updating
# Remove symlinks for books no longer selected
for book_id in to_remove:
try:
self._remove_symlink(current_synced[book_id])
self._update_sync_state(book_id, profile.id, None)
result.removed.append(book_id)
except Exception as e:
result.errors.append((book_id, f"Error removing symlink: {str(e)}"))
# Create new symlinks
for book_id in to_create:
try:
source, target = desired_links[book_id]
self._create_symlink(source, target, profile.use_relative_symlinks if hasattr(profile, 'use_relative_symlinks') else prefs['use_relative_symlinks'])
# Update sync state
self._update_sync_state(book_id, profile.id, {
'status': STATUS_SYNCED,
'symlink_path': target,
'target_format': os.path.splitext(source)[1][1:].upper(),
'last_sync': datetime.now().isoformat(),
'error_message': None
})
result.created.append(book_id)
except Exception as e:
self._update_sync_state(book_id, profile.id, {
'status': STATUS_ERROR,
'symlink_path': None,
'target_format': None,
'last_sync': datetime.now().isoformat(),
'error_message': str(e)
})
result.errors.append((book_id, f"Error creating symlink: {str(e)}"))
# Check existing symlinks for updates
for book_id in to_check:
try:
current_target = current_synced[book_id]
new_source, new_target = desired_links[book_id]
# Check if symlink needs updating
needs_update = False
if current_target != new_target:
needs_update = True
elif os.path.islink(current_target):
# Check if the link target changed
current_source = os.readlink(current_target)
if os.path.isabs(current_source):
needs_update = (current_source != new_source)
else:
# Resolve relative path
resolved = os.path.normpath(os.path.join(os.path.dirname(current_target), current_source))
needs_update = (resolved != new_source)
else:
# Not a symlink anymore, recreate
needs_update = True
if needs_update:
self._remove_symlink(current_target)
self._create_symlink(new_source, new_target, prefs['use_relative_symlinks'])
self._update_sync_state(book_id, profile.id, {
'status': STATUS_SYNCED,
'symlink_path': new_target,
'target_format': os.path.splitext(new_source)[1][1:].upper(),
'last_sync': datetime.now().isoformat(),
'error_message': None
})
result.updated.append(book_id)
except Exception as e:
self._update_sync_state(book_id, profile.id, {
'status': STATUS_ERROR,
'last_sync': datetime.now().isoformat(),
'error_message': str(e)
})
result.errors.append((book_id, f"Error updating symlink: {str(e)}"))
return result
def _get_selected_books(self, profile: SyncProfile) -> List[int]:
"""
Get list of book IDs selected for this profile.
Args:
profile: The sync profile
Returns:
List of book IDs
"""
method = prefs['selection_method']
if method == 'custom_column':
column_name = prefs['custom_column_name']
# Get all books where custom column contains this profile name
all_ids = self.db.all_book_ids()
selected = []
print(f"[Link Farm] Looking for profile '{profile.name}' in column '{column_name}'")
for book_id in all_ids:
try:
value = self.db.field_for(column_name, book_id, default_value=None)
# Check if profile name is in the column value
if value:
matched = False
if isinstance(value, (list, tuple)):
# Multi-value column (new style)
print(f"[Link Farm] Book {book_id}: value={value} (list), checking if '{profile.name}' in list")
if profile.name in value:
selected.append(book_id)
matched = True
elif isinstance(value, str):
# Single value string
value_list = [v.strip() for v in value.split(',') if v.strip()]
print(f"[Link Farm] Book {book_id}: value={value!r} (str), parsed={value_list}, checking if '{profile.name}' in list")
if profile.name in value_list:
selected.append(book_id)
matched = True
elif value is True:
# Old boolean column - sync to all profiles
selected.append(book_id)
matched = True
if matched:
print(f"[Link Farm] Book {book_id}: MATCHED")
except Exception as e:
# Column might not exist yet
print(f"[Link Farm] Book {book_id}: Error reading column: {e}")
continue
print(f"[Link Farm] Total selected books: {len(selected)}")
return selected
elif method == 'tags':
# Use search to find books with profile tag
tag_prefix = prefs['tag_prefix']
tag = f"{tag_prefix}{profile.name}"
query = f'tags:"={tag}"'
try:
return list(self.db.search(query))
except Exception:
return []
return []
def _compute_symlink(self, book_id: int, profile: SyncProfile) -> Tuple[Optional[str], Optional[str]]:
"""
Compute source and target paths for a symlink.
Args:
book_id: Book ID
profile: Sync profile
Returns:
Tuple of (source_path, target_path), or (None, None) if no suitable format
"""
# Select best format
format_path = self._select_format(book_id, profile.format_priority)
if not format_path:
return None, None
# Compute target path
target = self._compute_target_path(book_id, format_path, profile)
return format_path, target
def _select_format(self, book_id: int, format_priority: List[str]) -> Optional[str]:
"""
Select the best available format based on priority list.
Args:
book_id: Book ID
format_priority: Ordered list of preferred formats
Returns:
Absolute path to the selected format, or None if no suitable format
"""
formats_data = self.db.formats(book_id, verify_formats=True)
if not formats_data:
return None
# Handle both tuple/list and string formats
if isinstance(formats_data, (tuple, list)):
# Newer Calibre returns tuple/list of formats
available = set(fmt.upper().strip() for fmt in formats_data if fmt)
formats_list = list(formats_data)
elif isinstance(formats_data, str):
# Older Calibre returns comma-separated string
available = set(fmt.upper().strip() for fmt in formats_data.split(',') if fmt)
formats_list = [fmt.strip() for fmt in formats_data.split(',') if fmt]
else:
return None
if not available:
return None
# Try each format in priority order
for preferred in format_priority:
preferred_upper = preferred.upper().strip()
if preferred_upper in available:
try:
return self.db.format_abspath(book_id, preferred)
except Exception:
continue
# Fallback: use first available format
if formats_list:
fallback_fmt = formats_list[0]
try:
return self.db.format_abspath(book_id, fallback_fmt)
except Exception:
pass
return None
def _compute_target_path(self, book_id: int, format_path: str, profile: SyncProfile) -> str:
"""
Compute the target path for a symlink.
Args:
book_id: Book ID
format_path: Source format file path
profile: Sync profile
Returns:
Target path for the symlink
"""
# Get book metadata
title = self.db.field_for('title', book_id)
authors = self.db.field_for('authors', book_id)
# Sanitize names
safe_title = sanitize_filename(title)
safe_author = sanitize_filename(authors[0] if authors else 'Unknown')
# Get file extension
ext = os.path.splitext(format_path)[1]
# Build target path
if profile.preserve_structure:
# Structure: target_dir/Author/BookTitle.ext
author_dir = os.path.join(profile.target_directory, safe_author)
ensure_dir_exists(author_dir)
target_file = f"{safe_title}{ext}"
target_path = os.path.join(author_dir, target_file)
else:
# Flat structure: target_dir/BookTitle.ext
target_file = f"{safe_title}{ext}"
target_path = os.path.join(profile.target_directory, target_file)
# Handle duplicate filenames by appending book_id
if os.path.exists(target_path) and not os.path.islink(target_path):
# File exists and is not a symlink - add book_id suffix
name_without_ext = os.path.splitext(target_file)[0]
target_file = f"{name_without_ext} ({book_id}){ext}"
if profile.preserve_structure:
target_path = os.path.join(author_dir, target_file)
else:
target_path = os.path.join(profile.target_directory, target_file)
return target_path
def _create_symlink(self, source: str, target: str, use_relative: bool = True):
"""
Create a symlink from target to source.
Args:
source: Source file path
target: Target symlink path
use_relative: Use relative paths if possible
"""
# Remove old symlink if exists
if os.path.lexists(target):
os.unlink(target)
# Ensure target directory exists
ensure_dir_exists(os.path.dirname(target))
# Determine link source (relative or absolute)
if use_relative:
try:
link_source = get_relative_path(source, os.path.dirname(target))
except Exception:
link_source = source
else:
link_source = source
# Create symlink
os.symlink(link_source, target)
def _remove_symlink(self, target: str):
"""
Remove a symlink.
Args:
target: Symlink path to remove
"""
if os.path.lexists(target):
os.unlink(target)
def _get_currently_synced_books(self, profile_id: str) -> Dict[int, str]:
"""
Get currently synced books for a profile.
Args:
profile_id: Profile ID
Returns:
Dict mapping book_id to symlink_path
"""
synced = {}
all_ids = self.db.all_book_ids()
for book_id in all_ids:
state = self._get_sync_state(book_id, profile_id)
if state and state.get('status') == STATUS_SYNCED and state.get('symlink_path'):
synced[book_id] = state['symlink_path']
return synced
def _get_sync_state(self, book_id: int, profile_id: str) -> Optional[Dict]:
"""
Get sync state for a book/profile combination.
Args:
book_id: Book ID
profile_id: Profile ID
Returns:
Sync state dict, or None if not found
"""
try:
all_states = self.db.get_custom_book_data(CUSTOM_DATA_KEY, [book_id], {})
book_state = all_states.get(book_id, {})
return book_state.get(profile_id)
except Exception:
return None
def _update_sync_state(self, book_id: int, profile_id: str, state: Optional[Dict]):
"""
Update sync state for a book/profile combination.
Args:
book_id: Book ID
profile_id: Profile ID
state: New state dict, or None to remove state
"""
try:
# Get all states for this book
all_states = self.db.get_custom_book_data(CUSTOM_DATA_KEY, [book_id], {})
book_state = all_states.get(book_id, {})
if state is None:
# Remove state for this profile
if profile_id in book_state:
del book_state[profile_id]
else:
# Update state for this profile
book_state[profile_id] = state
# Save back to database
self.db.add_custom_book_data(CUSTOM_DATA_KEY, {book_id: book_state})
except Exception as e:
# Log error but don't fail the sync
print(f"Warning: Could not update sync state for book {book_id}: {e}")
def get_book_sync_status(self, book_id: int) -> Dict[str, Dict]:
"""
Get sync status for a book across all profiles.
Args:
book_id: Book ID
Returns:
Dict mapping profile_id to sync state
"""
try:
all_states = self.db.get_custom_book_data(CUSTOM_DATA_KEY, [book_id], {})
return all_states.get(book_id, {})
except Exception:
return {}
def clear_profile_state(self, profile_id: str):
"""
Clear sync state for all books in a profile.
Args:
profile_id: Profile ID
"""
all_ids = self.db.all_book_ids()
for book_id in all_ids:
try:
all_states = self.db.get_custom_book_data(CUSTOM_DATA_KEY, [book_id], {})
book_state = all_states.get(book_id, {})
if profile_id in book_state:
del book_state[profile_id]
self.db.add_custom_book_data(CUSTOM_DATA_KEY, {book_id: book_state})
except Exception:
continue