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@1039 | 6 | Copyright (C) 2014, 2015, 2016 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@1039 | 23 | from imiptools.config import STORE_DIR, PUBLISH_DIR, JOURNAL_DIR |
paul@301 | 24 | from imiptools.data import make_calendar, parse_object, to_stream |
paul@740 | 25 | from imiptools.dates import format_datetime, get_datetime, to_timezone |
paul@147 | 26 | from imiptools.filesys import fix_permissions, FileBase |
paul@458 | 27 | from imiptools.period import FreeBusyPeriod |
paul@1046 | 28 | from imiptools.text import parse_line |
paul@808 | 29 | from os.path import isdir, isfile, join |
paul@343 | 30 | from os import listdir, remove, rmdir |
paul@395 | 31 | import codecs |
paul@15 | 32 | |
paul@1039 | 33 | class FileStoreBase(FileBase): |
paul@50 | 34 | |
paul@1039 | 35 | "A file store supporting user-specific locking and tabular data." |
paul@147 | 36 | |
paul@303 | 37 | def acquire_lock(self, user, timeout=None): |
paul@303 | 38 | FileBase.acquire_lock(self, timeout, user) |
paul@303 | 39 | |
paul@303 | 40 | def release_lock(self, user): |
paul@303 | 41 | FileBase.release_lock(self, user) |
paul@303 | 42 | |
paul@648 | 43 | # Utility methods. |
paul@648 | 44 | |
paul@343 | 45 | def _set_defaults(self, t, empty_defaults): |
paul@343 | 46 | for i, default in empty_defaults: |
paul@343 | 47 | if i >= len(t): |
paul@343 | 48 | t += [None] * (i - len(t) + 1) |
paul@343 | 49 | if not t[i]: |
paul@343 | 50 | t[i] = default |
paul@343 | 51 | return t |
paul@343 | 52 | |
paul@1046 | 53 | def _get_table(self, user, filename, empty_defaults=None, tab_separated=True): |
paul@343 | 54 | |
paul@343 | 55 | """ |
paul@343 | 56 | From the file for the given 'user' having the given 'filename', return |
paul@343 | 57 | a list of tuples representing the file's contents. |
paul@343 | 58 | |
paul@343 | 59 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 60 | default value where a column either does not exist or provides an empty |
paul@343 | 61 | value. |
paul@1046 | 62 | |
paul@1046 | 63 | If 'tab_separated' is specified and is a false value, line parsing using |
paul@1046 | 64 | the imiptools.text.parse_line function will be performed instead of |
paul@1046 | 65 | splitting each line of the file using tab characters as separators. |
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@1046 | 72 | line = line.strip(" \r\n") |
paul@1046 | 73 | if tab_separated: |
paul@1046 | 74 | t = line.split("\t") |
paul@1046 | 75 | else: |
paul@1046 | 76 | t = parse_line(line) |
paul@702 | 77 | if empty_defaults: |
paul@702 | 78 | t = self._set_defaults(t, empty_defaults) |
paul@702 | 79 | l.append(tuple(t)) |
paul@702 | 80 | return l |
paul@702 | 81 | finally: |
paul@702 | 82 | f.close() |
paul@702 | 83 | |
paul@1046 | 84 | def _get_table_atomic(self, user, filename, empty_defaults=None, tab_separated=True): |
paul@702 | 85 | |
paul@702 | 86 | """ |
paul@702 | 87 | From the file for the given 'user' having the given 'filename', return |
paul@702 | 88 | a list of tuples representing the file's contents. |
paul@702 | 89 | |
paul@702 | 90 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@702 | 91 | default value where a column either does not exist or provides an empty |
paul@702 | 92 | value. |
paul@1046 | 93 | |
paul@1046 | 94 | If 'tab_separated' is specified and is a false value, line parsing using |
paul@1046 | 95 | the imiptools.text.parse_line function will be performed instead of |
paul@1046 | 96 | splitting each line of the file using tab characters as separators. |
paul@702 | 97 | """ |
paul@702 | 98 | |
paul@343 | 99 | self.acquire_lock(user) |
paul@343 | 100 | try: |
paul@1046 | 101 | return self._get_table(user, filename, empty_defaults, tab_separated) |
paul@343 | 102 | finally: |
paul@343 | 103 | self.release_lock(user) |
paul@343 | 104 | |
paul@343 | 105 | def _set_table(self, user, filename, items, empty_defaults=None): |
paul@343 | 106 | |
paul@343 | 107 | """ |
paul@343 | 108 | For the given 'user', write to the file having the given 'filename' the |
paul@343 | 109 | 'items'. |
paul@343 | 110 | |
paul@343 | 111 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 112 | default value where a column either does not exist or provides an empty |
paul@343 | 113 | value. |
paul@343 | 114 | """ |
paul@343 | 115 | |
paul@702 | 116 | f = codecs.open(filename, "wb", encoding="utf-8") |
paul@702 | 117 | try: |
paul@702 | 118 | for item in items: |
paul@747 | 119 | self._set_table_item(f, item, empty_defaults) |
paul@702 | 120 | finally: |
paul@702 | 121 | f.close() |
paul@702 | 122 | fix_permissions(filename) |
paul@702 | 123 | |
paul@747 | 124 | def _set_table_item(self, f, item, empty_defaults=None): |
paul@747 | 125 | |
paul@747 | 126 | "Set in table 'f' the given 'item', using any 'empty_defaults'." |
paul@747 | 127 | |
paul@747 | 128 | if empty_defaults: |
paul@747 | 129 | item = self._set_defaults(list(item), empty_defaults) |
paul@747 | 130 | f.write("\t".join(item) + "\n") |
paul@747 | 131 | |
paul@702 | 132 | def _set_table_atomic(self, user, filename, items, empty_defaults=None): |
paul@702 | 133 | |
paul@702 | 134 | """ |
paul@702 | 135 | For the given 'user', write to the file having the given 'filename' the |
paul@702 | 136 | 'items'. |
paul@702 | 137 | |
paul@702 | 138 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@702 | 139 | default value where a column either does not exist or provides an empty |
paul@702 | 140 | value. |
paul@702 | 141 | """ |
paul@702 | 142 | |
paul@343 | 143 | self.acquire_lock(user) |
paul@343 | 144 | try: |
paul@702 | 145 | self._set_table(user, filename, items, empty_defaults) |
paul@343 | 146 | finally: |
paul@343 | 147 | self.release_lock(user) |
paul@343 | 148 | |
paul@1039 | 149 | class FileStore(FileStoreBase): |
paul@1039 | 150 | |
paul@1039 | 151 | "A file store of tabular free/busy data and objects." |
paul@1039 | 152 | |
paul@1039 | 153 | def __init__(self, store_dir=None): |
paul@1039 | 154 | FileBase.__init__(self, store_dir or STORE_DIR) |
paul@1039 | 155 | |
paul@648 | 156 | # Store object access. |
paul@648 | 157 | |
paul@329 | 158 | def _get_object(self, user, filename): |
paul@329 | 159 | |
paul@329 | 160 | """ |
paul@329 | 161 | Return the parsed object for the given 'user' having the given |
paul@329 | 162 | 'filename'. |
paul@329 | 163 | """ |
paul@329 | 164 | |
paul@329 | 165 | self.acquire_lock(user) |
paul@329 | 166 | try: |
paul@329 | 167 | f = open(filename, "rb") |
paul@329 | 168 | try: |
paul@329 | 169 | return parse_object(f, "utf-8") |
paul@329 | 170 | finally: |
paul@329 | 171 | f.close() |
paul@329 | 172 | finally: |
paul@329 | 173 | self.release_lock(user) |
paul@329 | 174 | |
paul@329 | 175 | def _set_object(self, user, filename, node): |
paul@329 | 176 | |
paul@329 | 177 | """ |
paul@329 | 178 | Set an object for the given 'user' having the given 'filename', using |
paul@329 | 179 | 'node' to define the object. |
paul@329 | 180 | """ |
paul@329 | 181 | |
paul@329 | 182 | self.acquire_lock(user) |
paul@329 | 183 | try: |
paul@329 | 184 | f = open(filename, "wb") |
paul@329 | 185 | try: |
paul@329 | 186 | to_stream(f, node) |
paul@329 | 187 | finally: |
paul@329 | 188 | f.close() |
paul@329 | 189 | fix_permissions(filename) |
paul@329 | 190 | finally: |
paul@329 | 191 | self.release_lock(user) |
paul@329 | 192 | |
paul@329 | 193 | return True |
paul@329 | 194 | |
paul@329 | 195 | def _remove_object(self, filename): |
paul@329 | 196 | |
paul@329 | 197 | "Remove the object with the given 'filename'." |
paul@329 | 198 | |
paul@329 | 199 | try: |
paul@329 | 200 | remove(filename) |
paul@329 | 201 | except OSError: |
paul@329 | 202 | return False |
paul@329 | 203 | |
paul@329 | 204 | return True |
paul@329 | 205 | |
paul@343 | 206 | def _remove_collection(self, filename): |
paul@343 | 207 | |
paul@343 | 208 | "Remove the collection with the given 'filename'." |
paul@343 | 209 | |
paul@343 | 210 | try: |
paul@343 | 211 | rmdir(filename) |
paul@343 | 212 | except OSError: |
paul@343 | 213 | return False |
paul@343 | 214 | |
paul@343 | 215 | return True |
paul@343 | 216 | |
paul@670 | 217 | # User discovery. |
paul@670 | 218 | |
paul@670 | 219 | def get_users(self): |
paul@670 | 220 | |
paul@670 | 221 | "Return a list of users." |
paul@670 | 222 | |
paul@670 | 223 | return listdir(self.store_dir) |
paul@670 | 224 | |
paul@648 | 225 | # Event and event metadata access. |
paul@648 | 226 | |
paul@119 | 227 | def get_events(self, user): |
paul@119 | 228 | |
paul@119 | 229 | "Return a list of event identifiers." |
paul@119 | 230 | |
paul@138 | 231 | filename = self.get_object_in_store(user, "objects") |
paul@808 | 232 | if not filename or not isdir(filename): |
paul@119 | 233 | return None |
paul@119 | 234 | |
paul@119 | 235 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@119 | 236 | |
paul@648 | 237 | def get_all_events(self, user): |
paul@648 | 238 | |
paul@648 | 239 | "Return a set of (uid, recurrenceid) tuples for all events." |
paul@648 | 240 | |
paul@648 | 241 | uids = self.get_events(user) |
paul@674 | 242 | if not uids: |
paul@674 | 243 | return set() |
paul@648 | 244 | |
paul@648 | 245 | all_events = set() |
paul@648 | 246 | for uid in uids: |
paul@648 | 247 | all_events.add((uid, None)) |
paul@648 | 248 | all_events.update([(uid, recurrenceid) for recurrenceid in self.get_recurrences(user, uid)]) |
paul@648 | 249 | |
paul@648 | 250 | return all_events |
paul@648 | 251 | |
paul@760 | 252 | def get_event_filename(self, user, uid, recurrenceid=None, dirname=None, username=None): |
paul@648 | 253 | |
paul@694 | 254 | """ |
paul@694 | 255 | Get the filename providing the event for the given 'user' with the given |
paul@694 | 256 | 'uid'. If the optional 'recurrenceid' is specified, a specific instance |
paul@694 | 257 | or occurrence of an event is returned. |
paul@648 | 258 | |
paul@694 | 259 | Where 'dirname' is specified, the given directory name is used as the |
paul@694 | 260 | base of the location within which any filename will reside. |
paul@694 | 261 | """ |
paul@648 | 262 | |
paul@694 | 263 | if recurrenceid: |
paul@760 | 264 | return self.get_recurrence_filename(user, uid, recurrenceid, dirname, username) |
paul@694 | 265 | else: |
paul@760 | 266 | return self.get_complete_event_filename(user, uid, dirname, username) |
paul@648 | 267 | |
paul@858 | 268 | def get_event(self, user, uid, recurrenceid=None, dirname=None): |
paul@343 | 269 | |
paul@343 | 270 | """ |
paul@343 | 271 | Get the event for the given 'user' with the given 'uid'. If |
paul@343 | 272 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 273 | occurrence of an event is returned. |
paul@343 | 274 | """ |
paul@343 | 275 | |
paul@858 | 276 | filename = self.get_event_filename(user, uid, recurrenceid, dirname) |
paul@808 | 277 | if not filename or not isfile(filename): |
paul@694 | 278 | return None |
paul@694 | 279 | |
paul@694 | 280 | return filename and self._get_object(user, filename) |
paul@694 | 281 | |
paul@760 | 282 | def get_complete_event_filename(self, user, uid, dirname=None, username=None): |
paul@694 | 283 | |
paul@694 | 284 | """ |
paul@694 | 285 | Get the filename providing the event for the given 'user' with the given |
paul@694 | 286 | 'uid'. |
paul@694 | 287 | |
paul@694 | 288 | Where 'dirname' is specified, the given directory name is used as the |
paul@694 | 289 | base of the location within which any filename will reside. |
paul@760 | 290 | |
paul@760 | 291 | Where 'username' is specified, the event details will reside in a file |
paul@760 | 292 | bearing that name within a directory having 'uid' as its name. |
paul@694 | 293 | """ |
paul@694 | 294 | |
paul@760 | 295 | return self.get_object_in_store(user, dirname, "objects", uid, username) |
paul@343 | 296 | |
paul@343 | 297 | def get_complete_event(self, user, uid): |
paul@50 | 298 | |
paul@50 | 299 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 300 | |
paul@694 | 301 | filename = self.get_complete_event_filename(user, uid) |
paul@808 | 302 | if not filename or not isfile(filename): |
paul@50 | 303 | return None |
paul@50 | 304 | |
paul@694 | 305 | return filename and self._get_object(user, filename) |
paul@50 | 306 | |
paul@343 | 307 | def set_event(self, user, uid, recurrenceid, node): |
paul@343 | 308 | |
paul@343 | 309 | """ |
paul@343 | 310 | Set an event for 'user' having the given 'uid' and 'recurrenceid' (which |
paul@343 | 311 | if the latter is specified, a specific instance or occurrence of an |
paul@343 | 312 | event is referenced), using the given 'node' description. |
paul@343 | 313 | """ |
paul@343 | 314 | |
paul@343 | 315 | if recurrenceid: |
paul@343 | 316 | return self.set_recurrence(user, uid, recurrenceid, node) |
paul@343 | 317 | else: |
paul@343 | 318 | return self.set_complete_event(user, uid, node) |
paul@343 | 319 | |
paul@343 | 320 | def set_complete_event(self, user, uid, node): |
paul@50 | 321 | |
paul@50 | 322 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 323 | |
paul@138 | 324 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 325 | if not filename: |
paul@50 | 326 | return False |
paul@50 | 327 | |
paul@329 | 328 | return self._set_object(user, filename, node) |
paul@15 | 329 | |
paul@365 | 330 | def remove_event(self, user, uid, recurrenceid=None): |
paul@234 | 331 | |
paul@343 | 332 | """ |
paul@343 | 333 | Remove an event for 'user' having the given 'uid'. If the optional |
paul@343 | 334 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@343 | 335 | event is removed. |
paul@343 | 336 | """ |
paul@343 | 337 | |
paul@343 | 338 | if recurrenceid: |
paul@343 | 339 | return self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 340 | else: |
paul@343 | 341 | for recurrenceid in self.get_recurrences(user, uid) or []: |
paul@343 | 342 | self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 343 | return self.remove_complete_event(user, uid) |
paul@343 | 344 | |
paul@343 | 345 | def remove_complete_event(self, user, uid): |
paul@343 | 346 | |
paul@234 | 347 | "Remove an event for 'user' having the given 'uid'." |
paul@234 | 348 | |
paul@378 | 349 | self.remove_recurrences(user, uid) |
paul@1068 | 350 | return self.remove_parent_event(user, uid) |
paul@1068 | 351 | |
paul@1068 | 352 | def remove_parent_event(self, user, uid): |
paul@1068 | 353 | |
paul@1068 | 354 | "Remove the parent event for 'user' having the given 'uid'." |
paul@369 | 355 | |
paul@234 | 356 | filename = self.get_object_in_store(user, "objects", uid) |
paul@234 | 357 | if not filename: |
paul@234 | 358 | return False |
paul@234 | 359 | |
paul@329 | 360 | return self._remove_object(filename) |
paul@234 | 361 | |
paul@334 | 362 | def get_recurrences(self, user, uid): |
paul@334 | 363 | |
paul@334 | 364 | """ |
paul@334 | 365 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 366 | indicated 'uid'. Both active and cancelled recurrences are returned. |
paul@694 | 367 | """ |
paul@694 | 368 | |
paul@694 | 369 | return self.get_active_recurrences(user, uid) + self.get_cancelled_recurrences(user, uid) |
paul@694 | 370 | |
paul@694 | 371 | def get_active_recurrences(self, user, uid): |
paul@694 | 372 | |
paul@694 | 373 | """ |
paul@694 | 374 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 375 | indicated 'uid'. Cancelled recurrences are not returned. |
paul@334 | 376 | """ |
paul@334 | 377 | |
paul@334 | 378 | filename = self.get_object_in_store(user, "recurrences", uid) |
paul@808 | 379 | if not filename or not isdir(filename): |
paul@347 | 380 | return [] |
paul@334 | 381 | |
paul@334 | 382 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@334 | 383 | |
paul@694 | 384 | def get_cancelled_recurrences(self, user, uid): |
paul@694 | 385 | |
paul@694 | 386 | """ |
paul@694 | 387 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 388 | indicated 'uid'. Only cancelled recurrences are returned. |
paul@694 | 389 | """ |
paul@694 | 390 | |
paul@782 | 391 | filename = self.get_object_in_store(user, "cancellations", "recurrences", uid) |
paul@808 | 392 | if not filename or not isdir(filename): |
paul@694 | 393 | return [] |
paul@694 | 394 | |
paul@694 | 395 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@694 | 396 | |
paul@760 | 397 | def get_recurrence_filename(self, user, uid, recurrenceid, dirname=None, username=None): |
paul@694 | 398 | |
paul@694 | 399 | """ |
paul@694 | 400 | For the event of the given 'user' with the given 'uid', return the |
paul@694 | 401 | filename providing the recurrence with the given 'recurrenceid'. |
paul@694 | 402 | |
paul@694 | 403 | Where 'dirname' is specified, the given directory name is used as the |
paul@694 | 404 | base of the location within which any filename will reside. |
paul@760 | 405 | |
paul@760 | 406 | Where 'username' is specified, the event details will reside in a file |
paul@760 | 407 | bearing that name within a directory having 'uid' as its name. |
paul@694 | 408 | """ |
paul@694 | 409 | |
paul@760 | 410 | return self.get_object_in_store(user, dirname, "recurrences", uid, recurrenceid, username) |
paul@694 | 411 | |
paul@334 | 412 | def get_recurrence(self, user, uid, recurrenceid): |
paul@334 | 413 | |
paul@334 | 414 | """ |
paul@334 | 415 | For the event of the given 'user' with the given 'uid', return the |
paul@334 | 416 | specific recurrence indicated by the 'recurrenceid'. |
paul@334 | 417 | """ |
paul@334 | 418 | |
paul@694 | 419 | filename = self.get_recurrence_filename(user, uid, recurrenceid) |
paul@808 | 420 | if not filename or not isfile(filename): |
paul@334 | 421 | return None |
paul@334 | 422 | |
paul@694 | 423 | return filename and self._get_object(user, filename) |
paul@334 | 424 | |
paul@334 | 425 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@334 | 426 | |
paul@334 | 427 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@334 | 428 | |
paul@334 | 429 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 430 | if not filename: |
paul@334 | 431 | return False |
paul@334 | 432 | |
paul@334 | 433 | return self._set_object(user, filename, node) |
paul@334 | 434 | |
paul@334 | 435 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@334 | 436 | |
paul@378 | 437 | """ |
paul@378 | 438 | Remove a special recurrence from an event stored by 'user' having the |
paul@378 | 439 | given 'uid' and 'recurrenceid'. |
paul@378 | 440 | """ |
paul@334 | 441 | |
paul@378 | 442 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 443 | if not filename: |
paul@334 | 444 | return False |
paul@334 | 445 | |
paul@334 | 446 | return self._remove_object(filename) |
paul@334 | 447 | |
paul@378 | 448 | def remove_recurrences(self, user, uid): |
paul@378 | 449 | |
paul@378 | 450 | """ |
paul@378 | 451 | Remove all recurrences for an event stored by 'user' having the given |
paul@378 | 452 | 'uid'. |
paul@378 | 453 | """ |
paul@378 | 454 | |
paul@378 | 455 | for recurrenceid in self.get_recurrences(user, uid): |
paul@378 | 456 | self.remove_recurrence(user, uid, recurrenceid) |
paul@378 | 457 | |
paul@1068 | 458 | return self.remove_recurrence_collection(user, uid) |
paul@1068 | 459 | |
paul@1068 | 460 | def remove_recurrence_collection(self, user, uid): |
paul@1068 | 461 | |
paul@1068 | 462 | """ |
paul@1068 | 463 | Remove the collection of recurrences stored by 'user' having the given |
paul@1068 | 464 | 'uid'. |
paul@1068 | 465 | """ |
paul@1068 | 466 | |
paul@378 | 467 | recurrences = self.get_object_in_store(user, "recurrences", uid) |
paul@378 | 468 | if recurrences: |
paul@378 | 469 | return self._remove_collection(recurrences) |
paul@378 | 470 | |
paul@378 | 471 | return True |
paul@378 | 472 | |
paul@652 | 473 | # Free/busy period providers, upon extension of the free/busy records. |
paul@652 | 474 | |
paul@672 | 475 | def _get_freebusy_providers(self, user): |
paul@672 | 476 | |
paul@672 | 477 | """ |
paul@672 | 478 | Return the free/busy providers for the given 'user'. |
paul@672 | 479 | |
paul@672 | 480 | This function returns any stored datetime and a list of providers as a |
paul@672 | 481 | 2-tuple. Each provider is itself a (uid, recurrenceid) tuple. |
paul@672 | 482 | """ |
paul@672 | 483 | |
paul@672 | 484 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@808 | 485 | if not filename or not isfile(filename): |
paul@672 | 486 | return None |
paul@672 | 487 | |
paul@672 | 488 | # Attempt to read providers, with a declaration of the datetime |
paul@672 | 489 | # from which such providers are considered as still being active. |
paul@672 | 490 | |
paul@702 | 491 | t = self._get_table_atomic(user, filename, [(1, None)]) |
paul@672 | 492 | try: |
paul@672 | 493 | dt_string = t[0][0] |
paul@672 | 494 | except IndexError: |
paul@672 | 495 | return None |
paul@672 | 496 | |
paul@672 | 497 | return dt_string, t[1:] |
paul@672 | 498 | |
paul@652 | 499 | def get_freebusy_providers(self, user, dt=None): |
paul@652 | 500 | |
paul@652 | 501 | """ |
paul@652 | 502 | Return a set of uncancelled events of the form (uid, recurrenceid) |
paul@652 | 503 | providing free/busy details beyond the given datetime 'dt'. |
paul@654 | 504 | |
paul@654 | 505 | If 'dt' is not specified, all events previously found to provide |
paul@654 | 506 | details will be returned. Otherwise, if 'dt' is earlier than the |
paul@654 | 507 | datetime recorded for the known providers, None is returned, indicating |
paul@654 | 508 | that the list of providers must be recomputed. |
paul@672 | 509 | |
paul@672 | 510 | This function returns a list of (uid, recurrenceid) tuples upon success. |
paul@652 | 511 | """ |
paul@652 | 512 | |
paul@672 | 513 | t = self._get_freebusy_providers(user) |
paul@672 | 514 | if not t: |
paul@672 | 515 | return None |
paul@672 | 516 | |
paul@672 | 517 | dt_string, t = t |
paul@672 | 518 | |
paul@672 | 519 | # If the requested datetime is earlier than the stated datetime, the |
paul@672 | 520 | # providers will need to be recomputed. |
paul@672 | 521 | |
paul@672 | 522 | if dt: |
paul@672 | 523 | providers_dt = get_datetime(dt_string) |
paul@672 | 524 | if not providers_dt or providers_dt > dt: |
paul@672 | 525 | return None |
paul@672 | 526 | |
paul@672 | 527 | # Otherwise, return the providers. |
paul@672 | 528 | |
paul@672 | 529 | return t[1:] |
paul@672 | 530 | |
paul@672 | 531 | def _set_freebusy_providers(self, user, dt_string, t): |
paul@672 | 532 | |
paul@672 | 533 | "Set the given provider timestamp 'dt_string' and table 't'." |
paul@672 | 534 | |
paul@652 | 535 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@672 | 536 | if not filename: |
paul@672 | 537 | return False |
paul@652 | 538 | |
paul@672 | 539 | t.insert(0, (dt_string,)) |
paul@702 | 540 | self._set_table_atomic(user, filename, t, [(1, "")]) |
paul@672 | 541 | return True |
paul@652 | 542 | |
paul@654 | 543 | def set_freebusy_providers(self, user, dt, providers): |
paul@654 | 544 | |
paul@654 | 545 | """ |
paul@654 | 546 | Define the uncancelled events providing free/busy details beyond the |
paul@654 | 547 | given datetime 'dt'. |
paul@654 | 548 | """ |
paul@654 | 549 | |
paul@672 | 550 | t = [] |
paul@654 | 551 | |
paul@654 | 552 | for obj in providers: |
paul@672 | 553 | t.append((obj.get_uid(), obj.get_recurrenceid())) |
paul@672 | 554 | |
paul@672 | 555 | return self._set_freebusy_providers(user, format_datetime(dt), t) |
paul@654 | 556 | |
paul@672 | 557 | def append_freebusy_provider(self, user, provider): |
paul@672 | 558 | |
paul@672 | 559 | "For the given 'user', append the free/busy 'provider'." |
paul@672 | 560 | |
paul@672 | 561 | t = self._get_freebusy_providers(user) |
paul@672 | 562 | if not t: |
paul@654 | 563 | return False |
paul@654 | 564 | |
paul@672 | 565 | dt_string, t = t |
paul@672 | 566 | t.append((provider.get_uid(), provider.get_recurrenceid())) |
paul@672 | 567 | |
paul@672 | 568 | return self._set_freebusy_providers(user, dt_string, t) |
paul@672 | 569 | |
paul@672 | 570 | def remove_freebusy_provider(self, user, provider): |
paul@672 | 571 | |
paul@672 | 572 | "For the given 'user', remove the free/busy 'provider'." |
paul@672 | 573 | |
paul@672 | 574 | t = self._get_freebusy_providers(user) |
paul@672 | 575 | if not t: |
paul@672 | 576 | return False |
paul@672 | 577 | |
paul@672 | 578 | dt_string, t = t |
paul@672 | 579 | try: |
paul@672 | 580 | t.remove((provider.get_uid(), provider.get_recurrenceid())) |
paul@672 | 581 | except ValueError: |
paul@672 | 582 | return False |
paul@672 | 583 | |
paul@672 | 584 | return self._set_freebusy_providers(user, dt_string, t) |
paul@654 | 585 | |
paul@648 | 586 | # Free/busy period access. |
paul@648 | 587 | |
paul@1064 | 588 | def get_freebusy(self, user, name=None): |
paul@15 | 589 | |
paul@15 | 590 | "Get free/busy details for the given 'user'." |
paul@15 | 591 | |
paul@702 | 592 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@808 | 593 | if not filename or not isfile(filename): |
paul@167 | 594 | return [] |
paul@112 | 595 | else: |
paul@702 | 596 | return map(lambda t: FreeBusyPeriod(*t), |
paul@1066 | 597 | self._get_table_atomic(user, filename)) |
paul@702 | 598 | |
paul@1064 | 599 | def get_freebusy_for_other(self, user, other): |
paul@112 | 600 | |
paul@112 | 601 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 602 | |
paul@112 | 603 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@808 | 604 | if not filename or not isfile(filename): |
paul@167 | 605 | return [] |
paul@112 | 606 | else: |
paul@702 | 607 | return map(lambda t: FreeBusyPeriod(*t), |
paul@1066 | 608 | self._get_table_atomic(user, filename)) |
paul@702 | 609 | |
paul@1064 | 610 | def set_freebusy(self, user, freebusy, name=None): |
paul@15 | 611 | |
paul@15 | 612 | "For the given 'user', set 'freebusy' details." |
paul@15 | 613 | |
paul@702 | 614 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@15 | 615 | if not filename: |
paul@15 | 616 | return False |
paul@15 | 617 | |
paul@1064 | 618 | self._set_table_atomic(user, filename, |
paul@702 | 619 | map(lambda fb: fb.as_tuple(strings_only=True), freebusy)) |
paul@15 | 620 | return True |
paul@15 | 621 | |
paul@1064 | 622 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@110 | 623 | |
paul@110 | 624 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 625 | |
paul@110 | 626 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 627 | if not filename: |
paul@110 | 628 | return False |
paul@110 | 629 | |
paul@1064 | 630 | self._set_table_atomic(user, filename, |
paul@702 | 631 | map(lambda fb: fb.as_tuple(strings_only=True), freebusy)) |
paul@112 | 632 | return True |
paul@112 | 633 | |
paul@710 | 634 | # Tentative free/busy periods related to countering. |
paul@710 | 635 | |
paul@710 | 636 | def get_freebusy_offers(self, user): |
paul@710 | 637 | |
paul@710 | 638 | "Get free/busy offers for the given 'user'." |
paul@710 | 639 | |
paul@710 | 640 | offers = [] |
paul@710 | 641 | expired = [] |
paul@741 | 642 | now = to_timezone(datetime.utcnow(), "UTC") |
paul@710 | 643 | |
paul@710 | 644 | # Expire old offers and save the collection if modified. |
paul@710 | 645 | |
paul@730 | 646 | self.acquire_lock(user) |
paul@710 | 647 | try: |
paul@730 | 648 | l = self.get_freebusy(user, "freebusy-offers") |
paul@710 | 649 | for fb in l: |
paul@710 | 650 | if fb.expires and get_datetime(fb.expires) <= now: |
paul@710 | 651 | expired.append(fb) |
paul@710 | 652 | else: |
paul@710 | 653 | offers.append(fb) |
paul@710 | 654 | |
paul@710 | 655 | if expired: |
paul@730 | 656 | self.set_freebusy_offers(user, offers) |
paul@710 | 657 | finally: |
paul@730 | 658 | self.release_lock(user) |
paul@710 | 659 | |
paul@710 | 660 | return offers |
paul@710 | 661 | |
paul@710 | 662 | def set_freebusy_offers(self, user, freebusy): |
paul@710 | 663 | |
paul@710 | 664 | "For the given 'user', set 'freebusy' offers." |
paul@710 | 665 | |
paul@710 | 666 | return self.set_freebusy(user, freebusy, "freebusy-offers") |
paul@710 | 667 | |
paul@747 | 668 | # Requests and counter-proposals. |
paul@648 | 669 | |
paul@142 | 670 | def _get_requests(self, user, queue): |
paul@66 | 671 | |
paul@142 | 672 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 673 | |
paul@142 | 674 | filename = self.get_object_in_store(user, queue) |
paul@808 | 675 | if not filename or not isfile(filename): |
paul@66 | 676 | return None |
paul@66 | 677 | |
paul@747 | 678 | return self._get_table_atomic(user, filename, [(1, None), (2, None)]) |
paul@66 | 679 | |
paul@142 | 680 | def get_requests(self, user): |
paul@142 | 681 | |
paul@142 | 682 | "Get requests for the given 'user'." |
paul@142 | 683 | |
paul@142 | 684 | return self._get_requests(user, "requests") |
paul@142 | 685 | |
paul@142 | 686 | def _set_requests(self, user, requests, queue): |
paul@66 | 687 | |
paul@142 | 688 | """ |
paul@142 | 689 | For the given 'user', set the list of queued 'requests' in the given |
paul@142 | 690 | 'queue'. |
paul@142 | 691 | """ |
paul@142 | 692 | |
paul@142 | 693 | filename = self.get_object_in_store(user, queue) |
paul@66 | 694 | if not filename: |
paul@66 | 695 | return False |
paul@66 | 696 | |
paul@747 | 697 | self._set_table_atomic(user, filename, requests, [(1, ""), (2, "")]) |
paul@66 | 698 | return True |
paul@66 | 699 | |
paul@142 | 700 | def set_requests(self, user, requests): |
paul@142 | 701 | |
paul@142 | 702 | "For the given 'user', set the list of queued 'requests'." |
paul@142 | 703 | |
paul@142 | 704 | return self._set_requests(user, requests, "requests") |
paul@142 | 705 | |
paul@747 | 706 | def _set_request(self, user, request, queue): |
paul@142 | 707 | |
paul@343 | 708 | """ |
paul@747 | 709 | For the given 'user', set the given 'request' in the given 'queue'. |
paul@343 | 710 | """ |
paul@142 | 711 | |
paul@142 | 712 | filename = self.get_object_in_store(user, queue) |
paul@55 | 713 | if not filename: |
paul@55 | 714 | return False |
paul@55 | 715 | |
paul@303 | 716 | self.acquire_lock(user) |
paul@55 | 717 | try: |
paul@747 | 718 | f = codecs.open(filename, "ab", encoding="utf-8") |
paul@303 | 719 | try: |
paul@747 | 720 | self._set_table_item(f, request, [(1, ""), (2, "")]) |
paul@303 | 721 | finally: |
paul@303 | 722 | f.close() |
paul@303 | 723 | fix_permissions(filename) |
paul@55 | 724 | finally: |
paul@303 | 725 | self.release_lock(user) |
paul@55 | 726 | |
paul@55 | 727 | return True |
paul@55 | 728 | |
paul@747 | 729 | def set_request(self, user, uid, recurrenceid=None, type=None): |
paul@142 | 730 | |
paul@747 | 731 | """ |
paul@747 | 732 | For the given 'user', set the queued 'uid' and 'recurrenceid', |
paul@747 | 733 | indicating a request, along with any given 'type'. |
paul@747 | 734 | """ |
paul@142 | 735 | |
paul@747 | 736 | return self._set_request(user, (uid, recurrenceid, type), "requests") |
paul@747 | 737 | |
paul@747 | 738 | def queue_request(self, user, uid, recurrenceid=None, type=None): |
paul@142 | 739 | |
paul@343 | 740 | """ |
paul@343 | 741 | Queue a request for 'user' having the given 'uid'. If the optional |
paul@747 | 742 | 'recurrenceid' is specified, the entry refers to a specific instance |
paul@747 | 743 | or occurrence of an event. The 'type' parameter can be used to indicate |
paul@747 | 744 | a specific type of request. |
paul@747 | 745 | """ |
paul@747 | 746 | |
paul@747 | 747 | requests = self.get_requests(user) or [] |
paul@747 | 748 | |
paul@747 | 749 | if not self.have_request(requests, uid, recurrenceid): |
paul@747 | 750 | return self.set_request(user, uid, recurrenceid, type) |
paul@747 | 751 | |
paul@747 | 752 | return False |
paul@747 | 753 | |
paul@840 | 754 | def dequeue_request(self, user, uid, recurrenceid=None): |
paul@747 | 755 | |
paul@747 | 756 | """ |
paul@747 | 757 | Dequeue all requests for 'user' having the given 'uid'. If the optional |
paul@747 | 758 | 'recurrenceid' is specified, all requests for that specific instance or |
paul@747 | 759 | occurrence of an event are dequeued. |
paul@343 | 760 | """ |
paul@66 | 761 | |
paul@81 | 762 | requests = self.get_requests(user) or [] |
paul@750 | 763 | result = [] |
paul@747 | 764 | |
paul@750 | 765 | for request in requests: |
paul@808 | 766 | if request[:2] != (uid, recurrenceid): |
paul@750 | 767 | result.append(request) |
paul@747 | 768 | |
paul@750 | 769 | self.set_requests(user, result) |
paul@747 | 770 | return True |
paul@747 | 771 | |
paul@817 | 772 | def has_request(self, user, uid, recurrenceid=None, type=None, strict=False): |
paul@817 | 773 | return self.have_request(self.get_requests(user) or [], uid, recurrenceid, type, strict) |
paul@817 | 774 | |
paul@754 | 775 | def have_request(self, requests, uid, recurrenceid=None, type=None, strict=False): |
paul@754 | 776 | |
paul@754 | 777 | """ |
paul@754 | 778 | Return whether 'requests' contains a request with the given 'uid' and |
paul@754 | 779 | any specified 'recurrenceid' and 'type'. If 'strict' is set to a true |
paul@754 | 780 | value, the precise type of the request must match; otherwise, any type |
paul@754 | 781 | of request for the identified object may be matched. |
paul@754 | 782 | """ |
paul@754 | 783 | |
paul@750 | 784 | for request in requests: |
paul@762 | 785 | if request[:2] == (uid, recurrenceid) and ( |
paul@762 | 786 | not strict or |
paul@762 | 787 | not request[2:] and not type or |
paul@762 | 788 | request[2:] and request[2] == type): |
paul@762 | 789 | |
paul@750 | 790 | return True |
paul@762 | 791 | |
paul@750 | 792 | return False |
paul@747 | 793 | |
paul@760 | 794 | def get_counters(self, user, uid, recurrenceid=None): |
paul@754 | 795 | |
paul@754 | 796 | """ |
paul@766 | 797 | For the given 'user', return a list of users from whom counter-proposals |
paul@766 | 798 | have been received for the given 'uid' and optional 'recurrenceid'. |
paul@754 | 799 | """ |
paul@754 | 800 | |
paul@754 | 801 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@808 | 802 | if not filename or not isdir(filename): |
paul@754 | 803 | return False |
paul@754 | 804 | |
paul@766 | 805 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@760 | 806 | |
paul@760 | 807 | def get_counter(self, user, other, uid, recurrenceid=None): |
paul@105 | 808 | |
paul@343 | 809 | """ |
paul@760 | 810 | For the given 'user', return the counter-proposal from 'other' for the |
paul@760 | 811 | given 'uid' and optional 'recurrenceid'. |
paul@760 | 812 | """ |
paul@760 | 813 | |
paul@760 | 814 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@760 | 815 | if not filename: |
paul@760 | 816 | return False |
paul@760 | 817 | |
paul@760 | 818 | return self._get_object(user, filename) |
paul@760 | 819 | |
paul@760 | 820 | def set_counter(self, user, other, node, uid, recurrenceid=None): |
paul@760 | 821 | |
paul@760 | 822 | """ |
paul@760 | 823 | For the given 'user', store a counter-proposal received from 'other' the |
paul@760 | 824 | given 'node' representing that proposal for the given 'uid' and |
paul@760 | 825 | 'recurrenceid'. |
paul@760 | 826 | """ |
paul@760 | 827 | |
paul@760 | 828 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@760 | 829 | if not filename: |
paul@760 | 830 | return False |
paul@760 | 831 | |
paul@760 | 832 | return self._set_object(user, filename, node) |
paul@760 | 833 | |
paul@760 | 834 | def remove_counters(self, user, uid, recurrenceid=None): |
paul@760 | 835 | |
paul@760 | 836 | """ |
paul@760 | 837 | For the given 'user', remove all counter-proposals associated with the |
paul@760 | 838 | given 'uid' and 'recurrenceid'. |
paul@343 | 839 | """ |
paul@105 | 840 | |
paul@747 | 841 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@808 | 842 | if not filename or not isdir(filename): |
paul@747 | 843 | return False |
paul@747 | 844 | |
paul@760 | 845 | removed = False |
paul@747 | 846 | |
paul@760 | 847 | for other in listdir(filename): |
paul@760 | 848 | counter_filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@760 | 849 | removed = removed or self._remove_object(counter_filename) |
paul@760 | 850 | |
paul@760 | 851 | return removed |
paul@760 | 852 | |
paul@760 | 853 | def remove_counter(self, user, other, uid, recurrenceid=None): |
paul@105 | 854 | |
paul@747 | 855 | """ |
paul@760 | 856 | For the given 'user', remove any counter-proposal from 'other' |
paul@760 | 857 | associated with the given 'uid' and 'recurrenceid'. |
paul@747 | 858 | """ |
paul@747 | 859 | |
paul@760 | 860 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@808 | 861 | if not filename or not isfile(filename): |
paul@105 | 862 | return False |
paul@747 | 863 | |
paul@747 | 864 | return self._remove_object(filename) |
paul@747 | 865 | |
paul@747 | 866 | # Event cancellation. |
paul@105 | 867 | |
paul@343 | 868 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@142 | 869 | |
paul@343 | 870 | """ |
paul@694 | 871 | Cancel an event for 'user' having the given 'uid'. If the optional |
paul@694 | 872 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@694 | 873 | event is cancelled. |
paul@343 | 874 | """ |
paul@142 | 875 | |
paul@694 | 876 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@694 | 877 | cancelled_filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@142 | 878 | |
paul@808 | 879 | if filename and cancelled_filename and isfile(filename): |
paul@694 | 880 | return self.move_object(filename, cancelled_filename) |
paul@142 | 881 | |
paul@142 | 882 | return False |
paul@142 | 883 | |
paul@863 | 884 | def uncancel_event(self, user, uid, recurrenceid=None): |
paul@863 | 885 | |
paul@863 | 886 | """ |
paul@863 | 887 | Uncancel an event for 'user' having the given 'uid'. If the optional |
paul@863 | 888 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@863 | 889 | event is uncancelled. |
paul@863 | 890 | """ |
paul@863 | 891 | |
paul@863 | 892 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@863 | 893 | cancelled_filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@863 | 894 | |
paul@863 | 895 | if filename and cancelled_filename and isfile(cancelled_filename): |
paul@863 | 896 | return self.move_object(cancelled_filename, filename) |
paul@863 | 897 | |
paul@863 | 898 | return False |
paul@863 | 899 | |
paul@796 | 900 | def remove_cancellations(self, user, uid, recurrenceid=None): |
paul@796 | 901 | |
paul@796 | 902 | """ |
paul@796 | 903 | Remove cancellations for 'user' for any event having the given 'uid'. If |
paul@796 | 904 | the optional 'recurrenceid' is specified, a specific instance or |
paul@796 | 905 | occurrence of an event is affected. |
paul@796 | 906 | """ |
paul@796 | 907 | |
paul@796 | 908 | # Remove all recurrence cancellations if a general event is indicated. |
paul@796 | 909 | |
paul@796 | 910 | if not recurrenceid: |
paul@796 | 911 | for _recurrenceid in self.get_cancelled_recurrences(user, uid): |
paul@796 | 912 | self.remove_cancellation(user, uid, _recurrenceid) |
paul@796 | 913 | |
paul@796 | 914 | return self.remove_cancellation(user, uid, recurrenceid) |
paul@796 | 915 | |
paul@796 | 916 | def remove_cancellation(self, user, uid, recurrenceid=None): |
paul@796 | 917 | |
paul@796 | 918 | """ |
paul@796 | 919 | Remove a cancellation for 'user' for the event having the given 'uid'. |
paul@796 | 920 | If the optional 'recurrenceid' is specified, a specific instance or |
paul@796 | 921 | occurrence of an event is affected. |
paul@796 | 922 | """ |
paul@796 | 923 | |
paul@796 | 924 | # Remove any parent event cancellation or a specific recurrence |
paul@796 | 925 | # cancellation if indicated. |
paul@796 | 926 | |
paul@796 | 927 | filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@796 | 928 | |
paul@808 | 929 | if filename and isfile(filename): |
paul@796 | 930 | return self._remove_object(filename) |
paul@796 | 931 | |
paul@796 | 932 | return False |
paul@796 | 933 | |
paul@30 | 934 | class FilePublisher(FileBase): |
paul@30 | 935 | |
paul@30 | 936 | "A publisher of objects." |
paul@30 | 937 | |
paul@597 | 938 | def __init__(self, store_dir=None): |
paul@597 | 939 | FileBase.__init__(self, store_dir or PUBLISH_DIR) |
paul@30 | 940 | |
paul@30 | 941 | def set_freebusy(self, user, freebusy): |
paul@30 | 942 | |
paul@30 | 943 | "For the given 'user', set 'freebusy' details." |
paul@30 | 944 | |
paul@52 | 945 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 946 | if not filename: |
paul@30 | 947 | return False |
paul@30 | 948 | |
paul@30 | 949 | record = [] |
paul@30 | 950 | rwrite = record.append |
paul@30 | 951 | |
paul@30 | 952 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 953 | rwrite(("UID", {}, user)) |
paul@30 | 954 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 955 | |
paul@458 | 956 | for fb in freebusy: |
paul@458 | 957 | if not fb.transp or fb.transp == "OPAQUE": |
paul@529 | 958 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@563 | 959 | map(format_datetime, [fb.get_start_point(), fb.get_end_point()])))) |
paul@30 | 960 | |
paul@395 | 961 | f = open(filename, "wb") |
paul@30 | 962 | try: |
paul@30 | 963 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 964 | finally: |
paul@30 | 965 | f.close() |
paul@103 | 966 | fix_permissions(filename) |
paul@30 | 967 | |
paul@30 | 968 | return True |
paul@30 | 969 | |
paul@1039 | 970 | class FileJournal(FileStoreBase): |
paul@1039 | 971 | |
paul@1039 | 972 | "A journal system to support quotas." |
paul@1039 | 973 | |
paul@1039 | 974 | def __init__(self, store_dir=None): |
paul@1039 | 975 | FileBase.__init__(self, store_dir or JOURNAL_DIR) |
paul@1039 | 976 | |
paul@1049 | 977 | # Quota and user identity/group discovery. |
paul@1049 | 978 | |
paul@1049 | 979 | def get_quotas(self): |
paul@1049 | 980 | |
paul@1049 | 981 | "Return a list of quotas." |
paul@1049 | 982 | |
paul@1049 | 983 | return listdir(self.store_dir) |
paul@1049 | 984 | |
paul@1049 | 985 | def get_quota_users(self, quota): |
paul@1049 | 986 | |
paul@1049 | 987 | "Return a list of quota users." |
paul@1049 | 988 | |
paul@1049 | 989 | filename = self.get_object_in_store(quota, "journal") |
paul@1049 | 990 | if not filename or not isdir(filename): |
paul@1049 | 991 | return [] |
paul@1049 | 992 | |
paul@1049 | 993 | return listdir(filename) |
paul@1049 | 994 | |
paul@1039 | 995 | # Groups of users sharing quotas. |
paul@1039 | 996 | |
paul@1039 | 997 | def get_groups(self, quota): |
paul@1039 | 998 | |
paul@1039 | 999 | "Return the identity mappings for the given 'quota' as a dictionary." |
paul@1039 | 1000 | |
paul@1039 | 1001 | filename = self.get_object_in_store(quota, "groups") |
paul@1039 | 1002 | if not filename or not isfile(filename): |
paul@1039 | 1003 | return {} |
paul@1039 | 1004 | |
paul@1046 | 1005 | return dict(self._get_table_atomic(quota, filename, tab_separated=False)) |
paul@1039 | 1006 | |
paul@1039 | 1007 | def get_limits(self, quota): |
paul@1039 | 1008 | |
paul@1039 | 1009 | """ |
paul@1039 | 1010 | Return the limits for the 'quota' as a dictionary mapping identities or |
paul@1039 | 1011 | groups to durations. |
paul@1039 | 1012 | """ |
paul@1039 | 1013 | |
paul@1039 | 1014 | filename = self.get_object_in_store(quota, "limits") |
paul@1039 | 1015 | if not filename or not isfile(filename): |
paul@1039 | 1016 | return None |
paul@1039 | 1017 | |
paul@1046 | 1018 | return dict(self._get_table_atomic(quota, filename, tab_separated=False)) |
paul@1039 | 1019 | |
paul@1048 | 1020 | # Free/busy period access for users within quota groups. |
paul@1039 | 1021 | |
paul@1064 | 1022 | def get_freebusy(self, quota, user): |
paul@1039 | 1023 | |
paul@1039 | 1024 | "Get free/busy details for the given 'quota' and 'user'." |
paul@1039 | 1025 | |
paul@1039 | 1026 | filename = self.get_object_in_store(quota, "freebusy", user) |
paul@1039 | 1027 | if not filename or not isfile(filename): |
paul@1039 | 1028 | return [] |
paul@1059 | 1029 | |
paul@1059 | 1030 | return map(lambda t: FreeBusyPeriod(*t), |
paul@1066 | 1031 | self._get_table_atomic(quota, filename)) |
paul@1039 | 1032 | |
paul@1064 | 1033 | def set_freebusy(self, quota, user, freebusy): |
paul@1039 | 1034 | |
paul@1039 | 1035 | "For the given 'quota' and 'user', set 'freebusy' details." |
paul@1039 | 1036 | |
paul@1039 | 1037 | filename = self.get_object_in_store(quota, "freebusy", user) |
paul@1039 | 1038 | if not filename: |
paul@1039 | 1039 | return False |
paul@1039 | 1040 | |
paul@1064 | 1041 | self._set_table_atomic(quota, filename, |
paul@1039 | 1042 | map(lambda fb: fb.as_tuple(strings_only=True), freebusy)) |
paul@1039 | 1043 | return True |
paul@1039 | 1044 | |
paul@1039 | 1045 | # Journal entry methods. |
paul@1039 | 1046 | |
paul@1039 | 1047 | def get_entries(self, quota, group): |
paul@1039 | 1048 | |
paul@1039 | 1049 | """ |
paul@1039 | 1050 | Return a list of journal entries for the given 'quota' for the indicated |
paul@1039 | 1051 | 'group'. |
paul@1039 | 1052 | """ |
paul@1039 | 1053 | |
paul@1039 | 1054 | filename = self.get_object_in_store(quota, "journal", group) |
paul@1039 | 1055 | if not filename or not isfile(filename): |
paul@1039 | 1056 | return [] |
paul@1039 | 1057 | |
paul@1059 | 1058 | return map(lambda t: FreeBusyPeriod(*t), |
paul@1066 | 1059 | self._get_table_atomic(quota, filename)) |
paul@1039 | 1060 | |
paul@1039 | 1061 | def set_entries(self, quota, group, entries): |
paul@1039 | 1062 | |
paul@1039 | 1063 | """ |
paul@1039 | 1064 | For the given 'quota' and indicated 'group', set the list of journal |
paul@1039 | 1065 | 'entries'. |
paul@1039 | 1066 | """ |
paul@1039 | 1067 | |
paul@1039 | 1068 | filename = self.get_object_in_store(quota, "journal", group) |
paul@1039 | 1069 | if not filename: |
paul@1039 | 1070 | return False |
paul@1039 | 1071 | |
paul@1059 | 1072 | self._set_table_atomic(quota, filename, |
paul@1059 | 1073 | map(lambda fb: fb.as_tuple(strings_only=True), entries)) |
paul@1039 | 1074 | return True |
paul@1039 | 1075 | |
paul@2 | 1076 | # vim: tabstop=4 expandtab shiftwidth=4 |