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@119 | 24 | from os.path import abspath, commonprefix, exists, isfile, join, split |
paul@119 | 25 | from os import chmod, listdir, makedirs |
paul@15 | 26 | from vCalendar import iterwrite |
paul@2 | 27 | |
paul@2 | 28 | def check_dir(base, dir): |
paul@2 | 29 | return commonprefix([base, abspath(dir)]) == base |
paul@2 | 30 | |
paul@103 | 31 | def fix_permissions(filename): |
paul@103 | 32 | try: |
paul@103 | 33 | chmod(filename, 0660) |
paul@103 | 34 | except OSError: |
paul@103 | 35 | pass |
paul@103 | 36 | |
paul@30 | 37 | def make_calendar(fragment, method=None): |
paul@105 | 38 | |
paul@105 | 39 | """ |
paul@105 | 40 | Return a complete calendar item wrapping the given 'fragment' and employing |
paul@105 | 41 | the given 'method', if indicated. |
paul@105 | 42 | """ |
paul@105 | 43 | |
paul@30 | 44 | return ("VCALENDAR", {}, |
paul@30 | 45 | (method and [("METHOD", {}, method)] or []) + |
paul@30 | 46 | [("VERSION", {}, "2.0")] + |
paul@30 | 47 | fragment |
paul@30 | 48 | ) |
paul@30 | 49 | |
paul@26 | 50 | def to_stream(out, fragment, encoding="utf-8"): |
paul@26 | 51 | iterwrite(out, encoding=encoding).append(fragment) |
paul@15 | 52 | |
paul@30 | 53 | class FileBase: |
paul@2 | 54 | |
paul@30 | 55 | "Basic filesystem operations." |
paul@2 | 56 | |
paul@30 | 57 | def __init__(self, store_dir=STORE_DIR): |
paul@30 | 58 | self.store_dir = store_dir |
paul@2 | 59 | if not exists(self.store_dir): |
paul@2 | 60 | makedirs(self.store_dir) |
paul@2 | 61 | |
paul@15 | 62 | def get_file_object(self, base, *parts): |
paul@15 | 63 | pathname = join(base, *parts) |
paul@15 | 64 | return check_dir(base, pathname) and pathname or None |
paul@2 | 65 | |
paul@52 | 66 | def get_object_in_store(self, *parts): |
paul@52 | 67 | |
paul@52 | 68 | """ |
paul@52 | 69 | Return the name of any valid object stored within a hierarchy specified |
paul@52 | 70 | by the given 'parts'. |
paul@52 | 71 | """ |
paul@52 | 72 | |
paul@52 | 73 | parent = expected = self.store_dir |
paul@15 | 74 | |
paul@52 | 75 | for part in parts: |
paul@52 | 76 | filename = self.get_file_object(expected, part) |
paul@52 | 77 | if not filename: |
paul@52 | 78 | return False |
paul@52 | 79 | parent = expected |
paul@52 | 80 | expected = filename |
paul@15 | 81 | |
paul@52 | 82 | if not exists(parent): |
paul@52 | 83 | makedirs(parent) |
paul@15 | 84 | |
paul@50 | 85 | return filename |
paul@50 | 86 | |
paul@50 | 87 | class FileStore(FileBase): |
paul@50 | 88 | |
paul@50 | 89 | "A file store of tabular free/busy data and objects." |
paul@50 | 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@119 | 109 | return open(filename) or None |
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@15 | 119 | f = open(filename, "w") |
paul@15 | 120 | try: |
paul@26 | 121 | to_stream(f, node) |
paul@15 | 122 | finally: |
paul@15 | 123 | f.close() |
paul@103 | 124 | fix_permissions(filename) |
paul@15 | 125 | |
paul@15 | 126 | return True |
paul@15 | 127 | |
paul@15 | 128 | def get_freebusy(self, user): |
paul@15 | 129 | |
paul@15 | 130 | "Get free/busy details for the given 'user'." |
paul@15 | 131 | |
paul@52 | 132 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 133 | if not filename or not exists(filename): |
paul@2 | 134 | return None |
paul@112 | 135 | else: |
paul@112 | 136 | return self._get_freebusy(filename) |
paul@2 | 137 | |
paul@112 | 138 | def get_freebusy_for_other(self, user, other): |
paul@112 | 139 | |
paul@112 | 140 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 141 | |
paul@112 | 142 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@112 | 143 | if not filename: |
paul@112 | 144 | return None |
paul@112 | 145 | else: |
paul@112 | 146 | return self._get_freebusy(filename) |
paul@112 | 147 | |
paul@112 | 148 | def _get_freebusy(self, filename): |
paul@2 | 149 | f = open(filename) |
paul@2 | 150 | try: |
paul@2 | 151 | l = [] |
paul@2 | 152 | for line in f.readlines(): |
paul@17 | 153 | l.append(tuple(line.strip().split("\t"))) |
paul@2 | 154 | return l |
paul@2 | 155 | finally: |
paul@2 | 156 | f.close() |
paul@2 | 157 | |
paul@15 | 158 | def set_freebusy(self, user, freebusy): |
paul@15 | 159 | |
paul@15 | 160 | "For the given 'user', set 'freebusy' details." |
paul@15 | 161 | |
paul@52 | 162 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 163 | if not filename: |
paul@15 | 164 | return False |
paul@15 | 165 | |
paul@112 | 166 | self._set_freebusy(filename, freebusy) |
paul@15 | 167 | return True |
paul@15 | 168 | |
paul@110 | 169 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@110 | 170 | |
paul@110 | 171 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 172 | |
paul@110 | 173 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 174 | if not filename: |
paul@110 | 175 | return False |
paul@110 | 176 | |
paul@112 | 177 | self._set_freebusy(filename, freebusy) |
paul@112 | 178 | return True |
paul@112 | 179 | |
paul@112 | 180 | def _set_freebusy(self, filename, freebusy): |
paul@110 | 181 | f = open(filename, "w") |
paul@110 | 182 | try: |
paul@110 | 183 | for item in freebusy: |
paul@119 | 184 | f.write("\t".join([(value or "OPAQUE") for value in item]) + "\n") |
paul@110 | 185 | finally: |
paul@110 | 186 | f.close() |
paul@110 | 187 | fix_permissions(filename) |
paul@110 | 188 | |
paul@142 | 189 | def _get_requests(self, user, queue): |
paul@66 | 190 | |
paul@142 | 191 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 192 | |
paul@142 | 193 | filename = self.get_object_in_store(user, queue) |
paul@81 | 194 | if not filename or not exists(filename): |
paul@66 | 195 | return None |
paul@66 | 196 | |
paul@66 | 197 | f = open(filename) |
paul@66 | 198 | try: |
paul@66 | 199 | return [line.strip() for line in f.readlines()] |
paul@66 | 200 | finally: |
paul@66 | 201 | f.close() |
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@66 | 226 | f = open(filename, "w") |
paul@66 | 227 | try: |
paul@66 | 228 | for request in requests: |
paul@66 | 229 | print >>f, request |
paul@66 | 230 | finally: |
paul@66 | 231 | f.close() |
paul@103 | 232 | fix_permissions(filename) |
paul@66 | 233 | |
paul@66 | 234 | return True |
paul@66 | 235 | |
paul@142 | 236 | def set_requests(self, user, requests): |
paul@142 | 237 | |
paul@142 | 238 | "For the given 'user', set the list of queued 'requests'." |
paul@142 | 239 | |
paul@142 | 240 | return self._set_requests(user, requests, "requests") |
paul@142 | 241 | |
paul@142 | 242 | def set_cancellations(self, user, cancellations): |
paul@66 | 243 | |
paul@142 | 244 | "For the given 'user', set the list of queued 'cancellations'." |
paul@142 | 245 | |
paul@142 | 246 | return self._set_requests(user, cancellations, "cancellations") |
paul@55 | 247 | |
paul@142 | 248 | def _set_request(self, user, request, queue): |
paul@142 | 249 | |
paul@142 | 250 | "For the given 'user', set the queued 'request' in the given 'queue'." |
paul@142 | 251 | |
paul@142 | 252 | filename = self.get_object_in_store(user, queue) |
paul@55 | 253 | if not filename: |
paul@55 | 254 | return False |
paul@55 | 255 | |
paul@55 | 256 | f = open(filename, "a") |
paul@55 | 257 | try: |
paul@66 | 258 | print >>f, request |
paul@55 | 259 | finally: |
paul@55 | 260 | f.close() |
paul@103 | 261 | fix_permissions(filename) |
paul@55 | 262 | |
paul@55 | 263 | return True |
paul@55 | 264 | |
paul@142 | 265 | def set_request(self, user, request): |
paul@142 | 266 | |
paul@142 | 267 | "For the given 'user', set the queued 'request'." |
paul@142 | 268 | |
paul@142 | 269 | return self._set_request(user, request, "requests") |
paul@142 | 270 | |
paul@142 | 271 | def set_cancellation(self, user, cancellation): |
paul@142 | 272 | |
paul@142 | 273 | "For the given 'user', set the queued 'cancellation'." |
paul@142 | 274 | |
paul@142 | 275 | return self._set_request(user, cancellation, "cancellations") |
paul@142 | 276 | |
paul@66 | 277 | def queue_request(self, user, uid): |
paul@66 | 278 | |
paul@66 | 279 | "Queue a request for 'user' having the given 'uid'." |
paul@66 | 280 | |
paul@81 | 281 | requests = self.get_requests(user) or [] |
paul@66 | 282 | |
paul@66 | 283 | if uid not in requests: |
paul@66 | 284 | return self.set_request(user, uid) |
paul@66 | 285 | |
paul@66 | 286 | return False |
paul@66 | 287 | |
paul@105 | 288 | def dequeue_request(self, user, uid): |
paul@105 | 289 | |
paul@105 | 290 | "Dequeue a request for 'user' having the given 'uid'." |
paul@105 | 291 | |
paul@105 | 292 | requests = self.get_requests(user) or [] |
paul@105 | 293 | |
paul@105 | 294 | try: |
paul@105 | 295 | requests.remove(uid) |
paul@105 | 296 | self.set_requests(user, requests) |
paul@105 | 297 | except ValueError: |
paul@105 | 298 | return False |
paul@105 | 299 | else: |
paul@105 | 300 | return True |
paul@105 | 301 | |
paul@142 | 302 | def cancel_event(self, user, uid): |
paul@142 | 303 | |
paul@142 | 304 | "Queue an event for cancellation for 'user' having the given 'uid'." |
paul@142 | 305 | |
paul@142 | 306 | cancellations = self.get_cancellations(user) or [] |
paul@142 | 307 | |
paul@142 | 308 | if uid not in cancellations: |
paul@142 | 309 | return self.set_cancellation(user, uid) |
paul@142 | 310 | |
paul@142 | 311 | return False |
paul@142 | 312 | |
paul@30 | 313 | class FilePublisher(FileBase): |
paul@30 | 314 | |
paul@30 | 315 | "A publisher of objects." |
paul@30 | 316 | |
paul@30 | 317 | def __init__(self, store_dir=PUBLISH_DIR): |
paul@30 | 318 | FileBase.__init__(self, store_dir) |
paul@30 | 319 | |
paul@30 | 320 | def set_freebusy(self, user, freebusy): |
paul@30 | 321 | |
paul@30 | 322 | "For the given 'user', set 'freebusy' details." |
paul@30 | 323 | |
paul@52 | 324 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 325 | if not filename: |
paul@30 | 326 | return False |
paul@30 | 327 | |
paul@30 | 328 | record = [] |
paul@30 | 329 | rwrite = record.append |
paul@30 | 330 | |
paul@30 | 331 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 332 | rwrite(("UID", {}, user)) |
paul@30 | 333 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 334 | |
paul@112 | 335 | for start, end, uid, transp in freebusy: |
paul@119 | 336 | if not transp or transp == "OPAQUE": |
paul@112 | 337 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end]))) |
paul@30 | 338 | |
paul@30 | 339 | f = open(filename, "w") |
paul@30 | 340 | try: |
paul@30 | 341 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 342 | finally: |
paul@30 | 343 | f.close() |
paul@103 | 344 | fix_permissions(filename) |
paul@30 | 345 | |
paul@30 | 346 | return True |
paul@30 | 347 | |
paul@2 | 348 | # vim: tabstop=4 expandtab shiftwidth=4 |