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@234 | 27 | from os import listdir, remove |
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@329 | 43 | def _get_object(self, user, filename): |
paul@329 | 44 | |
paul@329 | 45 | """ |
paul@329 | 46 | Return the parsed object for the given 'user' having the given |
paul@329 | 47 | 'filename'. |
paul@329 | 48 | """ |
paul@329 | 49 | |
paul@329 | 50 | self.acquire_lock(user) |
paul@329 | 51 | try: |
paul@329 | 52 | f = open(filename, "rb") |
paul@329 | 53 | try: |
paul@329 | 54 | return parse_object(f, "utf-8") |
paul@329 | 55 | finally: |
paul@329 | 56 | f.close() |
paul@329 | 57 | finally: |
paul@329 | 58 | self.release_lock(user) |
paul@329 | 59 | |
paul@329 | 60 | def _set_object(self, user, filename, node): |
paul@329 | 61 | |
paul@329 | 62 | """ |
paul@329 | 63 | Set an object for the given 'user' having the given 'filename', using |
paul@329 | 64 | 'node' to define the object. |
paul@329 | 65 | """ |
paul@329 | 66 | |
paul@329 | 67 | self.acquire_lock(user) |
paul@329 | 68 | try: |
paul@329 | 69 | f = open(filename, "wb") |
paul@329 | 70 | try: |
paul@329 | 71 | to_stream(f, node) |
paul@329 | 72 | finally: |
paul@329 | 73 | f.close() |
paul@329 | 74 | fix_permissions(filename) |
paul@329 | 75 | finally: |
paul@329 | 76 | self.release_lock(user) |
paul@329 | 77 | |
paul@329 | 78 | return True |
paul@329 | 79 | |
paul@329 | 80 | def _remove_object(self, filename): |
paul@329 | 81 | |
paul@329 | 82 | "Remove the object with the given 'filename'." |
paul@329 | 83 | |
paul@329 | 84 | try: |
paul@329 | 85 | remove(filename) |
paul@329 | 86 | except OSError: |
paul@329 | 87 | return False |
paul@329 | 88 | |
paul@329 | 89 | return True |
paul@329 | 90 | |
paul@119 | 91 | def get_events(self, user): |
paul@119 | 92 | |
paul@119 | 93 | "Return a list of event identifiers." |
paul@119 | 94 | |
paul@138 | 95 | filename = self.get_object_in_store(user, "objects") |
paul@119 | 96 | if not filename or not exists(filename): |
paul@119 | 97 | return None |
paul@119 | 98 | |
paul@119 | 99 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@119 | 100 | |
paul@50 | 101 | def get_event(self, user, uid): |
paul@50 | 102 | |
paul@50 | 103 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 104 | |
paul@138 | 105 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 106 | if not filename or not exists(filename): |
paul@50 | 107 | return None |
paul@50 | 108 | |
paul@329 | 109 | return self._get_object(user, filename) |
paul@50 | 110 | |
paul@50 | 111 | def set_event(self, user, uid, node): |
paul@50 | 112 | |
paul@50 | 113 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 114 | |
paul@138 | 115 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 116 | if not filename: |
paul@50 | 117 | return False |
paul@50 | 118 | |
paul@329 | 119 | return self._set_object(user, filename, node) |
paul@15 | 120 | |
paul@234 | 121 | def remove_event(self, user, uid): |
paul@234 | 122 | |
paul@234 | 123 | "Remove an event for 'user' having the given 'uid'." |
paul@234 | 124 | |
paul@234 | 125 | filename = self.get_object_in_store(user, "objects", uid) |
paul@234 | 126 | if not filename: |
paul@234 | 127 | return False |
paul@234 | 128 | |
paul@329 | 129 | return self._remove_object(filename) |
paul@234 | 130 | |
paul@15 | 131 | def get_freebusy(self, user): |
paul@15 | 132 | |
paul@15 | 133 | "Get free/busy details for the given 'user'." |
paul@15 | 134 | |
paul@52 | 135 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 136 | if not filename or not exists(filename): |
paul@167 | 137 | return [] |
paul@112 | 138 | else: |
paul@303 | 139 | return self._get_freebusy(user, filename) |
paul@2 | 140 | |
paul@112 | 141 | def get_freebusy_for_other(self, user, other): |
paul@112 | 142 | |
paul@112 | 143 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 144 | |
paul@112 | 145 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@167 | 146 | if not filename or not exists(filename): |
paul@167 | 147 | return [] |
paul@112 | 148 | else: |
paul@303 | 149 | return self._get_freebusy(user, filename) |
paul@303 | 150 | |
paul@303 | 151 | def _get_freebusy(self, user, filename): |
paul@112 | 152 | |
paul@303 | 153 | "For the given 'user', get the free/busy details from 'filename'." |
paul@303 | 154 | |
paul@303 | 155 | self.acquire_lock(user) |
paul@2 | 156 | try: |
paul@303 | 157 | f = open(filename) |
paul@303 | 158 | try: |
paul@303 | 159 | l = [] |
paul@303 | 160 | for line in f.readlines(): |
paul@303 | 161 | l.append(tuple(line.strip().split("\t"))) |
paul@303 | 162 | return l |
paul@303 | 163 | finally: |
paul@303 | 164 | f.close() |
paul@2 | 165 | finally: |
paul@303 | 166 | self.release_lock(user) |
paul@2 | 167 | |
paul@15 | 168 | def set_freebusy(self, user, freebusy): |
paul@15 | 169 | |
paul@15 | 170 | "For the given 'user', set 'freebusy' details." |
paul@15 | 171 | |
paul@52 | 172 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 173 | if not filename: |
paul@15 | 174 | return False |
paul@15 | 175 | |
paul@303 | 176 | self._set_freebusy(user, filename, freebusy) |
paul@15 | 177 | return True |
paul@15 | 178 | |
paul@110 | 179 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@110 | 180 | |
paul@110 | 181 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 182 | |
paul@110 | 183 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 184 | if not filename: |
paul@110 | 185 | return False |
paul@110 | 186 | |
paul@303 | 187 | self._set_freebusy(user, filename, freebusy) |
paul@112 | 188 | return True |
paul@112 | 189 | |
paul@303 | 190 | def _set_freebusy(self, user, filename, freebusy): |
paul@303 | 191 | |
paul@303 | 192 | """ |
paul@303 | 193 | For the given 'user', write to the file having the given 'filename' the |
paul@303 | 194 | 'freebusy' details. |
paul@303 | 195 | """ |
paul@303 | 196 | |
paul@303 | 197 | self.acquire_lock(user) |
paul@110 | 198 | try: |
paul@303 | 199 | f = open(filename, "w") |
paul@303 | 200 | try: |
paul@303 | 201 | for item in freebusy: |
paul@303 | 202 | f.write("\t".join([(value or "OPAQUE") for value in item]) + "\n") |
paul@303 | 203 | finally: |
paul@303 | 204 | f.close() |
paul@303 | 205 | fix_permissions(filename) |
paul@110 | 206 | finally: |
paul@303 | 207 | self.release_lock(user) |
paul@110 | 208 | |
paul@142 | 209 | def _get_requests(self, user, queue): |
paul@66 | 210 | |
paul@142 | 211 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 212 | |
paul@142 | 213 | filename = self.get_object_in_store(user, queue) |
paul@81 | 214 | if not filename or not exists(filename): |
paul@66 | 215 | return None |
paul@66 | 216 | |
paul@303 | 217 | self.acquire_lock(user) |
paul@66 | 218 | try: |
paul@303 | 219 | f = open(filename) |
paul@303 | 220 | try: |
paul@303 | 221 | return [line.strip() for line in f.readlines()] |
paul@303 | 222 | finally: |
paul@303 | 223 | f.close() |
paul@66 | 224 | finally: |
paul@303 | 225 | self.release_lock(user) |
paul@66 | 226 | |
paul@142 | 227 | def get_requests(self, user): |
paul@142 | 228 | |
paul@142 | 229 | "Get requests for the given 'user'." |
paul@142 | 230 | |
paul@142 | 231 | return self._get_requests(user, "requests") |
paul@142 | 232 | |
paul@142 | 233 | def get_cancellations(self, user): |
paul@142 | 234 | |
paul@142 | 235 | "Get cancellations for the given 'user'." |
paul@55 | 236 | |
paul@142 | 237 | return self._get_requests(user, "cancellations") |
paul@142 | 238 | |
paul@142 | 239 | def _set_requests(self, user, requests, queue): |
paul@66 | 240 | |
paul@142 | 241 | """ |
paul@142 | 242 | For the given 'user', set the list of queued 'requests' in the given |
paul@142 | 243 | 'queue'. |
paul@142 | 244 | """ |
paul@142 | 245 | |
paul@142 | 246 | filename = self.get_object_in_store(user, queue) |
paul@66 | 247 | if not filename: |
paul@66 | 248 | return False |
paul@66 | 249 | |
paul@303 | 250 | self.acquire_lock(user) |
paul@66 | 251 | try: |
paul@303 | 252 | f = open(filename, "w") |
paul@303 | 253 | try: |
paul@303 | 254 | for request in requests: |
paul@303 | 255 | print >>f, request |
paul@303 | 256 | finally: |
paul@303 | 257 | f.close() |
paul@303 | 258 | fix_permissions(filename) |
paul@66 | 259 | finally: |
paul@303 | 260 | self.release_lock(user) |
paul@66 | 261 | |
paul@66 | 262 | return True |
paul@66 | 263 | |
paul@142 | 264 | def set_requests(self, user, requests): |
paul@142 | 265 | |
paul@142 | 266 | "For the given 'user', set the list of queued 'requests'." |
paul@142 | 267 | |
paul@142 | 268 | return self._set_requests(user, requests, "requests") |
paul@142 | 269 | |
paul@142 | 270 | def set_cancellations(self, user, cancellations): |
paul@66 | 271 | |
paul@142 | 272 | "For the given 'user', set the list of queued 'cancellations'." |
paul@142 | 273 | |
paul@142 | 274 | return self._set_requests(user, cancellations, "cancellations") |
paul@55 | 275 | |
paul@142 | 276 | def _set_request(self, user, request, queue): |
paul@142 | 277 | |
paul@142 | 278 | "For the given 'user', set the queued 'request' in the given 'queue'." |
paul@142 | 279 | |
paul@142 | 280 | filename = self.get_object_in_store(user, queue) |
paul@55 | 281 | if not filename: |
paul@55 | 282 | return False |
paul@55 | 283 | |
paul@303 | 284 | self.acquire_lock(user) |
paul@55 | 285 | try: |
paul@303 | 286 | f = open(filename, "a") |
paul@303 | 287 | try: |
paul@303 | 288 | print >>f, request |
paul@303 | 289 | finally: |
paul@303 | 290 | f.close() |
paul@303 | 291 | fix_permissions(filename) |
paul@55 | 292 | finally: |
paul@303 | 293 | self.release_lock(user) |
paul@55 | 294 | |
paul@55 | 295 | return True |
paul@55 | 296 | |
paul@142 | 297 | def set_request(self, user, request): |
paul@142 | 298 | |
paul@142 | 299 | "For the given 'user', set the queued 'request'." |
paul@142 | 300 | |
paul@142 | 301 | return self._set_request(user, request, "requests") |
paul@142 | 302 | |
paul@142 | 303 | def set_cancellation(self, user, cancellation): |
paul@142 | 304 | |
paul@142 | 305 | "For the given 'user', set the queued 'cancellation'." |
paul@142 | 306 | |
paul@142 | 307 | return self._set_request(user, cancellation, "cancellations") |
paul@142 | 308 | |
paul@66 | 309 | def queue_request(self, user, uid): |
paul@66 | 310 | |
paul@66 | 311 | "Queue a request for 'user' having the given 'uid'." |
paul@66 | 312 | |
paul@81 | 313 | requests = self.get_requests(user) or [] |
paul@66 | 314 | |
paul@66 | 315 | if uid not in requests: |
paul@66 | 316 | return self.set_request(user, uid) |
paul@66 | 317 | |
paul@66 | 318 | return False |
paul@66 | 319 | |
paul@105 | 320 | def dequeue_request(self, user, uid): |
paul@105 | 321 | |
paul@105 | 322 | "Dequeue a request for 'user' having the given 'uid'." |
paul@105 | 323 | |
paul@105 | 324 | requests = self.get_requests(user) or [] |
paul@105 | 325 | |
paul@105 | 326 | try: |
paul@105 | 327 | requests.remove(uid) |
paul@105 | 328 | self.set_requests(user, requests) |
paul@105 | 329 | except ValueError: |
paul@105 | 330 | return False |
paul@105 | 331 | else: |
paul@105 | 332 | return True |
paul@105 | 333 | |
paul@142 | 334 | def cancel_event(self, user, uid): |
paul@142 | 335 | |
paul@142 | 336 | "Queue an event for cancellation for 'user' having the given 'uid'." |
paul@142 | 337 | |
paul@142 | 338 | cancellations = self.get_cancellations(user) or [] |
paul@142 | 339 | |
paul@142 | 340 | if uid not in cancellations: |
paul@142 | 341 | return self.set_cancellation(user, uid) |
paul@142 | 342 | |
paul@142 | 343 | return False |
paul@142 | 344 | |
paul@30 | 345 | class FilePublisher(FileBase): |
paul@30 | 346 | |
paul@30 | 347 | "A publisher of objects." |
paul@30 | 348 | |
paul@30 | 349 | def __init__(self, store_dir=PUBLISH_DIR): |
paul@30 | 350 | FileBase.__init__(self, store_dir) |
paul@30 | 351 | |
paul@30 | 352 | def set_freebusy(self, user, freebusy): |
paul@30 | 353 | |
paul@30 | 354 | "For the given 'user', set 'freebusy' details." |
paul@30 | 355 | |
paul@52 | 356 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 357 | if not filename: |
paul@30 | 358 | return False |
paul@30 | 359 | |
paul@30 | 360 | record = [] |
paul@30 | 361 | rwrite = record.append |
paul@30 | 362 | |
paul@30 | 363 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 364 | rwrite(("UID", {}, user)) |
paul@30 | 365 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 366 | |
paul@112 | 367 | for start, end, uid, transp in freebusy: |
paul@119 | 368 | if not transp or transp == "OPAQUE": |
paul@112 | 369 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end]))) |
paul@30 | 370 | |
paul@30 | 371 | f = open(filename, "w") |
paul@30 | 372 | try: |
paul@30 | 373 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 374 | finally: |
paul@30 | 375 | f.close() |
paul@103 | 376 | fix_permissions(filename) |
paul@30 | 377 | |
paul@30 | 378 | return True |
paul@30 | 379 | |
paul@2 | 380 | # vim: tabstop=4 expandtab shiftwidth=4 |