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@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@146 | 20 | """ |
paul@146 | 21 | |
paul@30 | 22 | from datetime import datetime |
paul@68 | 23 | from imiptools.config import STORE_DIR, PUBLISH_DIR |
paul@301 | 24 | from imiptools.data import make_calendar, parse_object, to_stream |
paul@147 | 25 | from imiptools.filesys import fix_permissions, FileBase |
paul@147 | 26 | from os.path import exists, isfile, join |
paul@343 | 27 | from os import listdir, remove, rmdir |
paul@303 | 28 | from time import sleep |
paul@15 | 29 | |
paul@50 | 30 | class FileStore(FileBase): |
paul@50 | 31 | |
paul@50 | 32 | "A file store of tabular free/busy data and objects." |
paul@50 | 33 | |
paul@147 | 34 | def __init__(self, store_dir=STORE_DIR): |
paul@147 | 35 | FileBase.__init__(self, store_dir) |
paul@147 | 36 | |
paul@303 | 37 | def acquire_lock(self, user, timeout=None): |
paul@303 | 38 | FileBase.acquire_lock(self, timeout, user) |
paul@303 | 39 | |
paul@303 | 40 | def release_lock(self, user): |
paul@303 | 41 | FileBase.release_lock(self, user) |
paul@303 | 42 | |
paul@343 | 43 | def _set_defaults(self, t, empty_defaults): |
paul@343 | 44 | for i, default in empty_defaults: |
paul@343 | 45 | if i >= len(t): |
paul@343 | 46 | t += [None] * (i - len(t) + 1) |
paul@343 | 47 | if not t[i]: |
paul@343 | 48 | t[i] = default |
paul@343 | 49 | return t |
paul@343 | 50 | |
paul@343 | 51 | def _get_table(self, user, filename, empty_defaults=None): |
paul@343 | 52 | |
paul@343 | 53 | """ |
paul@343 | 54 | From the file for the given 'user' having the given 'filename', return |
paul@343 | 55 | a list of tuples representing the file's contents. |
paul@343 | 56 | |
paul@343 | 57 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 58 | default value where a column either does not exist or provides an empty |
paul@343 | 59 | value. |
paul@343 | 60 | """ |
paul@343 | 61 | |
paul@343 | 62 | self.acquire_lock(user) |
paul@343 | 63 | try: |
paul@343 | 64 | f = open(filename, "rb") |
paul@343 | 65 | try: |
paul@343 | 66 | l = [] |
paul@343 | 67 | for line in f.readlines(): |
paul@343 | 68 | t = line.strip().split("\t") |
paul@343 | 69 | if empty_defaults: |
paul@343 | 70 | t = self._set_defaults(t, empty_defaults) |
paul@343 | 71 | l.append(tuple(t)) |
paul@343 | 72 | return l |
paul@343 | 73 | finally: |
paul@343 | 74 | f.close() |
paul@343 | 75 | finally: |
paul@343 | 76 | self.release_lock(user) |
paul@343 | 77 | |
paul@343 | 78 | def _set_table(self, user, filename, items, empty_defaults=None): |
paul@343 | 79 | |
paul@343 | 80 | """ |
paul@343 | 81 | For the given 'user', write to the file having the given 'filename' the |
paul@343 | 82 | 'items'. |
paul@343 | 83 | |
paul@343 | 84 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 85 | default value where a column either does not exist or provides an empty |
paul@343 | 86 | value. |
paul@343 | 87 | """ |
paul@343 | 88 | |
paul@343 | 89 | self.acquire_lock(user) |
paul@343 | 90 | try: |
paul@343 | 91 | f = open(filename, "wb") |
paul@343 | 92 | try: |
paul@343 | 93 | for item in items: |
paul@343 | 94 | if empty_defaults: |
paul@343 | 95 | item = self._set_defaults(list(item), empty_defaults) |
paul@343 | 96 | f.write("\t".join(item) + "\n") |
paul@343 | 97 | finally: |
paul@343 | 98 | f.close() |
paul@343 | 99 | fix_permissions(filename) |
paul@343 | 100 | finally: |
paul@343 | 101 | self.release_lock(user) |
paul@343 | 102 | |
paul@329 | 103 | def _get_object(self, user, filename): |
paul@329 | 104 | |
paul@329 | 105 | """ |
paul@329 | 106 | Return the parsed object for the given 'user' having the given |
paul@329 | 107 | 'filename'. |
paul@329 | 108 | """ |
paul@329 | 109 | |
paul@329 | 110 | self.acquire_lock(user) |
paul@329 | 111 | try: |
paul@329 | 112 | f = open(filename, "rb") |
paul@329 | 113 | try: |
paul@329 | 114 | return parse_object(f, "utf-8") |
paul@329 | 115 | finally: |
paul@329 | 116 | f.close() |
paul@329 | 117 | finally: |
paul@329 | 118 | self.release_lock(user) |
paul@329 | 119 | |
paul@329 | 120 | def _set_object(self, user, filename, node): |
paul@329 | 121 | |
paul@329 | 122 | """ |
paul@329 | 123 | Set an object for the given 'user' having the given 'filename', using |
paul@329 | 124 | 'node' to define the object. |
paul@329 | 125 | """ |
paul@329 | 126 | |
paul@329 | 127 | self.acquire_lock(user) |
paul@329 | 128 | try: |
paul@329 | 129 | f = open(filename, "wb") |
paul@329 | 130 | try: |
paul@329 | 131 | to_stream(f, node) |
paul@329 | 132 | finally: |
paul@329 | 133 | f.close() |
paul@329 | 134 | fix_permissions(filename) |
paul@329 | 135 | finally: |
paul@329 | 136 | self.release_lock(user) |
paul@329 | 137 | |
paul@329 | 138 | return True |
paul@329 | 139 | |
paul@329 | 140 | def _remove_object(self, filename): |
paul@329 | 141 | |
paul@329 | 142 | "Remove the object with the given 'filename'." |
paul@329 | 143 | |
paul@329 | 144 | try: |
paul@329 | 145 | remove(filename) |
paul@329 | 146 | except OSError: |
paul@329 | 147 | return False |
paul@329 | 148 | |
paul@329 | 149 | return True |
paul@329 | 150 | |
paul@343 | 151 | def _remove_collection(self, filename): |
paul@343 | 152 | |
paul@343 | 153 | "Remove the collection with the given 'filename'." |
paul@343 | 154 | |
paul@343 | 155 | try: |
paul@343 | 156 | rmdir(filename) |
paul@343 | 157 | except OSError: |
paul@343 | 158 | return False |
paul@343 | 159 | |
paul@343 | 160 | return True |
paul@343 | 161 | |
paul@119 | 162 | def get_events(self, user): |
paul@119 | 163 | |
paul@119 | 164 | "Return a list of event identifiers." |
paul@119 | 165 | |
paul@138 | 166 | filename = self.get_object_in_store(user, "objects") |
paul@119 | 167 | if not filename or not exists(filename): |
paul@119 | 168 | return None |
paul@119 | 169 | |
paul@119 | 170 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@119 | 171 | |
paul@343 | 172 | def get_event(self, user, uid, recurrenceid=None): |
paul@343 | 173 | |
paul@343 | 174 | """ |
paul@343 | 175 | Get the event for the given 'user' with the given 'uid'. If |
paul@343 | 176 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 177 | occurrence of an event is returned. |
paul@343 | 178 | """ |
paul@343 | 179 | |
paul@343 | 180 | if recurrenceid: |
paul@343 | 181 | return self.get_recurrence(user, uid, recurrenceid) |
paul@343 | 182 | else: |
paul@343 | 183 | return self.get_complete_event(user, uid) |
paul@343 | 184 | |
paul@343 | 185 | def get_complete_event(self, user, uid): |
paul@50 | 186 | |
paul@50 | 187 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 188 | |
paul@138 | 189 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 190 | if not filename or not exists(filename): |
paul@50 | 191 | return None |
paul@50 | 192 | |
paul@329 | 193 | return self._get_object(user, filename) |
paul@50 | 194 | |
paul@343 | 195 | def set_event(self, user, uid, recurrenceid, node): |
paul@343 | 196 | |
paul@343 | 197 | """ |
paul@343 | 198 | Set an event for 'user' having the given 'uid' and 'recurrenceid' (which |
paul@343 | 199 | if the latter is specified, a specific instance or occurrence of an |
paul@343 | 200 | event is referenced), using the given 'node' description. |
paul@343 | 201 | """ |
paul@343 | 202 | |
paul@343 | 203 | if recurrenceid: |
paul@343 | 204 | return self.set_recurrence(user, uid, recurrenceid, node) |
paul@343 | 205 | else: |
paul@343 | 206 | return self.set_complete_event(user, uid, node) |
paul@343 | 207 | |
paul@343 | 208 | def set_complete_event(self, user, uid, node): |
paul@50 | 209 | |
paul@50 | 210 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 211 | |
paul@138 | 212 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 213 | if not filename: |
paul@50 | 214 | return False |
paul@50 | 215 | |
paul@329 | 216 | return self._set_object(user, filename, node) |
paul@15 | 217 | |
paul@365 | 218 | def remove_event(self, user, uid, recurrenceid=None): |
paul@234 | 219 | |
paul@343 | 220 | """ |
paul@343 | 221 | Remove an event for 'user' having the given 'uid'. If the optional |
paul@343 | 222 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@343 | 223 | event is removed. |
paul@343 | 224 | """ |
paul@343 | 225 | |
paul@343 | 226 | if recurrenceid: |
paul@343 | 227 | return self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 228 | else: |
paul@343 | 229 | for recurrenceid in self.get_recurrences(user, uid) or []: |
paul@343 | 230 | self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 231 | return self.remove_complete_event(user, uid) |
paul@343 | 232 | |
paul@343 | 233 | def remove_complete_event(self, user, uid): |
paul@343 | 234 | |
paul@234 | 235 | "Remove an event for 'user' having the given 'uid'." |
paul@234 | 236 | |
paul@378 | 237 | self.remove_recurrences(user, uid) |
paul@369 | 238 | |
paul@234 | 239 | filename = self.get_object_in_store(user, "objects", uid) |
paul@234 | 240 | if not filename: |
paul@234 | 241 | return False |
paul@234 | 242 | |
paul@329 | 243 | return self._remove_object(filename) |
paul@234 | 244 | |
paul@334 | 245 | def get_recurrences(self, user, uid): |
paul@334 | 246 | |
paul@334 | 247 | """ |
paul@334 | 248 | Get additional event instances for an event of the given 'user' with the |
paul@334 | 249 | indicated 'uid'. |
paul@334 | 250 | """ |
paul@334 | 251 | |
paul@334 | 252 | filename = self.get_object_in_store(user, "recurrences", uid) |
paul@334 | 253 | if not filename or not exists(filename): |
paul@347 | 254 | return [] |
paul@334 | 255 | |
paul@334 | 256 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@334 | 257 | |
paul@334 | 258 | def get_recurrence(self, user, uid, recurrenceid): |
paul@334 | 259 | |
paul@334 | 260 | """ |
paul@334 | 261 | For the event of the given 'user' with the given 'uid', return the |
paul@334 | 262 | specific recurrence indicated by the 'recurrenceid'. |
paul@334 | 263 | """ |
paul@334 | 264 | |
paul@334 | 265 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 266 | if not filename or not exists(filename): |
paul@334 | 267 | return None |
paul@334 | 268 | |
paul@334 | 269 | return self._get_object(user, filename) |
paul@334 | 270 | |
paul@334 | 271 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@334 | 272 | |
paul@334 | 273 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@334 | 274 | |
paul@334 | 275 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 276 | if not filename: |
paul@334 | 277 | return False |
paul@334 | 278 | |
paul@334 | 279 | return self._set_object(user, filename, node) |
paul@334 | 280 | |
paul@334 | 281 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@334 | 282 | |
paul@378 | 283 | """ |
paul@378 | 284 | Remove a special recurrence from an event stored by 'user' having the |
paul@378 | 285 | given 'uid' and 'recurrenceid'. |
paul@378 | 286 | """ |
paul@334 | 287 | |
paul@378 | 288 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 289 | if not filename: |
paul@334 | 290 | return False |
paul@334 | 291 | |
paul@334 | 292 | return self._remove_object(filename) |
paul@334 | 293 | |
paul@378 | 294 | def remove_recurrences(self, user, uid): |
paul@378 | 295 | |
paul@378 | 296 | """ |
paul@378 | 297 | Remove all recurrences for an event stored by 'user' having the given |
paul@378 | 298 | 'uid'. |
paul@378 | 299 | """ |
paul@378 | 300 | |
paul@378 | 301 | for recurrenceid in self.get_recurrences(user, uid): |
paul@378 | 302 | self.remove_recurrence(user, uid, recurrenceid) |
paul@378 | 303 | |
paul@378 | 304 | recurrences = self.get_object_in_store(user, "recurrences", uid) |
paul@378 | 305 | if recurrences: |
paul@378 | 306 | return self._remove_collection(recurrences) |
paul@378 | 307 | |
paul@378 | 308 | return True |
paul@378 | 309 | |
paul@15 | 310 | def get_freebusy(self, user): |
paul@15 | 311 | |
paul@15 | 312 | "Get free/busy details for the given 'user'." |
paul@15 | 313 | |
paul@52 | 314 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 315 | if not filename or not exists(filename): |
paul@167 | 316 | return [] |
paul@112 | 317 | else: |
paul@343 | 318 | return self._get_table(user, filename, [(4, None)]) |
paul@2 | 319 | |
paul@112 | 320 | def get_freebusy_for_other(self, user, other): |
paul@112 | 321 | |
paul@112 | 322 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 323 | |
paul@112 | 324 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@167 | 325 | if not filename or not exists(filename): |
paul@167 | 326 | return [] |
paul@112 | 327 | else: |
paul@343 | 328 | return self._get_table(user, filename, [(4, None)]) |
paul@2 | 329 | |
paul@15 | 330 | def set_freebusy(self, user, freebusy): |
paul@15 | 331 | |
paul@15 | 332 | "For the given 'user', set 'freebusy' details." |
paul@15 | 333 | |
paul@52 | 334 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 335 | if not filename: |
paul@15 | 336 | return False |
paul@15 | 337 | |
paul@343 | 338 | self._set_table(user, filename, freebusy, [(3, "OPAQUE"), (4, "")]) |
paul@15 | 339 | return True |
paul@15 | 340 | |
paul@110 | 341 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@110 | 342 | |
paul@110 | 343 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 344 | |
paul@110 | 345 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 346 | if not filename: |
paul@110 | 347 | return False |
paul@110 | 348 | |
paul@343 | 349 | self._set_table(user, filename, freebusy, [(2, ""), (3, "OPAQUE"), (4, "")]) |
paul@112 | 350 | return True |
paul@112 | 351 | |
paul@142 | 352 | def _get_requests(self, user, queue): |
paul@66 | 353 | |
paul@142 | 354 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 355 | |
paul@142 | 356 | filename = self.get_object_in_store(user, queue) |
paul@81 | 357 | if not filename or not exists(filename): |
paul@66 | 358 | return None |
paul@66 | 359 | |
paul@343 | 360 | return self._get_table(user, filename, [(1, None)]) |
paul@66 | 361 | |
paul@142 | 362 | def get_requests(self, user): |
paul@142 | 363 | |
paul@142 | 364 | "Get requests for the given 'user'." |
paul@142 | 365 | |
paul@142 | 366 | return self._get_requests(user, "requests") |
paul@142 | 367 | |
paul@142 | 368 | def get_cancellations(self, user): |
paul@142 | 369 | |
paul@142 | 370 | "Get cancellations for the given 'user'." |
paul@55 | 371 | |
paul@142 | 372 | return self._get_requests(user, "cancellations") |
paul@142 | 373 | |
paul@142 | 374 | def _set_requests(self, user, requests, queue): |
paul@66 | 375 | |
paul@142 | 376 | """ |
paul@142 | 377 | For the given 'user', set the list of queued 'requests' in the given |
paul@142 | 378 | 'queue'. |
paul@142 | 379 | """ |
paul@142 | 380 | |
paul@142 | 381 | filename = self.get_object_in_store(user, queue) |
paul@66 | 382 | if not filename: |
paul@66 | 383 | return False |
paul@66 | 384 | |
paul@303 | 385 | self.acquire_lock(user) |
paul@66 | 386 | try: |
paul@303 | 387 | f = open(filename, "w") |
paul@303 | 388 | try: |
paul@303 | 389 | for request in requests: |
paul@343 | 390 | print >>f, "\t".join([value or "" for value in request]) |
paul@303 | 391 | finally: |
paul@303 | 392 | f.close() |
paul@303 | 393 | fix_permissions(filename) |
paul@66 | 394 | finally: |
paul@303 | 395 | self.release_lock(user) |
paul@66 | 396 | |
paul@66 | 397 | return True |
paul@66 | 398 | |
paul@142 | 399 | def set_requests(self, user, requests): |
paul@142 | 400 | |
paul@142 | 401 | "For the given 'user', set the list of queued 'requests'." |
paul@142 | 402 | |
paul@142 | 403 | return self._set_requests(user, requests, "requests") |
paul@142 | 404 | |
paul@142 | 405 | def set_cancellations(self, user, cancellations): |
paul@66 | 406 | |
paul@142 | 407 | "For the given 'user', set the list of queued 'cancellations'." |
paul@142 | 408 | |
paul@142 | 409 | return self._set_requests(user, cancellations, "cancellations") |
paul@55 | 410 | |
paul@343 | 411 | def _set_request(self, user, uid, recurrenceid, queue): |
paul@142 | 412 | |
paul@343 | 413 | """ |
paul@343 | 414 | For the given 'user', set the queued 'uid' and 'recurrenceid' in the |
paul@343 | 415 | given 'queue'. |
paul@343 | 416 | """ |
paul@142 | 417 | |
paul@142 | 418 | filename = self.get_object_in_store(user, queue) |
paul@55 | 419 | if not filename: |
paul@55 | 420 | return False |
paul@55 | 421 | |
paul@303 | 422 | self.acquire_lock(user) |
paul@55 | 423 | try: |
paul@303 | 424 | f = open(filename, "a") |
paul@303 | 425 | try: |
paul@343 | 426 | print >>f, "\t".join([uid, recurrenceid or ""]) |
paul@303 | 427 | finally: |
paul@303 | 428 | f.close() |
paul@303 | 429 | fix_permissions(filename) |
paul@55 | 430 | finally: |
paul@303 | 431 | self.release_lock(user) |
paul@55 | 432 | |
paul@55 | 433 | return True |
paul@55 | 434 | |
paul@343 | 435 | def set_request(self, user, uid, recurrenceid=None): |
paul@142 | 436 | |
paul@343 | 437 | "For the given 'user', set the queued 'uid' and 'recurrenceid'." |
paul@142 | 438 | |
paul@343 | 439 | return self._set_request(user, uid, recurrenceid, "requests") |
paul@142 | 440 | |
paul@343 | 441 | def set_cancellation(self, user, uid, recurrenceid=None): |
paul@343 | 442 | |
paul@343 | 443 | "For the given 'user', set the queued 'uid' and 'recurrenceid'." |
paul@142 | 444 | |
paul@343 | 445 | return self._set_request(user, uid, recurrenceid, "cancellations") |
paul@142 | 446 | |
paul@343 | 447 | def queue_request(self, user, uid, recurrenceid=None): |
paul@142 | 448 | |
paul@343 | 449 | """ |
paul@343 | 450 | Queue a request for 'user' having the given 'uid'. If the optional |
paul@343 | 451 | 'recurrenceid' is specified, the request refers to a specific instance |
paul@343 | 452 | or occurrence of an event. |
paul@343 | 453 | """ |
paul@66 | 454 | |
paul@81 | 455 | requests = self.get_requests(user) or [] |
paul@66 | 456 | |
paul@343 | 457 | if (uid, recurrenceid) not in requests: |
paul@343 | 458 | return self.set_request(user, uid, recurrenceid) |
paul@66 | 459 | |
paul@66 | 460 | return False |
paul@66 | 461 | |
paul@343 | 462 | def dequeue_request(self, user, uid, recurrenceid=None): |
paul@105 | 463 | |
paul@343 | 464 | """ |
paul@343 | 465 | Dequeue a request for 'user' having the given 'uid'. If the optional |
paul@343 | 466 | 'recurrenceid' is specified, the request refers to a specific instance |
paul@343 | 467 | or occurrence of an event. |
paul@343 | 468 | """ |
paul@105 | 469 | |
paul@105 | 470 | requests = self.get_requests(user) or [] |
paul@105 | 471 | |
paul@105 | 472 | try: |
paul@343 | 473 | requests.remove((uid, recurrenceid)) |
paul@105 | 474 | self.set_requests(user, requests) |
paul@105 | 475 | except ValueError: |
paul@105 | 476 | return False |
paul@105 | 477 | else: |
paul@105 | 478 | return True |
paul@105 | 479 | |
paul@343 | 480 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@142 | 481 | |
paul@343 | 482 | """ |
paul@343 | 483 | Queue an event for cancellation for 'user' having the given 'uid'. If |
paul@343 | 484 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 485 | occurrence of an event is cancelled. |
paul@343 | 486 | """ |
paul@142 | 487 | |
paul@142 | 488 | cancellations = self.get_cancellations(user) or [] |
paul@142 | 489 | |
paul@343 | 490 | if (uid, recurrenceid) not in cancellations: |
paul@343 | 491 | return self.set_cancellation(user, uid, recurrenceid) |
paul@142 | 492 | |
paul@142 | 493 | return False |
paul@142 | 494 | |
paul@30 | 495 | class FilePublisher(FileBase): |
paul@30 | 496 | |
paul@30 | 497 | "A publisher of objects." |
paul@30 | 498 | |
paul@30 | 499 | def __init__(self, store_dir=PUBLISH_DIR): |
paul@30 | 500 | FileBase.__init__(self, store_dir) |
paul@30 | 501 | |
paul@30 | 502 | def set_freebusy(self, user, freebusy): |
paul@30 | 503 | |
paul@30 | 504 | "For the given 'user', set 'freebusy' details." |
paul@30 | 505 | |
paul@52 | 506 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 507 | if not filename: |
paul@30 | 508 | return False |
paul@30 | 509 | |
paul@30 | 510 | record = [] |
paul@30 | 511 | rwrite = record.append |
paul@30 | 512 | |
paul@30 | 513 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 514 | rwrite(("UID", {}, user)) |
paul@30 | 515 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 516 | |
paul@343 | 517 | for start, end, uid, transp, recurrenceid in freebusy: |
paul@119 | 518 | if not transp or transp == "OPAQUE": |
paul@112 | 519 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end]))) |
paul@30 | 520 | |
paul@30 | 521 | f = open(filename, "w") |
paul@30 | 522 | try: |
paul@30 | 523 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 524 | finally: |
paul@30 | 525 | f.close() |
paul@103 | 526 | fix_permissions(filename) |
paul@30 | 527 | |
paul@30 | 528 | return True |
paul@30 | 529 | |
paul@2 | 530 | # vim: tabstop=4 expandtab shiftwidth=4 |