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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382 | class NotesService(BaseService):
"""
Typed, developer-friendly Notes API.
The service exposes lightweight listing helpers, full-note retrieval,
attachment download helpers, and HTML render/export utilities while hiding
most CloudKit details behind ``NoteSummary``, ``Note``, and ``Attachment``
models.
"""
_CONTAINER = "com.apple.notes"
_ENV = "production"
_SCOPE = "private"
def __init__(
self,
service_root: str,
session: PyiCloudSession,
params: dict[str, str],
*,
cloudkit_validation_extra: CloudKitExtraMode | None = None,
) -> None:
super().__init__(service_root=service_root, session=session, params=params)
endpoint = (
f"{self.service_root}/database/1/{self._CONTAINER}/{self._ENV}/"
f"{self._SCOPE}"
)
# Sensible defaults; lower-case booleans are applied in the raw client
base_params = {
"remapEnums": True,
"getCurrentSyncToken": True,
**(params or {}),
}
self._raw = CloudKitNotesClient(
base_url=endpoint,
session=session,
base_params=base_params,
validation_extra=cloudkit_validation_extra,
)
# In-memory caches
self._folder_name_cache: dict[str, str | None] = {}
self._attachment_meta_cache: dict[str, Attachment] = {}
# -------------------------- Public API methods ---------------------------
def recents(self, *, limit: int = 50) -> Iterable[NoteSummary]:
"""
Yield the newest note summaries, ordered most-recent first.
Args:
limit: Maximum number of notes to yield.
Yields:
``NoteSummary`` instances with lightweight metadata suitable for
feeds, selectors, or navigation UIs.
"""
if limit <= 0:
return
desired_keys = [
NotesDesiredKey.TITLE_ENCRYPTED,
NotesDesiredKey.SNIPPET_ENCRYPTED,
NotesDesiredKey.MODIFICATION_DATE,
NotesDesiredKey.DELETED,
NotesDesiredKey.FOLDER,
NotesDesiredKey.FIRST_ATTACHMENT_UTI_ENCRYPTED,
NotesDesiredKey.FIRST_ATTACHMENT_THUMBNAIL,
NotesDesiredKey.FIRST_ATTACHMENT_THUMBNAIL_ORIENTATION,
NotesDesiredKey.ATTACHMENTS,
]
query = CKQueryObject(
recordType="SearchIndexes",
filterBy=[
CKQueryFilterBy(
comparator="EQUALS",
fieldName="indexName",
fieldValue=CKFVString(type="STRING", value="recents"),
)
],
sortBy=[CKQuerySortBy(fieldName="modTime", ascending=False)],
)
yielded = 0
cont: str | None = None
LOGGER.debug("Fetching recents: limit=%d", limit)
while True:
remaining = limit - yielded
if remaining <= 0:
return
resp: CKQueryResponse = self._raw.query(
query=query,
zone_id=NOTES_ZONE_REQ,
desired_keys=self._coerce_keys(desired_keys),
results_limit=min(200, remaining),
continuation=cont,
)
for rec in resp.records:
if isinstance(rec, CKRecord):
summary = self._summary_from_record(rec)
yielded += 1
yield summary
if yielded >= limit:
LOGGER.debug(
"Recents: yielded %d notes (limit reached)", yielded
)
return
cont = getattr(resp, "continuationMarker", None)
if not cont:
LOGGER.debug("Recents: no more continuation marker, done.")
return
def recents_in_folder(
self, folder_id: str, *, limit: int = 20
) -> Iterable[NoteSummary]:
"""
Yield recent note summaries that belong to ``folder_id``.
This is a convenience helper that filters the global recents feed rather
than scanning the full Notes history.
"""
if limit <= 0:
return
emitted = 0
# Pull a larger window than requested to increase the chance of finding
# matches fast.
window = max(200, limit * 5)
LOGGER.debug(
"Fetching recents in folder: folder_id=%s limit=%d", folder_id, limit
)
for n in self.recents(limit=window):
if n.folder_id == folder_id and not n.is_deleted:
yield n
emitted += 1
if emitted >= limit:
LOGGER.debug(
"Recents in folder: yielded %d notes (limit reached)", emitted
)
return
def iter_all(self, *, since: str | None = None) -> Iterable[NoteSummary]:
"""
Yield note summaries from the Notes changes feed.
Args:
since: Optional sync token from ``sync_cursor()``. When provided, the
iterator yields only note records changed since that cursor.
Yields:
``NoteSummary`` instances for full exports, indexing jobs, or local
cache refreshes.
"""
if self._matches_current_sync_cursor(since):
LOGGER.debug("Skipping Notes full scan because sync token is current")
return
LOGGER.debug("Iterating all notes%s", f" since={since}" if since else "")
for zone in self._raw.changes(
zone_req=CKZoneChangesZoneReq(
zoneID=NOTES_ZONE,
desiredRecordTypes=[NotesRecordType.Note],
desiredKeys=self._coerce_keys([
NotesDesiredKey.TITLE_ENCRYPTED,
NotesDesiredKey.SNIPPET_ENCRYPTED,
NotesDesiredKey.MODIFICATION_DATE,
NotesDesiredKey.DELETED,
NotesDesiredKey.FOLDER,
NotesDesiredKey.ATTACHMENTS,
]),
syncToken=since,
reverse=False,
),
# CloudKit page sizing is implicit; we cap our own yield volume by iterating
):
for rec in zone.records:
if isinstance(rec, CKRecord):
yield self._summary_from_record(rec)
def folders(self) -> Iterable[NoteFolder]:
"""Yield all Notes folders, including nested folders and their parents."""
# A root-only search index omits descendants, and HasSubfolder is not
# supplied for every folder. Enumerate Folder records and derive the
# relationship from ParentFolder rather than trusting an absent flag.
records: list[CKRecord] = []
for zone in self._raw.changes(
zone_req=CKZoneChangesZoneReq(
zoneID=NOTES_ZONE,
desiredRecordTypes=[NotesRecordType.Folder],
desiredKeys=self._coerce_keys([
NotesDesiredKey.TITLE_ENCRYPTED,
"ParentFolder",
]),
reverse=False,
)
):
records.extend(
record
for record in zone.records
if isinstance(record, CKRecord) and record.recordType == "Folder"
)
parents: dict[str, str | None] = {}
for record in records:
parent = record.fields.get_value("ParentFolder")
name = getattr(parent, "recordName", None)
parents[record.recordName] = (
name if isinstance(name, str) and name else None
)
with_children = {parent for parent in parents.values() if parent}
for record in records:
name = self._decode_encrypted(record.fields.get_value("TitleEncrypted"))
self._folder_name_cache.setdefault(record.recordName, name)
yield NoteFolder(
id=record.recordName,
name=name,
parent_id=parents[record.recordName],
has_subfolders=record.recordName in with_children,
count=None,
record_change_tag=record.recordChangeTag,
)
def in_folder(
self, folder_id: str, *, limit: int | None = None
) -> Iterable[NoteSummary]:
"""
Yield non-deleted notes in ``folder_id``, ordered newest first.
Args:
folder_id: Folder identifier from ``folders()`` or note metadata.
limit: Optional maximum number of notes to yield.
"""
emitted = 0
LOGGER.debug(
"Fetching notes in folder: folder_id=%s limit=%s", folder_id, limit
)
for zone in self._raw.changes(
zone_req=CKZoneChangesZoneReq(
zoneID=NOTES_ZONE,
desiredRecordTypes=[NotesRecordType.Note],
desiredKeys=self._coerce_keys([
NotesDesiredKey.TITLE_ENCRYPTED,
NotesDesiredKey.SNIPPET_ENCRYPTED,
NotesDesiredKey.MODIFICATION_DATE,
NotesDesiredKey.DELETED,
NotesDesiredKey.FOLDER,
NotesDesiredKey.FIRST_ATTACHMENT_UTI_ENCRYPTED,
NotesDesiredKey.FIRST_ATTACHMENT_THUMBNAIL,
NotesDesiredKey.FIRST_ATTACHMENT_THUMBNAIL_ORIENTATION,
NotesDesiredKey.ATTACHMENTS,
]),
reverse=True, # newest first
)
):
for rec in zone.records:
if not isinstance(rec, CKRecord):
continue
rec_folder_id = self._extract_folder_id(rec)
if self._is_deleted(rec) or rec_folder_id != folder_id:
continue
yield self._summary_from_record(rec)
emitted += 1
if limit and emitted >= limit:
LOGGER.debug(
"Notes in folder: yielded %d notes (limit reached)", emitted
)
return
def get(
self,
note_id: str,
*,
with_attachments: bool = False,
password: str | None = None,
) -> Note:
"""
Return a single note with decoded text and optional attachment metadata.
Args:
note_id: The CloudKit note record identifier.
with_attachments: When ``True``, resolve ``Attachment`` metadata and
include it on the returned ``Note``. Not supported for locked notes.
password: Optional custom Notes password for local, read-only decryption.
Raises:
NoteNotFound: If the note does not exist.
NoteLockedError: If the note is passphrase-locked and its content
cannot be read.
"""
LOGGER.debug(
"Fetching note: note_id=%s with_attachments=%s", note_id, with_attachments
)
target = self._lookup_note_record(
note_id,
desired_keys=[
NotesDesiredKey.TITLE_ENCRYPTED,
NotesDesiredKey.SNIPPET_ENCRYPTED,
NotesDesiredKey.MODIFICATION_DATE,
NotesDesiredKey.DELETED,
NotesDesiredKey.FOLDER,
NotesDesiredKey.ATTACHMENTS,
"TextDataEncrypted", # may or may not be present
],
)
summary = self._summary_from_record(target)
note_body = (
self._read_locked_body(target, summary, password, with_attachments)
if summary.is_locked
else self._decode_note_body(target)
)
# Minimal breadcrumbs for body decode outcome
try:
if note_body and note_body.text:
LOGGER.info(
"notes.body.decoded ok id=%s len=%d",
summary.id,
len(note_body.text),
)
else:
LOGGER.info("notes.body.decoded empty id=%s", summary.id)
except Exception:
LOGGER.info(
"notes.body.decoded %s",
"ok" if note_body and note_body.text else "empty",
)
attachments: list[Attachment] | None = None
html: str | None = None
text = note_body.text if note_body else None
attachment_ids: list[AttachmentId] = []
if note_body and note_body.attachment_ids:
attachment_ids = note_body.attachment_ids
if with_attachments:
attachments = self._resolve_attachments_for_record(
target, attachment_ids=attachment_ids
)
return Note(
id=summary.id,
title=summary.title,
snippet=summary.snippet,
modified_at=summary.modified_at,
folder_id=summary.folder_id,
folder_name=summary.folder_name,
is_deleted=summary.is_deleted,
is_locked=summary.is_locked,
text=text,
html=html,
attachments=attachments,
record_change_tag=target.recordChangeTag,
)
def _read_locked_body(
self,
record: CKRecord,
summary: NoteSummary,
password: str | None,
with_attachments: bool,
) -> NoteBody:
if password is None:
LOGGER.warning("Note is locked and cannot be read: %s", summary.id)
raise NoteLockedError(
f"Note '{summary.title or summary.id}' is locked and cannot be read."
)
if with_attachments:
raise NoteLockedError(
"Attachments are not supported for locked Notes reads."
)
raw = record.fields.get_value("TextDataEncrypted")
error = "Could not read this custom-password locked note."
if not isinstance(raw, bytes) or not self._is_private_record(record):
raise NoteLockedError(error)
try:
plaintext = decrypt_locked_note_body(raw, password)
except ValueError:
raise NoteLockedError(error) from None
body = BodyDecoder().decode(plaintext, max_document_bytes=2 * 1024 * 1024)
if body is None or not body.text:
raise NoteLockedError(error)
return body
def create_note(
self, title: str, text: str | RichNoteBody = "", *, folder_id: str
) -> NoteWriteResult:
"""Create a plain- or rich-text note in an explicit private folder."""
if not folder_id.strip():
raise ValueError("A Notes folder ID is required")
content = encode_new_note(title, text)
self._verify_private_folder_chain(folder_id)
folder = NotesFolderReference(recordName=folder_id, zoneID=NOTES_ZONE_REQ)
now = time.time_ns() // 1_000_000
fields = {
"CreationDate": NotesWriteField(value=now),
"ModificationDate": NotesWriteField(value=now),
"TitleEncrypted": NotesWriteField(value=content.title),
"SnippetEncrypted": NotesWriteField(value=content.snippet),
"TextDataEncrypted": NotesWriteField(value=content.body),
"Folder": NotesWriteField(value=folder),
"Folders": NotesWriteField(value=[folder]),
"FirstAttachmentThumbnail": NotesWriteField(),
"FirstAttachmentUTIEncrypted": NotesWriteField(),
"TextDataAsset": NotesWriteField(),
}
operation = NotesModifyOperation(
operationType="create",
record=NotesWriteRecord(
recordName=str(uuid4()),
createShortGUID=True,
parent=CKWriteParent(recordName=folder_id),
fields=fields,
),
)
return self._raw.modify_note(operation)
def _require_write_folder(
self, folder_id: str, *, allow_trash: bool = False
) -> CKRecord:
"""Look up the exact active private folder required for a write."""
if not folder_id.strip() or (folder_id == _TRASH_FOLDER_ID and not allow_trash):
raise ValueError(
"Select a private destination folder outside Recently Deleted"
)
response = self._raw.lookup([folder_id])
if len(response.records) == 1:
record = response.records[0]
if (
isinstance(record, CKRecord)
and record.recordName == folder_id
and record.recordType == "Folder"
and not record.deleted
and not record.fields.get_value("Deleted")
and self._is_private_record(record)
):
return record
raise NotesError("The requested Notes folder is not available for writing")
def create_folder(
self, name: str, *, parent_id: str | None = None
) -> NoteWriteResult:
"""Create a private top-level folder or subfolder."""
title = self._encode_folder_title(name)
self._verify_private_folder_chain(parent_id)
return self._submit_folder_write(str(uuid4()), title, parent_id)
def rename_folder(
self, folder_id: str, name: str, *, expected_change_tag: str
) -> NoteWriteResult:
"""Rename a private folder without changing its parent."""
title = self._encode_folder_title(name)
record = self._require_mutable_folder(folder_id, expected_change_tag)
return self._submit_folder_write(
folder_id, title, self._folder_parent(record), tag=expected_change_tag
)
def move_folder(
self, folder_id: str, *, parent_id: str | None, expected_change_tag: str
) -> NoteWriteResult:
"""Move a private folder without creating an ancestry cycle."""
record = self._require_mutable_folder(folder_id, expected_change_tag)
if self._folder_parent(record) == parent_id:
raise NotesError("The folder is already at the requested location")
title = record.fields.get_value("TitleEncrypted")
if not isinstance(title, bytes) or not title:
raise NotesError("Could not verify the existing Notes folder name")
self._verify_private_folder_chain(parent_id, excluded=folder_id)
return self._submit_folder_write(
folder_id,
base64.b64encode(title).decode("ascii"),
parent_id,
tag=expected_change_tag,
clear_parent=parent_id is None,
)
def delete_folder(
self, folder_id: str, *, expected_change_tag: str, confirm_empty: bool = False
) -> bool:
"""Permanently delete an empty private folder after a contents check.
The server does not provide an atomic emptiness check. Do not move
items into the folder concurrently with deletion.
"""
if not confirm_empty:
raise ValueError("Folder deletion requires explicit confirmation")
self._require_mutable_folder(folder_id, expected_change_tag)
self._verify_empty_folder(folder_id)
self._require_mutable_folder(folder_id, expected_change_tag)
deleted = self._raw.delete_empty_folder(folder_id)
self._folder_name_cache.pop(folder_id, None)
return deleted
@staticmethod
def _encode_folder_title(name: str) -> str:
if (
not name.strip()
or name.splitlines() != [name]
or any(ord(char) < 32 for char in name)
):
raise ValueError("A Notes folder name must be a non-empty single line")
try:
return base64.b64encode(name.encode("utf-8")).decode("ascii")
except UnicodeEncodeError:
raise ValueError("A Notes folder name must be valid Unicode") from None
@staticmethod
def _folder_parent(record: CKRecord) -> str | None:
ref = record.fields.get_value("ParentFolder")
if ref is None and record.parent is None:
return None
if (
isinstance(ref, CKReference)
and ref.recordName
and (ref.zoneID is None or ref.zoneID.zoneName == NOTES_ZONE_NAME)
and (record.parent is None or record.parent.recordName == ref.recordName)
):
return ref.recordName
raise NotesError("Could not verify the existing Notes folder parent")
def _require_mutable_folder(self, folder_id: str, tag: str) -> CKRecord:
try:
valid_id = str(UUID(folder_id)) == folder_id.lower()
except ValueError:
valid_id = False
if not valid_id or not tag.strip():
raise ValueError(
"A user-created folder ID and its current revision are required"
)
record = self._require_write_folder(folder_id)
if record.recordChangeTag != tag:
raise NoteRevisionConflict(
"The folder revision changed; list the folders again"
)
self._verify_private_folder_chain(
self._folder_parent(record), excluded=folder_id
)
return record
def _verify_private_folder_chain(
self, ancestor: str | None, *, excluded: str | None = None
) -> None:
"""Check each ancestor for sharing and reject cyclic folder chains."""
visited = {excluded} if excluded is not None else set()
while ancestor is not None:
if ancestor in visited or len(visited) >= 64:
raise NotesError("Could not verify a safe Notes folder ancestry")
visited.add(ancestor)
ancestor = self._folder_parent(self._require_write_folder(ancestor))
def _submit_folder_write(
self,
folder_id: str,
title: str,
parent_id: str | None,
*,
tag: str | None = None,
clear_parent: bool = False,
) -> NoteWriteResult:
fields = {"TitleEncrypted": NotesWriteField(value=title)}
parent = None
if parent_id is not None:
fields["ParentFolder"] = NotesWriteField(
value=NotesFolderReference(recordName=parent_id, zoneID=NOTES_ZONE_REQ)
)
parent = NotesFolderParent(recordName=parent_id)
elif tag is not None:
fields["ParentFolder"] = NotesWriteField()
if clear_parent:
parent = NotesFolderParent(recordName=None)
result = self._raw.modify_note(
NotesModifyOperation(
operationType="create" if tag is None else "update",
record=NotesFolderWriteRecord(
recordName=folder_id,
fields=fields,
parent=parent,
recordChangeTag=tag,
),
)
)
self._folder_name_cache.pop(folder_id, None)
return result
def _verify_empty_folder(self, folder_id: str) -> None:
"""Require a complete zone scan without any live children."""
tokens: set[str] = set()
for zone in self._raw.changes(
zone_req=CKZoneChangesZoneReq(
zoneID=NOTES_ZONE,
desiredRecordTypes=["Folder", "Note", "PasswordProtectedNote"],
desiredKeys=["ParentFolder", "Folder", "Folders", "Deleted"],
reverse=False,
)
):
if (
zone.zoneID.zoneName != NOTES_ZONE_NAME
or zone.syncToken in tokens
or len(tokens) >= 256
or "records" not in zone.model_fields_set
):
break
tokens.add(zone.syncToken)
for record in zone.records:
if isinstance(record, CKErrorItem):
raise NotesError(_UNVERIFIED_EMPTY_FOLDER)
if isinstance(record, CKRecord) and self._is_live_child(
record, folder_id
):
raise NotesError("The Notes folder is not empty")
if zone.moreComing is False:
return
raise NotesError(_UNVERIFIED_EMPTY_FOLDER)
@staticmethod
def _is_live_child(record: CKRecord, folder_id: str) -> bool:
"""Check all supported parent references on a non-deleted record."""
if record.deleted or record.fields.get_value("Deleted"):
return False
parents = [
record.parent,
record.fields.get_value("ParentFolder"),
record.fields.get_value("Folder"),
]
folders = record.fields.get_value("Folders")
if isinstance(folders, list):
parents.extend(folders)
return any(
getattr(parent, "recordName", None) == folder_id for parent in parents
)
def update_note(
self,
note_id: str,
*,
title: str,
text: str | RichNoteBody,
expected_change_tag: str,
) -> NoteWriteResult:
"""Replace the title and body of a supported private note."""
record, folder_id = self._require_write_note(note_id, expected_change_tag)
if folder_id == _TRASH_FOLDER_ID:
raise NotesError(_RESTORE_BEFORE_EDIT)
fields = self._existing_write_fields(record, folder_id)
compressed = record.fields.get_value("TextDataEncrypted")
if not isinstance(compressed, bytes):
raise NotesError(_UNSUPPORTED_WRITE)
encoded = encode_note_edit(compressed, title, text)
fields.update(
TitleEncrypted=NotesWriteField(value=encoded.title),
SnippetEncrypted=NotesWriteField(value=encoded.snippet),
TextDataEncrypted=NotesWriteField(value=encoded.body),
ModificationDate=NotesWriteField(value=time.time_ns() // 1_000_000),
)
return self._submit_note_update(record, folder_id, fields)
def trash_note(self, note_id: str, *, expected_change_tag: str) -> NoteWriteResult:
"""Move a supported private note to Recently Deleted, not a tombstone."""
record, folder_id = self._require_write_note(
note_id, expected_change_tag, existing_image=None
)
if folder_id == _TRASH_FOLDER_ID:
raise NotesError("The note is already in Recently Deleted")
return self._relocate_note(record, _TRASH_FOLDER_ID)
def delete_note_forever(
self, note_id: str, *, expected_change_tag: str, confirm: bool = False
) -> NoteWriteResult:
"""Permanently delete one supported private note from Recently Deleted."""
if confirm is not True:
raise ValueError("Confirm permanent deletion explicitly")
record, folder_id = self._require_write_note(note_id, expected_change_tag)
if folder_id != _TRASH_FOLDER_ID:
raise NotesError(
"Only notes in Recently Deleted can be permanently deleted"
)
compressed = record.fields.get_value("TextDataEncrypted")
if not isinstance(compressed, bytes) or inline_note_objects(compressed):
raise NotesError(_UNSUPPORTED_WRITE)
fields = self._existing_write_fields(record, folder_id)
fields["Deleted"] = NotesWriteField(value=1)
return self._submit_note_update(record, folder_id, fields)
def set_checklist_item(
self, note_id: str, *, item: int, checked: bool, expected_change_tag: str
) -> NoteWriteResult:
"""Check or uncheck a one-based item without replacing the note body."""
record, folder_id = self._require_write_note(note_id, expected_change_tag)
if folder_id == _TRASH_FOLDER_ID:
raise NotesError(_RESTORE_BEFORE_EDIT)
compressed = record.fields.get_value("TextDataEncrypted")
if not isinstance(compressed, bytes):
raise NotesError(_UNSUPPORTED_WRITE)
body = encode_checklist_state(compressed, item, checked=checked)
if body is None:
return NoteWriteResult(id=note_id, record_change_tag=expected_change_tag)
fields = self._existing_write_fields(record, folder_id)
fields.update(
TextDataEncrypted=NotesWriteField(value=body),
ModificationDate=NotesWriteField(value=time.time_ns() // 1_000_000),
)
return self._submit_note_update(record, folder_id, fields)
def set_note_pinned(
self, note_id: str, *, pinned: bool, expected_change_tag: str
) -> NotePinResult:
"""Pin or unpin a private note without changing its content revision."""
record = self._lookup_note_record(note_id, require_single=True)
if (
not expected_change_tag.strip()
or record.recordChangeTag != expected_change_tag
):
raise NoteRevisionConflict(
"The note revision changed; fetch the note again"
)
folder = record.fields.get_value("Folder")
if (
record.recordType != "Note"
or record.deleted
or self._is_deleted(record)
or not self._is_private_record(record)
or not isinstance(folder, CKReference)
or not folder.recordName
or folder.zoneID is None
or folder.zoneID.zoneName != NOTES_ZONE_NAME
or folder.zoneID.zoneType != "REGULAR_CUSTOM_ZONE"
):
raise NotesError(_UNVERIFIED_PIN)
owner = folder.zoneID.ownerRecordName
if not owner or not re.fullmatch(r"_[0-9a-fA-F]{32}", owner) or "::" in note_id:
raise NotesError(_UNVERIFIED_PIN)
self._verify_note_source_folder(folder.recordName)
pin_id = (
f"Note_UserSpecific::{owner}/Private::{NOTES_ZONE_NAME}::{owner}::{note_id}"
)
existing = self._lookup_pin_record(pin_id, note_id, owner)
previous = (
existing.fields.get_value("IsPinned") if existing is not None else None
)
if previous not in (None, 0, 1):
raise NotesError(_UNVERIFIED_PIN)
if bool(previous) == pinned:
return NotePinResult(id=note_id, pinned=pinned, changed=False)
viewed = (
existing.fields.get_value("LastViewedModificationDate")
if existing is not None
else None
)
if viewed is not None and not isinstance(viewed, datetime):
raise NotesError(_UNVERIFIED_PIN)
fields = {
"IsPinned": NotesWriteField(value=int(pinned)),
"Note": NotesWriteField(
value=NotesFolderReference(
recordName=note_id, action="NONE", zoneID=NOTES_ZONE_REQ
)
),
"Folder": NotesWriteField(
value=NotesFolderReference(
recordName=folder.recordName,
action="NONE",
zoneID=CKZoneIDReq(
zoneName=NOTES_ZONE_NAME,
ownerRecordName=owner,
zoneType=folder.zoneID.zoneType,
),
)
),
"LastViewedModificationDate": NotesWriteField(
value=0 if viewed is None else self._date_millis(viewed)
),
}
self._raw.modify_note(
NotesModifyOperation(
operationType="create" if existing is None else "update",
record=NotesUserSpecificWriteRecord(
recordName=pin_id,
fields=fields,
recordChangeTag=existing.recordChangeTag
if existing is not None
else None,
),
),
public_id=note_id,
)
return NotePinResult(id=note_id, pinned=pinned, changed=True)
def _lookup_pin_record(
self, pin_id: str, note_id: str, owner: str
) -> CKRecord | None:
records = self._raw.lookup([pin_id]).records
if len(records) != 1 or records[0].recordName != pin_id:
raise NotesError(_UNVERIFIED_PIN)
record = records[0]
if isinstance(record, CKErrorItem) and record.serverErrorCode == "NOT_FOUND":
return None
if (
not isinstance(record, CKRecord)
or record.recordType != "Note_UserSpecific"
or record.deleted
or not self._is_private_record(record)
or not record.recordChangeTag
or not set(record.fields) <= _PIN_FIELDS
or "LastViewedModificationDate" not in record.fields
):
raise NotesError(_UNVERIFIED_PIN)
note = record.fields.get_value("Note")
folder = record.fields.get_value("Folder")
if (
not isinstance(note, CKReference)
or note.recordName != note_id
or not isinstance(folder, CKReference)
or folder.zoneID is None
or folder.zoneID.zoneName != NOTES_ZONE_NAME
or folder.zoneID.ownerRecordName != owner
):
raise NotesError(_UNVERIFIED_PIN)
return record
def restore_note(
self, note_id: str, *, folder_id: str, expected_change_tag: str
) -> NoteWriteResult:
"""Restore a supported note to an explicit private folder."""
record, source = self._require_write_note(
note_id, expected_change_tag, existing_image=None
)
if source != _TRASH_FOLDER_ID:
raise NotesError("Only notes in Recently Deleted can be restored")
self._verify_private_folder_chain(folder_id)
return self._relocate_note(record, folder_id, restoring=True)
def move_note(
self, note_id: str, *, folder_id: str, expected_change_tag: str
) -> NoteWriteResult:
"""Move an active private note to another private folder."""
record, source = self._require_write_note(
note_id, expected_change_tag, existing_image=None
)
if source == _TRASH_FOLDER_ID:
raise NotesError("Use restore_note for a note in Recently Deleted")
if source == folder_id:
raise NotesError("The note is already in the destination folder")
self._verify_private_folder_chain(folder_id)
return self._relocate_note(record, folder_id)
def replace_note_text(
self, note_id: str, *, old: str, new: str, expected_change_tag: str
) -> NoteWriteResult:
"""Replace one text fragment while retaining supported linked objects."""
record, folder_id = self._require_write_note(
note_id, expected_change_tag, existing_image=None
)
if folder_id == _TRASH_FOLDER_ID:
raise NotesError(_RESTORE_BEFORE_EDIT)
compressed = record.fields.get_value("TextDataEncrypted")
if not isinstance(compressed, bytes):
raise NotesError(_UNVERIFIED_IMAGE)
encoded = replace_inline_note_text(compressed, old, new)
fields = self._linked_parent_fields(record, folder_id, compressed)
if (
encoded.attachment_id is not None
and "FirstAttachmentThumbnail" not in fields
):
fields["FirstAttachmentThumbnail"] = NotesWriteField(
value=self._image_preview_receipt(record, encoded.attachment_id)
)
fields.update(
SnippetEncrypted=NotesWriteField(value=encoded.snippet),
TextDataEncrypted=NotesWriteField(value=encoded.body),
ModificationDate=NotesWriteField(value=time.time_ns() // 1_000_000),
)
return self._submit_note_update(record, folder_id, fields)
def add_note_tag(
self, note_id: str, *, tag: str, expected_change_tag: str
) -> NoteTagResult:
"""Append a tag to an existing private note, preserving its other content."""
label = self._tag_label(tag)
record, folder_id, compressed = self._require_linked_note(
note_id, expected_change_tag
)
token = label.upper().encode("ascii")
tag_ids = [
obj.id for obj in inline_note_objects(compressed) if obj.uti == TAG_UTI
]
for tag_record in self._lookup_note_tags(note_id, tag_ids):
if tag_record.fields.get_value("TokenContentIdentifierEncrypted") == token:
return NoteTagResult(
id=note_id,
record_change_tag=expected_change_tag,
tag_id=tag_record.recordName,
changed=False,
)
tag_id = str(uuid4())
body = append_inline_note_object(compressed, InlineObject(tag_id, TAG_UTI))
fields = self._linked_parent_fields(record, folder_id, compressed)
now = time.time_ns() // 1_000_000
fields.update(
TextDataEncrypted=NotesWriteField(value=body),
ModificationDate=NotesWriteField(value=now),
)
child = NotesObjectWriteRecord(
recordName=tag_id,
recordType="InlineAttachment",
parent=CKWriteParent(recordName=note_id),
fields={
"CreationDate": NotesWriteField(value=now),
"Note": NotesWriteField(
value=NotesFolderReference(
recordName=note_id, zoneID=NOTES_ZONE_REQ
)
),
"Deleted": NotesWriteField(value=0),
"MinimumSupportedNotesVersion": NotesWriteField(value=7),
"UTIEncrypted": NotesWriteField(value=self._b64_text(TAG_UTI)),
"AltTextEncrypted": NotesWriteField(value=self._b64_text(f"#{label}")),
"TokenContentIdentifierEncrypted": NotesWriteField(
value=base64.b64encode(token).decode("ascii")
),
},
)
saved, _ = self._submit_linked_note_create(record, folder_id, fields, child)
return NoteTagResult(
id=saved.id,
record_change_tag=saved.record_change_tag,
tag_id=tag_id,
changed=True,
)
@staticmethod
def _tag_label(tag: str) -> str:
label = tag.removeprefix("#")
if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}", label):
raise ValueError(
"Use 1 to 64 ASCII letters, digits, hyphens, or underscores for a tag"
)
return label
def remove_note_tag(
self, note_id: str, *, tag: str, expected_change_tag: str
) -> NoteTagResult:
"""Remove one tag from this note, leaving the global hashtag alone."""
token = self._tag_label(tag).upper().encode("ascii")
record, folder_id, compressed = self._require_linked_note(
note_id, expected_change_tag
)
tag_ids = [
obj.id for obj in inline_note_objects(compressed) if obj.uti == TAG_UTI
]
matches = [
child
for child in self._lookup_note_tags(note_id, tag_ids)
if child.fields.get_value("TokenContentIdentifierEncrypted") == token
]
if len(matches) != 1:
raise NotesError("Expected exactly one matching tag on the note")
child = self._tag_removal_record(note_id, matches[0])
return self._submit_tag_removal(record, folder_id, compressed, child)
def _tag_removal_record(
self, note_id: str, record: CKRecord
) -> NotesObjectWriteRecord:
fields = {
"Deleted": NotesWriteField(value=1),
"Note": NotesWriteField(
value=NotesFolderReference(recordName=note_id, zoneID=NOTES_ZONE_REQ)
),
}
for name in (
"AltTextEncrypted",
"TokenContentIdentifierEncrypted",
"UTIEncrypted",
):
value = record.fields.get_value(name)
if not isinstance(value, bytes):
raise NotesError(_UNVERIFIED_TAGS)
fields[name] = NotesWriteField(
value=base64.b64encode(value).decode("ascii")
)
created = record.fields.get_value("CreationDate")
version = record.fields.get_value("MinimumSupportedNotesVersion")
if (
not record.recordChangeTag
or not isinstance(created, datetime)
or not isinstance(version, int)
or isinstance(version, bool)
):
raise NotesError(_UNVERIFIED_TAGS)
fields["CreationDate"] = NotesWriteField(value=self._date_millis(created))
fields["MinimumSupportedNotesVersion"] = NotesWriteField(value=version)
return NotesObjectWriteRecord(
recordName=record.recordName,
recordChangeTag=record.recordChangeTag,
recordType="InlineAttachment",
parent=CKWriteParent(recordName=note_id),
fields=fields,
)
def _submit_tag_removal(
self,
record: CKRecord,
folder_id: str,
compressed: bytes,
child: NotesObjectWriteRecord,
) -> NoteTagResult:
note_id = record.recordName
try:
saved = self._submit_inline_object_removal(
record, folder_id, compressed, InlineObject(child.recordName, TAG_UTI)
)
except NotesWriteUncertain:
raise NotesLinkedWriteIncomplete(
note_id,
child.recordName,
note_state="uncertain",
object_state="not_submitted",
) from None
try:
self._raw.modify_note(
NotesModifyOperation(operationType="update", record=child),
public_id=note_id,
)
except (NotesWriteRejected, NotesWriteUncertain) as exc:
raise NotesLinkedWriteIncomplete(
note_id,
child.recordName,
note_state="confirmed",
object_state="rejected"
if isinstance(exc, NotesWriteRejected)
else "uncertain",
error_code=exc.error_code
if isinstance(exc, NotesWriteRejected)
else None,
) from None
return NoteTagResult(
id=saved.id,
record_change_tag=saved.record_change_tag,
tag_id=child.recordName,
changed=True,
)
def remove_note_table(
self,
note_id: str,
*,
table_id: str,
expected_change_tag: str,
expected_table_change_tag: str,
confirm: bool = False,
) -> NoteWriteResult:
"""Remove one table from the note without deleting its CloudKit record."""
if confirm is not True:
raise ValueError("Confirm whole-table removal explicitly")
record, folder_id, compressed = self._require_linked_note(
note_id, expected_change_tag
)
obj = InlineObject(table_id, TABLE_UTI)
if obj not in inline_note_objects(compressed):
raise NotesError(_UNVERIFIED_TABLE)
_, _, revision = self._lookup_note_table(note_id, table_id)
if revision != expected_table_change_tag:
raise NoteRevisionConflict(
"The table revision changed; fetch the table again"
)
return self._submit_inline_object_removal(record, folder_id, compressed, obj)
def _submit_inline_object_removal(
self, record: CKRecord, folder_id: str, compressed: bytes, obj: InlineObject
) -> NoteWriteResult:
encoded = remove_inline_note_object(compressed, obj)
fields = self._linked_parent_fields(record, folder_id, compressed)
fields.update(
TextDataEncrypted=NotesWriteField(value=encoded.body),
SnippetEncrypted=NotesWriteField(value=encoded.snippet),
ModificationDate=NotesWriteField(value=time.time_ns() // 1_000_000),
)
return self._submit_note_update(record, folder_id, fields)
def add_note_table(
self, note_id: str, *, rows: Sequence[Sequence[str]], expected_change_tag: str
) -> NoteTableWriteResult:
"""Append an inline plain-text table without replacing existing content."""
encoded = encode_new_table(rows)
record, folder_id, compressed = self._require_linked_note(
note_id, expected_change_tag
)
table_id = str(uuid4())
body = append_inline_note_object(compressed, InlineObject(table_id, TABLE_UTI))
fields = self._linked_parent_fields(record, folder_id, compressed)
now = time.time_ns() // 1_000_000
fields.update(
TextDataEncrypted=NotesWriteField(value=body),
ModificationDate=NotesWriteField(value=now),
)
child_fields = self._table_body_fields(encoded)
child_fields.update(
CreationDate=NotesWriteField(value=now),
Note=NotesWriteField(
value=NotesFolderReference(recordName=note_id, zoneID=NOTES_ZONE_REQ)
),
UTI=NotesWriteField(value=TABLE_UTI),
MinimumSupportedNotesVersion=NotesWriteField(value=2),
TitleEncrypted=NotesWriteField(value=self._b64_text("Table")),
)
child = NotesObjectWriteRecord(
recordName=table_id,
recordType="Attachment",
parent=CKWriteParent(recordName=note_id),
fields=child_fields,
)
saved, table = self._submit_linked_note_create(record, folder_id, fields, child)
return NoteTableWriteResult(
id=note_id,
record_change_tag=saved.record_change_tag,
table_id=table_id,
table_change_tag=table.record_change_tag,
changed=True,
)
def get_note_table(self, note_id: str, *, table: int = 1) -> NoteTable:
"""Read a one-based table and the note and table revisions for editing."""
note = self._lookup_note_record(note_id, require_single=True)
body = note.fields.get_value("TextDataEncrypted")
if (
note.recordType != "Note"
or note.deleted
or not self._is_private_record(note)
or not note.recordChangeTag
or not isinstance(body, bytes)
):
raise NotesError(_UNVERIFIED_TABLE)
objects = [obj for obj in inline_note_objects(body) if obj.uti == TABLE_UTI]
if (
not isinstance(table, int)
or isinstance(table, bool)
or not 1 <= table <= len(objects)
):
raise ValueError("Notes table number does not exist")
table_id = objects[table - 1].id
record, raw, revision = self._lookup_note_table(note_id, table_id)
return NoteTable(
id=note_id,
record_change_tag=note.recordChangeTag,
table_id=record.recordName,
table_change_tag=revision,
rows=table_cells(raw),
)
def update_note_table_cell(
self,
note_id: str,
*,
table_id: str,
row: int,
column: int,
text: str,
expected_change_tag: str,
expected_table_change_tag: str,
) -> NoteTableWriteResult:
"""Change one plain-text table cell using both current revisions."""
_, folder_id, compressed = self._require_linked_note(
note_id, expected_change_tag
)
raw = self._require_table_revision(
note_id, table_id, compressed, expected_table_change_tag
)
encoded = encode_table_cell_edit(raw, row, column, text)
if encoded is None:
return NoteTableWriteResult(
id=note_id,
record_change_tag=expected_change_tag,
table_id=table_id,
table_change_tag=expected_table_change_tag,
changed=False,
)
try:
saved = self._submit_table_update(
note_id, table_id, encoded, expected_table_change_tag
)
except NotesWriteUncertain:
raise NotesLinkedWriteIncomplete(
note_id, table_id, note_state="not_submitted", object_state="uncertain"
) from None
note = self._touch_table_parent(note_id, folder_id, compressed, table_id)
return NoteTableWriteResult(
id=note_id,
record_change_tag=note.record_change_tag,
table_id=table_id,
table_change_tag=saved.record_change_tag,
changed=True,
)
def resize_note_table(
self,
note_id: str,
*,
table_id: str,
axis: TableAxis,
action: TableResizeAction,
index: int,
expected_change_tag: str,
expected_table_change_tag: str,
confirm_nonempty: bool = False,
) -> NoteTableWriteResult:
"""Insert before a one-based row or column, or delete it with confirmation."""
record, folder_id, compressed = self._require_linked_note(
note_id, expected_change_tag
)
raw = self._require_table_revision(
note_id, table_id, compressed, expected_table_change_tag
)
encoded = encode_table_resize(
raw,
axis=axis,
action=action,
index=index,
confirm_nonempty=confirm_nonempty,
)
fields = self._linked_parent_fields(record, folder_id, compressed)
fields["ModificationDate"] = NotesWriteField(value=time.time_ns() // 1_000_000)
try:
note = self._submit_note_update(record, folder_id, fields)
except NotesWriteUncertain:
raise NotesLinkedWriteIncomplete(
note_id, table_id, note_state="uncertain", object_state="not_submitted"
) from None
try:
saved = self._submit_table_update(
note_id, table_id, encoded, expected_table_change_tag
)
except (NotesWriteRejected, NotesWriteUncertain) as exc:
raise NotesLinkedWriteIncomplete(
note_id,
table_id,
note_state="confirmed",
object_state="rejected"
if isinstance(exc, NotesWriteRejected)
else "uncertain",
error_code=exc.error_code
if isinstance(exc, NotesWriteRejected)
else None,
) from None
return NoteTableWriteResult(
id=note_id,
record_change_tag=note.record_change_tag,
table_id=table_id,
table_change_tag=saved.record_change_tag,
changed=True,
)
def _require_table_revision(
self, note_id: str, table_id: str, compressed: bytes, revision: str
) -> bytes:
"""Read the current body only if the note links to this table revision."""
if InlineObject(table_id, TABLE_UTI) not in inline_note_objects(compressed):
raise NotesError(_UNVERIFIED_TABLE)
_, raw, current = self._lookup_note_table(note_id, table_id)
if current != revision:
raise NoteRevisionConflict(
"The table revision changed; fetch the table again"
)
return raw
def _submit_table_update(
self, note_id: str, table_id: str, encoded: str, revision: str
) -> NoteWriteResult:
"""Submit the revision-guarded update through the Notes mutation client."""
return self._raw.modify_note(
NotesModifyOperation(
operationType="update",
record=NotesObjectWriteRecord(
recordName=table_id,
recordType="Attachment",
parent=CKWriteParent(recordName=note_id),
fields=self._table_body_fields(encoded),
recordChangeTag=revision,
),
),
public_id=note_id,
)
def _touch_table_parent(
self, note_id: str, folder_id: str, compressed: bytes, table_id: str
) -> NoteWriteResult:
"""Advance the note timestamp after a confirmed table mutation."""
try:
current = self._lookup_note_record(note_id, require_single=True)
if (
current.recordType != "Note"
or current.deleted
or self._is_deleted(current)
or not self._is_private_record(current)
or self._extract_folder_id(current) != folder_id
or current.fields.get_value("TextDataEncrypted") != compressed
or not current.recordChangeTag
or not set(current.fields) <= _IMAGE_NOTE_FIELDS
or current.fields.get_value("TextDataAsset") is not None
):
raise NotesError(_UNVERIFIED_TABLE)
self._verify_note_source_folder(folder_id)
fields = self._linked_parent_fields(current, folder_id, compressed)
fields["ModificationDate"] = NotesWriteField(
value=time.time_ns() // 1_000_000
)
except (PyiCloudException, OSError, ValueError):
raise NotesLinkedWriteIncomplete(
note_id, table_id, note_state="not_submitted", object_state="confirmed"
) from None
try:
return self._submit_note_update(current, folder_id, fields)
except (NotesWriteRejected, NotesWriteUncertain) as exc:
raise NotesLinkedWriteIncomplete(
note_id,
table_id,
note_state="rejected"
if isinstance(exc, NotesWriteRejected)
else "uncertain",
object_state="confirmed",
error_code=exc.error_code
if isinstance(exc, NotesWriteRejected)
else None,
) from None
def _lookup_note_table(
self, note_id: str, table_id: str
) -> tuple[CKRecord, bytes, str]:
"""Read the exact private table linked to the note."""
records = self._raw.lookup([table_id]).records
if len(records) != 1 or not isinstance(records[0], CKRecord):
raise NotesError(_UNVERIFIED_TABLE)
record = records[0]
raw = record.fields.get_value("MergeableDataEncrypted")
if (
record.recordName != table_id
or record.recordType != "Attachment"
or record.deleted
or record.fields.get_value("Deleted")
or not self._is_private_record(record)
or record.parent is None
or record.parent.recordName != note_id
or getattr(record.fields.get_value("Note"), "recordName", None) != note_id
or record.fields.get_value("UTIEncrypted") != TABLE_UTI.encode("ascii")
or any(
record.fields.get_value(field) is not None
for field in _TABLE_ASSET_FIELDS
)
or not isinstance(raw, bytes)
or not record.recordChangeTag
):
raise NotesError(_UNVERIFIED_TABLE)
return record, raw, record.recordChangeTag
def _table_body_fields(self, encoded: str) -> dict[str, NotesWriteField]:
"""Build the inline table fields without external asset references."""
return {
**{field: NotesWriteField() for field in _TABLE_ASSET_FIELDS},
"UTIEncrypted": NotesWriteField(value=self._b64_text(TABLE_UTI)),
"MergeableDataEncrypted": NotesWriteField(value=encoded),
}
@staticmethod
def _b64_text(text: str) -> str:
return base64.b64encode(text.encode("utf-8")).decode("ascii")
def _require_linked_note(
self, note_id: str, tag: str
) -> tuple[CKRecord, str, bytes]:
record, folder_id = self._require_write_note(note_id, tag, existing_image=None)
if folder_id == _TRASH_FOLDER_ID:
raise NotesError(_RESTORE_BEFORE_EDIT)
compressed = record.fields.get_value("TextDataEncrypted")
if not isinstance(compressed, bytes):
raise NotesError(_UNSUPPORTED_WRITE)
return record, folder_id, compressed
def _lookup_note_tags(self, note_id: str, tag_ids: list[str]) -> list[CKRecord]:
if not tag_ids:
return []
records = self._raw.lookup(tag_ids).records
tags = [record for record in records if isinstance(record, CKRecord)]
if len(tags) != len(tag_ids) or {tag.recordName for tag in tags} != set(
tag_ids
):
raise NotesError(_UNVERIFIED_TAGS)
for tag in tags:
if (
tag.recordType != "InlineAttachment"
or tag.deleted
or tag.fields.get_value("Deleted")
or not self._is_private_record(tag)
or tag.parent is None
or tag.parent.recordName != note_id
or getattr(tag.fields.get_value("Note"), "recordName", None) != note_id
or tag.fields.get_value("UTIEncrypted") != TAG_UTI.encode("ascii")
or not isinstance(
tag.fields.get_value("TokenContentIdentifierEncrypted"), bytes
)
):
raise NotesError(_UNVERIFIED_TAGS)
return tags
def _submit_linked_note_create(
self,
record: CKRecord,
folder_id: str,
fields: dict[str, NotesWriteField],
child: NotesObjectWriteRecord,
) -> tuple[NoteWriteResult, NoteWriteResult]:
"""Write the parent link, then create and verify the linked record."""
try:
self._submit_note_update(record, folder_id, fields)
except NotesWriteUncertain:
raise NotesLinkedWriteIncomplete(
record.recordName,
child.recordName,
note_state="uncertain",
object_state="not_submitted",
) from None
try:
linked = self._raw.modify_note(
NotesModifyOperation(operationType="create", record=child),
public_id=record.recordName,
)
except (NotesWriteRejected, NotesWriteUncertain) as exc:
raise NotesLinkedWriteIncomplete(
record.recordName,
child.recordName,
note_state="confirmed",
object_state="rejected"
if isinstance(exc, NotesWriteRejected)
else "uncertain",
error_code=exc.error_code
if isinstance(exc, NotesWriteRejected)
else None,
) from None
failure = NotesLinkedWriteIncomplete(
record.recordName,
child.recordName,
note_state="uncertain",
object_state="confirmed",
)
try:
current = self._lookup_note_record(record.recordName, require_single=True)
body = current.fields.get_value("TextDataEncrypted")
if (
current.recordType == "Note"
and not current.deleted
and not self._is_deleted(current)
and self._is_private_record(current)
and self._extract_folder_id(current) == folder_id
and isinstance(body, bytes)
and base64.b64encode(body).decode("ascii")
== fields["TextDataEncrypted"].value
and current.recordChangeTag
and current.recordChangeTag != record.recordChangeTag
):
return NoteWriteResult(
id=current.recordName, record_change_tag=current.recordChangeTag
), linked
except (OSError, PyiCloudException):
raise failure from None
raise failure
def _linked_parent_fields(
self, record: CKRecord, folder_id: str, compressed: bytes
) -> dict[str, NotesWriteField]:
fields = self._preserved_write_fields(record, folder_id)
if record.fields.get_value("FirstAttachmentThumbnail") is not None:
fields["FirstAttachmentThumbnail"] = NotesWriteField(
value=self._image_preview_receipt(record, existing_image_id(compressed))
)
return fields
def _preserved_write_fields(
self, record: CKRecord, folder_id: str
) -> dict[str, NotesWriteField]:
fields = self._existing_write_fields(record, folder_id)
for name in (*_IMAGE_METADATA, "Deleted"):
value = record.fields.get_value(name)
if isinstance(value, bytes):
fields[name] = NotesWriteField(
value=base64.b64encode(value).decode("ascii")
)
elif isinstance(value, int) and not isinstance(value, bool):
fields[name] = NotesWriteField(value=value)
elif value is not None:
raise NotesError(_UNVERIFIED_IMAGE)
return fields
def _image_preview_receipt(
self, note: CKRecord, attachment_id: str
) -> NotesPreviewReceipt:
target = note.fields.get_value("FirstAttachmentThumbnail")
if (
not isinstance(target, CKAssetToken)
or not target.fileChecksum
or not target.referenceChecksum
or not target.size
):
raise NotesError(_UNVERIFIED_IMAGE)
records = self._raw.lookup([attachment_id]).records
if len(records) != 1 or not isinstance(records[0], CKRecord):
raise NotesError(_UNVERIFIED_IMAGE)
attachment = records[0]
owner = attachment.fields.get_value("Note")
previews = attachment.fields.get_value("PreviewImages")
if (
attachment.recordName != attachment_id
or attachment.recordType != "Attachment"
or attachment.deleted
or attachment.fields.get_value("Deleted")
or not self._is_private_record(attachment)
or not isinstance(owner, CKReference)
or owner.recordName != note.recordName
or attachment.parent is None
or attachment.parent.recordName != note.recordName
or not isinstance(previews, list)
):
raise NotesError(_UNVERIFIED_IMAGE)
# The note and attachment references have different reference checksums
# and wrapping keys even when they point to the same file.
expected = (target.fileChecksum, target.size)
matches = [
i
for i, asset in enumerate(previews)
if isinstance(asset, CKAssetToken)
and (asset.fileChecksum, asset.size) == expected
]
if len(matches) != 1:
raise NotesError(_UNVERIFIED_IMAGE)
receipt = self._raw.rereference_preview(
NotesPreviewLocator(recordName=attachment_id, listIndex=matches[0])
)
if (receipt.fileChecksum, receipt.size) != expected:
raise NotesError(_UNVERIFIED_IMAGE)
return receipt
@staticmethod
def _is_private_record(record: CKRecord) -> bool:
"""Reject records outside Notes or with explicit sharing metadata."""
return (
(record.zoneID is None or record.zoneID.zoneName == NOTES_ZONE_NAME)
and record.share is None
and record.currentUserParticipant is None
and not record.participants
and not record.publicPermission
)
def _require_write_note(
self, note_id: str, tag: str, *, existing_image: bool | None = False
) -> tuple[CKRecord, str]:
"""Verify the note revision, supported data shape, and folder privacy."""
if not note_id.strip() or not tag.strip():
raise ValueError("A note ID and its current revision are required")
record = self._lookup_note_record(note_id, require_single=True)
if record.recordChangeTag != tag:
raise NoteRevisionConflict(
"The note revision changed; fetch the note again"
)
folder = record.fields.get_value("Folder")
folders = record.fields.get_value("Folders")
allowed = (
_IMAGE_NOTE_FIELDS if existing_image is not False else _PLAIN_NOTE_FIELDS
)
if existing_image is None:
existing_image = (
record.fields.get_value("FirstAttachmentThumbnail") is not None
)
empty_assets = ("TextDataAsset",) if existing_image else _EMPTY_ASSET_FIELDS
if (
record.recordType != "Note"
or record.deleted
or record.fields.get_value("Deleted")
or not self._is_private_record(record)
or not record.shortGUID
or not set(record.fields.keys()) <= allowed
or any(record.fields.get_value(name) is not None for name in empty_assets)
or not isinstance(folder, CKReference)
or not folder.recordName.strip()
or (folder.zoneID is not None and folder.zoneID.zoneName != NOTES_ZONE_NAME)
or folders != [folder]
or (
record.parent is not None
and record.parent.recordName != folder.recordName
)
):
raise NotesError(_UNSUPPORTED_WRITE)
self._verify_note_source_folder(folder.recordName)
return record, folder.recordName
def _verify_note_source_folder(self, folder_id: str) -> None:
"""Verify privacy of the source, including the special Trash folder."""
if folder_id == _TRASH_FOLDER_ID:
trash = self._require_write_folder(folder_id, allow_trash=True)
self._verify_private_folder_chain(
self._folder_parent(trash), excluded=folder_id
)
return
self._verify_private_folder_chain(folder_id)
@staticmethod
def _date_millis(date: datetime) -> int:
delta = date - datetime(1970, 1, 1, tzinfo=timezone.utc)
return (
delta.days * 86_400_000 + delta.seconds * 1000 + delta.microseconds // 1000
)
@staticmethod
def _existing_write_fields(
record: CKRecord, folder_id: str
) -> dict[str, NotesWriteField]:
"""Keep the unchanged fields Apple echoes for plain-note updates."""
fields = {name: NotesWriteField() for name in _EMPTY_ASSET_FIELDS}
for name in _INLINE_FIELDS:
value = record.fields.get_value(name)
if not isinstance(value, bytes):
raise NotesError(_UNSUPPORTED_WRITE)
fields[name] = NotesWriteField(
value=base64.b64encode(value).decode("ascii")
)
for name in ("CreationDate", "ModificationDate", "FoldersModificationDate"):
date = record.fields.get_value(name)
if not isinstance(date, datetime):
raise NotesError(_UNSUPPORTED_WRITE)
fields[name] = NotesWriteField(value=NotesService._date_millis(date))
folder = NotesFolderReference(recordName=folder_id, zoneID=NOTES_ZONE_REQ)
fields["Folder"] = NotesWriteField(value=folder)
fields["Folders"] = NotesWriteField(value=[folder])
return fields
def _relocate_note(
self, record: CKRecord, folder_id: str, *, restoring: bool = False
) -> NoteWriteResult:
compressed = record.fields.get_value("TextDataEncrypted")
if not isinstance(compressed, bytes):
raise NotesError(_UNSUPPORTED_WRITE)
inline_note_objects(compressed)
fields = self._linked_parent_fields(record, folder_id, compressed)
fields["FoldersModificationDate"] = NotesWriteField(
value=time.time_ns() // 1_000_000
)
if restoring:
fields["ModificationDate"] = NotesWriteField(
value=time.time_ns() // 1_000_000
)
return self._submit_note_update(record, folder_id, fields)
def _submit_note_update(
self, record: CKRecord, folder_id: str, fields: dict[str, NotesWriteField]
) -> NoteWriteResult:
return self._raw.modify_note(
NotesModifyOperation(
operationType="update",
record=NotesWriteRecord(
recordName=record.recordName,
recordChangeTag=record.recordChangeTag,
shortGUID=record.shortGUID,
parent=CKWriteParent(recordName=folder_id),
fields=fields,
),
)
)
def sync_cursor(self) -> str:
"""
Return the current Notes sync token.
Persist this token and pass it back to ``iter_all(since=...)`` or
``iter_changes(since=...)`` on a later run to perform incremental syncs.
"""
LOGGER.debug("Fetching sync cursor for Notes zone")
return self._raw.current_sync_token(zone_name=NOTES_ZONE_NAME)
def export_note(self, note_id: str, output_dir: str, **config_kwargs: Any) -> str:
"""
Export a note to HTML on disk and return the generated file path.
Args:
note_id: The UUID of the note to export.
output_dir: Directory where the HTML file will be written.
**config_kwargs: Keyword arguments forwarded to ``ExportConfig``,
including ``export_mode``, ``assets_dir``, ``full_page``,
``preview_appearance``, ``pdf_object_height``, and link behavior
settings.
Returns:
The path to the generated HTML file.
Notes:
By default, this produces archival output: a full HTML page with
local asset downloads.
"""
target = self._lookup_note_record(note_id)
config = ExportConfig(**config_kwargs)
exporter = NoteExporter(self._raw, config=config)
path = exporter.export(target, output_dir=output_dir)
if not path:
raise NotesError(f"Failed to export note: {note_id}")
return path
def render_note(self, note_id: str, **config_kwargs: Any) -> str:
"""
Render a note to an HTML fragment string.
Args:
note_id: The UUID of the note to render.
**config_kwargs: Keyword arguments forwarded to ``ExportConfig`` to
tune preview selection or link behavior for the rendered HTML.
Returns:
An HTML fragment string. This method does not download assets or
write files to disk.
"""
target = self._lookup_note_record(note_id)
config = ExportConfig(**config_kwargs)
note = decode_and_parse_note(target)
if not note:
return ""
ds, _ = build_datasource(self._raw, target, note, config)
renderer = NoteRenderer(config)
return renderer.render(note, ds)
def iter_changes( # noqa: S3776
self, *, since: str | None = None
) -> Iterable[ChangeEvent]:
"""
Yield ``ChangeEvent`` items from the Notes changes feed.
Pass a sync token from ``sync_cursor()`` to process only new changes
since a previous run.
"""
if self._matches_current_sync_cursor(since):
LOGGER.debug("Skipping Notes change scan because sync token is current")
return
LOGGER.debug("Iterating changes%s", f" since={since}" if since else "")
for zone in self._raw.changes(
zone_req=CKZoneChangesZoneReq(
zoneID=NOTES_ZONE,
desiredRecordTypes=[NotesRecordType.Note],
desiredKeys=self._coerce_keys([
NotesDesiredKey.TITLE_ENCRYPTED,
NotesDesiredKey.SNIPPET_ENCRYPTED,
NotesDesiredKey.MODIFICATION_DATE,
NotesDesiredKey.DELETED,
NotesDesiredKey.FOLDER,
NotesDesiredKey.ATTACHMENTS,
]),
syncToken=since,
reverse=False,
)
):
for rec in zone.records:
if isinstance(rec, CKRecord):
deleted_flag = self._is_deleted(rec)
evt_type: Literal["updated", "deleted"] = (
"deleted" if deleted_flag else "updated"
)
yield ChangeEvent(
type=evt_type,
note=self._summary_from_record(rec),
)
continue
if isinstance(rec, CKTombstoneRecord):
record_name = getattr(rec, "recordName", None)
if record_name:
yield ChangeEvent(
type="deleted",
note=NoteSummary(
id=record_name,
title=None,
snippet=None,
modified_at=None,
folder_id=None,
folder_name=None,
is_deleted=True,
is_locked=False,
),
)
continue
if isinstance(rec, CKErrorItem):
details = {
"serverErrorCode": rec.serverErrorCode,
"reason": rec.reason,
"recordName": rec.recordName,
}
LOGGER.error(
"CloudKit error during change enumeration: %s (%s) record=%s",
rec.serverErrorCode or "UNKNOWN",
rec.reason,
rec.recordName,
)
raise NotesApiError(
(
"CloudKit error during change enumeration: "
f"{rec.serverErrorCode or 'UNKNOWN'}"
),
payload=details,
)
LOGGER.error("Unexpected record type in changes feed: %r", rec)
raise NotesApiError(
"Unexpected record type in changes feed",
payload={"record_repr": repr(rec)},
)
@property
def raw(self) -> CloudKitNotesClient:
"""
Return the authenticated low-level Notes CloudKit client.
This is an escape hatch for advanced or unsupported operations; prefer
the typed service methods above for normal use.
"""
return self._raw
# -------------------------- Internal helpers -----------------------------
def _matches_current_sync_cursor(self, since: str | None) -> bool:
"""Return whether an incremental Notes cursor is already current."""
if not since:
return False
try:
return self._raw.current_sync_token(zone_name=NOTES_ZONE_NAME) == since
except NotesApiError as exc:
LOGGER.warning("Failed to preflight Notes sync token: %s", exc)
return False
def _lookup_note_record(
self,
note_id: str,
*,
desired_keys: Iterable[object] | None = None,
require_single: bool = False,
) -> CKRecord:
"""Return one Notes record or raise ``NoteNotFound``."""
resp = self._raw.lookup(
record_names=[note_id],
desired_keys=self._coerce_keys(desired_keys),
)
if require_single and len(resp.records) != 1:
raise NoteNotFound(f"Could not verify one note record: {note_id}")
for rec in resp.records:
if isinstance(rec, CKRecord) and rec.recordName == note_id:
return rec
LOGGER.warning("Note not found: %s", note_id)
raise NoteNotFound(f"Note not found: {note_id}")
@staticmethod
def _coerce_keys(keys: Iterable[object] | None) -> list[str] | None:
if keys is None:
return None
out: list[str] = []
for k in keys:
if isinstance(k, NotesDesiredKey):
out.append(k.value)
else:
out.append(str(k))
return out
@staticmethod
def _decode_encrypted(b: bytes | str | None) -> str | None:
if b is None:
return None
if isinstance(b, str):
return b
try:
return b.decode("utf-8", "replace")
except Exception:
return None
def _extract_folder_id(self, rec: CKRecord) -> str | None:
f = rec.fields.get_field("Folder")
if isinstance(f, CKReferenceField) and f.value:
return f.value.recordName
fl = rec.fields.get_field("Folders")
if isinstance(fl, CKReferenceListField) and fl.value:
return fl.value[0].recordName
return None
def _is_deleted(self, record: CKRecord) -> bool:
return bool(record.fields.get_value("Deleted")) or (
self._extract_folder_id(record) == _TRASH_FOLDER_ID
)
def _folder_name(self, folder_id: str | None) -> str | None:
if not folder_id:
return None
if folder_id in self._folder_name_cache:
return self._folder_name_cache[folder_id]
try:
resp = self._raw.lookup([folder_id], desired_keys=["TitleEncrypted"])
name: str | None = None
for rec in resp.records:
if isinstance(rec, CKRecord) and rec.recordName == folder_id:
name = self._decode_encrypted(
rec.fields.get_value("TitleEncrypted")
)
break
self._folder_name_cache[folder_id] = name
LOGGER.debug("Folder name resolved: folder_id=%s name=%s", folder_id, name)
return name
except (NotesApiError, NotesAuthError, NotesRateLimited):
self._folder_name_cache[folder_id] = None
LOGGER.warning("Failed to resolve folder name: folder_id=%s", folder_id)
return None
def _summary_from_record(self, rec: CKRecord) -> NoteSummary:
title = self._decode_encrypted(rec.fields.get_value("TitleEncrypted"))
snippet = self._decode_encrypted(rec.fields.get_value("SnippetEncrypted"))
modified = rec.fields.get_value(
"ModificationDate"
) # already tz-aware datetime or None
deleted = self._is_deleted(rec)
folder_id = self._extract_folder_id(rec)
folder_name = self._folder_name(folder_id)
is_locked = (
str(getattr(rec, "recordType", "")).lower()
== NotesRecordType.PasswordProtectedNote.lower()
)
return NoteSummary(
id=rec.recordName,
title=title,
snippet=snippet,
modified_at=modified,
folder_id=folder_id,
folder_name=folder_name,
is_deleted=deleted,
is_locked=is_locked,
)
def _decode_note_body(self, rec: CKRecord) -> NoteBody | None:
"""Decode TextDataEncrypted into a NoteBody (text + attachment IDs)."""
raw = rec.fields.get_value("TextDataEncrypted")
if not raw:
LOGGER.debug("notes.body.missing TextDataEncrypted id=%s", rec.recordName)
return None
try:
nb = BodyDecoder().decode(raw)
if nb and isinstance(nb, NoteBody):
return nb
LOGGER.debug(
"notes.body.no_text id=%s bytes=%s",
rec.recordName,
(
len(getattr(nb, "bytes", b""))
if nb and getattr(nb, "bytes", None)
else "0"
),
)
return None
except Exception as e:
LOGGER.warning("notes.body.decode_failed id=%s err=%s", rec.recordName, e)
return None
def _resolve_attachments_for_record( # noqa: S3776
self,
rec: CKRecord,
*,
attachment_ids: list[AttachmentId] | None = None,
) -> list[Attachment]:
"""Hydrate attachment metadata for a note.
Combines attachment identifiers from CloudKit references and the decoded
protobuf body. Missing records are skipped gracefully.
"""
out: list[Attachment] = []
alias_ids: list[str] = []
lookup_candidates: list[str] = []
if attachment_ids:
for aid in attachment_ids:
ident = getattr(aid, "identifier", None)
if ident:
alias_ids.append(ident)
fld = rec.fields.get_field("Attachments")
if fld and hasattr(fld, "value"):
refs: list[CKReference] = getattr(fld, "value", []) or []
for ref in refs:
rn = getattr(ref, "recordName", None)
if rn:
lookup_candidates.append(rn)
alias_ids.append(rn)
# Deduplicate alias list while preserving order
seen_alias: set[str] = set()
ids: list[str] = []
for cid in alias_ids:
if cid not in seen_alias:
seen_alias.add(cid)
ids.append(cid)
LOGGER.debug(
"notes.attachments.candidates alias=%s lookup=%s",
ids,
lookup_candidates,
)
if not ids and not lookup_candidates:
return out
# Fetch metadata for uncached attachments
seen_lookup: set[str] = set()
lookup_ids: list[str] = []
lookup_source_ids = lookup_candidates if lookup_candidates else ids
for cid in lookup_source_ids:
if cid not in seen_lookup:
seen_lookup.add(cid)
lookup_ids.append(cid)
missing = [aid for aid in lookup_ids if aid not in self._attachment_meta_cache]
if missing:
desired_keys = [
"Filename",
"AttachmentUTI",
"UTI",
"Size",
"Thumbnail",
"FirstAttachmentThumbnail",
"PrimaryAsset",
"PreviewImages",
"PreviewAppearances",
"FallbackImage",
"FallbackPDF",
"PaperAssets",
"AttachmentIdentifier",
"attachmentIdentifier",
"Identifier",
]
try:
resp = self._raw.lookup(missing, desired_keys=desired_keys)
except NotesApiError as err:
LOGGER.debug(
"notes.attachments.lookup_failed ids=%s err=%s",
missing,
err,
)
resp = CKLookupResponse(records=[])
for rec_a in getattr(resp, "records", []):
if not isinstance(rec_a, CKRecord):
continue
attachment = self._build_attachment_from_record(rec_a)
if not attachment:
LOGGER.debug(
"notes.attachments.unhandled record=%s fields=%s",
getattr(rec_a, "recordName", None),
list(rec_a.fields.keys())
if getattr(rec_a, "fields", None)
else None,
)
continue
base_id = attachment.id
aliases = self._attachment_aliases(rec_a, base_id)
for alias in aliases:
self._attachment_meta_cache[alias] = attachment
LOGGER.debug(
"notes.attachments.cached base=%s alias=%s",
base_id,
alias,
)
for cid in ids or lookup_ids:
att = self._attachment_meta_cache.get(cid)
if att and att not in out:
out.append(att)
LOGGER.debug(
"Resolved %d attachments for note %s",
len(out),
getattr(rec, "recordName", None),
)
return out
@staticmethod
def _coerce_string(rec: CKRecord, names: list[str]) -> str | None:
for n in names:
v = rec.fields.get_value(n)
if v is None:
continue
if isinstance(v, bytes):
try:
return v.decode("utf-8", "replace")
except Exception:
continue
if isinstance(v, str):
return v
return None
@staticmethod
def _attachment_aliases(rec: CKRecord, record_name: str) -> list[str]:
aliases = [record_name]
identifier = NotesService._coerce_string(
rec, ["AttachmentIdentifier", "attachmentIdentifier", "Identifier"]
)
if identifier and identifier not in aliases:
aliases.append(identifier)
return aliases
def _build_attachment_from_record(self, rec: CKRecord) -> Attachment | None:
aid = getattr(rec, "recordName", None)
if not aid:
return None
filename = self._coerce_string(rec, ["Filename", "Name", "FileName"])
uti = self._coerce_string(rec, ["AttachmentUTI", "UTI"])
size = self._coerce_int(rec, ["Size", "FileSize"])
download_url = None
preview_url = None
thumbnail_url = None
download_url = download_url or self._coerce_asset_url(rec, ["PrimaryAsset"])
download_url = download_url or self._coerce_asset_url(rec, ["FallbackPDF"])
download_url = download_url or self._coerce_asset_url_from_list(
rec, "FallbackPDF"
)
download_url = download_url or self._coerce_asset_url_from_list(
rec, "PaperAssets"
)
preview_url = preview_url or self._coerce_asset_url_from_list(
rec, "PreviewImages"
)
preview_url = preview_url or self._coerce_asset_url(rec, ["FallbackImage"])
thumbnail_url = thumbnail_url or self._coerce_asset_url(
rec, ["Thumbnail", "FirstAttachmentThumbnail"]
)
thumbnail_url = thumbnail_url or preview_url
return Attachment(
id=aid,
filename=filename,
uti=uti,
size=size,
download_url=download_url,
preview_url=preview_url,
thumbnail_url=thumbnail_url,
)
@staticmethod
def _coerce_int(rec: CKRecord, names: list[str]) -> int | None:
for n in names:
v = rec.fields.get_value(n)
if isinstance(v, int):
return v
if isinstance(v, float):
return int(v)
if isinstance(v, str) and v.isdigit():
return int(v)
return None
@staticmethod
def _coerce_asset_url(rec: CKRecord, names: list[str]) -> str | None:
for n in names:
fld = rec.fields.get_field(n)
if not fld:
continue
# ASSET/ASSETID wrapper -> value.downloadURL if present
val = getattr(fld, "value", None)
url = getattr(val, "downloadURL", None)
if isinstance(url, str):
return url
return None
@staticmethod
def _coerce_asset_url_from_list(rec: CKRecord, name: str) -> str | None:
fld = rec.fields.get_field(name)
if not fld:
return None
val = getattr(fld, "value", None)
if isinstance(val, (list, tuple)):
for token in val:
url = None
if isinstance(token, dict):
url = token.get("downloadURL")
else:
url = getattr(token, "downloadURL", None)
if isinstance(url, str) and url:
return url
return None
def download_attachment_to(self, att: Attachment, directory: str) -> str:
"""Download an attachment to a directory and return the local path."""
url = att.download_url or att.preview_url or att.thumbnail_url
if not url:
raise NotesApiError("Attachment does not expose a download URL.")
LOGGER.debug("Downloading attachment %s to %s", att.id, directory)
return self._raw.download_asset_to(url, directory)
def stream_attachment(
self, att: Attachment, *, chunk_size: int = 65536
) -> Iterator[bytes]:
"""Yield the attachment bytes in chunks."""
url = att.download_url or att.preview_url or att.thumbnail_url
if not url:
raise NotesApiError("Attachment does not expose a download URL.")
LOGGER.debug("Streaming attachment %s chunk_size=%d", att.id, chunk_size)
yield from self._raw.download_asset_stream(url, chunk_size=chunk_size)
|