1.1 --- a/ItemSupport.py Wed Nov 06 12:41:01 2013 +0100
1.2 +++ b/ItemSupport.py Wed Nov 06 14:40:30 2013 +0100
1.3 @@ -6,11 +6,16 @@
1.4 @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.5 """
1.6
1.7 +from MoinMoin.Page import Page, RootPage
1.8 +from MoinMoin.PageEditor import PageEditor
1.9 from MoinMoin.util import lock
1.10 +import re
1.11 import os
1.12
1.13 # Content storage support.
1.14
1.15 +# Underlying storage mechanisms.
1.16 +
1.17 class GeneralItemStore:
1.18
1.19 "Common item store functionality."
1.20 @@ -208,6 +213,98 @@
1.21 finally:
1.22 self.writelock.release()
1.23
1.24 +class SubpageItemStore(GeneralItemStore):
1.25 +
1.26 + "A subpage-based item store."
1.27 +
1.28 + def __init__(self, page, lock_dir):
1.29 +
1.30 + "Initialise an item store for subpages under the given 'page'."
1.31 +
1.32 + GeneralItemStore.__init__(self, lock_dir)
1.33 + self.page = page
1.34 +
1.35 + def mtime(self):
1.36 +
1.37 + "Return the last modified time of the item store."
1.38 +
1.39 + keys = self.get_keys()
1.40 + if not keys:
1.41 + page = self.page
1.42 + else:
1.43 + page = Page(self.page.request, self.get_item_path(max(keys)))
1.44 +
1.45 + return wikiutil.version2timestamp(
1.46 + getMetadata(page)["last-modified"]
1.47 + )
1.48 +
1.49 + def get_next(self):
1.50 +
1.51 + "Return the next item number."
1.52 +
1.53 + return self.deduce_next()
1.54 +
1.55 + def get_keys(self):
1.56 +
1.57 + "Return the item keys."
1.58 +
1.59 + is_subpage = re.compile(u"^%s/" % re.escape(self.page.page_name), re.UNICODE).match
1.60 +
1.61 + # Collect the strict subpages of the parent page.
1.62 +
1.63 + leafnames = []
1.64 + parentname = self.page.page_name
1.65 +
1.66 + for pagename in RootPage(self.page.request).getPageList(filter=is_subpage):
1.67 + parts = pagename[len(parentname)+1:].split("/")
1.68 +
1.69 + # Only collect numbered pages immediately below the parent.
1.70 +
1.71 + if len(parts) == 1 and parts[0].isdigit():
1.72 + leafnames.append(int(parts[0]))
1.73 +
1.74 + return leafnames
1.75 +
1.76 + def write_item(self, item, next):
1.77 +
1.78 + "Write the given 'item' to a file with the given 'next' item number."
1.79 +
1.80 + page = PageEditor(self.page.request, self.get_item_path(next))
1.81 + page.saveText(item, 0)
1.82 +
1.83 + def read_item(self, number):
1.84 +
1.85 + "Read the item with the given item 'number'."
1.86 +
1.87 + page = Page(self.page.request, self.get_item_path(number))
1.88 + return page.get_raw_body()
1.89 +
1.90 + def remove_item(self, number):
1.91 +
1.92 + "Remove the item with the given item 'number'."
1.93 +
1.94 + page = PageEditor(self.page.request, self.get_item_path(number))
1.95 + page.deletePage()
1.96 +
1.97 + def get_item_path(self, number):
1.98 +
1.99 + "Get the path for the given item 'number'."
1.100 +
1.101 + return "%s/%s" % (self.page.page_name, number)
1.102 +
1.103 + # High-level methods.
1.104 +
1.105 + def append(self, item):
1.106 +
1.107 + "Append the given 'item' to the store."
1.108 +
1.109 + self.writelock.acquire()
1.110 + try:
1.111 + next = self.get_next()
1.112 + self.write_item(item, next)
1.113 + finally:
1.114 + self.writelock.release()
1.115 +
1.116 class ItemIterator:
1.117
1.118 "An iterator over items in a store."
1.119 @@ -256,4 +353,139 @@
1.120 def __iter__(self):
1.121 return self
1.122
1.123 +def getDirectoryItemStoreForPage(page, item_dir, lock_dir):
1.124 +
1.125 + """
1.126 + A convenience function returning a directory-based store for the given
1.127 + 'page', using the given 'item_dir' and 'lock_dir'.
1.128 + """
1.129 +
1.130 + item_dir_path = tuple(item_dir.split("/"))
1.131 + lock_dir_path = tuple(lock_dir.split("/"))
1.132 + return DirectoryItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path))
1.133 +
1.134 +def getSubpageItemStoreForPage(page, lock_dir):
1.135 +
1.136 + """
1.137 + A convenience function returning a subpage-based store for the given
1.138 + 'page', using the given 'lock_dir'.
1.139 + """
1.140 +
1.141 + lock_dir_path = tuple(lock_dir.split("/"))
1.142 + return SubpageItemStore(page, page.getPagePath(*lock_dir_path))
1.143 +
1.144 +# Page-oriented item store classes.
1.145 +
1.146 +class ItemStoreBase:
1.147 +
1.148 + "Access item stores via pages, observing page access restrictions."
1.149 +
1.150 + def __init__(self, page, store):
1.151 + self.page = page
1.152 + self.store = store
1.153 +
1.154 + def can_write(self):
1.155 +
1.156 + """
1.157 + Return whether the user associated with the request can write to the
1.158 + page owning this store.
1.159 + """
1.160 +
1.161 + user = self.page.request.user
1.162 + return user and user.may.write(self.page.page_name)
1.163 +
1.164 + def can_read(self):
1.165 +
1.166 + """
1.167 + Return whether the user associated with the request can read from the
1.168 + page owning this store.
1.169 + """
1.170 +
1.171 + user = self.page.request.user
1.172 + return user and user.may.read(self.page.page_name)
1.173 +
1.174 + def can_delete(self):
1.175 +
1.176 + """
1.177 + Return whether the user associated with the request can delete the
1.178 + page owning this store.
1.179 + """
1.180 +
1.181 + user = self.page.request.user
1.182 + return user and user.may.delete(self.page.page_name)
1.183 +
1.184 + # Store-specific methods.
1.185 +
1.186 + def mtime(self):
1.187 + return self.store.mtime()
1.188 +
1.189 + # High-level methods.
1.190 +
1.191 + def keys(self):
1.192 +
1.193 + "Return a list of keys for items in the store."
1.194 +
1.195 + if not self.can_read():
1.196 + return 0
1.197 +
1.198 + return self.store.keys()
1.199 +
1.200 + def append(self, item):
1.201 +
1.202 + "Append the given 'item' to the store."
1.203 +
1.204 + if not self.can_write():
1.205 + return
1.206 +
1.207 + self.store.append(item)
1.208 +
1.209 + def __len__(self):
1.210 +
1.211 + "Return the number of items in the store."
1.212 +
1.213 + if not self.can_read():
1.214 + return 0
1.215 +
1.216 + return len(self.store)
1.217 +
1.218 + def __getitem__(self, number):
1.219 +
1.220 + "Return the item with the given 'number'."
1.221 +
1.222 + if not self.can_read():
1.223 + raise IndexError, number
1.224 +
1.225 + return self.store.__getitem__(number)
1.226 +
1.227 + def __delitem__(self, number):
1.228 +
1.229 + "Remove the item with the given 'number'."
1.230 +
1.231 + if not self.can_delete():
1.232 + return
1.233 +
1.234 + return self.store.__delitem__(number)
1.235 +
1.236 + def __iter__(self):
1.237 + return self.store.__iter__()
1.238 +
1.239 + def next(self):
1.240 + return self.store.next()
1.241 +
1.242 +# Convenience store classes.
1.243 +
1.244 +class ItemStore(ItemStoreBase):
1.245 +
1.246 + "Store items in a directory via a page."
1.247 +
1.248 + def __init__(self, page, item_dir="items", lock_dir="item_locks"):
1.249 + ItemStoreBase.__init__(self, page, getDirectoryItemStoreForPage(page, item_dir, lock_dir))
1.250 +
1.251 +class ItemSubpageStore(ItemStoreBase):
1.252 +
1.253 + "Store items in subpages of a page."
1.254 +
1.255 + def __init__(self, page, lock_dir="item_locks"):
1.256 + ItemStoreBase.__init__(self, page, getSubpageItemStoreForPage(page, lock_dir))
1.257 +
1.258 # vim: tabstop=4 expandtab shiftwidth=4