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
2.1 --- a/MoinSupport.py Wed Nov 06 12:41:01 2013 +0100
2.2 +++ b/MoinSupport.py Wed Nov 06 14:40:30 2013 +0100
2.3 @@ -13,11 +13,8 @@
2.4 """
2.5
2.6 from DateSupport import *
2.7 -from ItemSupport import DirectoryItemStore, GeneralItemStore
2.8 from MoinMoin.parser import text_moin_wiki
2.9 -from MoinMoin.Page import Page, RootPage
2.10 -from MoinMoin.PageEditor import PageEditor
2.11 -from MoinMoin.util import lock
2.12 +from MoinMoin.Page import Page
2.13 from MoinMoin import config, search, wikiutil
2.14 from shlex import shlex
2.15 import re
2.16 @@ -37,7 +34,7 @@
2.17 except ImportError:
2.18 pass
2.19
2.20 -__version__ = "0.4.1"
2.21 +__version__ = "0.4.2"
2.22
2.23 # Extraction of shared fragments.
2.24
2.25 @@ -1091,235 +1088,4 @@
2.26 else:
2.27 return title
2.28
2.29 -# Content storage support.
2.30 -
2.31 -# Underlying storage mechanisms.
2.32 -
2.33 -class SubpageItemStore(GeneralItemStore):
2.34 -
2.35 - "A subpage-based item store."
2.36 -
2.37 - def __init__(self, page, lock_dir):
2.38 -
2.39 - "Initialise an item store for subpages under the given 'page'."
2.40 -
2.41 - GeneralItemStore.__init__(self, lock_dir)
2.42 - self.page = page
2.43 -
2.44 - def mtime(self):
2.45 -
2.46 - "Return the last modified time of the item store."
2.47 -
2.48 - keys = self.get_keys()
2.49 - if not keys:
2.50 - page = self.page
2.51 - else:
2.52 - page = Page(self.page.request, self.get_item_path(max(keys)))
2.53 -
2.54 - return wikiutil.version2timestamp(
2.55 - getMetadata(page)["last-modified"]
2.56 - )
2.57 -
2.58 - def get_next(self):
2.59 -
2.60 - "Return the next item number."
2.61 -
2.62 - return self.deduce_next()
2.63 -
2.64 - def get_keys(self):
2.65 -
2.66 - "Return the item keys."
2.67 -
2.68 - is_subpage = re.compile(u"^%s/" % re.escape(self.page.page_name), re.UNICODE).match
2.69 -
2.70 - # Collect the strict subpages of the parent page.
2.71 -
2.72 - leafnames = []
2.73 - parentname = self.page.page_name
2.74 -
2.75 - for pagename in RootPage(self.page.request).getPageList(filter=is_subpage):
2.76 - parts = pagename[len(parentname)+1:].split("/")
2.77 -
2.78 - # Only collect numbered pages immediately below the parent.
2.79 -
2.80 - if len(parts) == 1 and parts[0].isdigit():
2.81 - leafnames.append(int(parts[0]))
2.82 -
2.83 - return leafnames
2.84 -
2.85 - def write_item(self, item, next):
2.86 -
2.87 - "Write the given 'item' to a file with the given 'next' item number."
2.88 -
2.89 - page = PageEditor(self.page.request, self.get_item_path(next))
2.90 - page.saveText(item, 0)
2.91 -
2.92 - def read_item(self, number):
2.93 -
2.94 - "Read the item with the given item 'number'."
2.95 -
2.96 - page = Page(self.page.request, self.get_item_path(number))
2.97 - return page.get_raw_body()
2.98 -
2.99 - def remove_item(self, number):
2.100 -
2.101 - "Remove the item with the given item 'number'."
2.102 -
2.103 - page = PageEditor(self.page.request, self.get_item_path(number))
2.104 - page.deletePage()
2.105 -
2.106 - def get_item_path(self, number):
2.107 -
2.108 - "Get the path for the given item 'number'."
2.109 -
2.110 - return "%s/%s" % (self.page.page_name, number)
2.111 -
2.112 - # High-level methods.
2.113 -
2.114 - def append(self, item):
2.115 -
2.116 - "Append the given 'item' to the store."
2.117 -
2.118 - self.writelock.acquire()
2.119 - try:
2.120 - next = self.get_next()
2.121 - self.write_item(item, next)
2.122 - finally:
2.123 - self.writelock.release()
2.124 -
2.125 -def getDirectoryItemStoreForPage(page, item_dir, lock_dir):
2.126 -
2.127 - """
2.128 - A convenience function returning a directory-based store for the given
2.129 - 'page', using the given 'item_dir' and 'lock_dir'.
2.130 - """
2.131 -
2.132 - item_dir_path = tuple(item_dir.split("/"))
2.133 - lock_dir_path = tuple(lock_dir.split("/"))
2.134 - return DirectoryItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path))
2.135 -
2.136 -def getSubpageItemStoreForPage(page, lock_dir):
2.137 -
2.138 - """
2.139 - A convenience function returning a subpage-based store for the given
2.140 - 'page', using the given 'lock_dir'.
2.141 - """
2.142 -
2.143 - lock_dir_path = tuple(lock_dir.split("/"))
2.144 - return SubpageItemStore(page, page.getPagePath(*lock_dir_path))
2.145 -
2.146 -# Page-oriented item store classes.
2.147 -
2.148 -class ItemStoreBase:
2.149 -
2.150 - "Access item stores via pages, observing page access restrictions."
2.151 -
2.152 - def __init__(self, page, store):
2.153 - self.page = page
2.154 - self.store = store
2.155 -
2.156 - def can_write(self):
2.157 -
2.158 - """
2.159 - Return whether the user associated with the request can write to the
2.160 - page owning this store.
2.161 - """
2.162 -
2.163 - user = self.page.request.user
2.164 - return user and user.may.write(self.page.page_name)
2.165 -
2.166 - def can_read(self):
2.167 -
2.168 - """
2.169 - Return whether the user associated with the request can read from the
2.170 - page owning this store.
2.171 - """
2.172 -
2.173 - user = self.page.request.user
2.174 - return user and user.may.read(self.page.page_name)
2.175 -
2.176 - def can_delete(self):
2.177 -
2.178 - """
2.179 - Return whether the user associated with the request can delete the
2.180 - page owning this store.
2.181 - """
2.182 -
2.183 - user = self.page.request.user
2.184 - return user and user.may.delete(self.page.page_name)
2.185 -
2.186 - # Store-specific methods.
2.187 -
2.188 - def mtime(self):
2.189 - return self.store.mtime()
2.190 -
2.191 - # High-level methods.
2.192 -
2.193 - def keys(self):
2.194 -
2.195 - "Return a list of keys for items in the store."
2.196 -
2.197 - if not self.can_read():
2.198 - return 0
2.199 -
2.200 - return self.store.keys()
2.201 -
2.202 - def append(self, item):
2.203 -
2.204 - "Append the given 'item' to the store."
2.205 -
2.206 - if not self.can_write():
2.207 - return
2.208 -
2.209 - self.store.append(item)
2.210 -
2.211 - def __len__(self):
2.212 -
2.213 - "Return the number of items in the store."
2.214 -
2.215 - if not self.can_read():
2.216 - return 0
2.217 -
2.218 - return len(self.store)
2.219 -
2.220 - def __getitem__(self, number):
2.221 -
2.222 - "Return the item with the given 'number'."
2.223 -
2.224 - if not self.can_read():
2.225 - raise IndexError, number
2.226 -
2.227 - return self.store.__getitem__(number)
2.228 -
2.229 - def __delitem__(self, number):
2.230 -
2.231 - "Remove the item with the given 'number'."
2.232 -
2.233 - if not self.can_delete():
2.234 - return
2.235 -
2.236 - return self.store.__delitem__(number)
2.237 -
2.238 - def __iter__(self):
2.239 - return self.store.__iter__()
2.240 -
2.241 - def next(self):
2.242 - return self.store.next()
2.243 -
2.244 -# Convenience store classes.
2.245 -
2.246 -class ItemStore(ItemStoreBase):
2.247 -
2.248 - "Store items in a directory via a page."
2.249 -
2.250 - def __init__(self, page, item_dir="items", lock_dir="item_locks"):
2.251 - ItemStoreBase.__init__(self, page, getDirectoryItemStoreForPage(page, item_dir, lock_dir))
2.252 -
2.253 -class ItemSubpageStore(ItemStoreBase):
2.254 -
2.255 - "Store items in subpages of a page."
2.256 -
2.257 - def __init__(self, page, lock_dir="item_locks"):
2.258 - ItemStoreBase.__init__(self, page, getSubpageItemStoreForPage(page, lock_dir))
2.259 -
2.260 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/PKG-INFO Wed Nov 06 12:41:01 2013 +0100
3.2 +++ b/PKG-INFO Wed Nov 06 14:40:30 2013 +0100
3.3 @@ -1,12 +1,12 @@
3.4 Metadata-Version: 1.1
3.5 Name: MoinSupport
3.6 -Version: 0.4.1
3.7 +Version: 0.4.2
3.8 Author: Paul Boddie
3.9 Author-email: paul at boddie org uk
3.10 Maintainer: Paul Boddie
3.11 Maintainer-email: paul at boddie org uk
3.12 Home-page: http://hgweb.boddie.org.uk/MoinSupport
3.13 -Download-url: http://hgweb.boddie.org.uk/MoinSupport/archive/rel-0-4-1.tar.bz2
3.14 +Download-url: http://hgweb.boddie.org.uk/MoinSupport/archive/rel-0-4-2.tar.bz2
3.15 Summary: Support libraries for MoinMoin extensions
3.16 License: GPL (version 2 or later)
3.17 Description: The MoinSupport distribution provides libraries handling datetime
4.1 --- a/README.txt Wed Nov 06 12:41:01 2013 +0100
4.2 +++ b/README.txt Wed Nov 06 14:40:30 2013 +0100
4.3 @@ -64,6 +64,12 @@
4.4 If time zone handling is not required, pytz need not be installed. It is,
4.5 however, highly recommended that pytz be installed.
4.6
4.7 +New in MoinSupport 0.4.2 (Changes since MoinSupport 0.4.1)
4.8 +----------------------------------------------------------
4.9 +
4.10 + * Moved ItemStore and related functionality into ItemSupport.
4.11 + * Added support for subpage-based item stores.
4.12 +
4.13 New in MoinSupport 0.4.1 (Changes since MoinSupport 0.4)
4.14 --------------------------------------------------------
4.15
5.1 --- a/setup.py Wed Nov 06 12:41:01 2013 +0100
5.2 +++ b/setup.py Wed Nov 06 14:40:30 2013 +0100
5.3 @@ -8,7 +8,7 @@
5.4 author = "Paul Boddie",
5.5 author_email = "paul@boddie.org.uk",
5.6 url = "http://hgweb.boddie.org.uk/MoinSupport",
5.7 - version = "0.4.1",
5.8 + version = "0.4.2",
5.9 py_modules = ["ContentTypeSupport", "DateSupport", "GeneralSupport",
5.10 "ItemSupport", "LocationSupport", "MoinDateSupport",
5.11 "MoinRemoteSupport", "MoinSupport", "ViewSupport"]