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