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