paul@41 | 1 | # -*- coding: iso-8859-1 -*- |
paul@41 | 2 | """ |
paul@41 | 3 | MoinMoin - ItemSupport library |
paul@41 | 4 | |
paul@106 | 5 | @copyright: 2013, 2014 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@116 | 9 | from MoinMoin import config |
paul@96 | 10 | from MoinMoin.Page import Page |
paul@95 | 11 | from MoinMoin.PageEditor import PageEditor |
paul@98 | 12 | from MoinMoin.security import Permissions |
paul@41 | 13 | from MoinMoin.util import lock |
paul@96 | 14 | from MoinSupport import getMetadata, getPagesForSearch |
paul@41 | 15 | import os |
paul@41 | 16 | |
paul@41 | 17 | # Content storage support. |
paul@41 | 18 | |
paul@99 | 19 | class SpecialPermissionsForPage(Permissions): |
paul@98 | 20 | |
paul@98 | 21 | "Permit saving of ACL-enabled pages." |
paul@98 | 22 | |
paul@98 | 23 | def __init__(self, user, pagename): |
paul@98 | 24 | Permissions.__init__(self, user) |
paul@98 | 25 | self.pagename = pagename |
paul@98 | 26 | |
paul@98 | 27 | def admin(self, pagename): |
paul@98 | 28 | return pagename == self.pagename |
paul@98 | 29 | |
paul@99 | 30 | write = admin |
paul@99 | 31 | |
paul@99 | 32 | class ReadPermissionsForSubpages(Permissions): |
paul@99 | 33 | |
paul@99 | 34 | "Permit listing of ACL-affected subpages." |
paul@99 | 35 | |
paul@99 | 36 | def __init__(self, user, pagename): |
paul@99 | 37 | Permissions.__init__(self, user) |
paul@99 | 38 | self.pagename = pagename |
paul@99 | 39 | |
paul@99 | 40 | def read(self, pagename): |
paul@99 | 41 | return pagename.startswith("%s/" % self.pagename) |
paul@99 | 42 | |
paul@95 | 43 | # Underlying storage mechanisms. |
paul@95 | 44 | |
paul@92 | 45 | class GeneralItemStore: |
paul@91 | 46 | |
paul@91 | 47 | "Common item store functionality." |
paul@91 | 48 | |
paul@91 | 49 | def __init__(self, lock_dir): |
paul@91 | 50 | |
paul@91 | 51 | "Initialise an item store with the given 'lock_dir' guarding access." |
paul@91 | 52 | |
paul@91 | 53 | self.lock_dir = lock_dir |
paul@91 | 54 | self.writelock = lock.WriteLock(lock_dir) |
paul@91 | 55 | self.readlock = lock.ReadLock(lock_dir) |
paul@91 | 56 | |
paul@91 | 57 | # High-level methods. |
paul@91 | 58 | |
paul@91 | 59 | def __len__(self): |
paul@91 | 60 | |
paul@91 | 61 | """ |
paul@91 | 62 | Return the number of items. |
paul@91 | 63 | """ |
paul@91 | 64 | |
paul@91 | 65 | return len(self.keys()) |
paul@91 | 66 | |
paul@91 | 67 | def __iter__(self): |
paul@91 | 68 | |
paul@91 | 69 | "Return an iterator over the items in the store." |
paul@91 | 70 | |
paul@91 | 71 | return ItemIterator(self) |
paul@91 | 72 | |
paul@91 | 73 | def keys(self): |
paul@91 | 74 | |
paul@91 | 75 | "Return a list of keys for items in the store." |
paul@91 | 76 | |
paul@91 | 77 | self.readlock.acquire() |
paul@91 | 78 | try: |
paul@91 | 79 | return self.get_keys() |
paul@91 | 80 | finally: |
paul@91 | 81 | self.readlock.release() |
paul@91 | 82 | |
paul@91 | 83 | def __getitem__(self, number): |
paul@91 | 84 | |
paul@91 | 85 | "Return the item with the given 'number'." |
paul@91 | 86 | |
paul@91 | 87 | self.readlock.acquire() |
paul@91 | 88 | try: |
paul@91 | 89 | try: |
paul@91 | 90 | return self.read_item(number) |
paul@91 | 91 | except (IOError, OSError): |
paul@91 | 92 | raise IndexError, number |
paul@91 | 93 | finally: |
paul@91 | 94 | self.readlock.release() |
paul@91 | 95 | |
paul@91 | 96 | def __delitem__(self, number): |
paul@91 | 97 | |
paul@91 | 98 | "Remove the item with the given 'number' from the store." |
paul@91 | 99 | |
paul@91 | 100 | self.writelock.acquire() |
paul@91 | 101 | try: |
paul@91 | 102 | try: |
paul@91 | 103 | self.remove_item(number) |
paul@91 | 104 | except (IOError, OSError): |
paul@91 | 105 | raise IndexError, number |
paul@91 | 106 | finally: |
paul@91 | 107 | self.writelock.release() |
paul@91 | 108 | |
paul@91 | 109 | def next(self): |
paul@91 | 110 | |
paul@91 | 111 | """ |
paul@91 | 112 | Return the number of the next item (which should also be the number of |
paul@91 | 113 | items if none have been deleted). |
paul@91 | 114 | """ |
paul@91 | 115 | |
paul@91 | 116 | self.writelock.acquire() |
paul@91 | 117 | try: |
paul@91 | 118 | return self.get_next() |
paul@91 | 119 | finally: |
paul@91 | 120 | self.writelock.release() |
paul@91 | 121 | |
paul@116 | 122 | class SequentialAccess(GeneralItemStore): |
paul@41 | 123 | |
paul@116 | 124 | "Support sequential access to items." |
paul@47 | 125 | |
paul@116 | 126 | def deduce_next(self): |
paul@41 | 127 | |
paul@116 | 128 | "Deduce the next item number from the existing item files." |
paul@41 | 129 | |
paul@116 | 130 | return max(self.get_keys() or [-1]) + 1 |
paul@63 | 131 | |
paul@41 | 132 | def read_next(self): |
paul@41 | 133 | |
paul@41 | 134 | "Read the next item number from a special file." |
paul@41 | 135 | |
paul@41 | 136 | if not os.path.exists(self.next_path): |
paul@41 | 137 | return None |
paul@41 | 138 | |
paul@41 | 139 | f = open(self.next_path) |
paul@41 | 140 | try: |
paul@41 | 141 | try: |
paul@41 | 142 | return int(f.read()) |
paul@41 | 143 | except ValueError: |
paul@41 | 144 | return None |
paul@41 | 145 | finally: |
paul@41 | 146 | f.close() |
paul@41 | 147 | |
paul@41 | 148 | def write_next(self, next): |
paul@41 | 149 | |
paul@41 | 150 | "Write the 'next' item number to a special file." |
paul@41 | 151 | |
paul@41 | 152 | f = open(self.next_path, "w") |
paul@41 | 153 | try: |
paul@41 | 154 | f.write(str(next)) |
paul@41 | 155 | finally: |
paul@41 | 156 | f.close() |
paul@41 | 157 | |
paul@116 | 158 | class DirectoryStore(GeneralItemStore): |
paul@116 | 159 | |
paul@116 | 160 | "A directory-based item store." |
paul@116 | 161 | |
paul@116 | 162 | def __init__(self, path, lock_dir): |
paul@116 | 163 | |
paul@116 | 164 | "Initialise an item store for the given 'path' and 'lock_dir'." |
paul@116 | 165 | |
paul@116 | 166 | self.path = path |
paul@116 | 167 | self.next_path = os.path.join(self.path, "next") |
paul@116 | 168 | self.lock_dir = lock_dir |
paul@116 | 169 | self.writelock = lock.WriteLock(lock_dir) |
paul@116 | 170 | self.readlock = lock.ReadLock(lock_dir) |
paul@116 | 171 | |
paul@116 | 172 | def mtime(self): |
paul@116 | 173 | |
paul@116 | 174 | "Return the last modified time of the item store directory." |
paul@116 | 175 | |
paul@116 | 176 | return os.path.getmtime(self.path) |
paul@116 | 177 | |
paul@116 | 178 | def get_keys(self): |
paul@116 | 179 | |
paul@116 | 180 | "Return the item keys." |
paul@116 | 181 | |
paul@116 | 182 | return [int(filename) for filename in os.listdir(self.path) if filename.isdigit()] |
paul@116 | 183 | |
paul@41 | 184 | def write_item(self, item, next): |
paul@41 | 185 | |
paul@41 | 186 | "Write the given 'item' to a file with the given 'next' item number." |
paul@41 | 187 | |
paul@106 | 188 | f = open(self.get_item_path(next), "wb") |
paul@41 | 189 | try: |
paul@41 | 190 | f.write(item) |
paul@41 | 191 | finally: |
paul@41 | 192 | f.close() |
paul@41 | 193 | |
paul@116 | 194 | def read_item(self, identifier): |
paul@41 | 195 | |
paul@116 | 196 | "Read the item with the given item 'identifier'." |
paul@41 | 197 | |
paul@116 | 198 | f = open(self.get_item_path(identifier), "rb") |
paul@41 | 199 | try: |
paul@41 | 200 | return f.read() |
paul@41 | 201 | finally: |
paul@41 | 202 | f.close() |
paul@41 | 203 | |
paul@116 | 204 | def remove_item(self, identifier): |
paul@116 | 205 | |
paul@116 | 206 | "Remove the item with the given item 'identifier'." |
paul@63 | 207 | |
paul@116 | 208 | os.remove(self.get_item_path(identifier)) |
paul@63 | 209 | |
paul@116 | 210 | def get_item_path(self, identifier): |
paul@63 | 211 | |
paul@116 | 212 | "Get the path for the given item 'identifier'." |
paul@41 | 213 | |
paul@116 | 214 | if isinstance(identifier, unicode): |
paul@116 | 215 | filename = identifier.encode(config.charset) |
paul@116 | 216 | else: |
paul@116 | 217 | filename = identifier |
paul@41 | 218 | |
paul@116 | 219 | path = os.path.abspath(os.path.join(self.path, filename)) |
paul@41 | 220 | basepath = os.path.join(self.path, "") |
paul@41 | 221 | |
paul@41 | 222 | if os.path.commonprefix([path, basepath]) != basepath: |
paul@41 | 223 | raise OSError, path |
paul@41 | 224 | |
paul@41 | 225 | return path |
paul@41 | 226 | |
paul@116 | 227 | class DirectoryItemStore(DirectoryStore, SequentialAccess): |
paul@116 | 228 | |
paul@116 | 229 | "A directory-based item store with numeric keys." |
paul@116 | 230 | |
paul@116 | 231 | def get_next(self): |
paul@116 | 232 | |
paul@116 | 233 | "Return the next item number." |
paul@116 | 234 | |
paul@116 | 235 | next = self.read_next() |
paul@116 | 236 | if next is None: |
paul@116 | 237 | next = self.deduce_next() |
paul@116 | 238 | self.write_next(next) |
paul@116 | 239 | return next |
paul@116 | 240 | |
paul@41 | 241 | # High-level methods. |
paul@41 | 242 | |
paul@41 | 243 | def append(self, item): |
paul@41 | 244 | |
paul@41 | 245 | "Append the given 'item' to the store." |
paul@41 | 246 | |
paul@41 | 247 | self.writelock.acquire() |
paul@41 | 248 | try: |
paul@41 | 249 | next = self.get_next() |
paul@41 | 250 | self.write_item(item, next) |
paul@41 | 251 | self.write_next(next + 1) |
paul@41 | 252 | finally: |
paul@41 | 253 | self.writelock.release() |
paul@41 | 254 | |
paul@116 | 255 | class DirectoryNamedItemStore(DirectoryStore): |
paul@116 | 256 | |
paul@116 | 257 | "A directory-based item store with explicit keys." |
paul@116 | 258 | |
paul@116 | 259 | def __setitem__(self, name, item): |
paul@116 | 260 | |
paul@116 | 261 | "Using the given 'name', set the given 'item' in the store." |
paul@116 | 262 | |
paul@116 | 263 | self.writelock.acquire() |
paul@116 | 264 | try: |
paul@116 | 265 | self.write_item(item, name) |
paul@116 | 266 | finally: |
paul@116 | 267 | self.writelock.release() |
paul@116 | 268 | |
paul@116 | 269 | class SubpageItemStore(SequentialAccess): |
paul@95 | 270 | |
paul@95 | 271 | "A subpage-based item store." |
paul@95 | 272 | |
paul@95 | 273 | def __init__(self, page, lock_dir): |
paul@95 | 274 | |
paul@95 | 275 | "Initialise an item store for subpages under the given 'page'." |
paul@95 | 276 | |
paul@95 | 277 | GeneralItemStore.__init__(self, lock_dir) |
paul@95 | 278 | self.page = page |
paul@95 | 279 | |
paul@95 | 280 | def mtime(self): |
paul@95 | 281 | |
paul@95 | 282 | "Return the last modified time of the item store." |
paul@95 | 283 | |
paul@95 | 284 | keys = self.get_keys() |
paul@95 | 285 | if not keys: |
paul@95 | 286 | page = self.page |
paul@95 | 287 | else: |
paul@95 | 288 | page = Page(self.page.request, self.get_item_path(max(keys))) |
paul@95 | 289 | |
paul@95 | 290 | return wikiutil.version2timestamp( |
paul@95 | 291 | getMetadata(page)["last-modified"] |
paul@95 | 292 | ) |
paul@95 | 293 | |
paul@95 | 294 | def get_next(self): |
paul@95 | 295 | |
paul@95 | 296 | "Return the next item number." |
paul@95 | 297 | |
paul@95 | 298 | return self.deduce_next() |
paul@95 | 299 | |
paul@95 | 300 | def get_keys(self): |
paul@95 | 301 | |
paul@95 | 302 | "Return the item keys." |
paul@95 | 303 | |
paul@99 | 304 | request = self.page.request |
paul@99 | 305 | |
paul@95 | 306 | # Collect the strict subpages of the parent page. |
paul@95 | 307 | |
paul@95 | 308 | leafnames = [] |
paul@95 | 309 | parentname = self.page.page_name |
paul@95 | 310 | |
paul@99 | 311 | # To list pages whose ACLs may prevent access, a special policy is required. |
paul@95 | 312 | |
paul@99 | 313 | may = request.user.may |
paul@99 | 314 | request.user.may = ReadPermissionsForSubpages(request.user, parentname) |
paul@99 | 315 | |
paul@99 | 316 | try: |
paul@99 | 317 | for page in getPagesForSearch("title:regex:^%s/" % parentname, self.page.request): |
paul@99 | 318 | basename, leafname = page.page_name.rsplit("/", 1) |
paul@95 | 319 | |
paul@99 | 320 | # Only collect numbered pages immediately below the parent. |
paul@99 | 321 | |
paul@99 | 322 | if basename == parentname and leafname.isdigit(): |
paul@99 | 323 | leafnames.append(int(leafname)) |
paul@95 | 324 | |
paul@99 | 325 | return leafnames |
paul@99 | 326 | |
paul@99 | 327 | # Restore the original policy. |
paul@99 | 328 | |
paul@99 | 329 | finally: |
paul@99 | 330 | request.user.may = may |
paul@95 | 331 | |
paul@95 | 332 | def write_item(self, item, next): |
paul@95 | 333 | |
paul@116 | 334 | "Write the given 'item' to a page with the given 'next' item number." |
paul@95 | 335 | |
paul@98 | 336 | request = self.page.request |
paul@98 | 337 | pagename = self.get_item_path(next) |
paul@98 | 338 | |
paul@98 | 339 | # To add a page with an ACL, a special policy is required. |
paul@98 | 340 | |
paul@98 | 341 | may = request.user.may |
paul@99 | 342 | request.user.may = SpecialPermissionsForPage(request.user, pagename) |
paul@98 | 343 | |
paul@98 | 344 | # Attempt to save the page, copying any ACL. |
paul@98 | 345 | |
paul@98 | 346 | try: |
paul@98 | 347 | page = PageEditor(request, pagename) |
paul@99 | 348 | page.saveText(item, 0) |
paul@98 | 349 | |
paul@98 | 350 | # Restore the original policy. |
paul@98 | 351 | |
paul@98 | 352 | finally: |
paul@98 | 353 | request.user.may = may |
paul@95 | 354 | |
paul@95 | 355 | def read_item(self, number): |
paul@95 | 356 | |
paul@95 | 357 | "Read the item with the given item 'number'." |
paul@95 | 358 | |
paul@95 | 359 | page = Page(self.page.request, self.get_item_path(number)) |
paul@99 | 360 | return page.get_raw_body() |
paul@95 | 361 | |
paul@95 | 362 | def remove_item(self, number): |
paul@95 | 363 | |
paul@95 | 364 | "Remove the item with the given item 'number'." |
paul@95 | 365 | |
paul@95 | 366 | page = PageEditor(self.page.request, self.get_item_path(number)) |
paul@95 | 367 | page.deletePage() |
paul@95 | 368 | |
paul@95 | 369 | def get_item_path(self, number): |
paul@95 | 370 | |
paul@95 | 371 | "Get the path for the given item 'number'." |
paul@95 | 372 | |
paul@95 | 373 | return "%s/%s" % (self.page.page_name, number) |
paul@95 | 374 | |
paul@95 | 375 | # High-level methods. |
paul@95 | 376 | |
paul@95 | 377 | def append(self, item): |
paul@95 | 378 | |
paul@95 | 379 | "Append the given 'item' to the store." |
paul@95 | 380 | |
paul@95 | 381 | self.writelock.acquire() |
paul@95 | 382 | try: |
paul@95 | 383 | next = self.get_next() |
paul@95 | 384 | self.write_item(item, next) |
paul@95 | 385 | finally: |
paul@95 | 386 | self.writelock.release() |
paul@95 | 387 | |
paul@41 | 388 | class ItemIterator: |
paul@41 | 389 | |
paul@41 | 390 | "An iterator over items in a store." |
paul@41 | 391 | |
paul@116 | 392 | def __init__(self, store, direction=1, keys=None): |
paul@41 | 393 | self.store = store |
paul@46 | 394 | self.direction = direction |
paul@116 | 395 | self.keys = keys |
paul@46 | 396 | self.reset() |
paul@46 | 397 | |
paul@46 | 398 | def reset(self): |
paul@46 | 399 | if self.direction == 1: |
paul@46 | 400 | self._next = 0 |
paul@63 | 401 | self.final = self.store.next() |
paul@46 | 402 | else: |
paul@63 | 403 | self._next = self.store.next() - 1 |
paul@46 | 404 | self.final = 0 |
paul@46 | 405 | |
paul@46 | 406 | def more(self): |
paul@46 | 407 | if self.direction == 1: |
paul@46 | 408 | return self._next < self.final |
paul@46 | 409 | else: |
paul@46 | 410 | return self._next >= self.final |
paul@46 | 411 | |
paul@46 | 412 | def get_next(self): |
paul@116 | 413 | if self.keys: |
paul@116 | 414 | next = self.keys[self._next] |
paul@116 | 415 | else: |
paul@116 | 416 | next = self._next |
paul@46 | 417 | self._next += self.direction |
paul@46 | 418 | return next |
paul@41 | 419 | |
paul@41 | 420 | def next(self): |
paul@46 | 421 | while self.more(): |
paul@41 | 422 | try: |
paul@46 | 423 | return self.store[self.get_next()] |
paul@41 | 424 | except IndexError: |
paul@41 | 425 | pass |
paul@41 | 426 | |
paul@41 | 427 | raise StopIteration |
paul@41 | 428 | |
paul@46 | 429 | def reverse(self): |
paul@46 | 430 | self.direction = -self.direction |
paul@46 | 431 | self.reset() |
paul@46 | 432 | |
paul@46 | 433 | def reversed(self): |
paul@46 | 434 | self.reverse() |
paul@46 | 435 | return self |
paul@46 | 436 | |
paul@46 | 437 | def __iter__(self): |
paul@46 | 438 | return self |
paul@46 | 439 | |
paul@95 | 440 | def getDirectoryItemStoreForPage(page, item_dir, lock_dir): |
paul@95 | 441 | |
paul@95 | 442 | """ |
paul@95 | 443 | A convenience function returning a directory-based store for the given |
paul@95 | 444 | 'page', using the given 'item_dir' and 'lock_dir'. |
paul@95 | 445 | """ |
paul@95 | 446 | |
paul@95 | 447 | item_dir_path = tuple(item_dir.split("/")) |
paul@95 | 448 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@95 | 449 | return DirectoryItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) |
paul@95 | 450 | |
paul@116 | 451 | def getDirectoryNamedItemStoreForPage(page, item_dir, lock_dir): |
paul@116 | 452 | |
paul@116 | 453 | """ |
paul@116 | 454 | A convenience function returning a directory-based store for the given |
paul@116 | 455 | 'page', using the given 'item_dir' and 'lock_dir'. |
paul@116 | 456 | """ |
paul@116 | 457 | |
paul@116 | 458 | item_dir_path = tuple(item_dir.split("/")) |
paul@116 | 459 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@116 | 460 | return DirectoryNamedItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) |
paul@116 | 461 | |
paul@95 | 462 | def getSubpageItemStoreForPage(page, lock_dir): |
paul@95 | 463 | |
paul@95 | 464 | """ |
paul@95 | 465 | A convenience function returning a subpage-based store for the given |
paul@95 | 466 | 'page', using the given 'lock_dir'. |
paul@95 | 467 | """ |
paul@95 | 468 | |
paul@95 | 469 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@95 | 470 | return SubpageItemStore(page, page.getPagePath(*lock_dir_path)) |
paul@95 | 471 | |
paul@95 | 472 | # Page-oriented item store classes. |
paul@95 | 473 | |
paul@95 | 474 | class ItemStoreBase: |
paul@95 | 475 | |
paul@95 | 476 | "Access item stores via pages, observing page access restrictions." |
paul@95 | 477 | |
paul@95 | 478 | def __init__(self, page, store): |
paul@95 | 479 | self.page = page |
paul@95 | 480 | self.store = store |
paul@95 | 481 | |
paul@95 | 482 | def can_write(self): |
paul@95 | 483 | |
paul@95 | 484 | """ |
paul@95 | 485 | Return whether the user associated with the request can write to the |
paul@95 | 486 | page owning this store. |
paul@95 | 487 | """ |
paul@95 | 488 | |
paul@95 | 489 | user = self.page.request.user |
paul@95 | 490 | return user and user.may.write(self.page.page_name) |
paul@95 | 491 | |
paul@95 | 492 | def can_read(self): |
paul@95 | 493 | |
paul@95 | 494 | """ |
paul@95 | 495 | Return whether the user associated with the request can read from the |
paul@95 | 496 | page owning this store. |
paul@95 | 497 | """ |
paul@95 | 498 | |
paul@95 | 499 | user = self.page.request.user |
paul@95 | 500 | return user and user.may.read(self.page.page_name) |
paul@95 | 501 | |
paul@95 | 502 | def can_delete(self): |
paul@95 | 503 | |
paul@95 | 504 | """ |
paul@95 | 505 | Return whether the user associated with the request can delete the |
paul@95 | 506 | page owning this store. |
paul@95 | 507 | """ |
paul@95 | 508 | |
paul@95 | 509 | user = self.page.request.user |
paul@95 | 510 | return user and user.may.delete(self.page.page_name) |
paul@95 | 511 | |
paul@95 | 512 | # Store-specific methods. |
paul@95 | 513 | |
paul@95 | 514 | def mtime(self): |
paul@95 | 515 | return self.store.mtime() |
paul@95 | 516 | |
paul@95 | 517 | # High-level methods. |
paul@95 | 518 | |
paul@95 | 519 | def keys(self): |
paul@95 | 520 | |
paul@95 | 521 | "Return a list of keys for items in the store." |
paul@95 | 522 | |
paul@95 | 523 | if not self.can_read(): |
paul@95 | 524 | return 0 |
paul@95 | 525 | |
paul@95 | 526 | return self.store.keys() |
paul@95 | 527 | |
paul@95 | 528 | def __len__(self): |
paul@95 | 529 | |
paul@95 | 530 | "Return the number of items in the store." |
paul@95 | 531 | |
paul@95 | 532 | if not self.can_read(): |
paul@95 | 533 | return 0 |
paul@95 | 534 | |
paul@95 | 535 | return len(self.store) |
paul@95 | 536 | |
paul@95 | 537 | def __getitem__(self, number): |
paul@95 | 538 | |
paul@95 | 539 | "Return the item with the given 'number'." |
paul@95 | 540 | |
paul@95 | 541 | if not self.can_read(): |
paul@95 | 542 | raise IndexError, number |
paul@95 | 543 | |
paul@95 | 544 | return self.store.__getitem__(number) |
paul@95 | 545 | |
paul@95 | 546 | def __delitem__(self, number): |
paul@95 | 547 | |
paul@95 | 548 | "Remove the item with the given 'number'." |
paul@95 | 549 | |
paul@95 | 550 | if not self.can_delete(): |
paul@95 | 551 | return |
paul@95 | 552 | |
paul@95 | 553 | return self.store.__delitem__(number) |
paul@95 | 554 | |
paul@95 | 555 | def __iter__(self): |
paul@95 | 556 | return self.store.__iter__() |
paul@95 | 557 | |
paul@95 | 558 | def next(self): |
paul@95 | 559 | return self.store.next() |
paul@95 | 560 | |
paul@116 | 561 | class SequentialStoreBase: |
paul@116 | 562 | |
paul@116 | 563 | "Sequential access methods for item stores." |
paul@116 | 564 | |
paul@116 | 565 | def append(self, item): |
paul@116 | 566 | |
paul@116 | 567 | "Append the given 'item' to the store." |
paul@116 | 568 | |
paul@116 | 569 | if not self.can_write(): |
paul@116 | 570 | return |
paul@116 | 571 | |
paul@116 | 572 | self.store.append(item) |
paul@116 | 573 | |
paul@116 | 574 | class NamedStoreBase: |
paul@116 | 575 | |
paul@116 | 576 | "Name-based access methods for item stores." |
paul@116 | 577 | |
paul@116 | 578 | def __setitem__(self, name, item): |
paul@116 | 579 | |
paul@116 | 580 | "Using the given 'name', set the given 'item' in the store." |
paul@116 | 581 | |
paul@116 | 582 | if not self.can_write(): |
paul@116 | 583 | return |
paul@116 | 584 | |
paul@116 | 585 | self.store[name] = item |
paul@116 | 586 | |
paul@95 | 587 | # Convenience store classes. |
paul@95 | 588 | |
paul@116 | 589 | class ItemStore(ItemStoreBase, SequentialStoreBase): |
paul@95 | 590 | |
paul@95 | 591 | "Store items in a directory via a page." |
paul@95 | 592 | |
paul@115 | 593 | def __init__(self, page, item_dir="items", lock_dir=None): |
paul@115 | 594 | ItemStoreBase.__init__(self, page, getDirectoryItemStoreForPage(page, item_dir, lock_dir or ("%s-locks" % item_dir))) |
paul@95 | 595 | |
paul@116 | 596 | class NamedItemStore(ItemStoreBase, NamedStoreBase): |
paul@116 | 597 | |
paul@116 | 598 | "Store items in a directory via a page." |
paul@116 | 599 | |
paul@116 | 600 | def __init__(self, page, item_dir="items", lock_dir=None): |
paul@116 | 601 | ItemStoreBase.__init__(self, page, getDirectoryNamedItemStoreForPage(page, item_dir, lock_dir or ("%s-locks" % item_dir))) |
paul@116 | 602 | |
paul@95 | 603 | class ItemSubpageStore(ItemStoreBase): |
paul@95 | 604 | |
paul@95 | 605 | "Store items in subpages of a page." |
paul@95 | 606 | |
paul@115 | 607 | def __init__(self, page, lock_dir=None): |
paul@115 | 608 | ItemStoreBase.__init__(self, page, getSubpageItemStoreForPage(page, lock_dir or "subpage-items-locks")) |
paul@95 | 609 | |
paul@41 | 610 | # vim: tabstop=4 expandtab shiftwidth=4 |