# HG changeset patch # User Paul Boddie # Date 1383698782 -3600 # Node ID 839122fc06d253373609c1de0047bd959352be9d # Parent 4ae7dc2a3a92bef70116bea3fa52e97d07c3591f Separated the storage mechanisms from the store interface class. diff -r 4ae7dc2a3a92 -r 839122fc06d2 ItemSupport.py --- a/ItemSupport.py Wed Nov 06 00:24:17 2013 +0100 +++ b/ItemSupport.py Wed Nov 06 01:46:22 2013 +0100 @@ -12,7 +12,7 @@ # Content storage support. -class ItemStoreBase: +class GeneralItemStore: "Common item store functionality." @@ -95,7 +95,7 @@ finally: self.writelock.release() -class ItemDirectoryStore: +class DirectoryItemStore(GeneralItemStore): "A directory-based item store." diff -r 4ae7dc2a3a92 -r 839122fc06d2 MoinSupport.py --- a/MoinSupport.py Wed Nov 06 00:24:17 2013 +0100 +++ b/MoinSupport.py Wed Nov 06 01:46:22 2013 +0100 @@ -13,9 +13,9 @@ """ from DateSupport import * -from ItemSupport import ItemDirectoryStore, ItemStoreBase +from ItemSupport import DirectoryItemStore, GeneralItemStore from MoinMoin.parser import text_moin_wiki -from MoinMoin.Page import Page +from MoinMoin.Page import Page, RootPage from MoinMoin.PageEditor import PageEditor from MoinMoin.util import lock from MoinMoin import config, search, wikiutil @@ -1093,18 +1093,109 @@ # Content storage support. -class ItemStore: +# Specific storage mechanisms. + +class SubpageItemStore(GeneralItemStore): + + "A subpage-based item store." + + def __init__(self, page, lock_dir): + + "Initialise an item store for subpages under the given 'page'." + + GeneralItemStore.__init__(self, lock_dir) + self.page = page + + def mtime(self): + + "Return the last modified time of the item store." + + keys = self.get_keys() + if not keys: + page = self.page + else: + page = Page(self.page.request, self.get_item_path(max(keys))) - "A page-specific item store." + return wikiutil.version2timestamp( + getMetadata(page)["last-modified"] + ) + + def get_next(self): + + "Return the next item number." + + return self.deduce_next() + + def get_keys(self): + + "Return the item keys." - def __init__(self, page, item_dir="items", lock_dir="item_locks", store=None): + is_subpage = re.compile(u"^%s/" % re.escape(self.page.page_name), re.UNICODE).match + + # Collect the strict subpages of the parent page. + + leafnames = [] + parentname = self.page.page_name + + for pagename in RootPage(self.page.request).getPageList(filter=is_subpage): + parts = pagename[len(parentname)+1:].split("/") + + # Only collect numbered pages immediately below the parent. + + if len(parts) == 1 and parts[0].isdigit(): + leafnames.append(int(parts[0])) - "Initialise an item store for the given 'page'." + return leafnames + + def write_item(self, item, next): + + "Write the given 'item' to a file with the given 'next' item number." + + page = PageEditor(self.page.request, self.get_item_path(next)) + page.saveText(item, 0) + + def read_item(self, number): + + "Read the item with the given item 'number'." + + page = Page(self.page.request, self.get_item_path(number)) + return page.get_raw_body() + + def remove_item(self, number): + + "Remove the item with the given item 'number'." + + page = PageEditor(self.page.request, self.get_item_path(number)) + page.deletePage() + + def get_item_path(self, number): - item_dir_path = tuple(item_dir.split("/")) - lock_dir_path = tuple(lock_dir.split("/")) + "Get the path for the given item 'number'." + + return "%s/%s" % (self.page.page_name, number) + + # High-level methods. + + def append(self, item): + + "Append the given 'item' to the store." + + self.writelock.acquire() + try: + next = self.get_next() + self.write_item(item, next) + finally: + self.writelock.release() + +# General item store classes. + +class ItemStoreBase: + + "Support for page-specific item stores." + + def __init__(self, page, store): self.page = page - self.store = store or ItemDirectoryStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) + self.store = store def can_write(self): @@ -1194,97 +1285,27 @@ def next(self): return self.store.next() -class ItemSubpageStore(ItemStoreBase): - - "A subpage-based item store." - - def __init__(self, page, lock_dir="item_locks"): - - "Initialise an item store for subpages under the given 'page'." - - lock_dir_path = tuple(lock_dir.split("/")) - ItemStoreBase.__init__(self, page.getPagePath(*lock_dir_path)) - self.page = page - - def mtime(self): - - "Return the last modified time of the item store." - - keys = self.get_keys() - if not keys: - page = self.page - else: - page = Page(self.page.request, self.get_item_path(max(keys))) +def getDirectoryItemStoreForPage(page, item_dir, lock_dir): + item_dir_path = tuple(item_dir.split("/")) + lock_dir_path = tuple(lock_dir.split("/")) + return DirectoryItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) - return wikiutil.version2timestamp( - getMetadata(page)["last-modified"] - ) - - def get_next(self): - - "Return the next item number." - - return self.deduce_next() - - def get_keys(self): +def getSubpageItemStoreForPage(page, lock_dir): + lock_dir_path = tuple(lock_dir.split("/")) + return SubpageItemStore(page, page.getPagePath(*lock_dir_path)) - "Return the item keys." - - is_subpage = re.compile(u"^%s/" % re.escape(self.page.page_name), re.UNICODE).match - - # Collect the strict subpages of the parent page. - - leafnames = [] - parentname = self.page.page_name - - for pagename in RootPage(self.page.request).getPageList(filter=is_subpage): - parts = pagename[len(parentname)+1:].split("/") +# Convenience store constructors. - # Only collect numbered pages immediately below the parent. - - if len(parts) == 1 and parts[0].isdigit(): - leafnames.append(int(parts[0])) - - return leafnames - - def write_item(self, item, next): - - "Write the given 'item' to a file with the given 'next' item number." +def ItemStore(page, item_dir="items", lock_dir="item_locks"): - page = PageEditor(self.page.request, self.get_item_path(next)) - page.saveText(item, 0) - - def read_item(self, number): + "Store items in a directory via a page." - "Read the item with the given item 'number'." - - page = Page(self.page.request, self.get_item_path(number)) - return page.get_raw_body() - - def remove_item(self, number): + return ItemStoreBase(page, getDirectoryItemStoreForPage(page, item_dir, lock_dir)) - "Remove the item with the given item 'number'." - - page = PageEditor(self.page.request, self.get_item_path(number)) - page.deletePage() - - def get_item_path(self, number): - - "Get the path for the given item 'number'." - - return "%s/%s" % (self.page.page_name, number) +def ItemSubpageStore(page, lock_dir="item_locks"): - # High-level methods. - - def append(self, item): - - "Append the given 'item' to the store." + "Store items in subpages of a page." - self.writelock.acquire() - try: - next = self.get_next() - self.write_item(item, next) - finally: - self.writelock.release() + return ItemStoreBase(page, getSubpageItemStoreForPage(page, lock_dir)) # vim: tabstop=4 expandtab shiftwidth=4