paul@41 | 1 | # -*- coding: iso-8859-1 -*- |
paul@41 | 2 | """ |
paul@41 | 3 | MoinMoin - ItemSupport library |
paul@41 | 4 | |
paul@41 | 5 | @copyright: 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@41 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@41 | 7 | """ |
paul@41 | 8 | |
paul@41 | 9 | from MoinMoin.util import lock |
paul@41 | 10 | import os |
paul@41 | 11 | |
paul@41 | 12 | # Content storage support. |
paul@41 | 13 | |
paul@92 | 14 | class GeneralItemStore: |
paul@91 | 15 | |
paul@91 | 16 | "Common item store functionality." |
paul@91 | 17 | |
paul@91 | 18 | def __init__(self, lock_dir): |
paul@91 | 19 | |
paul@91 | 20 | "Initialise an item store with the given 'lock_dir' guarding access." |
paul@91 | 21 | |
paul@91 | 22 | self.lock_dir = lock_dir |
paul@91 | 23 | self.writelock = lock.WriteLock(lock_dir) |
paul@91 | 24 | self.readlock = lock.ReadLock(lock_dir) |
paul@91 | 25 | |
paul@91 | 26 | def deduce_next(self): |
paul@91 | 27 | |
paul@91 | 28 | "Deduce the next item number from the existing item files." |
paul@91 | 29 | |
paul@91 | 30 | return max(self.get_keys() or [-1]) + 1 |
paul@91 | 31 | |
paul@91 | 32 | # High-level methods. |
paul@91 | 33 | |
paul@91 | 34 | def __len__(self): |
paul@91 | 35 | |
paul@91 | 36 | """ |
paul@91 | 37 | Return the number of items. |
paul@91 | 38 | """ |
paul@91 | 39 | |
paul@91 | 40 | return len(self.keys()) |
paul@91 | 41 | |
paul@91 | 42 | def __iter__(self): |
paul@91 | 43 | |
paul@91 | 44 | "Return an iterator over the items in the store." |
paul@91 | 45 | |
paul@91 | 46 | return ItemIterator(self) |
paul@91 | 47 | |
paul@91 | 48 | def keys(self): |
paul@91 | 49 | |
paul@91 | 50 | "Return a list of keys for items in the store." |
paul@91 | 51 | |
paul@91 | 52 | self.readlock.acquire() |
paul@91 | 53 | try: |
paul@91 | 54 | return self.get_keys() |
paul@91 | 55 | finally: |
paul@91 | 56 | self.readlock.release() |
paul@91 | 57 | |
paul@91 | 58 | def __getitem__(self, number): |
paul@91 | 59 | |
paul@91 | 60 | "Return the item with the given 'number'." |
paul@91 | 61 | |
paul@91 | 62 | self.readlock.acquire() |
paul@91 | 63 | try: |
paul@91 | 64 | try: |
paul@91 | 65 | return self.read_item(number) |
paul@91 | 66 | except (IOError, OSError): |
paul@91 | 67 | raise IndexError, number |
paul@91 | 68 | finally: |
paul@91 | 69 | self.readlock.release() |
paul@91 | 70 | |
paul@91 | 71 | def __delitem__(self, number): |
paul@91 | 72 | |
paul@91 | 73 | "Remove the item with the given 'number' from the store." |
paul@91 | 74 | |
paul@91 | 75 | self.writelock.acquire() |
paul@91 | 76 | try: |
paul@91 | 77 | try: |
paul@91 | 78 | self.remove_item(number) |
paul@91 | 79 | except (IOError, OSError): |
paul@91 | 80 | raise IndexError, number |
paul@91 | 81 | finally: |
paul@91 | 82 | self.writelock.release() |
paul@91 | 83 | |
paul@91 | 84 | def next(self): |
paul@91 | 85 | |
paul@91 | 86 | """ |
paul@91 | 87 | Return the number of the next item (which should also be the number of |
paul@91 | 88 | items if none have been deleted). |
paul@91 | 89 | """ |
paul@91 | 90 | |
paul@91 | 91 | self.writelock.acquire() |
paul@91 | 92 | try: |
paul@91 | 93 | return self.get_next() |
paul@91 | 94 | finally: |
paul@91 | 95 | self.writelock.release() |
paul@91 | 96 | |
paul@92 | 97 | class DirectoryItemStore(GeneralItemStore): |
paul@41 | 98 | |
paul@41 | 99 | "A directory-based item store." |
paul@41 | 100 | |
paul@41 | 101 | def __init__(self, path, lock_dir): |
paul@41 | 102 | |
paul@41 | 103 | "Initialise an item store for the given 'path' and 'lock_dir'." |
paul@41 | 104 | |
paul@41 | 105 | self.path = path |
paul@41 | 106 | self.next_path = os.path.join(self.path, "next") |
paul@41 | 107 | self.lock_dir = lock_dir |
paul@41 | 108 | self.writelock = lock.WriteLock(lock_dir) |
paul@41 | 109 | self.readlock = lock.ReadLock(lock_dir) |
paul@41 | 110 | |
paul@47 | 111 | def mtime(self): |
paul@47 | 112 | |
paul@47 | 113 | "Return the last modified time of the item store directory." |
paul@47 | 114 | |
paul@47 | 115 | return os.path.getmtime(self.path) |
paul@47 | 116 | |
paul@41 | 117 | def get_next(self): |
paul@41 | 118 | |
paul@41 | 119 | "Return the next item number." |
paul@41 | 120 | |
paul@41 | 121 | next = self.read_next() |
paul@41 | 122 | if next is None: |
paul@41 | 123 | next = self.deduce_next() |
paul@41 | 124 | self.write_next(next) |
paul@41 | 125 | return next |
paul@41 | 126 | |
paul@63 | 127 | def get_keys(self): |
paul@63 | 128 | |
paul@63 | 129 | "Return the item keys." |
paul@63 | 130 | |
paul@63 | 131 | return [int(filename) for filename in os.listdir(self.path) if filename.isdigit()] |
paul@63 | 132 | |
paul@41 | 133 | def read_next(self): |
paul@41 | 134 | |
paul@41 | 135 | "Read the next item number from a special file." |
paul@41 | 136 | |
paul@41 | 137 | if not os.path.exists(self.next_path): |
paul@41 | 138 | return None |
paul@41 | 139 | |
paul@41 | 140 | f = open(self.next_path) |
paul@41 | 141 | try: |
paul@41 | 142 | try: |
paul@41 | 143 | return int(f.read()) |
paul@41 | 144 | except ValueError: |
paul@41 | 145 | return None |
paul@41 | 146 | finally: |
paul@41 | 147 | f.close() |
paul@41 | 148 | |
paul@41 | 149 | def write_next(self, next): |
paul@41 | 150 | |
paul@41 | 151 | "Write the 'next' item number to a special file." |
paul@41 | 152 | |
paul@41 | 153 | f = open(self.next_path, "w") |
paul@41 | 154 | try: |
paul@41 | 155 | f.write(str(next)) |
paul@41 | 156 | finally: |
paul@41 | 157 | f.close() |
paul@41 | 158 | |
paul@41 | 159 | def write_item(self, item, next): |
paul@41 | 160 | |
paul@41 | 161 | "Write the given 'item' to a file with the given 'next' item number." |
paul@41 | 162 | |
paul@41 | 163 | f = open(self.get_item_path(next), "w") |
paul@41 | 164 | try: |
paul@41 | 165 | f.write(item) |
paul@41 | 166 | finally: |
paul@41 | 167 | f.close() |
paul@41 | 168 | |
paul@41 | 169 | def read_item(self, number): |
paul@41 | 170 | |
paul@41 | 171 | "Read the item with the given item 'number'." |
paul@41 | 172 | |
paul@41 | 173 | f = open(self.get_item_path(number)) |
paul@41 | 174 | try: |
paul@41 | 175 | return f.read() |
paul@41 | 176 | finally: |
paul@41 | 177 | f.close() |
paul@41 | 178 | |
paul@63 | 179 | def remove_item(self, number): |
paul@63 | 180 | |
paul@63 | 181 | "Remove the item with the given item 'number'." |
paul@63 | 182 | |
paul@63 | 183 | os.remove(self.get_item_path(number)) |
paul@63 | 184 | |
paul@41 | 185 | def get_item_path(self, number): |
paul@41 | 186 | |
paul@41 | 187 | "Get the path for the given item 'number'." |
paul@41 | 188 | |
paul@41 | 189 | path = os.path.abspath(os.path.join(self.path, str(number))) |
paul@41 | 190 | basepath = os.path.join(self.path, "") |
paul@41 | 191 | |
paul@41 | 192 | if os.path.commonprefix([path, basepath]) != basepath: |
paul@41 | 193 | raise OSError, path |
paul@41 | 194 | |
paul@41 | 195 | return path |
paul@41 | 196 | |
paul@41 | 197 | # High-level methods. |
paul@41 | 198 | |
paul@41 | 199 | def append(self, item): |
paul@41 | 200 | |
paul@41 | 201 | "Append the given 'item' to the store." |
paul@41 | 202 | |
paul@41 | 203 | self.writelock.acquire() |
paul@41 | 204 | try: |
paul@41 | 205 | next = self.get_next() |
paul@41 | 206 | self.write_item(item, next) |
paul@41 | 207 | self.write_next(next + 1) |
paul@41 | 208 | finally: |
paul@41 | 209 | self.writelock.release() |
paul@41 | 210 | |
paul@41 | 211 | class ItemIterator: |
paul@41 | 212 | |
paul@41 | 213 | "An iterator over items in a store." |
paul@41 | 214 | |
paul@46 | 215 | def __init__(self, store, direction=1): |
paul@41 | 216 | self.store = store |
paul@46 | 217 | self.direction = direction |
paul@46 | 218 | self.reset() |
paul@46 | 219 | |
paul@46 | 220 | def reset(self): |
paul@46 | 221 | if self.direction == 1: |
paul@46 | 222 | self._next = 0 |
paul@63 | 223 | self.final = self.store.next() |
paul@46 | 224 | else: |
paul@63 | 225 | self._next = self.store.next() - 1 |
paul@46 | 226 | self.final = 0 |
paul@46 | 227 | |
paul@46 | 228 | def more(self): |
paul@46 | 229 | if self.direction == 1: |
paul@46 | 230 | return self._next < self.final |
paul@46 | 231 | else: |
paul@46 | 232 | return self._next >= self.final |
paul@46 | 233 | |
paul@46 | 234 | def get_next(self): |
paul@46 | 235 | next = self._next |
paul@46 | 236 | self._next += self.direction |
paul@46 | 237 | return next |
paul@41 | 238 | |
paul@41 | 239 | def next(self): |
paul@46 | 240 | while self.more(): |
paul@41 | 241 | try: |
paul@46 | 242 | return self.store[self.get_next()] |
paul@41 | 243 | except IndexError: |
paul@41 | 244 | pass |
paul@41 | 245 | |
paul@41 | 246 | raise StopIteration |
paul@41 | 247 | |
paul@46 | 248 | def reverse(self): |
paul@46 | 249 | self.direction = -self.direction |
paul@46 | 250 | self.reset() |
paul@46 | 251 | |
paul@46 | 252 | def reversed(self): |
paul@46 | 253 | self.reverse() |
paul@46 | 254 | return self |
paul@46 | 255 | |
paul@46 | 256 | def __iter__(self): |
paul@46 | 257 | return self |
paul@46 | 258 | |
paul@41 | 259 | # vim: tabstop=4 expandtab shiftwidth=4 |