imip-agent

Annotated imip_store.py

2:f5795f1b7ff9
2014-09-22 Paul Boddie Fixed conversion to dictionary of calendar data. Restructured the handler mechanism. Added a simple store implementation.
paul@2 1
#!/usr/bin/env python
paul@2 2
paul@2 3
from os.path import abspath, commonprefix, exists, join
paul@2 4
from os import makedirs
paul@2 5
paul@2 6
STORE_DIR = "/tmp/imip"
paul@2 7
paul@2 8
def check_dir(base, dir):
paul@2 9
    return commonprefix([base, abspath(dir)]) == base
paul@2 10
paul@2 11
class FileStore:
paul@2 12
paul@2 13
    "A file store of tabular data."
paul@2 14
paul@2 15
    def __init__(self):
paul@2 16
        self.store_dir = STORE_DIR
paul@2 17
        if not exists(self.store_dir):
paul@2 18
            makedirs(self.store_dir)
paul@2 19
paul@2 20
    def get_freebusy(self, calendar):
paul@2 21
paul@2 22
        "Get free/busy details from the given 'calendar'."
paul@2 23
paul@2 24
        dir = join(self.store_dir, calendar)
paul@2 25
        if not check_dir(self.store_dir, dir):
paul@2 26
            return None
paul@2 27
paul@2 28
        filename = join(dir, "freebusy")
paul@2 29
        if not exists(filename):
paul@2 30
            return None
paul@2 31
paul@2 32
        f = open(filename)
paul@2 33
        try:
paul@2 34
            l = []
paul@2 35
            for line in f.readlines():
paul@2 36
                l.append(line.strip().split("\t"))
paul@2 37
            return l
paul@2 38
        finally:
paul@2 39
            f.close()
paul@2 40
paul@2 41
# vim: tabstop=4 expandtab shiftwidth=4