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@41 | 14 | class ItemDirectoryStore: |
paul@41 | 15 | |
paul@41 | 16 | "A directory-based item store." |
paul@41 | 17 | |
paul@41 | 18 | def __init__(self, path, lock_dir): |
paul@41 | 19 | |
paul@41 | 20 | "Initialise an item store for the given 'path' and 'lock_dir'." |
paul@41 | 21 | |
paul@41 | 22 | self.path = path |
paul@41 | 23 | self.next_path = os.path.join(self.path, "next") |
paul@41 | 24 | self.lock_dir = lock_dir |
paul@41 | 25 | self.writelock = lock.WriteLock(lock_dir) |
paul@41 | 26 | self.readlock = lock.ReadLock(lock_dir) |
paul@41 | 27 | |
paul@47 | 28 | def mtime(self): |
paul@47 | 29 | |
paul@47 | 30 | "Return the last modified time of the item store directory." |
paul@47 | 31 | |
paul@47 | 32 | return os.path.getmtime(self.path) |
paul@47 | 33 | |
paul@41 | 34 | def get_next(self): |
paul@41 | 35 | |
paul@41 | 36 | "Return the next item number." |
paul@41 | 37 | |
paul@41 | 38 | next = self.read_next() |
paul@41 | 39 | if next is None: |
paul@41 | 40 | next = self.deduce_next() |
paul@41 | 41 | self.write_next(next) |
paul@41 | 42 | return next |
paul@41 | 43 | |
paul@41 | 44 | def deduce_next(self): |
paul@41 | 45 | |
paul@41 | 46 | "Deduce the next item number from the existing item files." |
paul@41 | 47 | |
paul@41 | 48 | return max([int(filename) for filename in os.listdir(self.path) if filename.isdigit()] or [-1]) + 1 |
paul@41 | 49 | |
paul@41 | 50 | def read_next(self): |
paul@41 | 51 | |
paul@41 | 52 | "Read the next item number from a special file." |
paul@41 | 53 | |
paul@41 | 54 | if not os.path.exists(self.next_path): |
paul@41 | 55 | return None |
paul@41 | 56 | |
paul@41 | 57 | f = open(self.next_path) |
paul@41 | 58 | try: |
paul@41 | 59 | try: |
paul@41 | 60 | return int(f.read()) |
paul@41 | 61 | except ValueError: |
paul@41 | 62 | return None |
paul@41 | 63 | finally: |
paul@41 | 64 | f.close() |
paul@41 | 65 | |
paul@41 | 66 | def write_next(self, next): |
paul@41 | 67 | |
paul@41 | 68 | "Write the 'next' item number to a special file." |
paul@41 | 69 | |
paul@41 | 70 | f = open(self.next_path, "w") |
paul@41 | 71 | try: |
paul@41 | 72 | f.write(str(next)) |
paul@41 | 73 | finally: |
paul@41 | 74 | f.close() |
paul@41 | 75 | |
paul@41 | 76 | def write_item(self, item, next): |
paul@41 | 77 | |
paul@41 | 78 | "Write the given 'item' to a file with the given 'next' item number." |
paul@41 | 79 | |
paul@41 | 80 | f = open(self.get_item_path(next), "w") |
paul@41 | 81 | try: |
paul@41 | 82 | f.write(item) |
paul@41 | 83 | finally: |
paul@41 | 84 | f.close() |
paul@41 | 85 | |
paul@41 | 86 | def read_item(self, number): |
paul@41 | 87 | |
paul@41 | 88 | "Read the item with the given item 'number'." |
paul@41 | 89 | |
paul@41 | 90 | f = open(self.get_item_path(number)) |
paul@41 | 91 | try: |
paul@41 | 92 | return f.read() |
paul@41 | 93 | finally: |
paul@41 | 94 | f.close() |
paul@41 | 95 | |
paul@41 | 96 | def get_item_path(self, number): |
paul@41 | 97 | |
paul@41 | 98 | "Get the path for the given item 'number'." |
paul@41 | 99 | |
paul@41 | 100 | path = os.path.abspath(os.path.join(self.path, str(number))) |
paul@41 | 101 | basepath = os.path.join(self.path, "") |
paul@41 | 102 | |
paul@41 | 103 | if os.path.commonprefix([path, basepath]) != basepath: |
paul@41 | 104 | raise OSError, path |
paul@41 | 105 | |
paul@41 | 106 | return path |
paul@41 | 107 | |
paul@41 | 108 | # High-level methods. |
paul@41 | 109 | |
paul@41 | 110 | def append(self, item): |
paul@41 | 111 | |
paul@41 | 112 | "Append the given 'item' to the store." |
paul@41 | 113 | |
paul@41 | 114 | self.writelock.acquire() |
paul@41 | 115 | try: |
paul@41 | 116 | next = self.get_next() |
paul@41 | 117 | self.write_item(item, next) |
paul@41 | 118 | self.write_next(next + 1) |
paul@41 | 119 | finally: |
paul@41 | 120 | self.writelock.release() |
paul@41 | 121 | |
paul@41 | 122 | def __len__(self): |
paul@41 | 123 | |
paul@41 | 124 | """ |
paul@41 | 125 | Return the number of the next item (which should also be the number of |
paul@41 | 126 | items). |
paul@41 | 127 | """ |
paul@41 | 128 | |
paul@41 | 129 | self.writelock.acquire() |
paul@41 | 130 | try: |
paul@41 | 131 | return self.get_next() |
paul@41 | 132 | finally: |
paul@41 | 133 | self.writelock.release() |
paul@41 | 134 | |
paul@41 | 135 | def __iter__(self): |
paul@41 | 136 | |
paul@41 | 137 | "Return an iterator over the items in the store." |
paul@41 | 138 | |
paul@41 | 139 | return ItemIterator(self) |
paul@41 | 140 | |
paul@41 | 141 | def __getitem__(self, number): |
paul@41 | 142 | |
paul@41 | 143 | "Return the item with the given 'number'." |
paul@41 | 144 | |
paul@41 | 145 | self.readlock.acquire() |
paul@41 | 146 | try: |
paul@41 | 147 | try: |
paul@41 | 148 | return self.read_item(number) |
paul@41 | 149 | except (IOError, OSError): |
paul@41 | 150 | raise IndexError, number |
paul@41 | 151 | finally: |
paul@41 | 152 | self.readlock.release() |
paul@41 | 153 | |
paul@41 | 154 | class ItemIterator: |
paul@41 | 155 | |
paul@41 | 156 | "An iterator over items in a store." |
paul@41 | 157 | |
paul@46 | 158 | def __init__(self, store, direction=1): |
paul@41 | 159 | self.store = store |
paul@46 | 160 | self.direction = direction |
paul@46 | 161 | self.reset() |
paul@46 | 162 | |
paul@46 | 163 | def reset(self): |
paul@46 | 164 | if self.direction == 1: |
paul@46 | 165 | self._next = 0 |
paul@46 | 166 | self.final = len(self.store) |
paul@46 | 167 | else: |
paul@46 | 168 | self._next = len(self.store) - 1 |
paul@46 | 169 | self.final = 0 |
paul@46 | 170 | |
paul@46 | 171 | def more(self): |
paul@46 | 172 | if self.direction == 1: |
paul@46 | 173 | return self._next < self.final |
paul@46 | 174 | else: |
paul@46 | 175 | return self._next >= self.final |
paul@46 | 176 | |
paul@46 | 177 | def get_next(self): |
paul@46 | 178 | next = self._next |
paul@46 | 179 | self._next += self.direction |
paul@46 | 180 | return next |
paul@41 | 181 | |
paul@41 | 182 | def next(self): |
paul@46 | 183 | while self.more(): |
paul@41 | 184 | try: |
paul@46 | 185 | return self.store[self.get_next()] |
paul@41 | 186 | except IndexError: |
paul@41 | 187 | pass |
paul@41 | 188 | |
paul@41 | 189 | raise StopIteration |
paul@41 | 190 | |
paul@46 | 191 | def reverse(self): |
paul@46 | 192 | self.direction = -self.direction |
paul@46 | 193 | self.reset() |
paul@46 | 194 | |
paul@46 | 195 | def reversed(self): |
paul@46 | 196 | self.reverse() |
paul@46 | 197 | return self |
paul@46 | 198 | |
paul@46 | 199 | def __iter__(self): |
paul@46 | 200 | return self |
paul@46 | 201 | |
paul@41 | 202 | # vim: tabstop=4 expandtab shiftwidth=4 |