zync-py 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zync/__init__.py +23 -0
- zync/cli/__init__.py +1 -0
- zync/cli/app.py +183 -0
- zync/core/__init__.py +21 -0
- zync/core/attachments.py +57 -0
- zync/core/concurrency.py +22 -0
- zync/core/folders.py +184 -0
- zync/core/library.py +89 -0
- zync/core/patterns.py +86 -0
- zync/core/removals.py +57 -0
- zync/core/settings.py +27 -0
- zync/core/sync/__init__.py +2 -0
- zync/core/sync/additions.py +61 -0
- zync/core/sync/deletions.py +95 -0
- zync/core/sync/index.py +85 -0
- zync/core/sync/paths.py +7 -0
- zync/core/sync/report.py +37 -0
- zync/core/sync/zync.py +133 -0
- zync/infra/__init__.py +10 -0
- zync/infra/disk.py +30 -0
- zync/infra/zotero/__init__.py +11 -0
- zync/infra/zotero/credentials.py +33 -0
- zync/infra/zotero/library.py +278 -0
- zync/py.typed +0 -0
- zync_py-0.1.0.dist-info/METADATA +138 -0
- zync_py-0.1.0.dist-info/RECORD +29 -0
- zync_py-0.1.0.dist-info/WHEEL +4 -0
- zync_py-0.1.0.dist-info/entry_points.txt +2 -0
- zync_py-0.1.0.dist-info/licenses/LICENSE +21 -0
zync/core/removals.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from typing import Protocol
|
|
2
|
+
|
|
3
|
+
from zync.core.library import Attachment, ZoteroLibrary
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class RemovalMode(Protocol):
|
|
7
|
+
"""What happens in the library to attachments whose files were deleted."""
|
|
8
|
+
|
|
9
|
+
async def remove(
|
|
10
|
+
self, attachments: list[Attachment], *, library: ZoteroLibrary
|
|
11
|
+
) -> None:
|
|
12
|
+
"""Handle the attachments whose files were deleted from the synced folders."""
|
|
13
|
+
...
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _Trash:
|
|
17
|
+
async def remove(
|
|
18
|
+
self, attachments: list[Attachment], *, library: ZoteroLibrary
|
|
19
|
+
) -> None:
|
|
20
|
+
removed_keys = {attachment.key for attachment in attachments}
|
|
21
|
+
trashed_keys = list(
|
|
22
|
+
dict.fromkeys(
|
|
23
|
+
_trashed_key(attachment, removed_keys) for attachment in attachments
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
if trashed_keys:
|
|
27
|
+
await library.trash(trashed_keys)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class _Keep:
|
|
31
|
+
async def remove(
|
|
32
|
+
self, attachments: list[Attachment], *, library: ZoteroLibrary
|
|
33
|
+
) -> None:
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class removals:
|
|
38
|
+
"""The ways to handle attachments whose files were deleted."""
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def trash() -> RemovalMode:
|
|
42
|
+
"""Move each attachment to the trash.
|
|
43
|
+
|
|
44
|
+
When all attachments of an item are removed, the whole item goes to
|
|
45
|
+
the trash instead, with its notes.
|
|
46
|
+
"""
|
|
47
|
+
return _Trash()
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def keep() -> RemovalMode:
|
|
51
|
+
"""Leave the library unchanged."""
|
|
52
|
+
return _Keep()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _trashed_key(attachment: Attachment, removed_keys: set[str]) -> str:
|
|
56
|
+
is_last_attachment = attachment.item_attachment_keys <= removed_keys
|
|
57
|
+
return attachment.item_key if is_last_attachment else attachment.key
|
zync/core/settings.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from zync.core.attachments import AttachmentMode, attachments
|
|
4
|
+
from zync.core.removals import RemovalMode, removals
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class SyncSettings:
|
|
9
|
+
"""What the sync looks for below the root folder, and how it changes the library.
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
folder_patterns: Glob patterns for the paths of the folders
|
|
13
|
+
to sync, relative to the root folder. The folders in curly braces
|
|
14
|
+
name the collections, as `FolderPattern` describes.
|
|
15
|
+
skipped_folder_names: Folders with these names are not searched.
|
|
16
|
+
file_extensions: Only files with these extensions are synchronized.
|
|
17
|
+
The comparison ignores case.
|
|
18
|
+
attachment_mode: How a file that is not in the library yet is added.
|
|
19
|
+
removal_mode: What happens to the attachment of a file that was
|
|
20
|
+
deleted from a synced folder.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
folder_patterns: tuple[str, ...] = ("{*}/**/Literature",)
|
|
24
|
+
skipped_folder_names: frozenset[str] = frozenset({".git", ".venv", "node_modules"})
|
|
25
|
+
file_extensions: frozenset[str] = frozenset({".pdf"})
|
|
26
|
+
attachment_mode: AttachmentMode = field(default_factory=attachments.link)
|
|
27
|
+
removal_mode: RemovalMode = field(default_factory=removals.trash)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from zync.core.library import CollectionPath, ZoteroLibrary
|
|
5
|
+
from zync.core.settings import SyncSettings
|
|
6
|
+
from zync.core.sync.index import AttachmentIndex
|
|
7
|
+
from zync.core.sync.report import CollectionUpdate
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class Additions:
|
|
12
|
+
"""Adds the files of synced folders to their collections."""
|
|
13
|
+
|
|
14
|
+
library: ZoteroLibrary
|
|
15
|
+
settings: SyncSettings
|
|
16
|
+
index: AttachmentIndex
|
|
17
|
+
|
|
18
|
+
async def add(
|
|
19
|
+
self, files: list[Path], *, collection_path: CollectionPath
|
|
20
|
+
) -> CollectionUpdate:
|
|
21
|
+
"""Add the files that are not in the collection yet.
|
|
22
|
+
|
|
23
|
+
A file that is already in the library adds its item to the collection.
|
|
24
|
+
Any other file becomes a new attachment.
|
|
25
|
+
"""
|
|
26
|
+
await self.library.ensure_collection(collection_path)
|
|
27
|
+
missing_files = [
|
|
28
|
+
file
|
|
29
|
+
for file in files
|
|
30
|
+
if not self.index.is_in_collection(file, collection_path)
|
|
31
|
+
]
|
|
32
|
+
existing_files = [
|
|
33
|
+
file for file in missing_files if self.index.is_attached(file)
|
|
34
|
+
]
|
|
35
|
+
new_files = [file for file in missing_files if not self.index.is_attached(file)]
|
|
36
|
+
await self._add_items_of(existing_files, collection_path)
|
|
37
|
+
await self._attach(new_files, collection_path)
|
|
38
|
+
return CollectionUpdate(
|
|
39
|
+
collection_path=collection_path,
|
|
40
|
+
new_files=new_files,
|
|
41
|
+
existing_files=existing_files,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
async def _add_items_of(
|
|
45
|
+
self, files: list[Path], collection_path: CollectionPath
|
|
46
|
+
) -> None:
|
|
47
|
+
for file in files:
|
|
48
|
+
# Two files can belong to the same item, which the first of them already added.
|
|
49
|
+
if not self.index.is_in_collection(file, collection_path):
|
|
50
|
+
await self._add_item_of(file, collection_path)
|
|
51
|
+
|
|
52
|
+
async def _add_item_of(self, file: Path, collection_path: CollectionPath) -> None:
|
|
53
|
+
item_key = self.index.item_key(file)
|
|
54
|
+
await self.library.add_to_collection(item_key, collection_path)
|
|
55
|
+
self.index.record_membership(item_key, collection_path)
|
|
56
|
+
|
|
57
|
+
async def _attach(self, files: list[Path], collection_path: CollectionPath) -> None:
|
|
58
|
+
if files:
|
|
59
|
+
await self.settings.attachment_mode.attach(
|
|
60
|
+
files, library=self.library, collection_path=collection_path
|
|
61
|
+
)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from collections.abc import Callable, Collection
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from zync.core.library import (
|
|
6
|
+
AttachedFiles,
|
|
7
|
+
Attachment,
|
|
8
|
+
CollectionPath,
|
|
9
|
+
LinkedFile,
|
|
10
|
+
StoredFile,
|
|
11
|
+
)
|
|
12
|
+
from zync.core.sync.index import AttachmentIndex
|
|
13
|
+
from zync.core.sync.paths import comparable_path
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class DeletedFiles:
|
|
18
|
+
"""The attachments whose files were deleted from the synced folders."""
|
|
19
|
+
|
|
20
|
+
linked_files: list[LinkedFile]
|
|
21
|
+
stored_files: list[StoredFile]
|
|
22
|
+
|
|
23
|
+
def attachments(self) -> list[Attachment]:
|
|
24
|
+
return [linked_file.attachment for linked_file in self.linked_files] + [
|
|
25
|
+
stored_file.attachment for stored_file in self.stored_files
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
def names(self) -> list[str]:
|
|
29
|
+
"""Return the paths of the linked files and the titles of the stored copies."""
|
|
30
|
+
return [str(linked_file.path) for linked_file in self.linked_files] + [
|
|
31
|
+
stored_file.title for stored_file in self.stored_files
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def find_deleted_files(
|
|
36
|
+
attached_files: AttachedFiles,
|
|
37
|
+
*,
|
|
38
|
+
existing_files: list[Path],
|
|
39
|
+
synced_collection_paths: Collection[CollectionPath],
|
|
40
|
+
index: AttachmentIndex,
|
|
41
|
+
is_in_synced_folder: Callable[[Path], bool],
|
|
42
|
+
) -> DeletedFiles:
|
|
43
|
+
"""Return the attachments whose files are gone.
|
|
44
|
+
|
|
45
|
+
A linked file counts as deleted when its path lies in a synced folder but
|
|
46
|
+
the file does not exist. A stored copy counts as deleted when its item is
|
|
47
|
+
in a synced collection and no existing file has its content.
|
|
48
|
+
"""
|
|
49
|
+
return DeletedFiles(
|
|
50
|
+
linked_files=_deleted_linked_files(
|
|
51
|
+
attached_files.linked_files,
|
|
52
|
+
existing_files=existing_files,
|
|
53
|
+
is_in_synced_folder=is_in_synced_folder,
|
|
54
|
+
),
|
|
55
|
+
stored_files=_deleted_stored_files(
|
|
56
|
+
attached_files.stored_files,
|
|
57
|
+
synced_collection_paths=synced_collection_paths,
|
|
58
|
+
index=index,
|
|
59
|
+
),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _deleted_linked_files(
|
|
64
|
+
linked_files: list[LinkedFile],
|
|
65
|
+
*,
|
|
66
|
+
existing_files: list[Path],
|
|
67
|
+
is_in_synced_folder: Callable[[Path], bool],
|
|
68
|
+
) -> list[LinkedFile]:
|
|
69
|
+
existing_paths = {comparable_path(file) for file in existing_files}
|
|
70
|
+
return [
|
|
71
|
+
linked_file
|
|
72
|
+
for linked_file in linked_files
|
|
73
|
+
if comparable_path(linked_file.path) not in existing_paths
|
|
74
|
+
and is_in_synced_folder(linked_file.path)
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _deleted_stored_files(
|
|
79
|
+
stored_files: list[StoredFile],
|
|
80
|
+
*,
|
|
81
|
+
synced_collection_paths: Collection[CollectionPath],
|
|
82
|
+
index: AttachmentIndex,
|
|
83
|
+
) -> list[StoredFile]:
|
|
84
|
+
return [
|
|
85
|
+
stored_file
|
|
86
|
+
for stored_file in stored_files
|
|
87
|
+
if _is_in_any(stored_file.attachment, synced_collection_paths)
|
|
88
|
+
and not index.has_content(stored_file.md5)
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _is_in_any(
|
|
93
|
+
attachment: Attachment, collection_paths: Collection[CollectionPath]
|
|
94
|
+
) -> bool:
|
|
95
|
+
return not attachment.collection_paths.isdisjoint(collection_paths)
|
zync/core/sync/index.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from zync.core.concurrency import map_concurrently
|
|
4
|
+
from zync.core.folders import FolderTree
|
|
5
|
+
from zync.core.library import AttachedFiles, Attachment, CollectionPath
|
|
6
|
+
from zync.core.sync.paths import comparable_path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class AttachmentIndex:
|
|
10
|
+
"""Finds the library item that holds a file.
|
|
11
|
+
|
|
12
|
+
Linked files are found by their path, stored copies by their content.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
contents: The MD5 hex digests of the files that may match a stored copy.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self, attached_files: AttachedFiles, *, contents: dict[Path, str]
|
|
20
|
+
) -> None:
|
|
21
|
+
self._contents = contents
|
|
22
|
+
self._item_keys_by_path: dict[str, str] = {}
|
|
23
|
+
self._item_keys_by_content: dict[str, str] = {}
|
|
24
|
+
self._collection_paths: dict[str, set[CollectionPath]] = {}
|
|
25
|
+
for linked_file in attached_files.linked_files:
|
|
26
|
+
self._item_keys_by_path.setdefault(
|
|
27
|
+
comparable_path(linked_file.path), linked_file.attachment.item_key
|
|
28
|
+
)
|
|
29
|
+
self._add_collection_paths(linked_file.attachment)
|
|
30
|
+
for stored_file in attached_files.stored_files:
|
|
31
|
+
self._item_keys_by_content.setdefault(
|
|
32
|
+
stored_file.md5, stored_file.attachment.item_key
|
|
33
|
+
)
|
|
34
|
+
self._add_collection_paths(stored_file.attachment)
|
|
35
|
+
|
|
36
|
+
def has_content(self, md5: str) -> bool:
|
|
37
|
+
"""Return whether one of the indexed files has this content."""
|
|
38
|
+
return md5 in self._contents.values()
|
|
39
|
+
|
|
40
|
+
def is_attached(self, file: Path) -> bool:
|
|
41
|
+
return self._is_linked(file) or self._has_stored_copy(file)
|
|
42
|
+
|
|
43
|
+
def is_in_collection(self, file: Path, collection_path: CollectionPath) -> bool:
|
|
44
|
+
return (
|
|
45
|
+
self.is_attached(file)
|
|
46
|
+
and collection_path in self._collection_paths[self.item_key(file)]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def item_key(self, file: Path) -> str:
|
|
50
|
+
"""Return the key of the item that holds the file, which must be attached."""
|
|
51
|
+
return (
|
|
52
|
+
self._item_keys_by_path.get(comparable_path(file))
|
|
53
|
+
or self._item_keys_by_content[self._contents[file]]
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def record_membership(self, item_key: str, collection_path: CollectionPath) -> None:
|
|
57
|
+
"""Note that the item was added to the collection."""
|
|
58
|
+
self._collection_paths[item_key].add(collection_path)
|
|
59
|
+
|
|
60
|
+
def _is_linked(self, file: Path) -> bool:
|
|
61
|
+
return comparable_path(file) in self._item_keys_by_path
|
|
62
|
+
|
|
63
|
+
def _has_stored_copy(self, file: Path) -> bool:
|
|
64
|
+
return (
|
|
65
|
+
file in self._contents
|
|
66
|
+
and self._contents[file] in self._item_keys_by_content
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def _add_collection_paths(self, attachment: Attachment) -> None:
|
|
70
|
+
self._collection_paths.setdefault(attachment.item_key, set()).update(
|
|
71
|
+
attachment.collection_paths
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
async def read_contents(
|
|
76
|
+
files: list[Path], *, folder_tree: FolderTree, attached_files: AttachedFiles
|
|
77
|
+
) -> dict[Path, str]:
|
|
78
|
+
"""Return the MD5 hex digests of the files, read concurrently.
|
|
79
|
+
|
|
80
|
+
Contents only matter for comparing files with stored copies, so without
|
|
81
|
+
stored copies in the library no file is read.
|
|
82
|
+
"""
|
|
83
|
+
if not attached_files.stored_files:
|
|
84
|
+
return {}
|
|
85
|
+
return dict(zip(files, await map_concurrently(folder_tree.md5, files), strict=True))
|
zync/core/sync/paths.py
ADDED
zync/core/sync/report.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from zync.core.library import CollectionPath
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class CollectionUpdate:
|
|
9
|
+
"""The changes that one sync made to one collection.
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
new_files: Files that were not in the library and were added as
|
|
13
|
+
new attachments.
|
|
14
|
+
existing_files: Files that were already in the library, whose top-level
|
|
15
|
+
items were added to the collection.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
collection_path: CollectionPath
|
|
19
|
+
new_files: list[Path]
|
|
20
|
+
existing_files: list[Path]
|
|
21
|
+
|
|
22
|
+
def has_changes(self) -> bool:
|
|
23
|
+
return bool(self.new_files or self.existing_files)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class SyncReport:
|
|
28
|
+
"""What one sync changed.
|
|
29
|
+
|
|
30
|
+
Attributes:
|
|
31
|
+
collection_updates: One update per collection that got new items.
|
|
32
|
+
deleted_files: The paths of linked files, and the titles of stored
|
|
33
|
+
copies, whose files were deleted from the synced folders.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
collection_updates: list[CollectionUpdate]
|
|
37
|
+
deleted_files: list[str]
|
zync/core/sync/zync.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from zync.core.folders import (
|
|
4
|
+
FolderTree,
|
|
5
|
+
SyncedFile,
|
|
6
|
+
find_synced_files,
|
|
7
|
+
is_in_synced_folder,
|
|
8
|
+
)
|
|
9
|
+
from zync.core.library import AttachedFiles, CollectionPath, ZoteroLibrary
|
|
10
|
+
from zync.core.patterns import FolderPattern
|
|
11
|
+
from zync.core.settings import SyncSettings
|
|
12
|
+
from zync.core.sync.additions import Additions
|
|
13
|
+
from zync.core.sync.deletions import DeletedFiles, find_deleted_files
|
|
14
|
+
from zync.core.sync.index import AttachmentIndex, read_contents
|
|
15
|
+
from zync.core.sync.report import CollectionUpdate, SyncReport
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Zync:
|
|
19
|
+
"""Adds the files of the synced folders below a root folder to a Zotero library.
|
|
20
|
+
|
|
21
|
+
Raises:
|
|
22
|
+
InvalidPatternError: A folder pattern in the settings cannot be used.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
*,
|
|
28
|
+
root: Path,
|
|
29
|
+
folder_tree: FolderTree,
|
|
30
|
+
library: ZoteroLibrary,
|
|
31
|
+
settings: SyncSettings = SyncSettings(),
|
|
32
|
+
) -> None:
|
|
33
|
+
self._root = root
|
|
34
|
+
self._folder_tree = folder_tree
|
|
35
|
+
self._library = library
|
|
36
|
+
self._settings = settings
|
|
37
|
+
self._patterns = [
|
|
38
|
+
FolderPattern(pattern) for pattern in settings.folder_patterns
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
async def synchronize(self) -> SyncReport:
|
|
42
|
+
"""Make the library match the files in the synced folders.
|
|
43
|
+
|
|
44
|
+
A file that is already in the library, linked by its path or stored
|
|
45
|
+
with the same content, is added to its collection instead of being
|
|
46
|
+
added again. The removal mode decides what happens to the attachments
|
|
47
|
+
of deleted files.
|
|
48
|
+
"""
|
|
49
|
+
files_by_collection = _group_by_collection(await self._find_synced_files())
|
|
50
|
+
attached_files = await self._library.attached_files()
|
|
51
|
+
index = await self._index(attached_files, files_by_collection)
|
|
52
|
+
updates = await self._add_files(files_by_collection, index)
|
|
53
|
+
deleted_files = self._find_deleted_files(
|
|
54
|
+
attached_files, files_by_collection, index
|
|
55
|
+
)
|
|
56
|
+
await self._settings.removal_mode.remove(
|
|
57
|
+
deleted_files.attachments(), library=self._library
|
|
58
|
+
)
|
|
59
|
+
return SyncReport(
|
|
60
|
+
collection_updates=[update for update in updates if update.has_changes()],
|
|
61
|
+
deleted_files=deleted_files.names(),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
async def _find_synced_files(self) -> list[SyncedFile]:
|
|
65
|
+
return await find_synced_files(
|
|
66
|
+
root=self._root,
|
|
67
|
+
folder_tree=self._folder_tree,
|
|
68
|
+
patterns=self._patterns,
|
|
69
|
+
settings=self._settings,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
async def _index(
|
|
73
|
+
self,
|
|
74
|
+
attached_files: AttachedFiles,
|
|
75
|
+
files_by_collection: dict[CollectionPath, list[Path]],
|
|
76
|
+
) -> AttachmentIndex:
|
|
77
|
+
contents = await read_contents(
|
|
78
|
+
_all_files(files_by_collection),
|
|
79
|
+
folder_tree=self._folder_tree,
|
|
80
|
+
attached_files=attached_files,
|
|
81
|
+
)
|
|
82
|
+
return AttachmentIndex(attached_files, contents=contents)
|
|
83
|
+
|
|
84
|
+
async def _add_files(
|
|
85
|
+
self,
|
|
86
|
+
files_by_collection: dict[CollectionPath, list[Path]],
|
|
87
|
+
index: AttachmentIndex,
|
|
88
|
+
) -> list[CollectionUpdate]:
|
|
89
|
+
additions = Additions(
|
|
90
|
+
library=self._library, settings=self._settings, index=index
|
|
91
|
+
)
|
|
92
|
+
return [
|
|
93
|
+
await additions.add(files, collection_path=collection_path)
|
|
94
|
+
for collection_path, files in files_by_collection.items()
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
def _find_deleted_files(
|
|
98
|
+
self,
|
|
99
|
+
attached_files: AttachedFiles,
|
|
100
|
+
files_by_collection: dict[CollectionPath, list[Path]],
|
|
101
|
+
index: AttachmentIndex,
|
|
102
|
+
) -> DeletedFiles:
|
|
103
|
+
return find_deleted_files(
|
|
104
|
+
attached_files,
|
|
105
|
+
existing_files=_all_files(files_by_collection),
|
|
106
|
+
synced_collection_paths=files_by_collection.keys(),
|
|
107
|
+
index=index,
|
|
108
|
+
is_in_synced_folder=self._is_in_synced_folder,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
def _is_in_synced_folder(self, file: Path) -> bool:
|
|
112
|
+
return is_in_synced_folder(
|
|
113
|
+
file,
|
|
114
|
+
root=self._root,
|
|
115
|
+
folder_tree=self._folder_tree,
|
|
116
|
+
patterns=self._patterns,
|
|
117
|
+
settings=self._settings,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _group_by_collection(
|
|
122
|
+
synced_files: list[SyncedFile],
|
|
123
|
+
) -> dict[CollectionPath, list[Path]]:
|
|
124
|
+
files_by_collection: dict[CollectionPath, list[Path]] = {}
|
|
125
|
+
for synced_file in synced_files:
|
|
126
|
+
files_by_collection.setdefault(synced_file.collection_path, []).append(
|
|
127
|
+
synced_file.path
|
|
128
|
+
)
|
|
129
|
+
return files_by_collection
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _all_files(files_by_collection: dict[CollectionPath, list[Path]]) -> list[Path]:
|
|
133
|
+
return [file for files in files_by_collection.values() for file in files]
|
zync/infra/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from .disk import LocalFolderTree as LocalFolderTree
|
|
2
|
+
from .zotero import (
|
|
3
|
+
LocalAuthorizationError as LocalAuthorizationError,
|
|
4
|
+
MissingLocalApiKeyError as MissingLocalApiKeyError,
|
|
5
|
+
ZoteroDesktopLibrary as ZoteroDesktopLibrary,
|
|
6
|
+
ZoteroWriteError as ZoteroWriteError,
|
|
7
|
+
authorize_local_api as authorize_local_api,
|
|
8
|
+
load_local_api_key as load_local_api_key,
|
|
9
|
+
save_local_api_key as save_local_api_key,
|
|
10
|
+
)
|
zync/infra/disk.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import trio
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class LocalFolderTree:
|
|
8
|
+
"""The folders and files on the local disk."""
|
|
9
|
+
|
|
10
|
+
async def subfolders(self, folder: Path) -> list[Path]:
|
|
11
|
+
return await trio.to_thread.run_sync(_subfolders, folder)
|
|
12
|
+
|
|
13
|
+
async def files(self, folder: Path) -> list[Path]:
|
|
14
|
+
return await trio.to_thread.run_sync(_files, folder)
|
|
15
|
+
|
|
16
|
+
async def md5(self, file: Path) -> str:
|
|
17
|
+
return await trio.to_thread.run_sync(_md5, file)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _subfolders(folder: Path) -> list[Path]:
|
|
21
|
+
return sorted(entry for entry in folder.iterdir() if entry.is_dir())
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _files(folder: Path) -> list[Path]:
|
|
25
|
+
return sorted(entry for entry in folder.iterdir() if entry.is_file())
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _md5(file: Path) -> str:
|
|
29
|
+
with file.open("rb") as content:
|
|
30
|
+
return hashlib.file_digest(content, "md5").hexdigest()
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .credentials import (
|
|
2
|
+
MissingLocalApiKeyError as MissingLocalApiKeyError,
|
|
3
|
+
load_local_api_key as load_local_api_key,
|
|
4
|
+
save_local_api_key as save_local_api_key,
|
|
5
|
+
)
|
|
6
|
+
from .library import (
|
|
7
|
+
LocalAuthorizationError as LocalAuthorizationError,
|
|
8
|
+
ZoteroDesktopLibrary as ZoteroDesktopLibrary,
|
|
9
|
+
ZoteroWriteError as ZoteroWriteError,
|
|
10
|
+
authorize_local_api as authorize_local_api,
|
|
11
|
+
)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import trio
|
|
4
|
+
from platformdirs import user_config_path
|
|
5
|
+
|
|
6
|
+
LOCAL_API_KEY_FILE = user_config_path("zync") / "local-api-key"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MissingLocalApiKeyError(Exception):
|
|
10
|
+
"""No local API key has been saved yet."""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
async def save_local_api_key(key: str) -> Path:
|
|
14
|
+
"""Save the key for later runs and return the file it was saved to."""
|
|
15
|
+
key_file = trio.Path(LOCAL_API_KEY_FILE)
|
|
16
|
+
await key_file.parent.mkdir(parents=True, exist_ok=True)
|
|
17
|
+
await key_file.write_text(key, encoding="utf-8")
|
|
18
|
+
return LOCAL_API_KEY_FILE
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def load_local_api_key() -> str:
|
|
22
|
+
"""Return the saved key.
|
|
23
|
+
|
|
24
|
+
Raises:
|
|
25
|
+
MissingLocalApiKeyError: The "authorize" command has not been run.
|
|
26
|
+
"""
|
|
27
|
+
key_file = trio.Path(LOCAL_API_KEY_FILE)
|
|
28
|
+
if not await key_file.exists():
|
|
29
|
+
raise MissingLocalApiKeyError(
|
|
30
|
+
"Zync is not allowed to change your Zotero library yet: expected the file "
|
|
31
|
+
f'{LOCAL_API_KEY_FILE}, found none. Run "zync authorize" first.'
|
|
32
|
+
)
|
|
33
|
+
return (await key_file.read_text(encoding="utf-8")).strip()
|