import repository from arizona
[raven.git] / apps / gacks / gackscalendar_list.py
1 import time
2 from gackscalendar import *
3
4 class GacksListCalendar(GacksCalendar):
5     def __init__(self):
6         GacksCalendar.__init__(self)
7         self.filename = "calendar.txt"
8         self.items = []
9
10     def reset(self):
11         self.items = []
12
13     def query_overlap(self, id=None, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY, hasAllocator=None, isLastAllocator=None):
14         list = []
15         for item in self.items:
16             if self.test_id(id, item.id) and \
17                self.test_lesser(unitStart, item.unitStop) and \
18                self.test_greater(unitStop, item.unitStart) and \
19                self.test_lesser(timeStart, item.timeStop) and \
20                self.test_greater(timeStop, item.timeStart) and \
21                self.test_allocator(hasAllocator, item.allocatorHRNs) and \
22                self.test_last_allocator(isLastAllocator, item.allocatorHRNs):
23                  list.append(item)
24         return list
25
26     def query_exact(self, id=None, unitStart=None, unitStop=None, timeStart=None, timeStop=None, hasAllocator=None, isLastAllocator=None):
27         list = []
28         for item in self.items:
29             if self.test_id(id, item.id) and \
30                self.test_equal(unitStart, item.unitStart) and \
31                self.test_equal(unitStop, item.unitStop) and \
32                self.test_equal(timeStart, item.timeStart) and \
33                self.test_equal(timeStop, item.timeStop) and \
34                self.test_allocator(hasAllocator, item.allocatorHRNs) and \
35                self.test_last_allocator(isLastAllocator, item.allocatorHRNs):
36                  list.append(item)
37         return list
38
39     def garbage_collect(self, timeStop=None):
40         if not timeStop:
41             timeStop = time.time()
42         new_items = []
43         for item in self.items:
44             if self.test_equal(item.timeStop, timeStop) or self.test_greater(item.timeStop, timeStop):
45                 new_items.append(item)
46         self.items = new_items
47
48     def find_record(self, item_to_delete):
49         list = []
50         for item in self.items:
51             if item.is_same_cell(item_to_delete):
52                 list.append(item)
53
54         if not list:
55             return None
56
57         if len(list) > 1:
58             raise GacksMultipleRecordCollision(item_to_delete.as_string())
59
60         return list[0]
61
62     def insert_record(self, item):
63         conflicts = self.query_overlap(item.id, item.unitStart, item.unitStop, item.timeStart, item.timeStop)
64         if conflicts:
65             raise GacksConflictingInsert(item.as_string())
66
67         self.items.append(item)
68
69     def remove_record(self, item):
70          existing_record = self.find_record(item)
71          if existing_record:
72              self.items.remove(existing_record)
73
74     def is_empty(self):
75         return (not self.items)
76
77     def save(self):
78         itemDicts = []
79         for item in self.items:
80             itemDicts.append(item.as_dict())
81
82         f = open(self.filename, "wt")
83         f.write(str(itemDicts))
84         f.close()
85
86     def open(self):
87         if os.path.exists(self.filename):
88             f = open(self.filename, "rt")
89             itemDictsStr = f.read()
90             f.close()
91
92             itemDicts = eval(itemDictsStr)
93             items = []
94             for itemDict in itemDicts:
95                 item = GacksRecord(dict = itemDict)
96                 items.append(item)
97
98             self.items = items
99
100