-
Notifications
You must be signed in to change notification settings - Fork 4
/
radicale_remind.py
350 lines (271 loc) · 11.7 KB
/
radicale_remind.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Remind, Abook, Taskwarrior Storage backend for Radicale
#
# Copyright (C) 2013-2024 Jochen Sprickerhof
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Remind, Abook, Taskwarrior Storage backend for Radicale."""
from collections.abc import Iterable, Iterator, Mapping
from colorsys import hsv_to_rgb
from os.path import basename, dirname, expanduser, join
from time import gmtime, strftime
from typing import Callable, ContextManager, overload
from zoneinfo import ZoneInfo
from abook import Abook
from icstask import IcsTask
from radicale import config
from radicale import item as radicale_item
from radicale import types
from radicale.item import Item
from radicale.log import logger
from radicale.pathutils import sanitize_path
from radicale.storage import BaseCollection, BaseStorage
from remind import Remind
from vobject.base import Component
class MinCollection(BaseCollection):
def __init__(self, path: str) -> None:
self._path = sanitize_path(path).strip("/")
@property
def path(self) -> str:
"""The sanitized path of the collection without leading or
trailing ``/``."""
return self._path
# fmt: off
@overload
def get_meta(self, key: None = None) -> Mapping[str, str]: ...
@overload
def get_meta(self, key: str) -> None | str: ...
def get_meta(self, key: None | str = None
) -> None | Mapping[str, str] | str:
"""Get metadata value for collection.
Return the value of the property ``key``. If ``key`` is ``None`` return
a dict with all properties
"""
# fmt: on
return None if key else {}
def get_multi(self, hrefs: Iterable[str]
) -> Iterable[tuple[str, None | radicale_item.Item]]:
"""Fetch multiple items.
It's not required to return the requested items in the correct order.
Duplicated hrefs can be ignored.
Returns tuples with the href and the item or None if the item doesn't
exist.
"""
return ()
class Collection(BaseCollection):
uid_cache: dict[str, str] = {}
"""Collection stored in adapters for Remind, Abook, Taskwarrior."""
def __init__(self, path: str, filename: str, adapter: Abook | IcsTask | Remind) -> None:
self._path = sanitize_path(path).strip("/")
self.filename = filename
self.adapter = adapter
@property
def path(self) -> str:
"""The sanitized path of the collection without leading or
trailing ``/``."""
return self._path
# fmt: off
def get_multi(self, hrefs: Iterable[str]
) -> Iterable[tuple[str, None | radicale_item.Item]]:
"""Fetch multiple items.
It's not required to return the requested items in the correct order.
Duplicated hrefs can be ignored.
Returns tuples with the href and the item or None if the item doesn't
exist.
"""
# fmt: on
hrefs = [Collection.uid_cache.get(href, href) for href in hrefs]
return (
(x[0], self._convert(x))
for x in self.adapter.to_vobjects(self.filename, hrefs)
)
def get_all(self) -> Iterable[radicale_item.Item]:
"""Fetch all items."""
return (self._convert(x) for x in self.adapter.to_vobjects(self.filename))
def _list(self) -> Iterable[str]:
"""List collection items."""
yield from self.adapter.get_uids(self.filename)
def _convert(self, elem: tuple[str, Component, str]) -> radicale_item.Item:
"""Fetch a single item."""
return Item(
collection=self,
vobject_item=elem[1],
href=elem[0],
last_modified=self.last_modified,
etag=elem[2],
)
def _get(self, href: str) -> radicale_item.Item:
"""Fetch a single item."""
item, etag = self.adapter.to_vobject_etag(self.filename, href)
return self._convert((href, item, etag))
def has_uid(self, uid: str) -> bool:
"""Check if a UID exists in the collection."""
return uid in self.adapter.get_uids()
# fmt: off
def upload(self, href: str, item: radicale_item.Item) -> (
radicale_item.Item):
"""Upload a new or replace an existing item."""
# fmt: on
href = Collection.uid_cache.get(href, href)
if href in self.adapter.get_uids(self.filename):
uid = self.adapter.replace_vobject(href, item.vobject_item, self.filename)
else:
uid = self.adapter.append_vobject(item.vobject_item, self.filename)
Collection.uid_cache[href] = uid
try:
return self._get(uid)
except KeyError as error:
logger.warning(
"Unable to find uploaded event, maybe increase remind_lookahead_month"
)
raise ValueError(f"Failed to store item {href} in collection {self.path}: {error}") from error
def delete(self, href: None | str = None) -> None:
"""Delete an item.
When ``href`` is ``None``, delete the collection.
"""
if not href:
raise NotImplementedError
href = Collection.uid_cache.get(href, href)
self.adapter.remove(href, self.filename)
def _get_color(self) -> str:
files = self.adapter.get_filesnames()
index = files.index(self.filename)
rgb = hsv_to_rgb(index / len(files), 0.5, 0.9)
red, green, blue = (int(255 * x) for x in rgb)
return f"#{red:02x}{green:02x}{blue:02x}"
# fmt: off
@overload
def get_meta(self, key: None = None) -> Mapping[str, str]: ...
@overload
def get_meta(self, key: str) -> None | str: ...
def get_meta(self, key: None | str = None
) -> Mapping[str, str] | str | None:
"""Get metadata value for collection.
Return the value of the property ``key``. If ``key`` is ``None`` return
a dict with all properties
"""
# fmt: on
meta = self.adapter.get_meta()
meta["D:displayname"] = basename(self.path)
meta["ICAL:calendar-color"] = self._get_color()
return meta.get(key) if key else meta
def set_meta(self, props: Mapping[str, str]) -> None:
"""Set metadata values for collection.
``props`` a dict with values for properties.
"""
@property
def last_modified(self) -> str:
"""Get the HTTP-datetime of when the collection was modified."""
return strftime(
"%a, %d %b %Y %H:%M:%S +0000", gmtime(self.adapter.last_modified())
)
class Storage(BaseStorage):
def __init__(self, configuration: "config.Configuration") -> None:
"""Initialize BaseStorage.
``configuration`` see ``radicale.config`` module.
The ``configuration`` must not change during the lifetime of
this object, it is kept as an internal reference.
"""
super().__init__(configuration)
self.adapters: list[Abook | IcsTask | Remind] = []
self.filesystem_folder = expanduser(configuration.get("storage", "filesystem_folder"))
if "remind_file" in configuration.options("storage"):
zone = None
if "remind_timezone" in configuration.options("storage"):
zone = ZoneInfo(configuration.get("storage", "remind_timezone"))
month = 15
if "remind_lookahead_month" in configuration.options("storage"):
month = configuration.get("storage", "remind_lookahead_month")
self.adapters.append(Remind(configuration.get("storage", "remind_file"), zone, month=month))
if "abook_file" in configuration.options("storage"):
self.adapters.append(Abook(configuration.get("storage", "abook_file")))
if "task_folder" in configuration.options("storage"):
task_folder = configuration.get("storage", "task_folder")
task_projects = []
if "task_projects" in configuration.options("storage"):
task_projects = configuration.get("storage", "task_projects").split(",")
task_start = True
if "task_start" in configuration.options("storage"):
task_start = configuration.get("storage", "task_start")
self.adapters.append(IcsTask(task_folder, task_projects=task_projects, start_task=task_start))
# fmt: off
def discover(
self, path: str, depth: str = "0",
child_context_manager:
Callable[[str, str | None ], ContextManager[None]] | None = None,
user_groups: set[str] = set([])) -> Iterable[types.CollectionOrItem]:
"""Discover a list of collections under the given ``path``.
``path`` is sanitized.
If ``depth`` is "0", only the actual object under ``path`` is
returned.
If ``depth`` is anything but "0", it is considered as "1" and direct
children are included in the result.
The root collection "/" must always exist.
"""
# fmt: on
if path.count("/") < 3:
yield MinCollection(path)
if depth != "0":
for adapter in self.adapters:
for filename in adapter.get_filesnames():
yield Collection(
filename.replace(self.filesystem_folder, ""),
filename,
adapter,
)
return
filename = join(self.filesystem_folder, dirname(path).strip("/"))
collection = None
for adapter in self.adapters:
if filename in adapter.get_filesnames():
collection = Collection(path, filename, adapter)
break
if not collection:
return
if path.endswith("/"):
yield collection
if depth != "0":
for uid in collection._list():
yield collection._get(uid)
return
if basename(path) in collection._list():
yield collection._get(basename(path))
return
# fmt: off
def move(self, item: radicale_item.Item, to_collection: BaseCollection,
to_href: str) -> None:
"""Move an object.
``item`` is the item to move.
``to_collection`` is the target collection.
``to_href`` is the target name in ``to_collection``. An item with the
same name might already exist.
"""
# fmt: on
if not isinstance(item.collection, Collection) or not isinstance(to_collection, Collection):
raise NotImplementedError
if item.collection.path == to_collection.path and item.href == to_href:
return
to_collection.adapter.move_vobject(
to_href, item.collection.filename, to_collection.filename
)
@types.contextmanager
def acquire_lock(self, mode: str, user: str = "") -> Iterator[None]:
"""Set a context manager to lock the whole storage.
``mode`` must either be "r" for shared access or "w" for exclusive
access.
``user`` is the name of the logged in user or empty.
"""
yield
def verify(self) -> bool:
"""Check the storage for errors."""
return True