paul@2 | 1 | #!/usr/bin/env python |
paul@2 | 2 | |
paul@30 | 3 | from datetime import datetime |
paul@15 | 4 | from os.path import abspath, commonprefix, exists, join, split |
paul@2 | 5 | from os import makedirs |
paul@15 | 6 | from vCalendar import iterwrite |
paul@2 | 7 | |
paul@15 | 8 | STORE_DIR = "/var/lib/imip-agent/store" |
paul@30 | 9 | PUBLISH_DIR = "/var/www/imip-agent" |
paul@2 | 10 | |
paul@2 | 11 | def check_dir(base, dir): |
paul@2 | 12 | return commonprefix([base, abspath(dir)]) == base |
paul@2 | 13 | |
paul@30 | 14 | def make_calendar(fragment, method=None): |
paul@30 | 15 | return ("VCALENDAR", {}, |
paul@30 | 16 | (method and [("METHOD", {}, method)] or []) + |
paul@30 | 17 | [("VERSION", {}, "2.0")] + |
paul@30 | 18 | fragment |
paul@30 | 19 | ) |
paul@30 | 20 | |
paul@26 | 21 | def to_stream(out, fragment, encoding="utf-8"): |
paul@26 | 22 | iterwrite(out, encoding=encoding).append(fragment) |
paul@15 | 23 | |
paul@30 | 24 | class FileBase: |
paul@2 | 25 | |
paul@30 | 26 | "Basic filesystem operations." |
paul@2 | 27 | |
paul@30 | 28 | def __init__(self, store_dir=STORE_DIR): |
paul@30 | 29 | self.store_dir = store_dir |
paul@2 | 30 | if not exists(self.store_dir): |
paul@2 | 31 | makedirs(self.store_dir) |
paul@2 | 32 | |
paul@15 | 33 | def get_file_object(self, base, *parts): |
paul@15 | 34 | pathname = join(base, *parts) |
paul@15 | 35 | return check_dir(base, pathname) and pathname or None |
paul@2 | 36 | |
paul@52 | 37 | def get_object_in_store(self, *parts): |
paul@52 | 38 | |
paul@52 | 39 | """ |
paul@52 | 40 | Return the name of any valid object stored within a hierarchy specified |
paul@52 | 41 | by the given 'parts'. |
paul@52 | 42 | """ |
paul@52 | 43 | |
paul@52 | 44 | parent = expected = self.store_dir |
paul@15 | 45 | |
paul@52 | 46 | for part in parts: |
paul@52 | 47 | filename = self.get_file_object(expected, part) |
paul@52 | 48 | if not filename: |
paul@52 | 49 | return False |
paul@52 | 50 | parent = expected |
paul@52 | 51 | expected = filename |
paul@15 | 52 | |
paul@52 | 53 | if not exists(parent): |
paul@52 | 54 | makedirs(parent) |
paul@15 | 55 | |
paul@50 | 56 | return filename |
paul@50 | 57 | |
paul@50 | 58 | class FileStore(FileBase): |
paul@50 | 59 | |
paul@50 | 60 | "A file store of tabular free/busy data and objects." |
paul@50 | 61 | |
paul@50 | 62 | def get_event(self, user, uid): |
paul@50 | 63 | |
paul@50 | 64 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 65 | |
paul@52 | 66 | filename = self.get_object_in_store(user, uid) |
paul@50 | 67 | if not filename or not exists(filename): |
paul@50 | 68 | return None |
paul@50 | 69 | |
paul@50 | 70 | return exists(filename) and open(filename) or None |
paul@50 | 71 | |
paul@50 | 72 | def set_event(self, user, uid, node): |
paul@50 | 73 | |
paul@50 | 74 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 75 | |
paul@52 | 76 | filename = self.get_object_in_store(user, uid) |
paul@50 | 77 | if not filename: |
paul@50 | 78 | return False |
paul@50 | 79 | |
paul@15 | 80 | f = open(filename, "w") |
paul@15 | 81 | try: |
paul@26 | 82 | to_stream(f, node) |
paul@15 | 83 | finally: |
paul@15 | 84 | f.close() |
paul@15 | 85 | |
paul@15 | 86 | return True |
paul@15 | 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@2 | 94 | return None |
paul@2 | 95 | |
paul@2 | 96 | f = open(filename) |
paul@2 | 97 | try: |
paul@2 | 98 | l = [] |
paul@2 | 99 | for line in f.readlines(): |
paul@17 | 100 | l.append(tuple(line.strip().split("\t"))) |
paul@2 | 101 | return l |
paul@2 | 102 | finally: |
paul@2 | 103 | f.close() |
paul@2 | 104 | |
paul@15 | 105 | def set_freebusy(self, user, freebusy): |
paul@15 | 106 | |
paul@15 | 107 | "For the given 'user', set 'freebusy' details." |
paul@15 | 108 | |
paul@52 | 109 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 110 | if not filename: |
paul@15 | 111 | return False |
paul@15 | 112 | |
paul@15 | 113 | f = open(filename, "w") |
paul@15 | 114 | try: |
paul@15 | 115 | for item in freebusy: |
paul@15 | 116 | f.write("\t".join(item) + "\n") |
paul@15 | 117 | finally: |
paul@15 | 118 | f.close() |
paul@15 | 119 | |
paul@15 | 120 | return True |
paul@15 | 121 | |
paul@55 | 122 | def queue_request(self, user, uid): |
paul@55 | 123 | |
paul@55 | 124 | "Queue a request for 'user' having the given 'uid'." |
paul@55 | 125 | |
paul@55 | 126 | filename = self.get_object_in_store(user, "requests") |
paul@55 | 127 | if not filename: |
paul@55 | 128 | return False |
paul@55 | 129 | |
paul@55 | 130 | f = open(filename, "a") |
paul@55 | 131 | try: |
paul@55 | 132 | print >>f, uid |
paul@55 | 133 | finally: |
paul@55 | 134 | f.close() |
paul@55 | 135 | |
paul@55 | 136 | return True |
paul@55 | 137 | |
paul@30 | 138 | class FilePublisher(FileBase): |
paul@30 | 139 | |
paul@30 | 140 | "A publisher of objects." |
paul@30 | 141 | |
paul@30 | 142 | def __init__(self, store_dir=PUBLISH_DIR): |
paul@30 | 143 | FileBase.__init__(self, store_dir) |
paul@30 | 144 | |
paul@30 | 145 | def set_freebusy(self, user, freebusy): |
paul@30 | 146 | |
paul@30 | 147 | "For the given 'user', set 'freebusy' details." |
paul@30 | 148 | |
paul@52 | 149 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 150 | if not filename: |
paul@30 | 151 | return False |
paul@30 | 152 | |
paul@30 | 153 | record = [] |
paul@30 | 154 | rwrite = record.append |
paul@30 | 155 | |
paul@30 | 156 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 157 | rwrite(("UID", {}, user)) |
paul@30 | 158 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 159 | |
paul@30 | 160 | for start, end, uid in freebusy: |
paul@30 | 161 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end]))) |
paul@30 | 162 | |
paul@30 | 163 | f = open(filename, "w") |
paul@30 | 164 | try: |
paul@30 | 165 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 166 | finally: |
paul@30 | 167 | f.close() |
paul@30 | 168 | |
paul@30 | 169 | return True |
paul@30 | 170 | |
paul@2 | 171 | # vim: tabstop=4 expandtab shiftwidth=4 |