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