paul@2 | 1 | #!/usr/bin/env python |
paul@2 | 2 | |
paul@146 | 3 | """ |
paul@146 | 4 | A simple filesystem-based store of calendar data. |
paul@146 | 5 | |
paul@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@146 | 20 | """ |
paul@146 | 21 | |
paul@747 | 22 | from bisect import bisect_left |
paul@30 | 23 | from datetime import datetime |
paul@68 | 24 | from imiptools.config import STORE_DIR, PUBLISH_DIR |
paul@301 | 25 | from imiptools.data import make_calendar, parse_object, to_stream |
paul@740 | 26 | from imiptools.dates import format_datetime, get_datetime, to_timezone |
paul@147 | 27 | from imiptools.filesys import fix_permissions, FileBase |
paul@458 | 28 | from imiptools.period import FreeBusyPeriod |
paul@147 | 29 | from os.path import exists, isfile, join |
paul@343 | 30 | from os import listdir, remove, rmdir |
paul@303 | 31 | from time import sleep |
paul@395 | 32 | import codecs |
paul@15 | 33 | |
paul@50 | 34 | class FileStore(FileBase): |
paul@50 | 35 | |
paul@50 | 36 | "A file store of tabular free/busy data and objects." |
paul@50 | 37 | |
paul@597 | 38 | def __init__(self, store_dir=None): |
paul@597 | 39 | FileBase.__init__(self, store_dir or STORE_DIR) |
paul@147 | 40 | |
paul@303 | 41 | def acquire_lock(self, user, timeout=None): |
paul@303 | 42 | FileBase.acquire_lock(self, timeout, user) |
paul@303 | 43 | |
paul@303 | 44 | def release_lock(self, user): |
paul@303 | 45 | FileBase.release_lock(self, user) |
paul@303 | 46 | |
paul@648 | 47 | # Utility methods. |
paul@648 | 48 | |
paul@343 | 49 | def _set_defaults(self, t, empty_defaults): |
paul@343 | 50 | for i, default in empty_defaults: |
paul@343 | 51 | if i >= len(t): |
paul@343 | 52 | t += [None] * (i - len(t) + 1) |
paul@343 | 53 | if not t[i]: |
paul@343 | 54 | t[i] = default |
paul@343 | 55 | return t |
paul@343 | 56 | |
paul@343 | 57 | def _get_table(self, user, filename, empty_defaults=None): |
paul@343 | 58 | |
paul@343 | 59 | """ |
paul@343 | 60 | From the file for the given 'user' having the given 'filename', return |
paul@343 | 61 | a list of tuples representing the file's contents. |
paul@343 | 62 | |
paul@343 | 63 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 64 | default value where a column either does not exist or provides an empty |
paul@343 | 65 | value. |
paul@343 | 66 | """ |
paul@343 | 67 | |
paul@702 | 68 | f = codecs.open(filename, "rb", encoding="utf-8") |
paul@702 | 69 | try: |
paul@702 | 70 | l = [] |
paul@702 | 71 | for line in f.readlines(): |
paul@702 | 72 | t = line.strip(" \r\n").split("\t") |
paul@702 | 73 | if empty_defaults: |
paul@702 | 74 | t = self._set_defaults(t, empty_defaults) |
paul@702 | 75 | l.append(tuple(t)) |
paul@702 | 76 | return l |
paul@702 | 77 | finally: |
paul@702 | 78 | f.close() |
paul@702 | 79 | |
paul@702 | 80 | def _get_table_atomic(self, user, filename, empty_defaults=None): |
paul@702 | 81 | |
paul@702 | 82 | """ |
paul@702 | 83 | From the file for the given 'user' having the given 'filename', return |
paul@702 | 84 | a list of tuples representing the file's contents. |
paul@702 | 85 | |
paul@702 | 86 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@702 | 87 | default value where a column either does not exist or provides an empty |
paul@702 | 88 | value. |
paul@702 | 89 | """ |
paul@702 | 90 | |
paul@343 | 91 | self.acquire_lock(user) |
paul@343 | 92 | try: |
paul@702 | 93 | return self._get_table(user, filename, empty_defaults) |
paul@343 | 94 | finally: |
paul@343 | 95 | self.release_lock(user) |
paul@343 | 96 | |
paul@343 | 97 | def _set_table(self, user, filename, items, empty_defaults=None): |
paul@343 | 98 | |
paul@343 | 99 | """ |
paul@343 | 100 | For the given 'user', write to the file having the given 'filename' the |
paul@343 | 101 | 'items'. |
paul@343 | 102 | |
paul@343 | 103 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 104 | default value where a column either does not exist or provides an empty |
paul@343 | 105 | value. |
paul@343 | 106 | """ |
paul@343 | 107 | |
paul@702 | 108 | f = codecs.open(filename, "wb", encoding="utf-8") |
paul@702 | 109 | try: |
paul@702 | 110 | for item in items: |
paul@747 | 111 | self._set_table_item(f, item, empty_defaults) |
paul@702 | 112 | finally: |
paul@702 | 113 | f.close() |
paul@702 | 114 | fix_permissions(filename) |
paul@702 | 115 | |
paul@747 | 116 | def _set_table_item(self, f, item, empty_defaults=None): |
paul@747 | 117 | |
paul@747 | 118 | "Set in table 'f' the given 'item', using any 'empty_defaults'." |
paul@747 | 119 | |
paul@747 | 120 | if empty_defaults: |
paul@747 | 121 | item = self._set_defaults(list(item), empty_defaults) |
paul@747 | 122 | f.write("\t".join(item) + "\n") |
paul@747 | 123 | |
paul@702 | 124 | def _set_table_atomic(self, user, filename, items, empty_defaults=None): |
paul@702 | 125 | |
paul@702 | 126 | """ |
paul@702 | 127 | For the given 'user', write to the file having the given 'filename' the |
paul@702 | 128 | 'items'. |
paul@702 | 129 | |
paul@702 | 130 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@702 | 131 | default value where a column either does not exist or provides an empty |
paul@702 | 132 | value. |
paul@702 | 133 | """ |
paul@702 | 134 | |
paul@343 | 135 | self.acquire_lock(user) |
paul@343 | 136 | try: |
paul@702 | 137 | self._set_table(user, filename, items, empty_defaults) |
paul@343 | 138 | finally: |
paul@343 | 139 | self.release_lock(user) |
paul@343 | 140 | |
paul@648 | 141 | # Store object access. |
paul@648 | 142 | |
paul@329 | 143 | def _get_object(self, user, filename): |
paul@329 | 144 | |
paul@329 | 145 | """ |
paul@329 | 146 | Return the parsed object for the given 'user' having the given |
paul@329 | 147 | 'filename'. |
paul@329 | 148 | """ |
paul@329 | 149 | |
paul@329 | 150 | self.acquire_lock(user) |
paul@329 | 151 | try: |
paul@329 | 152 | f = open(filename, "rb") |
paul@329 | 153 | try: |
paul@329 | 154 | return parse_object(f, "utf-8") |
paul@329 | 155 | finally: |
paul@329 | 156 | f.close() |
paul@329 | 157 | finally: |
paul@329 | 158 | self.release_lock(user) |
paul@329 | 159 | |
paul@329 | 160 | def _set_object(self, user, filename, node): |
paul@329 | 161 | |
paul@329 | 162 | """ |
paul@329 | 163 | Set an object for the given 'user' having the given 'filename', using |
paul@329 | 164 | 'node' to define the object. |
paul@329 | 165 | """ |
paul@329 | 166 | |
paul@329 | 167 | self.acquire_lock(user) |
paul@329 | 168 | try: |
paul@329 | 169 | f = open(filename, "wb") |
paul@329 | 170 | try: |
paul@329 | 171 | to_stream(f, node) |
paul@329 | 172 | finally: |
paul@329 | 173 | f.close() |
paul@329 | 174 | fix_permissions(filename) |
paul@329 | 175 | finally: |
paul@329 | 176 | self.release_lock(user) |
paul@329 | 177 | |
paul@329 | 178 | return True |
paul@329 | 179 | |
paul@329 | 180 | def _remove_object(self, filename): |
paul@329 | 181 | |
paul@329 | 182 | "Remove the object with the given 'filename'." |
paul@329 | 183 | |
paul@329 | 184 | try: |
paul@329 | 185 | remove(filename) |
paul@329 | 186 | except OSError: |
paul@329 | 187 | return False |
paul@329 | 188 | |
paul@329 | 189 | return True |
paul@329 | 190 | |
paul@343 | 191 | def _remove_collection(self, filename): |
paul@343 | 192 | |
paul@343 | 193 | "Remove the collection with the given 'filename'." |
paul@343 | 194 | |
paul@343 | 195 | try: |
paul@343 | 196 | rmdir(filename) |
paul@343 | 197 | except OSError: |
paul@343 | 198 | return False |
paul@343 | 199 | |
paul@343 | 200 | return True |
paul@343 | 201 | |
paul@670 | 202 | # User discovery. |
paul@670 | 203 | |
paul@670 | 204 | def get_users(self): |
paul@670 | 205 | |
paul@670 | 206 | "Return a list of users." |
paul@670 | 207 | |
paul@670 | 208 | return listdir(self.store_dir) |
paul@670 | 209 | |
paul@648 | 210 | # Event and event metadata access. |
paul@648 | 211 | |
paul@119 | 212 | def get_events(self, user): |
paul@119 | 213 | |
paul@119 | 214 | "Return a list of event identifiers." |
paul@119 | 215 | |
paul@138 | 216 | filename = self.get_object_in_store(user, "objects") |
paul@119 | 217 | if not filename or not exists(filename): |
paul@119 | 218 | return None |
paul@119 | 219 | |
paul@119 | 220 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@119 | 221 | |
paul@648 | 222 | def get_all_events(self, user): |
paul@648 | 223 | |
paul@648 | 224 | "Return a set of (uid, recurrenceid) tuples for all events." |
paul@648 | 225 | |
paul@648 | 226 | uids = self.get_events(user) |
paul@674 | 227 | if not uids: |
paul@674 | 228 | return set() |
paul@648 | 229 | |
paul@648 | 230 | all_events = set() |
paul@648 | 231 | for uid in uids: |
paul@648 | 232 | all_events.add((uid, None)) |
paul@648 | 233 | all_events.update([(uid, recurrenceid) for recurrenceid in self.get_recurrences(user, uid)]) |
paul@648 | 234 | |
paul@648 | 235 | return all_events |
paul@648 | 236 | |
paul@694 | 237 | def get_event_filename(self, user, uid, recurrenceid=None, dirname=None): |
paul@648 | 238 | |
paul@694 | 239 | """ |
paul@694 | 240 | Get the filename providing the event for the given 'user' with the given |
paul@694 | 241 | 'uid'. If the optional 'recurrenceid' is specified, a specific instance |
paul@694 | 242 | or occurrence of an event is returned. |
paul@648 | 243 | |
paul@694 | 244 | Where 'dirname' is specified, the given directory name is used as the |
paul@694 | 245 | base of the location within which any filename will reside. |
paul@694 | 246 | """ |
paul@648 | 247 | |
paul@694 | 248 | if recurrenceid: |
paul@694 | 249 | return self.get_recurrence_filename(user, uid, recurrenceid, dirname) |
paul@694 | 250 | else: |
paul@694 | 251 | return self.get_complete_event_filename(user, uid, dirname) |
paul@648 | 252 | |
paul@343 | 253 | def get_event(self, user, uid, recurrenceid=None): |
paul@343 | 254 | |
paul@343 | 255 | """ |
paul@343 | 256 | Get the event for the given 'user' with the given 'uid'. If |
paul@343 | 257 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 258 | occurrence of an event is returned. |
paul@343 | 259 | """ |
paul@343 | 260 | |
paul@694 | 261 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@694 | 262 | if not filename or not exists(filename): |
paul@694 | 263 | return None |
paul@694 | 264 | |
paul@694 | 265 | return filename and self._get_object(user, filename) |
paul@694 | 266 | |
paul@694 | 267 | def get_complete_event_filename(self, user, uid, dirname=None): |
paul@694 | 268 | |
paul@694 | 269 | """ |
paul@694 | 270 | Get the filename providing the event for the given 'user' with the given |
paul@694 | 271 | 'uid'. |
paul@694 | 272 | |
paul@694 | 273 | Where 'dirname' is specified, the given directory name is used as the |
paul@694 | 274 | base of the location within which any filename will reside. |
paul@694 | 275 | """ |
paul@694 | 276 | |
paul@694 | 277 | return self.get_object_in_store(user, dirname, "objects", uid) |
paul@343 | 278 | |
paul@343 | 279 | def get_complete_event(self, user, uid): |
paul@50 | 280 | |
paul@50 | 281 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 282 | |
paul@694 | 283 | filename = self.get_complete_event_filename(user, uid) |
paul@50 | 284 | if not filename or not exists(filename): |
paul@50 | 285 | return None |
paul@50 | 286 | |
paul@694 | 287 | return filename and self._get_object(user, filename) |
paul@50 | 288 | |
paul@343 | 289 | def set_event(self, user, uid, recurrenceid, node): |
paul@343 | 290 | |
paul@343 | 291 | """ |
paul@343 | 292 | Set an event for 'user' having the given 'uid' and 'recurrenceid' (which |
paul@343 | 293 | if the latter is specified, a specific instance or occurrence of an |
paul@343 | 294 | event is referenced), using the given 'node' description. |
paul@343 | 295 | """ |
paul@343 | 296 | |
paul@343 | 297 | if recurrenceid: |
paul@343 | 298 | return self.set_recurrence(user, uid, recurrenceid, node) |
paul@343 | 299 | else: |
paul@343 | 300 | return self.set_complete_event(user, uid, node) |
paul@343 | 301 | |
paul@343 | 302 | def set_complete_event(self, user, uid, node): |
paul@50 | 303 | |
paul@50 | 304 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 305 | |
paul@138 | 306 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 307 | if not filename: |
paul@50 | 308 | return False |
paul@50 | 309 | |
paul@329 | 310 | return self._set_object(user, filename, node) |
paul@15 | 311 | |
paul@365 | 312 | def remove_event(self, user, uid, recurrenceid=None): |
paul@234 | 313 | |
paul@343 | 314 | """ |
paul@343 | 315 | Remove an event for 'user' having the given 'uid'. If the optional |
paul@343 | 316 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@343 | 317 | event is removed. |
paul@343 | 318 | """ |
paul@343 | 319 | |
paul@343 | 320 | if recurrenceid: |
paul@343 | 321 | return self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 322 | else: |
paul@343 | 323 | for recurrenceid in self.get_recurrences(user, uid) or []: |
paul@343 | 324 | self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 325 | return self.remove_complete_event(user, uid) |
paul@343 | 326 | |
paul@343 | 327 | def remove_complete_event(self, user, uid): |
paul@343 | 328 | |
paul@234 | 329 | "Remove an event for 'user' having the given 'uid'." |
paul@234 | 330 | |
paul@378 | 331 | self.remove_recurrences(user, uid) |
paul@369 | 332 | |
paul@234 | 333 | filename = self.get_object_in_store(user, "objects", uid) |
paul@234 | 334 | if not filename: |
paul@234 | 335 | return False |
paul@234 | 336 | |
paul@329 | 337 | return self._remove_object(filename) |
paul@234 | 338 | |
paul@334 | 339 | def get_recurrences(self, user, uid): |
paul@334 | 340 | |
paul@334 | 341 | """ |
paul@334 | 342 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 343 | indicated 'uid'. Both active and cancelled recurrences are returned. |
paul@694 | 344 | """ |
paul@694 | 345 | |
paul@694 | 346 | return self.get_active_recurrences(user, uid) + self.get_cancelled_recurrences(user, uid) |
paul@694 | 347 | |
paul@694 | 348 | def get_active_recurrences(self, user, uid): |
paul@694 | 349 | |
paul@694 | 350 | """ |
paul@694 | 351 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 352 | indicated 'uid'. Cancelled recurrences are not returned. |
paul@334 | 353 | """ |
paul@334 | 354 | |
paul@334 | 355 | filename = self.get_object_in_store(user, "recurrences", uid) |
paul@334 | 356 | if not filename or not exists(filename): |
paul@347 | 357 | return [] |
paul@334 | 358 | |
paul@334 | 359 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@334 | 360 | |
paul@694 | 361 | def get_cancelled_recurrences(self, user, uid): |
paul@694 | 362 | |
paul@694 | 363 | """ |
paul@694 | 364 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 365 | indicated 'uid'. Only cancelled recurrences are returned. |
paul@694 | 366 | """ |
paul@694 | 367 | |
paul@694 | 368 | filename = self.get_object_in_store(user, "cancelled", "recurrences", uid) |
paul@694 | 369 | if not filename or not exists(filename): |
paul@694 | 370 | return [] |
paul@694 | 371 | |
paul@694 | 372 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@694 | 373 | |
paul@694 | 374 | def get_recurrence_filename(self, user, uid, recurrenceid, dirname=None): |
paul@694 | 375 | |
paul@694 | 376 | """ |
paul@694 | 377 | For the event of the given 'user' with the given 'uid', return the |
paul@694 | 378 | filename providing the recurrence with the given 'recurrenceid'. |
paul@694 | 379 | |
paul@694 | 380 | Where 'dirname' is specified, the given directory name is used as the |
paul@694 | 381 | base of the location within which any filename will reside. |
paul@694 | 382 | """ |
paul@694 | 383 | |
paul@694 | 384 | return self.get_object_in_store(user, dirname, "recurrences", uid, recurrenceid) |
paul@694 | 385 | |
paul@334 | 386 | def get_recurrence(self, user, uid, recurrenceid): |
paul@334 | 387 | |
paul@334 | 388 | """ |
paul@334 | 389 | For the event of the given 'user' with the given 'uid', return the |
paul@334 | 390 | specific recurrence indicated by the 'recurrenceid'. |
paul@334 | 391 | """ |
paul@334 | 392 | |
paul@694 | 393 | filename = self.get_recurrence_filename(user, uid, recurrenceid) |
paul@334 | 394 | if not filename or not exists(filename): |
paul@334 | 395 | return None |
paul@334 | 396 | |
paul@694 | 397 | return filename and self._get_object(user, filename) |
paul@334 | 398 | |
paul@334 | 399 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@334 | 400 | |
paul@334 | 401 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@334 | 402 | |
paul@334 | 403 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 404 | if not filename: |
paul@334 | 405 | return False |
paul@334 | 406 | |
paul@334 | 407 | return self._set_object(user, filename, node) |
paul@334 | 408 | |
paul@334 | 409 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@334 | 410 | |
paul@378 | 411 | """ |
paul@378 | 412 | Remove a special recurrence from an event stored by 'user' having the |
paul@378 | 413 | given 'uid' and 'recurrenceid'. |
paul@378 | 414 | """ |
paul@334 | 415 | |
paul@378 | 416 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 417 | if not filename: |
paul@334 | 418 | return False |
paul@334 | 419 | |
paul@334 | 420 | return self._remove_object(filename) |
paul@334 | 421 | |
paul@378 | 422 | def remove_recurrences(self, user, uid): |
paul@378 | 423 | |
paul@378 | 424 | """ |
paul@378 | 425 | Remove all recurrences for an event stored by 'user' having the given |
paul@378 | 426 | 'uid'. |
paul@378 | 427 | """ |
paul@378 | 428 | |
paul@378 | 429 | for recurrenceid in self.get_recurrences(user, uid): |
paul@378 | 430 | self.remove_recurrence(user, uid, recurrenceid) |
paul@378 | 431 | |
paul@378 | 432 | recurrences = self.get_object_in_store(user, "recurrences", uid) |
paul@378 | 433 | if recurrences: |
paul@378 | 434 | return self._remove_collection(recurrences) |
paul@378 | 435 | |
paul@378 | 436 | return True |
paul@378 | 437 | |
paul@652 | 438 | # Free/busy period providers, upon extension of the free/busy records. |
paul@652 | 439 | |
paul@672 | 440 | def _get_freebusy_providers(self, user): |
paul@672 | 441 | |
paul@672 | 442 | """ |
paul@672 | 443 | Return the free/busy providers for the given 'user'. |
paul@672 | 444 | |
paul@672 | 445 | This function returns any stored datetime and a list of providers as a |
paul@672 | 446 | 2-tuple. Each provider is itself a (uid, recurrenceid) tuple. |
paul@672 | 447 | """ |
paul@672 | 448 | |
paul@672 | 449 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@672 | 450 | if not filename or not exists(filename): |
paul@672 | 451 | return None |
paul@672 | 452 | |
paul@672 | 453 | # Attempt to read providers, with a declaration of the datetime |
paul@672 | 454 | # from which such providers are considered as still being active. |
paul@672 | 455 | |
paul@702 | 456 | t = self._get_table_atomic(user, filename, [(1, None)]) |
paul@672 | 457 | try: |
paul@672 | 458 | dt_string = t[0][0] |
paul@672 | 459 | except IndexError: |
paul@672 | 460 | return None |
paul@672 | 461 | |
paul@672 | 462 | return dt_string, t[1:] |
paul@672 | 463 | |
paul@652 | 464 | def get_freebusy_providers(self, user, dt=None): |
paul@652 | 465 | |
paul@652 | 466 | """ |
paul@652 | 467 | Return a set of uncancelled events of the form (uid, recurrenceid) |
paul@652 | 468 | providing free/busy details beyond the given datetime 'dt'. |
paul@654 | 469 | |
paul@654 | 470 | If 'dt' is not specified, all events previously found to provide |
paul@654 | 471 | details will be returned. Otherwise, if 'dt' is earlier than the |
paul@654 | 472 | datetime recorded for the known providers, None is returned, indicating |
paul@654 | 473 | that the list of providers must be recomputed. |
paul@672 | 474 | |
paul@672 | 475 | This function returns a list of (uid, recurrenceid) tuples upon success. |
paul@652 | 476 | """ |
paul@652 | 477 | |
paul@672 | 478 | t = self._get_freebusy_providers(user) |
paul@672 | 479 | if not t: |
paul@672 | 480 | return None |
paul@672 | 481 | |
paul@672 | 482 | dt_string, t = t |
paul@672 | 483 | |
paul@672 | 484 | # If the requested datetime is earlier than the stated datetime, the |
paul@672 | 485 | # providers will need to be recomputed. |
paul@672 | 486 | |
paul@672 | 487 | if dt: |
paul@672 | 488 | providers_dt = get_datetime(dt_string) |
paul@672 | 489 | if not providers_dt or providers_dt > dt: |
paul@672 | 490 | return None |
paul@672 | 491 | |
paul@672 | 492 | # Otherwise, return the providers. |
paul@672 | 493 | |
paul@672 | 494 | return t[1:] |
paul@672 | 495 | |
paul@672 | 496 | def _set_freebusy_providers(self, user, dt_string, t): |
paul@672 | 497 | |
paul@672 | 498 | "Set the given provider timestamp 'dt_string' and table 't'." |
paul@672 | 499 | |
paul@652 | 500 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@672 | 501 | if not filename: |
paul@672 | 502 | return False |
paul@652 | 503 | |
paul@672 | 504 | t.insert(0, (dt_string,)) |
paul@702 | 505 | self._set_table_atomic(user, filename, t, [(1, "")]) |
paul@672 | 506 | return True |
paul@652 | 507 | |
paul@654 | 508 | def set_freebusy_providers(self, user, dt, providers): |
paul@654 | 509 | |
paul@654 | 510 | """ |
paul@654 | 511 | Define the uncancelled events providing free/busy details beyond the |
paul@654 | 512 | given datetime 'dt'. |
paul@654 | 513 | """ |
paul@654 | 514 | |
paul@672 | 515 | t = [] |
paul@654 | 516 | |
paul@654 | 517 | for obj in providers: |
paul@672 | 518 | t.append((obj.get_uid(), obj.get_recurrenceid())) |
paul@672 | 519 | |
paul@672 | 520 | return self._set_freebusy_providers(user, format_datetime(dt), t) |
paul@654 | 521 | |
paul@672 | 522 | def append_freebusy_provider(self, user, provider): |
paul@672 | 523 | |
paul@672 | 524 | "For the given 'user', append the free/busy 'provider'." |
paul@672 | 525 | |
paul@672 | 526 | t = self._get_freebusy_providers(user) |
paul@672 | 527 | if not t: |
paul@654 | 528 | return False |
paul@654 | 529 | |
paul@672 | 530 | dt_string, t = t |
paul@672 | 531 | t.append((provider.get_uid(), provider.get_recurrenceid())) |
paul@672 | 532 | |
paul@672 | 533 | return self._set_freebusy_providers(user, dt_string, t) |
paul@672 | 534 | |
paul@672 | 535 | def remove_freebusy_provider(self, user, provider): |
paul@672 | 536 | |
paul@672 | 537 | "For the given 'user', remove the free/busy 'provider'." |
paul@672 | 538 | |
paul@672 | 539 | t = self._get_freebusy_providers(user) |
paul@672 | 540 | if not t: |
paul@672 | 541 | return False |
paul@672 | 542 | |
paul@672 | 543 | dt_string, t = t |
paul@672 | 544 | try: |
paul@672 | 545 | t.remove((provider.get_uid(), provider.get_recurrenceid())) |
paul@672 | 546 | except ValueError: |
paul@672 | 547 | return False |
paul@672 | 548 | |
paul@672 | 549 | return self._set_freebusy_providers(user, dt_string, t) |
paul@654 | 550 | |
paul@648 | 551 | # Free/busy period access. |
paul@648 | 552 | |
paul@702 | 553 | def get_freebusy(self, user, name=None, get_table=None): |
paul@15 | 554 | |
paul@15 | 555 | "Get free/busy details for the given 'user'." |
paul@15 | 556 | |
paul@702 | 557 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@15 | 558 | if not filename or not exists(filename): |
paul@167 | 559 | return [] |
paul@112 | 560 | else: |
paul@702 | 561 | return map(lambda t: FreeBusyPeriod(*t), |
paul@702 | 562 | (get_table or self._get_table_atomic)(user, filename, [(4, None)])) |
paul@702 | 563 | |
paul@702 | 564 | def get_freebusy_for_other(self, user, other, get_table=None): |
paul@112 | 565 | |
paul@112 | 566 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 567 | |
paul@112 | 568 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@167 | 569 | if not filename or not exists(filename): |
paul@167 | 570 | return [] |
paul@112 | 571 | else: |
paul@702 | 572 | return map(lambda t: FreeBusyPeriod(*t), |
paul@702 | 573 | (get_table or self._get_table_atomic)(user, filename, [(4, None)])) |
paul@702 | 574 | |
paul@702 | 575 | def set_freebusy(self, user, freebusy, name=None, set_table=None): |
paul@15 | 576 | |
paul@15 | 577 | "For the given 'user', set 'freebusy' details." |
paul@15 | 578 | |
paul@702 | 579 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@15 | 580 | if not filename: |
paul@15 | 581 | return False |
paul@15 | 582 | |
paul@702 | 583 | (set_table or self._set_table_atomic)(user, filename, |
paul@702 | 584 | map(lambda fb: fb.as_tuple(strings_only=True), freebusy)) |
paul@15 | 585 | return True |
paul@15 | 586 | |
paul@702 | 587 | def set_freebusy_for_other(self, user, freebusy, other, set_table=None): |
paul@110 | 588 | |
paul@110 | 589 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 590 | |
paul@110 | 591 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 592 | if not filename: |
paul@110 | 593 | return False |
paul@110 | 594 | |
paul@702 | 595 | (set_table or self._set_table_atomic)(user, filename, |
paul@702 | 596 | map(lambda fb: fb.as_tuple(strings_only=True), freebusy)) |
paul@112 | 597 | return True |
paul@112 | 598 | |
paul@710 | 599 | # Tentative free/busy periods related to countering. |
paul@710 | 600 | |
paul@710 | 601 | def get_freebusy_offers(self, user): |
paul@710 | 602 | |
paul@710 | 603 | "Get free/busy offers for the given 'user'." |
paul@710 | 604 | |
paul@710 | 605 | offers = [] |
paul@710 | 606 | expired = [] |
paul@741 | 607 | now = to_timezone(datetime.utcnow(), "UTC") |
paul@710 | 608 | |
paul@710 | 609 | # Expire old offers and save the collection if modified. |
paul@710 | 610 | |
paul@730 | 611 | self.acquire_lock(user) |
paul@710 | 612 | try: |
paul@730 | 613 | l = self.get_freebusy(user, "freebusy-offers") |
paul@710 | 614 | for fb in l: |
paul@710 | 615 | if fb.expires and get_datetime(fb.expires) <= now: |
paul@710 | 616 | expired.append(fb) |
paul@710 | 617 | else: |
paul@710 | 618 | offers.append(fb) |
paul@710 | 619 | |
paul@710 | 620 | if expired: |
paul@730 | 621 | self.set_freebusy_offers(user, offers) |
paul@710 | 622 | finally: |
paul@730 | 623 | self.release_lock(user) |
paul@710 | 624 | |
paul@710 | 625 | return offers |
paul@710 | 626 | |
paul@710 | 627 | def set_freebusy_offers(self, user, freebusy): |
paul@710 | 628 | |
paul@710 | 629 | "For the given 'user', set 'freebusy' offers." |
paul@710 | 630 | |
paul@710 | 631 | return self.set_freebusy(user, freebusy, "freebusy-offers") |
paul@710 | 632 | |
paul@747 | 633 | # Requests and counter-proposals. |
paul@648 | 634 | |
paul@142 | 635 | def _get_requests(self, user, queue): |
paul@66 | 636 | |
paul@142 | 637 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 638 | |
paul@142 | 639 | filename = self.get_object_in_store(user, queue) |
paul@81 | 640 | if not filename or not exists(filename): |
paul@66 | 641 | return None |
paul@66 | 642 | |
paul@747 | 643 | return self._get_table_atomic(user, filename, [(1, None), (2, None)]) |
paul@66 | 644 | |
paul@142 | 645 | def get_requests(self, user): |
paul@142 | 646 | |
paul@142 | 647 | "Get requests for the given 'user'." |
paul@142 | 648 | |
paul@142 | 649 | return self._get_requests(user, "requests") |
paul@142 | 650 | |
paul@142 | 651 | def _set_requests(self, user, requests, queue): |
paul@66 | 652 | |
paul@142 | 653 | """ |
paul@142 | 654 | For the given 'user', set the list of queued 'requests' in the given |
paul@142 | 655 | 'queue'. |
paul@142 | 656 | """ |
paul@142 | 657 | |
paul@142 | 658 | filename = self.get_object_in_store(user, queue) |
paul@66 | 659 | if not filename: |
paul@66 | 660 | return False |
paul@66 | 661 | |
paul@747 | 662 | self._set_table_atomic(user, filename, requests, [(1, ""), (2, "")]) |
paul@66 | 663 | return True |
paul@66 | 664 | |
paul@142 | 665 | def set_requests(self, user, requests): |
paul@142 | 666 | |
paul@142 | 667 | "For the given 'user', set the list of queued 'requests'." |
paul@142 | 668 | |
paul@142 | 669 | return self._set_requests(user, requests, "requests") |
paul@142 | 670 | |
paul@747 | 671 | def _set_request(self, user, request, queue): |
paul@142 | 672 | |
paul@343 | 673 | """ |
paul@747 | 674 | For the given 'user', set the given 'request' in the given 'queue'. |
paul@343 | 675 | """ |
paul@142 | 676 | |
paul@142 | 677 | filename = self.get_object_in_store(user, queue) |
paul@55 | 678 | if not filename: |
paul@55 | 679 | return False |
paul@55 | 680 | |
paul@303 | 681 | self.acquire_lock(user) |
paul@55 | 682 | try: |
paul@747 | 683 | f = codecs.open(filename, "ab", encoding="utf-8") |
paul@303 | 684 | try: |
paul@747 | 685 | self._set_table_item(f, request, [(1, ""), (2, "")]) |
paul@303 | 686 | finally: |
paul@303 | 687 | f.close() |
paul@303 | 688 | fix_permissions(filename) |
paul@55 | 689 | finally: |
paul@303 | 690 | self.release_lock(user) |
paul@55 | 691 | |
paul@55 | 692 | return True |
paul@55 | 693 | |
paul@747 | 694 | def set_request(self, user, uid, recurrenceid=None, type=None): |
paul@142 | 695 | |
paul@747 | 696 | """ |
paul@747 | 697 | For the given 'user', set the queued 'uid' and 'recurrenceid', |
paul@747 | 698 | indicating a request, along with any given 'type'. |
paul@747 | 699 | """ |
paul@142 | 700 | |
paul@747 | 701 | return self._set_request(user, (uid, recurrenceid, type), "requests") |
paul@747 | 702 | |
paul@747 | 703 | def queue_request(self, user, uid, recurrenceid=None, type=None): |
paul@142 | 704 | |
paul@343 | 705 | """ |
paul@343 | 706 | Queue a request for 'user' having the given 'uid'. If the optional |
paul@747 | 707 | 'recurrenceid' is specified, the entry refers to a specific instance |
paul@747 | 708 | or occurrence of an event. The 'type' parameter can be used to indicate |
paul@747 | 709 | a specific type of request. |
paul@747 | 710 | """ |
paul@747 | 711 | |
paul@747 | 712 | requests = self.get_requests(user) or [] |
paul@747 | 713 | |
paul@747 | 714 | if not self.have_request(requests, uid, recurrenceid): |
paul@747 | 715 | return self.set_request(user, uid, recurrenceid, type) |
paul@747 | 716 | |
paul@747 | 717 | return False |
paul@747 | 718 | |
paul@747 | 719 | def dequeue_request(self, user, uid, recurrenceid=None, type=None): |
paul@747 | 720 | |
paul@747 | 721 | """ |
paul@747 | 722 | Dequeue all requests for 'user' having the given 'uid'. If the optional |
paul@747 | 723 | 'recurrenceid' is specified, all requests for that specific instance or |
paul@747 | 724 | occurrence of an event are dequeued. |
paul@343 | 725 | """ |
paul@66 | 726 | |
paul@81 | 727 | requests = self.get_requests(user) or [] |
paul@66 | 728 | |
paul@747 | 729 | if not self.have_request(requests, uid, recurrenceid): |
paul@747 | 730 | return False |
paul@747 | 731 | |
paul@747 | 732 | i = bisect_left(requests, (uid, recurrenceid)) |
paul@747 | 733 | while i < len(requests) and requests[i][:2] == (uid, recurrenceid): |
paul@747 | 734 | |
paul@747 | 735 | # Remove associated objects. |
paul@747 | 736 | |
paul@747 | 737 | type = requests[i][2] |
paul@747 | 738 | if type == "COUNTER": |
paul@747 | 739 | self.remove_counter(user, uid, recurrenceid) |
paul@66 | 740 | |
paul@747 | 741 | # Remove the request. |
paul@747 | 742 | |
paul@747 | 743 | del requests[i] |
paul@747 | 744 | i += 1 |
paul@66 | 745 | |
paul@747 | 746 | self.set_requests(user, requests) |
paul@747 | 747 | return True |
paul@747 | 748 | |
paul@747 | 749 | def have_request(self, requests, uid, recurrenceid=None): |
paul@747 | 750 | i = bisect_left(requests, (uid, recurrenceid)) |
paul@747 | 751 | return i < len(requests) and requests[i][:2] == (uid, recurrenceid) |
paul@747 | 752 | |
paul@747 | 753 | def set_counter(self, user, node, uid, recurrenceid=None): |
paul@105 | 754 | |
paul@343 | 755 | """ |
paul@747 | 756 | For the given 'user', store the given 'node' for the given 'uid' and |
paul@747 | 757 | 'recurrenceid' as a counter-proposal. |
paul@343 | 758 | """ |
paul@105 | 759 | |
paul@747 | 760 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@747 | 761 | if not filename: |
paul@747 | 762 | return False |
paul@747 | 763 | |
paul@747 | 764 | return self._set_object(user, filename, node) |
paul@747 | 765 | |
paul@747 | 766 | def remove_counter(self, user, uid, recurrenceid=None): |
paul@105 | 767 | |
paul@747 | 768 | """ |
paul@747 | 769 | For the given 'user', remove any counter-proposal associated with the |
paul@747 | 770 | given 'uid' and 'recurrenceid'. |
paul@747 | 771 | """ |
paul@747 | 772 | |
paul@747 | 773 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@747 | 774 | if not filename: |
paul@105 | 775 | return False |
paul@747 | 776 | |
paul@747 | 777 | return self._remove_object(filename) |
paul@747 | 778 | |
paul@747 | 779 | # Event cancellation. |
paul@105 | 780 | |
paul@343 | 781 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@142 | 782 | |
paul@343 | 783 | """ |
paul@694 | 784 | Cancel an event for 'user' having the given 'uid'. If the optional |
paul@694 | 785 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@694 | 786 | event is cancelled. |
paul@343 | 787 | """ |
paul@142 | 788 | |
paul@694 | 789 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@694 | 790 | cancelled_filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@142 | 791 | |
paul@694 | 792 | if filename and cancelled_filename and exists(filename): |
paul@694 | 793 | return self.move_object(filename, cancelled_filename) |
paul@142 | 794 | |
paul@142 | 795 | return False |
paul@142 | 796 | |
paul@30 | 797 | class FilePublisher(FileBase): |
paul@30 | 798 | |
paul@30 | 799 | "A publisher of objects." |
paul@30 | 800 | |
paul@597 | 801 | def __init__(self, store_dir=None): |
paul@597 | 802 | FileBase.__init__(self, store_dir or PUBLISH_DIR) |
paul@30 | 803 | |
paul@30 | 804 | def set_freebusy(self, user, freebusy): |
paul@30 | 805 | |
paul@30 | 806 | "For the given 'user', set 'freebusy' details." |
paul@30 | 807 | |
paul@52 | 808 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 809 | if not filename: |
paul@30 | 810 | return False |
paul@30 | 811 | |
paul@30 | 812 | record = [] |
paul@30 | 813 | rwrite = record.append |
paul@30 | 814 | |
paul@30 | 815 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 816 | rwrite(("UID", {}, user)) |
paul@30 | 817 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 818 | |
paul@458 | 819 | for fb in freebusy: |
paul@458 | 820 | if not fb.transp or fb.transp == "OPAQUE": |
paul@529 | 821 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@563 | 822 | map(format_datetime, [fb.get_start_point(), fb.get_end_point()])))) |
paul@30 | 823 | |
paul@395 | 824 | f = open(filename, "wb") |
paul@30 | 825 | try: |
paul@30 | 826 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 827 | finally: |
paul@30 | 828 | f.close() |
paul@103 | 829 | fix_permissions(filename) |
paul@30 | 830 | |
paul@30 | 831 | return True |
paul@30 | 832 | |
paul@2 | 833 | # vim: tabstop=4 expandtab shiftwidth=4 |