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