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@41 | 28 | def get_next(self): |
paul@41 | 29 | |
paul@41 | 30 | "Return the next item number." |
paul@41 | 31 | |
paul@41 | 32 | next = self.read_next() |
paul@41 | 33 | if next is None: |
paul@41 | 34 | next = self.deduce_next() |
paul@41 | 35 | self.write_next(next) |
paul@41 | 36 | return next |
paul@41 | 37 | |
paul@41 | 38 | def deduce_next(self): |
paul@41 | 39 | |
paul@41 | 40 | "Deduce the next item number from the existing item files." |
paul@41 | 41 | |
paul@41 | 42 | return max([int(filename) for filename in os.listdir(self.path) if filename.isdigit()] or [-1]) + 1 |
paul@41 | 43 | |
paul@41 | 44 | def read_next(self): |
paul@41 | 45 | |
paul@41 | 46 | "Read the next item number from a special file." |
paul@41 | 47 | |
paul@41 | 48 | if not os.path.exists(self.next_path): |
paul@41 | 49 | return None |
paul@41 | 50 | |
paul@41 | 51 | f = open(self.next_path) |
paul@41 | 52 | try: |
paul@41 | 53 | try: |
paul@41 | 54 | return int(f.read()) |
paul@41 | 55 | except ValueError: |
paul@41 | 56 | return None |
paul@41 | 57 | finally: |
paul@41 | 58 | f.close() |
paul@41 | 59 | |
paul@41 | 60 | def write_next(self, next): |
paul@41 | 61 | |
paul@41 | 62 | "Write the 'next' item number to a special file." |
paul@41 | 63 | |
paul@41 | 64 | f = open(self.next_path, "w") |
paul@41 | 65 | try: |
paul@41 | 66 | f.write(str(next)) |
paul@41 | 67 | finally: |
paul@41 | 68 | f.close() |
paul@41 | 69 | |
paul@41 | 70 | def write_item(self, item, next): |
paul@41 | 71 | |
paul@41 | 72 | "Write the given 'item' to a file with the given 'next' item number." |
paul@41 | 73 | |
paul@41 | 74 | f = open(self.get_item_path(next), "w") |
paul@41 | 75 | try: |
paul@41 | 76 | f.write(item) |
paul@41 | 77 | finally: |
paul@41 | 78 | f.close() |
paul@41 | 79 | |
paul@41 | 80 | def read_item(self, number): |
paul@41 | 81 | |
paul@41 | 82 | "Read the item with the given item 'number'." |
paul@41 | 83 | |
paul@41 | 84 | f = open(self.get_item_path(number)) |
paul@41 | 85 | try: |
paul@41 | 86 | return f.read() |
paul@41 | 87 | finally: |
paul@41 | 88 | f.close() |
paul@41 | 89 | |
paul@41 | 90 | def get_item_path(self, number): |
paul@41 | 91 | |
paul@41 | 92 | "Get the path for the given item 'number'." |
paul@41 | 93 | |
paul@41 | 94 | path = os.path.abspath(os.path.join(self.path, str(number))) |
paul@41 | 95 | basepath = os.path.join(self.path, "") |
paul@41 | 96 | |
paul@41 | 97 | if os.path.commonprefix([path, basepath]) != basepath: |
paul@41 | 98 | raise OSError, path |
paul@41 | 99 | |
paul@41 | 100 | return path |
paul@41 | 101 | |
paul@41 | 102 | # High-level methods. |
paul@41 | 103 | |
paul@41 | 104 | def append(self, item): |
paul@41 | 105 | |
paul@41 | 106 | "Append the given 'item' to the store." |
paul@41 | 107 | |
paul@41 | 108 | self.writelock.acquire() |
paul@41 | 109 | try: |
paul@41 | 110 | next = self.get_next() |
paul@41 | 111 | self.write_item(item, next) |
paul@41 | 112 | self.write_next(next + 1) |
paul@41 | 113 | finally: |
paul@41 | 114 | self.writelock.release() |
paul@41 | 115 | |
paul@41 | 116 | def __len__(self): |
paul@41 | 117 | |
paul@41 | 118 | """ |
paul@41 | 119 | Return the number of the next item (which should also be the number of |
paul@41 | 120 | items). |
paul@41 | 121 | """ |
paul@41 | 122 | |
paul@41 | 123 | self.writelock.acquire() |
paul@41 | 124 | try: |
paul@41 | 125 | return self.get_next() |
paul@41 | 126 | finally: |
paul@41 | 127 | self.writelock.release() |
paul@41 | 128 | |
paul@41 | 129 | def __iter__(self): |
paul@41 | 130 | |
paul@41 | 131 | "Return an iterator over the items in the store." |
paul@41 | 132 | |
paul@41 | 133 | return ItemIterator(self) |
paul@41 | 134 | |
paul@41 | 135 | def __getitem__(self, number): |
paul@41 | 136 | |
paul@41 | 137 | "Return the item with the given 'number'." |
paul@41 | 138 | |
paul@41 | 139 | self.readlock.acquire() |
paul@41 | 140 | try: |
paul@41 | 141 | try: |
paul@41 | 142 | return self.read_item(number) |
paul@41 | 143 | except (IOError, OSError): |
paul@41 | 144 | raise IndexError, number |
paul@41 | 145 | finally: |
paul@41 | 146 | self.readlock.release() |
paul@41 | 147 | |
paul@41 | 148 | class ItemIterator: |
paul@41 | 149 | |
paul@41 | 150 | "An iterator over items in a store." |
paul@41 | 151 | |
paul@41 | 152 | def __init__(self, store): |
paul@41 | 153 | self.store = store |
paul@41 | 154 | self._next = -1 |
paul@41 | 155 | |
paul@41 | 156 | def next(self): |
paul@41 | 157 | final = len(self.store) |
paul@41 | 158 | |
paul@41 | 159 | while self._next < final: |
paul@41 | 160 | self._next += 1 |
paul@41 | 161 | try: |
paul@41 | 162 | return self.store[self._next] |
paul@41 | 163 | except IndexError: |
paul@41 | 164 | pass |
paul@41 | 165 | |
paul@41 | 166 | raise StopIteration |
paul@41 | 167 | |
paul@41 | 168 | # vim: tabstop=4 expandtab shiftwidth=4 |