1.1 --- a/imip_store.py Thu Sep 25 23:21:21 2014 +0200
1.2 +++ b/imip_store.py Wed Oct 01 00:02:17 2014 +0200
1.3 @@ -1,23 +1,32 @@
1.4 #!/usr/bin/env python
1.5
1.6 +from datetime import datetime
1.7 from os.path import abspath, commonprefix, exists, join, split
1.8 from os import makedirs
1.9 from vCalendar import iterwrite
1.10
1.11 STORE_DIR = "/var/lib/imip-agent/store"
1.12 +PUBLISH_DIR = "/var/www/imip-agent"
1.13
1.14 def check_dir(base, dir):
1.15 return commonprefix([base, abspath(dir)]) == base
1.16
1.17 +def make_calendar(fragment, method=None):
1.18 + return ("VCALENDAR", {},
1.19 + (method and [("METHOD", {}, method)] or []) +
1.20 + [("VERSION", {}, "2.0")] +
1.21 + fragment
1.22 + )
1.23 +
1.24 def to_stream(out, fragment, encoding="utf-8"):
1.25 iterwrite(out, encoding=encoding).append(fragment)
1.26
1.27 -class FileStore:
1.28 +class FileBase:
1.29
1.30 - "A file store of tabular data."
1.31 + "Basic filesystem operations."
1.32
1.33 - def __init__(self):
1.34 - self.store_dir = STORE_DIR
1.35 + def __init__(self, store_dir=STORE_DIR):
1.36 + self.store_dir = store_dir
1.37 if not exists(self.store_dir):
1.38 makedirs(self.store_dir)
1.39
1.40 @@ -25,6 +34,10 @@
1.41 pathname = join(base, *parts)
1.42 return check_dir(base, pathname) and pathname or None
1.43
1.44 +class FileStore(FileBase):
1.45 +
1.46 + "A file store of tabular free/busy data and objects."
1.47 +
1.48 def get_event(self, user, uid):
1.49
1.50 "Get the event for the given 'user' with the given 'uid'."
1.51 @@ -97,4 +110,42 @@
1.52
1.53 return True
1.54
1.55 +class FilePublisher(FileBase):
1.56 +
1.57 + "A publisher of objects."
1.58 +
1.59 + def __init__(self, store_dir=PUBLISH_DIR):
1.60 + FileBase.__init__(self, store_dir)
1.61 +
1.62 + def set_freebusy(self, user, freebusy):
1.63 +
1.64 + "For the given 'user', set 'freebusy' details."
1.65 +
1.66 + filename = self.get_file_object(self.store_dir, user, "freebusy")
1.67 + if not filename:
1.68 + return False
1.69 +
1.70 + dir = split(filename)[0]
1.71 +
1.72 + if not exists(dir):
1.73 + makedirs(dir)
1.74 +
1.75 + record = []
1.76 + rwrite = record.append
1.77 +
1.78 + rwrite(("ORGANIZER", {}, user))
1.79 + rwrite(("UID", {}, user))
1.80 + rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")))
1.81 +
1.82 + for start, end, uid in freebusy:
1.83 + rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end])))
1.84 +
1.85 + f = open(filename, "w")
1.86 + try:
1.87 + to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH"))
1.88 + finally:
1.89 + f.close()
1.90 +
1.91 + return True
1.92 +
1.93 # vim: tabstop=4 expandtab shiftwidth=4