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