# HG changeset patch # User Paul Boddie # Date 1358608717 -3600 # Node ID 6659510890acfc164a272068e14a93658d416f81 # Parent 367c9d96bc2aff275adf9044ca0f4fe60dda8111 Added support for page-specific item storage. diff -r 367c9d96bc2a -r 6659510890ac MoinSupport.py --- a/MoinSupport.py Sat Jan 19 16:08:29 2013 +0100 +++ b/MoinSupport.py Sat Jan 19 16:18:37 2013 +0100 @@ -10,11 +10,13 @@ from DateSupport import * from MoinMoin.Page import Page +from MoinMoin.util import lock from MoinMoin import config, search, wikiutil from StringIO import StringIO from shlex import shlex import re import time +import os # Moin 1.9 request parameters. @@ -868,4 +870,83 @@ else: return title +# Content storage support. + +class ItemStore: + + "A page-specific item store." + + def __init__(self, page, item_dir_name="items", lock_dir_name="item-locks"): + + "Initialise an item store for the given 'page'." + + self.path = page.getPagePath(item_dir_name) + self.next_path = os.path.join(self.path, "next") + lock_dir = page.getPagePath(lock_dir_name) + self.lock = lock.WriteLock(lock_dir) + + def get_next(self): + + "Return the next item number." + + next = self.read_next() + if not next: + next = self.deduce_next() + self.write_next(next) + return next + + def deduce_next(self): + + "Deduce the next item number from the existing item files." + + return max([int(filename) for filename in os.listdir(self.path) if filename.isdigit()] or [0]) + 1 + + def read_next(self): + + "Read the next item number from a special file." + + if not os.path.exists(self.next_path): + return 0 + + f = open(self.next_path) + try: + try: + return int(f.read()) + except ValueError: + return 0 + finally: + f.close() + + def write_next(self, next): + + "Write the 'next' item number to a special file." + + f = open(self.next_path, "w") + try: + f.write(str(next)) + finally: + f.close() + + def write_item(self, item, next): + + "Write the given 'item' to a file with the given 'next' item number." + + f = open(os.path.join(self.path, str(next)), "w") + try: + f.write(item.as_string()) + finally: + f.close() + + def append(self, item): + + "Append the given 'item' to the store." + + self.lock.acquire() + try: + next = self.get_next() + self.write_item(item, next) + self.write_next(next + 1) + finally: + self.lock.release() + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 367c9d96bc2a -r 6659510890ac README.txt --- a/README.txt Sat Jan 19 16:08:29 2013 +0100 +++ b/README.txt Sat Jan 19 16:18:37 2013 +0100 @@ -86,6 +86,7 @@ be generated for formatted text. * Added various content/media type functions from EventAggregator and other projects to ContentTypeSupport. + * Added support for page-specific item storage in MoinSupport. Release Procedures ------------------