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@96 | 9 | from MoinMoin.Page import Page |
paul@95 | 10 | from MoinMoin.PageEditor import PageEditor |
paul@41 | 11 | from MoinMoin.util import lock |
paul@96 | 12 | from MoinSupport import getMetadata, getPagesForSearch |
paul@41 | 13 | import os |
paul@41 | 14 | |
paul@41 | 15 | # Content storage support. |
paul@41 | 16 | |
paul@95 | 17 | # Underlying storage mechanisms. |
paul@95 | 18 | |
paul@92 | 19 | class GeneralItemStore: |
paul@91 | 20 | |
paul@91 | 21 | "Common item store functionality." |
paul@91 | 22 | |
paul@91 | 23 | def __init__(self, lock_dir): |
paul@91 | 24 | |
paul@91 | 25 | "Initialise an item store with the given 'lock_dir' guarding access." |
paul@91 | 26 | |
paul@91 | 27 | self.lock_dir = lock_dir |
paul@91 | 28 | self.writelock = lock.WriteLock(lock_dir) |
paul@91 | 29 | self.readlock = lock.ReadLock(lock_dir) |
paul@91 | 30 | |
paul@91 | 31 | def deduce_next(self): |
paul@91 | 32 | |
paul@91 | 33 | "Deduce the next item number from the existing item files." |
paul@91 | 34 | |
paul@91 | 35 | return max(self.get_keys() or [-1]) + 1 |
paul@91 | 36 | |
paul@91 | 37 | # High-level methods. |
paul@91 | 38 | |
paul@91 | 39 | def __len__(self): |
paul@91 | 40 | |
paul@91 | 41 | """ |
paul@91 | 42 | Return the number of items. |
paul@91 | 43 | """ |
paul@91 | 44 | |
paul@91 | 45 | return len(self.keys()) |
paul@91 | 46 | |
paul@91 | 47 | def __iter__(self): |
paul@91 | 48 | |
paul@91 | 49 | "Return an iterator over the items in the store." |
paul@91 | 50 | |
paul@91 | 51 | return ItemIterator(self) |
paul@91 | 52 | |
paul@91 | 53 | def keys(self): |
paul@91 | 54 | |
paul@91 | 55 | "Return a list of keys for items in the store." |
paul@91 | 56 | |
paul@91 | 57 | self.readlock.acquire() |
paul@91 | 58 | try: |
paul@91 | 59 | return self.get_keys() |
paul@91 | 60 | finally: |
paul@91 | 61 | self.readlock.release() |
paul@91 | 62 | |
paul@91 | 63 | def __getitem__(self, number): |
paul@91 | 64 | |
paul@91 | 65 | "Return the item with the given 'number'." |
paul@91 | 66 | |
paul@91 | 67 | self.readlock.acquire() |
paul@91 | 68 | try: |
paul@91 | 69 | try: |
paul@91 | 70 | return self.read_item(number) |
paul@91 | 71 | except (IOError, OSError): |
paul@91 | 72 | raise IndexError, number |
paul@91 | 73 | finally: |
paul@91 | 74 | self.readlock.release() |
paul@91 | 75 | |
paul@91 | 76 | def __delitem__(self, number): |
paul@91 | 77 | |
paul@91 | 78 | "Remove the item with the given 'number' from the store." |
paul@91 | 79 | |
paul@91 | 80 | self.writelock.acquire() |
paul@91 | 81 | try: |
paul@91 | 82 | try: |
paul@91 | 83 | self.remove_item(number) |
paul@91 | 84 | except (IOError, OSError): |
paul@91 | 85 | raise IndexError, number |
paul@91 | 86 | finally: |
paul@91 | 87 | self.writelock.release() |
paul@91 | 88 | |
paul@91 | 89 | def next(self): |
paul@91 | 90 | |
paul@91 | 91 | """ |
paul@91 | 92 | Return the number of the next item (which should also be the number of |
paul@91 | 93 | items if none have been deleted). |
paul@91 | 94 | """ |
paul@91 | 95 | |
paul@91 | 96 | self.writelock.acquire() |
paul@91 | 97 | try: |
paul@91 | 98 | return self.get_next() |
paul@91 | 99 | finally: |
paul@91 | 100 | self.writelock.release() |
paul@91 | 101 | |
paul@92 | 102 | class DirectoryItemStore(GeneralItemStore): |
paul@41 | 103 | |
paul@41 | 104 | "A directory-based item store." |
paul@41 | 105 | |
paul@41 | 106 | def __init__(self, path, lock_dir): |
paul@41 | 107 | |
paul@41 | 108 | "Initialise an item store for the given 'path' and 'lock_dir'." |
paul@41 | 109 | |
paul@41 | 110 | self.path = path |
paul@41 | 111 | self.next_path = os.path.join(self.path, "next") |
paul@41 | 112 | self.lock_dir = lock_dir |
paul@41 | 113 | self.writelock = lock.WriteLock(lock_dir) |
paul@41 | 114 | self.readlock = lock.ReadLock(lock_dir) |
paul@41 | 115 | |
paul@47 | 116 | def mtime(self): |
paul@47 | 117 | |
paul@47 | 118 | "Return the last modified time of the item store directory." |
paul@47 | 119 | |
paul@47 | 120 | return os.path.getmtime(self.path) |
paul@47 | 121 | |
paul@41 | 122 | def get_next(self): |
paul@41 | 123 | |
paul@41 | 124 | "Return the next item number." |
paul@41 | 125 | |
paul@41 | 126 | next = self.read_next() |
paul@41 | 127 | if next is None: |
paul@41 | 128 | next = self.deduce_next() |
paul@41 | 129 | self.write_next(next) |
paul@41 | 130 | return next |
paul@41 | 131 | |
paul@63 | 132 | def get_keys(self): |
paul@63 | 133 | |
paul@63 | 134 | "Return the item keys." |
paul@63 | 135 | |
paul@63 | 136 | return [int(filename) for filename in os.listdir(self.path) if filename.isdigit()] |
paul@63 | 137 | |
paul@41 | 138 | def read_next(self): |
paul@41 | 139 | |
paul@41 | 140 | "Read the next item number from a special file." |
paul@41 | 141 | |
paul@41 | 142 | if not os.path.exists(self.next_path): |
paul@41 | 143 | return None |
paul@41 | 144 | |
paul@41 | 145 | f = open(self.next_path) |
paul@41 | 146 | try: |
paul@41 | 147 | try: |
paul@41 | 148 | return int(f.read()) |
paul@41 | 149 | except ValueError: |
paul@41 | 150 | return None |
paul@41 | 151 | finally: |
paul@41 | 152 | f.close() |
paul@41 | 153 | |
paul@41 | 154 | def write_next(self, next): |
paul@41 | 155 | |
paul@41 | 156 | "Write the 'next' item number to a special file." |
paul@41 | 157 | |
paul@41 | 158 | f = open(self.next_path, "w") |
paul@41 | 159 | try: |
paul@41 | 160 | f.write(str(next)) |
paul@41 | 161 | finally: |
paul@41 | 162 | f.close() |
paul@41 | 163 | |
paul@41 | 164 | def write_item(self, item, next): |
paul@41 | 165 | |
paul@41 | 166 | "Write the given 'item' to a file with the given 'next' item number." |
paul@41 | 167 | |
paul@41 | 168 | f = open(self.get_item_path(next), "w") |
paul@41 | 169 | try: |
paul@41 | 170 | f.write(item) |
paul@41 | 171 | finally: |
paul@41 | 172 | f.close() |
paul@41 | 173 | |
paul@41 | 174 | def read_item(self, number): |
paul@41 | 175 | |
paul@41 | 176 | "Read the item with the given item 'number'." |
paul@41 | 177 | |
paul@41 | 178 | f = open(self.get_item_path(number)) |
paul@41 | 179 | try: |
paul@41 | 180 | return f.read() |
paul@41 | 181 | finally: |
paul@41 | 182 | f.close() |
paul@41 | 183 | |
paul@63 | 184 | def remove_item(self, number): |
paul@63 | 185 | |
paul@63 | 186 | "Remove the item with the given item 'number'." |
paul@63 | 187 | |
paul@63 | 188 | os.remove(self.get_item_path(number)) |
paul@63 | 189 | |
paul@41 | 190 | def get_item_path(self, number): |
paul@41 | 191 | |
paul@41 | 192 | "Get the path for the given item 'number'." |
paul@41 | 193 | |
paul@41 | 194 | path = os.path.abspath(os.path.join(self.path, str(number))) |
paul@41 | 195 | basepath = os.path.join(self.path, "") |
paul@41 | 196 | |
paul@41 | 197 | if os.path.commonprefix([path, basepath]) != basepath: |
paul@41 | 198 | raise OSError, path |
paul@41 | 199 | |
paul@41 | 200 | return path |
paul@41 | 201 | |
paul@41 | 202 | # High-level methods. |
paul@41 | 203 | |
paul@41 | 204 | def append(self, item): |
paul@41 | 205 | |
paul@41 | 206 | "Append the given 'item' to the store." |
paul@41 | 207 | |
paul@41 | 208 | self.writelock.acquire() |
paul@41 | 209 | try: |
paul@41 | 210 | next = self.get_next() |
paul@41 | 211 | self.write_item(item, next) |
paul@41 | 212 | self.write_next(next + 1) |
paul@41 | 213 | finally: |
paul@41 | 214 | self.writelock.release() |
paul@41 | 215 | |
paul@95 | 216 | class SubpageItemStore(GeneralItemStore): |
paul@95 | 217 | |
paul@95 | 218 | "A subpage-based item store." |
paul@95 | 219 | |
paul@95 | 220 | def __init__(self, page, lock_dir): |
paul@95 | 221 | |
paul@95 | 222 | "Initialise an item store for subpages under the given 'page'." |
paul@95 | 223 | |
paul@95 | 224 | GeneralItemStore.__init__(self, lock_dir) |
paul@95 | 225 | self.page = page |
paul@95 | 226 | |
paul@95 | 227 | def mtime(self): |
paul@95 | 228 | |
paul@95 | 229 | "Return the last modified time of the item store." |
paul@95 | 230 | |
paul@95 | 231 | keys = self.get_keys() |
paul@95 | 232 | if not keys: |
paul@95 | 233 | page = self.page |
paul@95 | 234 | else: |
paul@95 | 235 | page = Page(self.page.request, self.get_item_path(max(keys))) |
paul@95 | 236 | |
paul@95 | 237 | return wikiutil.version2timestamp( |
paul@95 | 238 | getMetadata(page)["last-modified"] |
paul@95 | 239 | ) |
paul@95 | 240 | |
paul@95 | 241 | def get_next(self): |
paul@95 | 242 | |
paul@95 | 243 | "Return the next item number." |
paul@95 | 244 | |
paul@95 | 245 | return self.deduce_next() |
paul@95 | 246 | |
paul@95 | 247 | def get_keys(self): |
paul@95 | 248 | |
paul@95 | 249 | "Return the item keys." |
paul@95 | 250 | |
paul@95 | 251 | # Collect the strict subpages of the parent page. |
paul@95 | 252 | |
paul@95 | 253 | leafnames = [] |
paul@95 | 254 | parentname = self.page.page_name |
paul@95 | 255 | |
paul@96 | 256 | for page in getPagesForSearch("title:regex:^%s/" % parentname, self.page.request): |
paul@96 | 257 | basename, leafname = page.page_name.rsplit("/", 1) |
paul@95 | 258 | |
paul@95 | 259 | # Only collect numbered pages immediately below the parent. |
paul@95 | 260 | |
paul@96 | 261 | if basename == parentname and leafname.isdigit(): |
paul@96 | 262 | leafnames.append(int(leafname)) |
paul@95 | 263 | |
paul@95 | 264 | return leafnames |
paul@95 | 265 | |
paul@95 | 266 | def write_item(self, item, next): |
paul@95 | 267 | |
paul@95 | 268 | "Write the given 'item' to a file with the given 'next' item number." |
paul@95 | 269 | |
paul@95 | 270 | page = PageEditor(self.page.request, self.get_item_path(next)) |
paul@95 | 271 | page.saveText(item, 0) |
paul@95 | 272 | |
paul@95 | 273 | def read_item(self, number): |
paul@95 | 274 | |
paul@95 | 275 | "Read the item with the given item 'number'." |
paul@95 | 276 | |
paul@95 | 277 | page = Page(self.page.request, self.get_item_path(number)) |
paul@95 | 278 | return page.get_raw_body() |
paul@95 | 279 | |
paul@95 | 280 | def remove_item(self, number): |
paul@95 | 281 | |
paul@95 | 282 | "Remove the item with the given item 'number'." |
paul@95 | 283 | |
paul@95 | 284 | page = PageEditor(self.page.request, self.get_item_path(number)) |
paul@95 | 285 | page.deletePage() |
paul@95 | 286 | |
paul@95 | 287 | def get_item_path(self, number): |
paul@95 | 288 | |
paul@95 | 289 | "Get the path for the given item 'number'." |
paul@95 | 290 | |
paul@95 | 291 | return "%s/%s" % (self.page.page_name, number) |
paul@95 | 292 | |
paul@95 | 293 | # High-level methods. |
paul@95 | 294 | |
paul@95 | 295 | def append(self, item): |
paul@95 | 296 | |
paul@95 | 297 | "Append the given 'item' to the store." |
paul@95 | 298 | |
paul@95 | 299 | self.writelock.acquire() |
paul@95 | 300 | try: |
paul@95 | 301 | next = self.get_next() |
paul@95 | 302 | self.write_item(item, next) |
paul@95 | 303 | finally: |
paul@95 | 304 | self.writelock.release() |
paul@95 | 305 | |
paul@41 | 306 | class ItemIterator: |
paul@41 | 307 | |
paul@41 | 308 | "An iterator over items in a store." |
paul@41 | 309 | |
paul@46 | 310 | def __init__(self, store, direction=1): |
paul@41 | 311 | self.store = store |
paul@46 | 312 | self.direction = direction |
paul@46 | 313 | self.reset() |
paul@46 | 314 | |
paul@46 | 315 | def reset(self): |
paul@46 | 316 | if self.direction == 1: |
paul@46 | 317 | self._next = 0 |
paul@63 | 318 | self.final = self.store.next() |
paul@46 | 319 | else: |
paul@63 | 320 | self._next = self.store.next() - 1 |
paul@46 | 321 | self.final = 0 |
paul@46 | 322 | |
paul@46 | 323 | def more(self): |
paul@46 | 324 | if self.direction == 1: |
paul@46 | 325 | return self._next < self.final |
paul@46 | 326 | else: |
paul@46 | 327 | return self._next >= self.final |
paul@46 | 328 | |
paul@46 | 329 | def get_next(self): |
paul@46 | 330 | next = self._next |
paul@46 | 331 | self._next += self.direction |
paul@46 | 332 | return next |
paul@41 | 333 | |
paul@41 | 334 | def next(self): |
paul@46 | 335 | while self.more(): |
paul@41 | 336 | try: |
paul@46 | 337 | return self.store[self.get_next()] |
paul@41 | 338 | except IndexError: |
paul@41 | 339 | pass |
paul@41 | 340 | |
paul@41 | 341 | raise StopIteration |
paul@41 | 342 | |
paul@46 | 343 | def reverse(self): |
paul@46 | 344 | self.direction = -self.direction |
paul@46 | 345 | self.reset() |
paul@46 | 346 | |
paul@46 | 347 | def reversed(self): |
paul@46 | 348 | self.reverse() |
paul@46 | 349 | return self |
paul@46 | 350 | |
paul@46 | 351 | def __iter__(self): |
paul@46 | 352 | return self |
paul@46 | 353 | |
paul@95 | 354 | def getDirectoryItemStoreForPage(page, item_dir, lock_dir): |
paul@95 | 355 | |
paul@95 | 356 | """ |
paul@95 | 357 | A convenience function returning a directory-based store for the given |
paul@95 | 358 | 'page', using the given 'item_dir' and 'lock_dir'. |
paul@95 | 359 | """ |
paul@95 | 360 | |
paul@95 | 361 | item_dir_path = tuple(item_dir.split("/")) |
paul@95 | 362 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@95 | 363 | return DirectoryItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) |
paul@95 | 364 | |
paul@95 | 365 | def getSubpageItemStoreForPage(page, lock_dir): |
paul@95 | 366 | |
paul@95 | 367 | """ |
paul@95 | 368 | A convenience function returning a subpage-based store for the given |
paul@95 | 369 | 'page', using the given 'lock_dir'. |
paul@95 | 370 | """ |
paul@95 | 371 | |
paul@95 | 372 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@95 | 373 | return SubpageItemStore(page, page.getPagePath(*lock_dir_path)) |
paul@95 | 374 | |
paul@95 | 375 | # Page-oriented item store classes. |
paul@95 | 376 | |
paul@95 | 377 | class ItemStoreBase: |
paul@95 | 378 | |
paul@95 | 379 | "Access item stores via pages, observing page access restrictions." |
paul@95 | 380 | |
paul@95 | 381 | def __init__(self, page, store): |
paul@95 | 382 | self.page = page |
paul@95 | 383 | self.store = store |
paul@95 | 384 | |
paul@95 | 385 | def can_write(self): |
paul@95 | 386 | |
paul@95 | 387 | """ |
paul@95 | 388 | Return whether the user associated with the request can write to the |
paul@95 | 389 | page owning this store. |
paul@95 | 390 | """ |
paul@95 | 391 | |
paul@95 | 392 | user = self.page.request.user |
paul@95 | 393 | return user and user.may.write(self.page.page_name) |
paul@95 | 394 | |
paul@95 | 395 | def can_read(self): |
paul@95 | 396 | |
paul@95 | 397 | """ |
paul@95 | 398 | Return whether the user associated with the request can read from the |
paul@95 | 399 | page owning this store. |
paul@95 | 400 | """ |
paul@95 | 401 | |
paul@95 | 402 | user = self.page.request.user |
paul@95 | 403 | return user and user.may.read(self.page.page_name) |
paul@95 | 404 | |
paul@95 | 405 | def can_delete(self): |
paul@95 | 406 | |
paul@95 | 407 | """ |
paul@95 | 408 | Return whether the user associated with the request can delete the |
paul@95 | 409 | page owning this store. |
paul@95 | 410 | """ |
paul@95 | 411 | |
paul@95 | 412 | user = self.page.request.user |
paul@95 | 413 | return user and user.may.delete(self.page.page_name) |
paul@95 | 414 | |
paul@95 | 415 | # Store-specific methods. |
paul@95 | 416 | |
paul@95 | 417 | def mtime(self): |
paul@95 | 418 | return self.store.mtime() |
paul@95 | 419 | |
paul@95 | 420 | # High-level methods. |
paul@95 | 421 | |
paul@95 | 422 | def keys(self): |
paul@95 | 423 | |
paul@95 | 424 | "Return a list of keys for items in the store." |
paul@95 | 425 | |
paul@95 | 426 | if not self.can_read(): |
paul@95 | 427 | return 0 |
paul@95 | 428 | |
paul@95 | 429 | return self.store.keys() |
paul@95 | 430 | |
paul@95 | 431 | def append(self, item): |
paul@95 | 432 | |
paul@95 | 433 | "Append the given 'item' to the store." |
paul@95 | 434 | |
paul@95 | 435 | if not self.can_write(): |
paul@95 | 436 | return |
paul@95 | 437 | |
paul@95 | 438 | self.store.append(item) |
paul@95 | 439 | |
paul@95 | 440 | def __len__(self): |
paul@95 | 441 | |
paul@95 | 442 | "Return the number of items in the store." |
paul@95 | 443 | |
paul@95 | 444 | if not self.can_read(): |
paul@95 | 445 | return 0 |
paul@95 | 446 | |
paul@95 | 447 | return len(self.store) |
paul@95 | 448 | |
paul@95 | 449 | def __getitem__(self, number): |
paul@95 | 450 | |
paul@95 | 451 | "Return the item with the given 'number'." |
paul@95 | 452 | |
paul@95 | 453 | if not self.can_read(): |
paul@95 | 454 | raise IndexError, number |
paul@95 | 455 | |
paul@95 | 456 | return self.store.__getitem__(number) |
paul@95 | 457 | |
paul@95 | 458 | def __delitem__(self, number): |
paul@95 | 459 | |
paul@95 | 460 | "Remove the item with the given 'number'." |
paul@95 | 461 | |
paul@95 | 462 | if not self.can_delete(): |
paul@95 | 463 | return |
paul@95 | 464 | |
paul@95 | 465 | return self.store.__delitem__(number) |
paul@95 | 466 | |
paul@95 | 467 | def __iter__(self): |
paul@95 | 468 | return self.store.__iter__() |
paul@95 | 469 | |
paul@95 | 470 | def next(self): |
paul@95 | 471 | return self.store.next() |
paul@95 | 472 | |
paul@95 | 473 | # Convenience store classes. |
paul@95 | 474 | |
paul@95 | 475 | class ItemStore(ItemStoreBase): |
paul@95 | 476 | |
paul@95 | 477 | "Store items in a directory via a page." |
paul@95 | 478 | |
paul@95 | 479 | def __init__(self, page, item_dir="items", lock_dir="item_locks"): |
paul@95 | 480 | ItemStoreBase.__init__(self, page, getDirectoryItemStoreForPage(page, item_dir, lock_dir)) |
paul@95 | 481 | |
paul@95 | 482 | class ItemSubpageStore(ItemStoreBase): |
paul@95 | 483 | |
paul@95 | 484 | "Store items in subpages of a page." |
paul@95 | 485 | |
paul@95 | 486 | def __init__(self, page, lock_dir="item_locks"): |
paul@95 | 487 | ItemStoreBase.__init__(self, page, getSubpageItemStoreForPage(page, lock_dir)) |
paul@95 | 488 | |
paul@41 | 489 | # vim: tabstop=4 expandtab shiftwidth=4 |