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