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