paul@2 | 1 | #!/usr/bin/env python |
paul@2 | 2 | |
paul@30 | 3 | from datetime import datetime |
paul@68 | 4 | from imiptools.config import STORE_DIR, PUBLISH_DIR |
paul@15 | 5 | from os.path import abspath, commonprefix, exists, join, split |
paul@98 | 6 | from os import chmod, makedirs |
paul@15 | 7 | from vCalendar import iterwrite |
paul@2 | 8 | |
paul@2 | 9 | def check_dir(base, dir): |
paul@2 | 10 | return commonprefix([base, abspath(dir)]) == base |
paul@2 | 11 | |
paul@103 | 12 | def fix_permissions(filename): |
paul@103 | 13 | try: |
paul@103 | 14 | chmod(filename, 0660) |
paul@103 | 15 | except OSError: |
paul@103 | 16 | pass |
paul@103 | 17 | |
paul@30 | 18 | def make_calendar(fragment, method=None): |
paul@105 | 19 | |
paul@105 | 20 | """ |
paul@105 | 21 | Return a complete calendar item wrapping the given 'fragment' and employing |
paul@105 | 22 | the given 'method', if indicated. |
paul@105 | 23 | """ |
paul@105 | 24 | |
paul@30 | 25 | return ("VCALENDAR", {}, |
paul@30 | 26 | (method and [("METHOD", {}, method)] or []) + |
paul@30 | 27 | [("VERSION", {}, "2.0")] + |
paul@30 | 28 | fragment |
paul@30 | 29 | ) |
paul@30 | 30 | |
paul@26 | 31 | def to_stream(out, fragment, encoding="utf-8"): |
paul@26 | 32 | iterwrite(out, encoding=encoding).append(fragment) |
paul@15 | 33 | |
paul@30 | 34 | class FileBase: |
paul@2 | 35 | |
paul@30 | 36 | "Basic filesystem operations." |
paul@2 | 37 | |
paul@30 | 38 | def __init__(self, store_dir=STORE_DIR): |
paul@30 | 39 | self.store_dir = store_dir |
paul@2 | 40 | if not exists(self.store_dir): |
paul@2 | 41 | makedirs(self.store_dir) |
paul@2 | 42 | |
paul@15 | 43 | def get_file_object(self, base, *parts): |
paul@15 | 44 | pathname = join(base, *parts) |
paul@15 | 45 | return check_dir(base, pathname) and pathname or None |
paul@2 | 46 | |
paul@52 | 47 | def get_object_in_store(self, *parts): |
paul@52 | 48 | |
paul@52 | 49 | """ |
paul@52 | 50 | Return the name of any valid object stored within a hierarchy specified |
paul@52 | 51 | by the given 'parts'. |
paul@52 | 52 | """ |
paul@52 | 53 | |
paul@52 | 54 | parent = expected = self.store_dir |
paul@15 | 55 | |
paul@52 | 56 | for part in parts: |
paul@52 | 57 | filename = self.get_file_object(expected, part) |
paul@52 | 58 | if not filename: |
paul@52 | 59 | return False |
paul@52 | 60 | parent = expected |
paul@52 | 61 | expected = filename |
paul@15 | 62 | |
paul@52 | 63 | if not exists(parent): |
paul@52 | 64 | makedirs(parent) |
paul@15 | 65 | |
paul@50 | 66 | return filename |
paul@50 | 67 | |
paul@50 | 68 | class FileStore(FileBase): |
paul@50 | 69 | |
paul@50 | 70 | "A file store of tabular free/busy data and objects." |
paul@50 | 71 | |
paul@50 | 72 | def get_event(self, user, uid): |
paul@50 | 73 | |
paul@50 | 74 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 75 | |
paul@52 | 76 | filename = self.get_object_in_store(user, uid) |
paul@50 | 77 | if not filename or not exists(filename): |
paul@50 | 78 | return None |
paul@50 | 79 | |
paul@50 | 80 | return exists(filename) and open(filename) or None |
paul@50 | 81 | |
paul@50 | 82 | def set_event(self, user, uid, node): |
paul@50 | 83 | |
paul@50 | 84 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 85 | |
paul@52 | 86 | filename = self.get_object_in_store(user, uid) |
paul@50 | 87 | if not filename: |
paul@50 | 88 | return False |
paul@50 | 89 | |
paul@15 | 90 | f = open(filename, "w") |
paul@15 | 91 | try: |
paul@26 | 92 | to_stream(f, node) |
paul@15 | 93 | finally: |
paul@15 | 94 | f.close() |
paul@103 | 95 | fix_permissions(filename) |
paul@15 | 96 | |
paul@15 | 97 | return True |
paul@15 | 98 | |
paul@15 | 99 | def get_freebusy(self, user): |
paul@15 | 100 | |
paul@15 | 101 | "Get free/busy details for the given 'user'." |
paul@15 | 102 | |
paul@52 | 103 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 104 | if not filename or not exists(filename): |
paul@2 | 105 | return None |
paul@2 | 106 | |
paul@2 | 107 | f = open(filename) |
paul@2 | 108 | try: |
paul@2 | 109 | l = [] |
paul@2 | 110 | for line in f.readlines(): |
paul@17 | 111 | l.append(tuple(line.strip().split("\t"))) |
paul@2 | 112 | return l |
paul@2 | 113 | finally: |
paul@2 | 114 | f.close() |
paul@2 | 115 | |
paul@15 | 116 | def set_freebusy(self, user, freebusy): |
paul@15 | 117 | |
paul@15 | 118 | "For the given 'user', set 'freebusy' details." |
paul@15 | 119 | |
paul@52 | 120 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 121 | if not filename: |
paul@15 | 122 | return False |
paul@15 | 123 | |
paul@15 | 124 | f = open(filename, "w") |
paul@15 | 125 | try: |
paul@15 | 126 | for item in freebusy: |
paul@15 | 127 | f.write("\t".join(item) + "\n") |
paul@15 | 128 | finally: |
paul@15 | 129 | f.close() |
paul@103 | 130 | fix_permissions(filename) |
paul@15 | 131 | |
paul@15 | 132 | return True |
paul@15 | 133 | |
paul@110 | 134 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@110 | 135 | |
paul@110 | 136 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 137 | |
paul@110 | 138 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 139 | if not filename: |
paul@110 | 140 | return False |
paul@110 | 141 | |
paul@110 | 142 | f = open(filename, "w") |
paul@110 | 143 | try: |
paul@110 | 144 | for item in freebusy: |
paul@110 | 145 | f.write("\t".join(item) + "\n") |
paul@110 | 146 | finally: |
paul@110 | 147 | f.close() |
paul@110 | 148 | fix_permissions(filename) |
paul@110 | 149 | |
paul@110 | 150 | return True |
paul@110 | 151 | |
paul@66 | 152 | def get_requests(self, user): |
paul@66 | 153 | |
paul@66 | 154 | "Get requests for the given 'user'." |
paul@66 | 155 | |
paul@66 | 156 | filename = self.get_object_in_store(user, "requests") |
paul@81 | 157 | if not filename or not exists(filename): |
paul@66 | 158 | return None |
paul@66 | 159 | |
paul@66 | 160 | f = open(filename) |
paul@66 | 161 | try: |
paul@66 | 162 | return [line.strip() for line in f.readlines()] |
paul@66 | 163 | finally: |
paul@66 | 164 | f.close() |
paul@66 | 165 | |
paul@66 | 166 | def set_requests(self, user, requests): |
paul@55 | 167 | |
paul@66 | 168 | "For the given 'user', set the list of queued 'requests'." |
paul@66 | 169 | |
paul@66 | 170 | filename = self.get_object_in_store(user, "requests") |
paul@66 | 171 | if not filename: |
paul@66 | 172 | return False |
paul@66 | 173 | |
paul@66 | 174 | f = open(filename, "w") |
paul@66 | 175 | try: |
paul@66 | 176 | for request in requests: |
paul@66 | 177 | print >>f, request |
paul@66 | 178 | finally: |
paul@66 | 179 | f.close() |
paul@103 | 180 | fix_permissions(filename) |
paul@66 | 181 | |
paul@66 | 182 | return True |
paul@66 | 183 | |
paul@66 | 184 | def set_request(self, user, request): |
paul@66 | 185 | |
paul@66 | 186 | "For the given 'user', set the queued 'request'." |
paul@55 | 187 | |
paul@55 | 188 | filename = self.get_object_in_store(user, "requests") |
paul@55 | 189 | if not filename: |
paul@55 | 190 | return False |
paul@55 | 191 | |
paul@55 | 192 | f = open(filename, "a") |
paul@55 | 193 | try: |
paul@66 | 194 | print >>f, request |
paul@55 | 195 | finally: |
paul@55 | 196 | f.close() |
paul@103 | 197 | fix_permissions(filename) |
paul@55 | 198 | |
paul@55 | 199 | return True |
paul@55 | 200 | |
paul@66 | 201 | def queue_request(self, user, uid): |
paul@66 | 202 | |
paul@66 | 203 | "Queue a request for 'user' having the given 'uid'." |
paul@66 | 204 | |
paul@81 | 205 | requests = self.get_requests(user) or [] |
paul@66 | 206 | |
paul@66 | 207 | if uid not in requests: |
paul@66 | 208 | return self.set_request(user, uid) |
paul@66 | 209 | |
paul@66 | 210 | return False |
paul@66 | 211 | |
paul@105 | 212 | def dequeue_request(self, user, uid): |
paul@105 | 213 | |
paul@105 | 214 | "Dequeue a request for 'user' having the given 'uid'." |
paul@105 | 215 | |
paul@105 | 216 | requests = self.get_requests(user) or [] |
paul@105 | 217 | |
paul@105 | 218 | try: |
paul@105 | 219 | requests.remove(uid) |
paul@105 | 220 | self.set_requests(user, requests) |
paul@105 | 221 | except ValueError: |
paul@105 | 222 | return False |
paul@105 | 223 | else: |
paul@105 | 224 | return True |
paul@105 | 225 | |
paul@30 | 226 | class FilePublisher(FileBase): |
paul@30 | 227 | |
paul@30 | 228 | "A publisher of objects." |
paul@30 | 229 | |
paul@30 | 230 | def __init__(self, store_dir=PUBLISH_DIR): |
paul@30 | 231 | FileBase.__init__(self, store_dir) |
paul@30 | 232 | |
paul@30 | 233 | def set_freebusy(self, user, freebusy): |
paul@30 | 234 | |
paul@30 | 235 | "For the given 'user', set 'freebusy' details." |
paul@30 | 236 | |
paul@52 | 237 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 238 | if not filename: |
paul@30 | 239 | return False |
paul@30 | 240 | |
paul@30 | 241 | record = [] |
paul@30 | 242 | rwrite = record.append |
paul@30 | 243 | |
paul@30 | 244 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 245 | rwrite(("UID", {}, user)) |
paul@30 | 246 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 247 | |
paul@30 | 248 | for start, end, uid in freebusy: |
paul@30 | 249 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end]))) |
paul@30 | 250 | |
paul@30 | 251 | f = open(filename, "w") |
paul@30 | 252 | try: |
paul@30 | 253 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 254 | finally: |
paul@30 | 255 | f.close() |
paul@103 | 256 | fix_permissions(filename) |
paul@30 | 257 | |
paul@30 | 258 | return True |
paul@30 | 259 | |
paul@2 | 260 | # vim: tabstop=4 expandtab shiftwidth=4 |