-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepsync_models.py
More file actions
295 lines (251 loc) · 10.1 KB
/
Copy pathkeepsync_models.py
File metadata and controls
295 lines (251 loc) · 10.1 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
"""Core note data models and normalization helpers for KeepSync Notes."""
import hashlib
import json
import mimetypes
import re
import uuid
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum
from pathlib import Path
from typing import Any, List, Optional
KEEP_COLOR_PALETTE = {
"": ("Default", "#1e293b"),
"red": ("Red", "#f28b82"),
"orange": ("Orange", "#fbbc04"),
"yellow": ("Yellow", "#fff475"),
"green": ("Green", "#ccff90"),
"teal": ("Teal", "#a7ffeb"),
"blue": ("Blue", "#cbf0f8"),
"darkblue": ("Dark blue", "#aecbfa"),
"purple": ("Purple", "#d7aefb"),
"pink": ("Pink", "#fdcfe8"),
"brown": ("Brown", "#e6c9a8"),
"gray": ("Gray", "#e8eaed"),
}
KEEP_COLOR_ALIASES = {
"default": "",
"white": "",
"none": "",
"": "",
"dark_blue": "darkblue",
"dark blue": "darkblue",
}
def normalize_keep_color(value: Any) -> str:
"""Normalize Keep color names from Takeout/gkeepapi/storage."""
if value is None:
return ""
color = str(value).strip().lower().replace("colorvalue.", "")
color = color.replace("-", "_")
color = KEEP_COLOR_ALIASES.get(color, color)
return color if color in KEEP_COLOR_PALETTE else ""
def keep_color_hex(value: Any) -> str:
color = normalize_keep_color(value)
return KEEP_COLOR_PALETTE[color][1]
def keep_color_name(value: Any) -> str:
color = normalize_keep_color(value)
return KEEP_COLOR_PALETTE[color][0]
def clamp_checklist_indent(value: Any) -> int:
try:
indent = int(value or 0)
except (TypeError, ValueError):
indent = 0
return max(0, min(4, indent))
def normalize_people(values: Any) -> List[str]:
"""Normalize shared-with metadata from Takeout/export structures."""
people = []
if not values:
return people
if isinstance(values, str):
values = [values]
if isinstance(values, dict):
values = [values]
for item in values:
person = ""
if isinstance(item, str):
person = item
elif isinstance(item, dict):
for key in ("email", "emailAddress", "displayName", "name", "userName"):
if item.get(key):
person = str(item[key])
break
if not person and isinstance(item.get("user"), dict):
nested_people = normalize_people(item["user"])
person = nested_people[0] if nested_people else ""
person = person.strip()
if person and person not in people:
people.append(person)
return people
def sanitize_filename(value: str) -> str:
name = Path(value or "attachment").name
return re.sub(r"[^A-Za-z0-9._ -]+", "_", name).strip(" .") or "attachment"
def guess_attachment_mime(path: str, fallback: str = "") -> str:
guessed, _ = mimetypes.guess_type(path or "")
return fallback or guessed or "application/octet-stream"
class SyncStatus(Enum):
LOCAL_ONLY = "local_only"
SYNCED = "synced"
PENDING_PUSH = "pending_push"
PENDING_PULL = "pending_pull"
CONFLICT = "conflict"
DELETED_REMOTE = "deleted_remote"
ERROR = "error"
class NoteType(Enum):
NOTE = "note"
CHECKLIST = "checklist"
@dataclass
class Attachment:
filename: str
stored_path: str
mime_type: str = ""
source_path: str = ""
id: str = field(default_factory=lambda: str(uuid.uuid4()))
def __post_init__(self):
self.filename = sanitize_filename(self.filename)
self.mime_type = guess_attachment_mime(self.stored_path or self.filename, self.mime_type)
def to_dict(self) -> dict:
return {
"id": self.id,
"filename": self.filename,
"stored_path": self.stored_path,
"source_path": self.source_path,
"mime_type": self.mime_type,
}
@classmethod
def from_dict(cls, data: dict) -> "Attachment":
return cls(
id=data.get("id", str(uuid.uuid4())),
filename=data.get("filename", Path(data.get("stored_path", "attachment")).name),
stored_path=data.get("stored_path", ""),
source_path=data.get("source_path", ""),
mime_type=data.get("mime_type", ""),
)
@property
def is_image(self) -> bool:
return self.mime_type.startswith("image/")
@property
def exists(self) -> bool:
return bool(self.stored_path and Path(self.stored_path).exists())
@dataclass
class ChecklistItem:
text: str
checked: bool = False
id: str = field(default_factory=lambda: str(uuid.uuid4()))
indent: int = 0
def to_dict(self) -> dict:
return {"id": self.id, "text": self.text, "checked": self.checked, "indent": self.indent}
@classmethod
def from_dict(cls, data: dict) -> "ChecklistItem":
return cls(
id=data.get("id", str(uuid.uuid4())),
text=data.get("text", ""),
checked=data.get("checked", False),
indent=clamp_checklist_indent(data.get("indent", 0)),
)
@dataclass
class Note:
id: str
title: str
content: str
note_type: NoteType = NoteType.NOTE
checklist_items: List[ChecklistItem] = field(default_factory=list)
labels: List[str] = field(default_factory=list)
pinned: bool = False
archived: bool = False
trashed: bool = False
color: str = ""
reminder_at: Optional[datetime] = None
reminder_location: str = ""
reminder_notified: bool = False
shared_with: List[str] = field(default_factory=list)
attachments: List[Attachment] = field(default_factory=list)
keep_id: Optional[str] = None
sync_status: SyncStatus = SyncStatus.LOCAL_ONLY
local_modified: Optional[datetime] = None
remote_modified: Optional[datetime] = None
content_hash: str = ""
created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
updated_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
def __post_init__(self):
self.color = normalize_keep_color(self.color)
self.shared_with = normalize_people(self.shared_with)
self.attachments = [Attachment.from_dict(a) if isinstance(a, dict) else a for a in self.attachments]
for item in self.checklist_items:
item.indent = clamp_checklist_indent(item.indent)
self.update_hash()
def update_hash(self):
"""Generate content hash for change detection."""
content = (
f"{self.title}|{self.content}|{json.dumps([i.to_dict() for i in self.checklist_items])}|"
f"{json.dumps(self.labels)}|{self.pinned}|{self.archived}|{self.trashed}|{self.color}|"
f"{self.reminder_at.isoformat() if self.reminder_at else ''}|{self.reminder_location}|"
f"{json.dumps(self.shared_with)}|{json.dumps([a.to_dict() for a in self.attachments])}"
)
self.content_hash = hashlib.md5(content.encode()).hexdigest()
def to_dict(self) -> dict:
return {
"id": self.id,
"title": self.title,
"content": self.content,
"note_type": self.note_type.value,
"checklist_items": [i.to_dict() for i in self.checklist_items],
"labels": self.labels,
"pinned": self.pinned,
"archived": self.archived,
"trashed": self.trashed,
"color": self.color,
"reminder_at": self.reminder_at.isoformat() if self.reminder_at else None,
"reminder_location": self.reminder_location,
"reminder_notified": self.reminder_notified,
"shared_with": self.shared_with,
"attachments": [a.to_dict() for a in self.attachments],
"keep_id": self.keep_id,
"sync_status": self.sync_status.value,
"local_modified": self.local_modified.isoformat() if self.local_modified else None,
"remote_modified": self.remote_modified.isoformat() if self.remote_modified else None,
"content_hash": self.content_hash,
"created_at": self.created_at.isoformat(),
"updated_at": self.updated_at.isoformat(),
}
@classmethod
def from_dict(cls, data: dict) -> "Note":
return cls(
id=data["id"],
title=data.get("title", ""),
content=data.get("content", ""),
note_type=NoteType(data.get("note_type", "note")),
checklist_items=[ChecklistItem.from_dict(i) for i in data.get("checklist_items", [])],
labels=data.get("labels", []),
pinned=data.get("pinned", False),
archived=data.get("archived", False),
trashed=data.get("trashed", False),
color=normalize_keep_color(data.get("color", "")),
reminder_at=datetime.fromisoformat(data["reminder_at"]) if data.get("reminder_at") else None,
reminder_location=data.get("reminder_location", ""),
reminder_notified=data.get("reminder_notified", False),
shared_with=normalize_people(data.get("shared_with", [])),
attachments=[Attachment.from_dict(a) for a in data.get("attachments", [])],
keep_id=data.get("keep_id"),
sync_status=SyncStatus(data.get("sync_status", "local_only")),
local_modified=datetime.fromisoformat(data["local_modified"]) if data.get("local_modified") else None,
remote_modified=datetime.fromisoformat(data["remote_modified"]) if data.get("remote_modified") else None,
content_hash=data.get("content_hash", ""),
created_at=datetime.fromisoformat(data["created_at"]) if data.get("created_at") else datetime.now(timezone.utc),
updated_at=datetime.fromisoformat(data["updated_at"]) if data.get("updated_at") else datetime.now(timezone.utc),
)
@dataclass
class Label:
id: str
name: str
color: str = ""
keep_id: Optional[str] = None
def to_dict(self) -> dict:
return {"id": self.id, "name": self.name, "color": self.color, "keep_id": self.keep_id}
@classmethod
def from_dict(cls, data: dict) -> "Label":
return cls(
id=data["id"],
name=data["name"],
color=data.get("color", ""),
keep_id=data.get("keep_id"),
)