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@1210 | 6 | Copyright (C) 2014, 2015, 2016, 2017 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@1088 | 22 | from imiptools.stores.common import StoreBase, PublisherBase, JournalBase |
paul@1069 | 23 | |
paul@30 | 24 | from datetime import datetime |
paul@1210 | 25 | from imiptools.config import settings |
paul@301 | 26 | from imiptools.data import make_calendar, parse_object, to_stream |
paul@740 | 27 | from imiptools.dates import format_datetime, get_datetime, to_timezone |
paul@147 | 28 | from imiptools.filesys import fix_permissions, FileBase |
paul@1171 | 29 | from imiptools.period import FreeBusyPeriod, FreeBusyGroupPeriod, \ |
paul@1171 | 30 | FreeBusyOfferPeriod, FreeBusyCollection, \ |
paul@1171 | 31 | FreeBusyGroupCollection, FreeBusyOffersCollection |
paul@1174 | 32 | from imiptools.text import get_table, set_defaults |
paul@808 | 33 | from os.path import isdir, isfile, join |
paul@343 | 34 | from os import listdir, remove, rmdir |
paul@395 | 35 | import codecs |
paul@15 | 36 | |
paul@1210 | 37 | STORE_DIR = settings["STORE_DIR"] |
paul@1210 | 38 | PUBLISH_DIR = settings["PUBLISH_DIR"] |
paul@1210 | 39 | JOURNAL_DIR = settings["JOURNAL_DIR"] |
paul@1210 | 40 | |
paul@1039 | 41 | class FileStoreBase(FileBase): |
paul@50 | 42 | |
paul@1039 | 43 | "A file store supporting user-specific locking and tabular data." |
paul@147 | 44 | |
paul@303 | 45 | def acquire_lock(self, user, timeout=None): |
paul@303 | 46 | FileBase.acquire_lock(self, timeout, user) |
paul@303 | 47 | |
paul@303 | 48 | def release_lock(self, user): |
paul@303 | 49 | FileBase.release_lock(self, user) |
paul@303 | 50 | |
paul@648 | 51 | # Utility methods. |
paul@648 | 52 | |
paul@343 | 53 | def _set_defaults(self, t, empty_defaults): |
paul@1174 | 54 | return set_defaults(t, empty_defaults) |
paul@343 | 55 | |
paul@1174 | 56 | def _get_table(self, filename, empty_defaults=None, tab_separated=True): |
paul@343 | 57 | |
paul@343 | 58 | """ |
paul@1174 | 59 | From the file having the given 'filename', return a list of tuples |
paul@1174 | 60 | 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@1046 | 65 | |
paul@1046 | 66 | If 'tab_separated' is specified and is a false value, line parsing using |
paul@1046 | 67 | the imiptools.text.parse_line function will be performed instead of |
paul@1046 | 68 | splitting each line of the file using tab characters as separators. |
paul@343 | 69 | """ |
paul@343 | 70 | |
paul@1174 | 71 | return get_table(filename, empty_defaults, tab_separated) |
paul@702 | 72 | |
paul@1046 | 73 | def _get_table_atomic(self, user, filename, empty_defaults=None, tab_separated=True): |
paul@702 | 74 | |
paul@702 | 75 | """ |
paul@702 | 76 | From the file for the given 'user' having the given 'filename', return |
paul@702 | 77 | a list of tuples representing the file's contents. |
paul@702 | 78 | |
paul@702 | 79 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@702 | 80 | default value where a column either does not exist or provides an empty |
paul@702 | 81 | value. |
paul@1046 | 82 | |
paul@1046 | 83 | If 'tab_separated' is specified and is a false value, line parsing using |
paul@1046 | 84 | the imiptools.text.parse_line function will be performed instead of |
paul@1046 | 85 | splitting each line of the file using tab characters as separators. |
paul@702 | 86 | """ |
paul@702 | 87 | |
paul@343 | 88 | self.acquire_lock(user) |
paul@343 | 89 | try: |
paul@1174 | 90 | return self._get_table(filename, empty_defaults, tab_separated) |
paul@343 | 91 | finally: |
paul@343 | 92 | self.release_lock(user) |
paul@343 | 93 | |
paul@1174 | 94 | def _set_table(self, filename, items, empty_defaults=None): |
paul@343 | 95 | |
paul@343 | 96 | """ |
paul@1174 | 97 | Write to the file having the given 'filename' the 'items'. |
paul@343 | 98 | |
paul@343 | 99 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 100 | default value where a column either does not exist or provides an empty |
paul@343 | 101 | value. |
paul@343 | 102 | """ |
paul@343 | 103 | |
paul@702 | 104 | f = codecs.open(filename, "wb", encoding="utf-8") |
paul@702 | 105 | try: |
paul@702 | 106 | for item in items: |
paul@747 | 107 | self._set_table_item(f, item, empty_defaults) |
paul@702 | 108 | finally: |
paul@702 | 109 | f.close() |
paul@702 | 110 | fix_permissions(filename) |
paul@702 | 111 | |
paul@747 | 112 | def _set_table_item(self, f, item, empty_defaults=None): |
paul@747 | 113 | |
paul@747 | 114 | "Set in table 'f' the given 'item', using any 'empty_defaults'." |
paul@747 | 115 | |
paul@747 | 116 | if empty_defaults: |
paul@747 | 117 | item = self._set_defaults(list(item), empty_defaults) |
paul@747 | 118 | f.write("\t".join(item) + "\n") |
paul@747 | 119 | |
paul@702 | 120 | def _set_table_atomic(self, user, filename, items, empty_defaults=None): |
paul@702 | 121 | |
paul@702 | 122 | """ |
paul@702 | 123 | For the given 'user', write to the file having the given 'filename' the |
paul@702 | 124 | 'items'. |
paul@702 | 125 | |
paul@702 | 126 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@702 | 127 | default value where a column either does not exist or provides an empty |
paul@702 | 128 | value. |
paul@702 | 129 | """ |
paul@702 | 130 | |
paul@343 | 131 | self.acquire_lock(user) |
paul@343 | 132 | try: |
paul@1174 | 133 | self._set_table(filename, items, empty_defaults) |
paul@343 | 134 | finally: |
paul@343 | 135 | self.release_lock(user) |
paul@343 | 136 | |
paul@1171 | 137 | def _set_freebusy(self, user, freebusy, filename): |
paul@1171 | 138 | |
paul@1171 | 139 | """ |
paul@1171 | 140 | For the given 'user', convert the 'freebusy' details to a form suitable |
paul@1171 | 141 | for writing to 'filename'. |
paul@1171 | 142 | """ |
paul@1171 | 143 | |
paul@1171 | 144 | # Obtain tuples from the free/busy objects. |
paul@1171 | 145 | |
paul@1171 | 146 | self._set_table_atomic(user, filename, |
paul@1171 | 147 | map(lambda fb: freebusy.make_tuple(fb.as_tuple(strings_only=True)), list(freebusy))) |
paul@1171 | 148 | |
paul@1088 | 149 | class Store(FileStoreBase, StoreBase): |
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@1142 | 233 | return [] |
paul@119 | 234 | |
paul@119 | 235 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@119 | 236 | |
paul@1142 | 237 | def get_cancelled_events(self, user): |
paul@648 | 238 | |
paul@1142 | 239 | "Return a list of event identifiers for cancelled events." |
paul@648 | 240 | |
paul@1142 | 241 | filename = self.get_object_in_store(user, "cancellations", "objects") |
paul@1142 | 242 | if not filename or not isdir(filename): |
paul@1142 | 243 | return [] |
paul@648 | 244 | |
paul@1142 | 245 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@648 | 246 | |
paul@858 | 247 | def get_event(self, user, uid, recurrenceid=None, dirname=None): |
paul@343 | 248 | |
paul@343 | 249 | """ |
paul@343 | 250 | Get the event for the given 'user' with the given 'uid'. If |
paul@343 | 251 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 252 | occurrence of an event is returned. |
paul@343 | 253 | """ |
paul@343 | 254 | |
paul@858 | 255 | filename = self.get_event_filename(user, uid, recurrenceid, dirname) |
paul@808 | 256 | if not filename or not isfile(filename): |
paul@694 | 257 | return None |
paul@694 | 258 | |
paul@694 | 259 | return filename and self._get_object(user, filename) |
paul@694 | 260 | |
paul@343 | 261 | def get_complete_event(self, user, uid): |
paul@50 | 262 | |
paul@50 | 263 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 264 | |
paul@694 | 265 | filename = self.get_complete_event_filename(user, uid) |
paul@808 | 266 | if not filename or not isfile(filename): |
paul@50 | 267 | return None |
paul@50 | 268 | |
paul@694 | 269 | return filename and self._get_object(user, filename) |
paul@50 | 270 | |
paul@343 | 271 | def set_complete_event(self, user, uid, node): |
paul@50 | 272 | |
paul@50 | 273 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 274 | |
paul@138 | 275 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 276 | if not filename: |
paul@50 | 277 | return False |
paul@50 | 278 | |
paul@329 | 279 | return self._set_object(user, filename, node) |
paul@15 | 280 | |
paul@1068 | 281 | def remove_parent_event(self, user, uid): |
paul@1068 | 282 | |
paul@1068 | 283 | "Remove the parent event for 'user' having the given 'uid'." |
paul@369 | 284 | |
paul@234 | 285 | filename = self.get_object_in_store(user, "objects", uid) |
paul@234 | 286 | if not filename: |
paul@234 | 287 | return False |
paul@234 | 288 | |
paul@329 | 289 | return self._remove_object(filename) |
paul@234 | 290 | |
paul@334 | 291 | def get_recurrences(self, user, uid): |
paul@334 | 292 | |
paul@334 | 293 | """ |
paul@334 | 294 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 295 | indicated 'uid'. Both active and cancelled recurrences are returned. |
paul@694 | 296 | """ |
paul@694 | 297 | |
paul@694 | 298 | return self.get_active_recurrences(user, uid) + self.get_cancelled_recurrences(user, uid) |
paul@694 | 299 | |
paul@694 | 300 | def get_active_recurrences(self, user, uid): |
paul@694 | 301 | |
paul@694 | 302 | """ |
paul@694 | 303 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 304 | indicated 'uid'. Cancelled recurrences are not returned. |
paul@334 | 305 | """ |
paul@334 | 306 | |
paul@334 | 307 | filename = self.get_object_in_store(user, "recurrences", uid) |
paul@808 | 308 | if not filename or not isdir(filename): |
paul@347 | 309 | return [] |
paul@334 | 310 | |
paul@334 | 311 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@334 | 312 | |
paul@694 | 313 | def get_cancelled_recurrences(self, user, uid): |
paul@694 | 314 | |
paul@694 | 315 | """ |
paul@694 | 316 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 317 | indicated 'uid'. Only cancelled recurrences are returned. |
paul@694 | 318 | """ |
paul@694 | 319 | |
paul@782 | 320 | filename = self.get_object_in_store(user, "cancellations", "recurrences", uid) |
paul@808 | 321 | if not filename or not isdir(filename): |
paul@694 | 322 | return [] |
paul@694 | 323 | |
paul@694 | 324 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@694 | 325 | |
paul@334 | 326 | def get_recurrence(self, user, uid, recurrenceid): |
paul@334 | 327 | |
paul@334 | 328 | """ |
paul@334 | 329 | For the event of the given 'user' with the given 'uid', return the |
paul@334 | 330 | specific recurrence indicated by the 'recurrenceid'. |
paul@334 | 331 | """ |
paul@334 | 332 | |
paul@694 | 333 | filename = self.get_recurrence_filename(user, uid, recurrenceid) |
paul@808 | 334 | if not filename or not isfile(filename): |
paul@334 | 335 | return None |
paul@334 | 336 | |
paul@694 | 337 | return filename and self._get_object(user, filename) |
paul@334 | 338 | |
paul@334 | 339 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@334 | 340 | |
paul@334 | 341 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@334 | 342 | |
paul@334 | 343 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 344 | if not filename: |
paul@334 | 345 | return False |
paul@334 | 346 | |
paul@334 | 347 | return self._set_object(user, filename, node) |
paul@334 | 348 | |
paul@334 | 349 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@334 | 350 | |
paul@378 | 351 | """ |
paul@378 | 352 | Remove a special recurrence from an event stored by 'user' having the |
paul@378 | 353 | given 'uid' and 'recurrenceid'. |
paul@378 | 354 | """ |
paul@334 | 355 | |
paul@378 | 356 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 357 | if not filename: |
paul@334 | 358 | return False |
paul@334 | 359 | |
paul@334 | 360 | return self._remove_object(filename) |
paul@334 | 361 | |
paul@1068 | 362 | def remove_recurrence_collection(self, user, uid): |
paul@1068 | 363 | |
paul@1068 | 364 | """ |
paul@1068 | 365 | Remove the collection of recurrences stored by 'user' having the given |
paul@1068 | 366 | 'uid'. |
paul@1068 | 367 | """ |
paul@1068 | 368 | |
paul@378 | 369 | recurrences = self.get_object_in_store(user, "recurrences", uid) |
paul@378 | 370 | if recurrences: |
paul@378 | 371 | return self._remove_collection(recurrences) |
paul@378 | 372 | |
paul@378 | 373 | return True |
paul@378 | 374 | |
paul@1142 | 375 | # Event filename computation. |
paul@1142 | 376 | |
paul@1142 | 377 | def get_event_filename(self, user, uid, recurrenceid=None, dirname=None, username=None): |
paul@1142 | 378 | |
paul@1142 | 379 | """ |
paul@1142 | 380 | Get the filename providing the event for the given 'user' with the given |
paul@1142 | 381 | 'uid'. If the optional 'recurrenceid' is specified, a specific instance |
paul@1142 | 382 | or occurrence of an event is returned. |
paul@1142 | 383 | |
paul@1142 | 384 | Where 'dirname' is specified, the given directory name is used as the |
paul@1142 | 385 | base of the location within which any filename will reside. |
paul@1142 | 386 | """ |
paul@1142 | 387 | |
paul@1142 | 388 | if recurrenceid: |
paul@1142 | 389 | return self.get_recurrence_filename(user, uid, recurrenceid, dirname, username) |
paul@1142 | 390 | else: |
paul@1142 | 391 | return self.get_complete_event_filename(user, uid, dirname, username) |
paul@1142 | 392 | |
paul@1142 | 393 | def get_recurrence_filename(self, user, uid, recurrenceid, dirname=None, username=None): |
paul@1142 | 394 | |
paul@1142 | 395 | """ |
paul@1142 | 396 | For the event of the given 'user' with the given 'uid', return the |
paul@1142 | 397 | filename providing the recurrence with the given 'recurrenceid'. |
paul@1142 | 398 | |
paul@1142 | 399 | Where 'dirname' is specified, the given directory name is used as the |
paul@1142 | 400 | base of the location within which any filename will reside. |
paul@1142 | 401 | |
paul@1142 | 402 | Where 'username' is specified, the event details will reside in a file |
paul@1142 | 403 | bearing that name within a directory having 'uid' as its name. |
paul@1142 | 404 | """ |
paul@1142 | 405 | |
paul@1142 | 406 | return self.get_object_in_store(user, dirname, "recurrences", uid, recurrenceid, username) |
paul@1142 | 407 | |
paul@1142 | 408 | def get_complete_event_filename(self, user, uid, dirname=None, username=None): |
paul@1142 | 409 | |
paul@1142 | 410 | """ |
paul@1142 | 411 | Get the filename providing the event for the given 'user' with the given |
paul@1142 | 412 | 'uid'. |
paul@1142 | 413 | |
paul@1142 | 414 | Where 'dirname' is specified, the given directory name is used as the |
paul@1142 | 415 | base of the location within which any filename will reside. |
paul@1142 | 416 | |
paul@1142 | 417 | Where 'username' is specified, the event details will reside in a file |
paul@1142 | 418 | bearing that name within a directory having 'uid' as its name. |
paul@1142 | 419 | """ |
paul@1142 | 420 | |
paul@1142 | 421 | return self.get_object_in_store(user, dirname, "objects", uid, username) |
paul@1142 | 422 | |
paul@652 | 423 | # Free/busy period providers, upon extension of the free/busy records. |
paul@652 | 424 | |
paul@672 | 425 | def _get_freebusy_providers(self, user): |
paul@672 | 426 | |
paul@672 | 427 | """ |
paul@672 | 428 | Return the free/busy providers for the given 'user'. |
paul@672 | 429 | |
paul@672 | 430 | This function returns any stored datetime and a list of providers as a |
paul@672 | 431 | 2-tuple. Each provider is itself a (uid, recurrenceid) tuple. |
paul@672 | 432 | """ |
paul@672 | 433 | |
paul@672 | 434 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@808 | 435 | if not filename or not isfile(filename): |
paul@672 | 436 | return None |
paul@672 | 437 | |
paul@672 | 438 | # Attempt to read providers, with a declaration of the datetime |
paul@672 | 439 | # from which such providers are considered as still being active. |
paul@672 | 440 | |
paul@702 | 441 | t = self._get_table_atomic(user, filename, [(1, None)]) |
paul@672 | 442 | try: |
paul@672 | 443 | dt_string = t[0][0] |
paul@672 | 444 | except IndexError: |
paul@672 | 445 | return None |
paul@672 | 446 | |
paul@672 | 447 | return dt_string, t[1:] |
paul@672 | 448 | |
paul@672 | 449 | def _set_freebusy_providers(self, user, dt_string, t): |
paul@672 | 450 | |
paul@672 | 451 | "Set the given provider timestamp 'dt_string' and table 't'." |
paul@672 | 452 | |
paul@652 | 453 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@672 | 454 | if not filename: |
paul@672 | 455 | return False |
paul@652 | 456 | |
paul@672 | 457 | t.insert(0, (dt_string,)) |
paul@702 | 458 | self._set_table_atomic(user, filename, t, [(1, "")]) |
paul@672 | 459 | return True |
paul@652 | 460 | |
paul@648 | 461 | # Free/busy period access. |
paul@648 | 462 | |
paul@1171 | 463 | def get_freebusy(self, user, name=None, mutable=False, cls=None): |
paul@15 | 464 | |
paul@15 | 465 | "Get free/busy details for the given 'user'." |
paul@15 | 466 | |
paul@702 | 467 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@1062 | 468 | |
paul@808 | 469 | if not filename or not isfile(filename): |
paul@1062 | 470 | periods = [] |
paul@112 | 471 | else: |
paul@1171 | 472 | cls = cls or FreeBusyPeriod |
paul@1171 | 473 | periods = map(lambda t: cls(*t), |
paul@1066 | 474 | self._get_table_atomic(user, filename)) |
paul@702 | 475 | |
paul@1071 | 476 | return FreeBusyCollection(periods, mutable) |
paul@1071 | 477 | |
paul@1192 | 478 | def get_freebusy_for_other(self, user, other, mutable=False, cls=None, collection=None): |
paul@112 | 479 | |
paul@112 | 480 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 481 | |
paul@112 | 482 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@1062 | 483 | |
paul@808 | 484 | if not filename or not isfile(filename): |
paul@1062 | 485 | periods = [] |
paul@112 | 486 | else: |
paul@1192 | 487 | cls = cls or FreeBusyPeriod |
paul@1192 | 488 | periods = map(lambda t: cls(*t), |
paul@1066 | 489 | self._get_table_atomic(user, filename)) |
paul@702 | 490 | |
paul@1192 | 491 | collection = collection or FreeBusyCollection |
paul@1192 | 492 | return collection(periods, mutable) |
paul@1071 | 493 | |
paul@1064 | 494 | def set_freebusy(self, user, freebusy, name=None): |
paul@15 | 495 | |
paul@15 | 496 | "For the given 'user', set 'freebusy' details." |
paul@15 | 497 | |
paul@702 | 498 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@15 | 499 | if not filename: |
paul@15 | 500 | return False |
paul@15 | 501 | |
paul@1171 | 502 | self._set_freebusy(user, freebusy, filename) |
paul@15 | 503 | return True |
paul@15 | 504 | |
paul@1064 | 505 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@110 | 506 | |
paul@110 | 507 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 508 | |
paul@110 | 509 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 510 | if not filename: |
paul@110 | 511 | return False |
paul@110 | 512 | |
paul@1171 | 513 | self._set_freebusy(user, freebusy, filename) |
paul@112 | 514 | return True |
paul@112 | 515 | |
paul@1142 | 516 | def get_freebusy_others(self, user): |
paul@1142 | 517 | |
paul@1142 | 518 | """ |
paul@1142 | 519 | For the given 'user', return a list of other users for whom free/busy |
paul@1142 | 520 | information is retained. |
paul@1142 | 521 | """ |
paul@1142 | 522 | |
paul@1142 | 523 | filename = self.get_object_in_store(user, "freebusy-other") |
paul@1142 | 524 | |
paul@1142 | 525 | if not filename or not isdir(filename): |
paul@1142 | 526 | return [] |
paul@1142 | 527 | |
paul@1142 | 528 | return listdir(filename) |
paul@1142 | 529 | |
paul@710 | 530 | # Tentative free/busy periods related to countering. |
paul@710 | 531 | |
paul@1071 | 532 | def get_freebusy_offers(self, user, mutable=False): |
paul@710 | 533 | |
paul@710 | 534 | "Get free/busy offers for the given 'user'." |
paul@710 | 535 | |
paul@710 | 536 | offers = [] |
paul@710 | 537 | expired = [] |
paul@741 | 538 | now = to_timezone(datetime.utcnow(), "UTC") |
paul@710 | 539 | |
paul@710 | 540 | # Expire old offers and save the collection if modified. |
paul@710 | 541 | |
paul@730 | 542 | self.acquire_lock(user) |
paul@710 | 543 | try: |
paul@1171 | 544 | l = self.get_freebusy(user, "freebusy-offers", cls=FreeBusyOfferPeriod) |
paul@710 | 545 | for fb in l: |
paul@710 | 546 | if fb.expires and get_datetime(fb.expires) <= now: |
paul@710 | 547 | expired.append(fb) |
paul@710 | 548 | else: |
paul@710 | 549 | offers.append(fb) |
paul@710 | 550 | |
paul@710 | 551 | if expired: |
paul@730 | 552 | self.set_freebusy_offers(user, offers) |
paul@710 | 553 | finally: |
paul@730 | 554 | self.release_lock(user) |
paul@710 | 555 | |
paul@1171 | 556 | return FreeBusyOffersCollection(offers, mutable) |
paul@710 | 557 | |
paul@747 | 558 | # Requests and counter-proposals. |
paul@648 | 559 | |
paul@142 | 560 | def _get_requests(self, user, queue): |
paul@66 | 561 | |
paul@142 | 562 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 563 | |
paul@142 | 564 | filename = self.get_object_in_store(user, queue) |
paul@808 | 565 | if not filename or not isfile(filename): |
paul@1142 | 566 | return [] |
paul@66 | 567 | |
paul@747 | 568 | return self._get_table_atomic(user, filename, [(1, None), (2, None)]) |
paul@66 | 569 | |
paul@142 | 570 | def get_requests(self, user): |
paul@142 | 571 | |
paul@142 | 572 | "Get requests for the given 'user'." |
paul@142 | 573 | |
paul@142 | 574 | return self._get_requests(user, "requests") |
paul@142 | 575 | |
paul@142 | 576 | def _set_requests(self, user, requests, queue): |
paul@66 | 577 | |
paul@142 | 578 | """ |
paul@142 | 579 | For the given 'user', set the list of queued 'requests' in the given |
paul@142 | 580 | 'queue'. |
paul@142 | 581 | """ |
paul@142 | 582 | |
paul@142 | 583 | filename = self.get_object_in_store(user, queue) |
paul@66 | 584 | if not filename: |
paul@66 | 585 | return False |
paul@66 | 586 | |
paul@747 | 587 | self._set_table_atomic(user, filename, requests, [(1, ""), (2, "")]) |
paul@66 | 588 | return True |
paul@66 | 589 | |
paul@142 | 590 | def set_requests(self, user, requests): |
paul@142 | 591 | |
paul@142 | 592 | "For the given 'user', set the list of queued 'requests'." |
paul@142 | 593 | |
paul@142 | 594 | return self._set_requests(user, requests, "requests") |
paul@142 | 595 | |
paul@747 | 596 | def _set_request(self, user, request, queue): |
paul@142 | 597 | |
paul@343 | 598 | """ |
paul@747 | 599 | For the given 'user', set the given 'request' in the given 'queue'. |
paul@343 | 600 | """ |
paul@142 | 601 | |
paul@142 | 602 | filename = self.get_object_in_store(user, queue) |
paul@55 | 603 | if not filename: |
paul@55 | 604 | return False |
paul@55 | 605 | |
paul@303 | 606 | self.acquire_lock(user) |
paul@55 | 607 | try: |
paul@747 | 608 | f = codecs.open(filename, "ab", encoding="utf-8") |
paul@303 | 609 | try: |
paul@747 | 610 | self._set_table_item(f, request, [(1, ""), (2, "")]) |
paul@303 | 611 | finally: |
paul@303 | 612 | f.close() |
paul@303 | 613 | fix_permissions(filename) |
paul@55 | 614 | finally: |
paul@303 | 615 | self.release_lock(user) |
paul@55 | 616 | |
paul@55 | 617 | return True |
paul@55 | 618 | |
paul@747 | 619 | def set_request(self, user, uid, recurrenceid=None, type=None): |
paul@142 | 620 | |
paul@747 | 621 | """ |
paul@747 | 622 | For the given 'user', set the queued 'uid' and 'recurrenceid', |
paul@747 | 623 | indicating a request, along with any given 'type'. |
paul@747 | 624 | """ |
paul@142 | 625 | |
paul@747 | 626 | return self._set_request(user, (uid, recurrenceid, type), "requests") |
paul@747 | 627 | |
paul@760 | 628 | def get_counters(self, user, uid, recurrenceid=None): |
paul@754 | 629 | |
paul@754 | 630 | """ |
paul@766 | 631 | For the given 'user', return a list of users from whom counter-proposals |
paul@766 | 632 | have been received for the given 'uid' and optional 'recurrenceid'. |
paul@754 | 633 | """ |
paul@754 | 634 | |
paul@754 | 635 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@808 | 636 | if not filename or not isdir(filename): |
paul@1142 | 637 | return [] |
paul@754 | 638 | |
paul@766 | 639 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@760 | 640 | |
paul@760 | 641 | def get_counter(self, user, other, uid, recurrenceid=None): |
paul@105 | 642 | |
paul@343 | 643 | """ |
paul@760 | 644 | For the given 'user', return the counter-proposal from 'other' for the |
paul@760 | 645 | given 'uid' and optional 'recurrenceid'. |
paul@760 | 646 | """ |
paul@760 | 647 | |
paul@760 | 648 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@1090 | 649 | if not filename or not isfile(filename): |
paul@1142 | 650 | return None |
paul@760 | 651 | |
paul@760 | 652 | return self._get_object(user, filename) |
paul@760 | 653 | |
paul@760 | 654 | def set_counter(self, user, other, node, uid, recurrenceid=None): |
paul@760 | 655 | |
paul@760 | 656 | """ |
paul@760 | 657 | For the given 'user', store a counter-proposal received from 'other' the |
paul@760 | 658 | given 'node' representing that proposal for the given 'uid' and |
paul@760 | 659 | 'recurrenceid'. |
paul@760 | 660 | """ |
paul@760 | 661 | |
paul@760 | 662 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@760 | 663 | if not filename: |
paul@760 | 664 | return False |
paul@760 | 665 | |
paul@760 | 666 | return self._set_object(user, filename, node) |
paul@760 | 667 | |
paul@760 | 668 | def remove_counters(self, user, uid, recurrenceid=None): |
paul@760 | 669 | |
paul@760 | 670 | """ |
paul@760 | 671 | For the given 'user', remove all counter-proposals associated with the |
paul@760 | 672 | given 'uid' and 'recurrenceid'. |
paul@343 | 673 | """ |
paul@105 | 674 | |
paul@747 | 675 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@808 | 676 | if not filename or not isdir(filename): |
paul@747 | 677 | return False |
paul@747 | 678 | |
paul@760 | 679 | removed = False |
paul@747 | 680 | |
paul@760 | 681 | for other in listdir(filename): |
paul@760 | 682 | counter_filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@760 | 683 | removed = removed or self._remove_object(counter_filename) |
paul@760 | 684 | |
paul@760 | 685 | return removed |
paul@760 | 686 | |
paul@760 | 687 | def remove_counter(self, user, other, uid, recurrenceid=None): |
paul@105 | 688 | |
paul@747 | 689 | """ |
paul@760 | 690 | For the given 'user', remove any counter-proposal from 'other' |
paul@760 | 691 | associated with the given 'uid' and 'recurrenceid'. |
paul@747 | 692 | """ |
paul@747 | 693 | |
paul@760 | 694 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@808 | 695 | if not filename or not isfile(filename): |
paul@105 | 696 | return False |
paul@747 | 697 | |
paul@747 | 698 | return self._remove_object(filename) |
paul@747 | 699 | |
paul@747 | 700 | # Event cancellation. |
paul@105 | 701 | |
paul@343 | 702 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@142 | 703 | |
paul@343 | 704 | """ |
paul@694 | 705 | Cancel an event for 'user' having the given 'uid'. If the optional |
paul@694 | 706 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@694 | 707 | event is cancelled. |
paul@343 | 708 | """ |
paul@142 | 709 | |
paul@694 | 710 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@694 | 711 | cancelled_filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@142 | 712 | |
paul@808 | 713 | if filename and cancelled_filename and isfile(filename): |
paul@694 | 714 | return self.move_object(filename, cancelled_filename) |
paul@142 | 715 | |
paul@142 | 716 | return False |
paul@142 | 717 | |
paul@863 | 718 | def uncancel_event(self, user, uid, recurrenceid=None): |
paul@863 | 719 | |
paul@863 | 720 | """ |
paul@863 | 721 | Uncancel an event for 'user' having the given 'uid'. If the optional |
paul@863 | 722 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@863 | 723 | event is uncancelled. |
paul@863 | 724 | """ |
paul@863 | 725 | |
paul@863 | 726 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@863 | 727 | cancelled_filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@863 | 728 | |
paul@863 | 729 | if filename and cancelled_filename and isfile(cancelled_filename): |
paul@863 | 730 | return self.move_object(cancelled_filename, filename) |
paul@863 | 731 | |
paul@863 | 732 | return False |
paul@863 | 733 | |
paul@796 | 734 | def remove_cancellation(self, user, uid, recurrenceid=None): |
paul@796 | 735 | |
paul@796 | 736 | """ |
paul@796 | 737 | Remove a cancellation for 'user' for the event having the given 'uid'. |
paul@796 | 738 | If the optional 'recurrenceid' is specified, a specific instance or |
paul@796 | 739 | occurrence of an event is affected. |
paul@796 | 740 | """ |
paul@796 | 741 | |
paul@796 | 742 | # Remove any parent event cancellation or a specific recurrence |
paul@796 | 743 | # cancellation if indicated. |
paul@796 | 744 | |
paul@796 | 745 | filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@796 | 746 | |
paul@808 | 747 | if filename and isfile(filename): |
paul@796 | 748 | return self._remove_object(filename) |
paul@796 | 749 | |
paul@796 | 750 | return False |
paul@796 | 751 | |
paul@1088 | 752 | class Publisher(FileBase, PublisherBase): |
paul@30 | 753 | |
paul@30 | 754 | "A publisher of objects." |
paul@30 | 755 | |
paul@597 | 756 | def __init__(self, store_dir=None): |
paul@597 | 757 | FileBase.__init__(self, store_dir or PUBLISH_DIR) |
paul@30 | 758 | |
paul@30 | 759 | def set_freebusy(self, user, freebusy): |
paul@30 | 760 | |
paul@30 | 761 | "For the given 'user', set 'freebusy' details." |
paul@30 | 762 | |
paul@52 | 763 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 764 | if not filename: |
paul@30 | 765 | return False |
paul@30 | 766 | |
paul@30 | 767 | record = [] |
paul@30 | 768 | rwrite = record.append |
paul@30 | 769 | |
paul@30 | 770 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 771 | rwrite(("UID", {}, user)) |
paul@30 | 772 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 773 | |
paul@458 | 774 | for fb in freebusy: |
paul@458 | 775 | if not fb.transp or fb.transp == "OPAQUE": |
paul@529 | 776 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@563 | 777 | map(format_datetime, [fb.get_start_point(), fb.get_end_point()])))) |
paul@30 | 778 | |
paul@395 | 779 | f = open(filename, "wb") |
paul@30 | 780 | try: |
paul@30 | 781 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 782 | finally: |
paul@30 | 783 | f.close() |
paul@103 | 784 | fix_permissions(filename) |
paul@30 | 785 | |
paul@30 | 786 | return True |
paul@30 | 787 | |
paul@1192 | 788 | class Journal(Store, JournalBase): |
paul@1039 | 789 | |
paul@1039 | 790 | "A journal system to support quotas." |
paul@1039 | 791 | |
paul@1049 | 792 | # Quota and user identity/group discovery. |
paul@1049 | 793 | |
paul@1194 | 794 | get_quotas = Store.get_users |
paul@1194 | 795 | get_quota_users = Store.get_freebusy_others |
paul@1176 | 796 | |
paul@1176 | 797 | # Delegate information for the quota. |
paul@1176 | 798 | |
paul@1176 | 799 | def get_delegates(self, quota): |
paul@1176 | 800 | |
paul@1176 | 801 | "Return a list of delegates for 'quota'." |
paul@1176 | 802 | |
paul@1176 | 803 | filename = self.get_object_in_store(quota, "delegates") |
paul@1176 | 804 | if not filename or not isfile(filename): |
paul@1176 | 805 | return [] |
paul@1176 | 806 | |
paul@1176 | 807 | return [value for (value,) in self._get_table_atomic(quota, filename)] |
paul@1176 | 808 | |
paul@1176 | 809 | def set_delegates(self, quota, delegates): |
paul@1176 | 810 | |
paul@1176 | 811 | "For the given 'quota', set the list of 'delegates'." |
paul@1176 | 812 | |
paul@1176 | 813 | filename = self.get_object_in_store(quota, "delegates") |
paul@1176 | 814 | if not filename: |
paul@1176 | 815 | return False |
paul@1176 | 816 | |
paul@1176 | 817 | self._set_table_atomic(quota, filename, [(value,) for value in delegates]) |
paul@1176 | 818 | return True |
paul@1049 | 819 | |
paul@1039 | 820 | # Groups of users sharing quotas. |
paul@1039 | 821 | |
paul@1039 | 822 | def get_groups(self, quota): |
paul@1039 | 823 | |
paul@1039 | 824 | "Return the identity mappings for the given 'quota' as a dictionary." |
paul@1039 | 825 | |
paul@1039 | 826 | filename = self.get_object_in_store(quota, "groups") |
paul@1039 | 827 | if not filename or not isfile(filename): |
paul@1039 | 828 | return {} |
paul@1039 | 829 | |
paul@1046 | 830 | return dict(self._get_table_atomic(quota, filename, tab_separated=False)) |
paul@1039 | 831 | |
paul@1176 | 832 | def set_groups(self, quota, groups): |
paul@1142 | 833 | |
paul@1176 | 834 | "For the given 'quota', set 'groups' mapping users to groups." |
paul@1142 | 835 | |
paul@1142 | 836 | filename = self.get_object_in_store(quota, "groups") |
paul@1142 | 837 | if not filename: |
paul@1142 | 838 | return False |
paul@1142 | 839 | |
paul@1142 | 840 | self._set_table_atomic(quota, filename, groups.items()) |
paul@1142 | 841 | return True |
paul@1142 | 842 | |
paul@1039 | 843 | def get_limits(self, quota): |
paul@1039 | 844 | |
paul@1039 | 845 | """ |
paul@1039 | 846 | Return the limits for the 'quota' as a dictionary mapping identities or |
paul@1039 | 847 | groups to durations. |
paul@1039 | 848 | """ |
paul@1039 | 849 | |
paul@1039 | 850 | filename = self.get_object_in_store(quota, "limits") |
paul@1039 | 851 | if not filename or not isfile(filename): |
paul@1142 | 852 | return {} |
paul@1039 | 853 | |
paul@1046 | 854 | return dict(self._get_table_atomic(quota, filename, tab_separated=False)) |
paul@1039 | 855 | |
paul@1176 | 856 | def set_limits(self, quota, limits): |
paul@1089 | 857 | |
paul@1089 | 858 | """ |
paul@1176 | 859 | For the given 'quota', set the given 'limits' on resource usage mapping |
paul@1176 | 860 | groups to limits. |
paul@1089 | 861 | """ |
paul@1089 | 862 | |
paul@1089 | 863 | filename = self.get_object_in_store(quota, "limits") |
paul@1089 | 864 | if not filename: |
paul@1142 | 865 | return False |
paul@1089 | 866 | |
paul@1089 | 867 | self._set_table_atomic(quota, filename, limits.items()) |
paul@1089 | 868 | return True |
paul@1089 | 869 | |
paul@1039 | 870 | # Journal entry methods. |
paul@1039 | 871 | |
paul@1071 | 872 | def get_entries(self, quota, group, mutable=False): |
paul@1039 | 873 | |
paul@1039 | 874 | """ |
paul@1039 | 875 | Return a list of journal entries for the given 'quota' for the indicated |
paul@1039 | 876 | 'group'. |
paul@1039 | 877 | """ |
paul@1039 | 878 | |
paul@1193 | 879 | return self.get_freebusy_for_other(quota, group, mutable) |
paul@1039 | 880 | |
paul@1039 | 881 | def set_entries(self, quota, group, entries): |
paul@1039 | 882 | |
paul@1039 | 883 | """ |
paul@1039 | 884 | For the given 'quota' and indicated 'group', set the list of journal |
paul@1039 | 885 | 'entries'. |
paul@1039 | 886 | """ |
paul@1039 | 887 | |
paul@1192 | 888 | return self.set_freebusy_for_other(quota, entries, group) |
paul@1039 | 889 | |
paul@1193 | 890 | # Compatibility methods. |
paul@1193 | 891 | |
paul@1193 | 892 | def get_freebusy_for_other(self, user, other, mutable=False): |
paul@1193 | 893 | return Store.get_freebusy_for_other(self, user, other, mutable, cls=FreeBusyGroupPeriod, collection=FreeBusyGroupCollection) |
paul@1193 | 894 | |
paul@2 | 895 | # vim: tabstop=4 expandtab shiftwidth=4 |