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