# HG changeset patch # User Paul Boddie # Date 1358626716 -3600 # Node ID b9b72e0ee0a8dd994dca6805f825864cac0ea001 # Parent 48e5623c0724c0bf354bb8fd90b4a4b9432bffd3 Moved the MessageStore class to the MoinSupport module, making it more generic. diff -r 48e5623c0724 -r b9b72e0ee0a8 MoinMessage.py --- a/MoinMessage.py Fri Jan 18 00:36:13 2013 +0100 +++ b/MoinMessage.py Sat Jan 19 21:18:36 2013 +0100 @@ -14,7 +14,6 @@ from subprocess import Popen, PIPE from tempfile import mkstemp from urlparse import urlsplit -from MoinMoin.util import lock import httplib import os @@ -131,83 +130,6 @@ return mailbox -class MessageStore: - - "A page-specific message store." - - def __init__(self, page): - - "Initialise a message store for the given 'page'." - - self.path = page.getPagePath("messages") - self.next_path = os.path.join(self.path, "next") - lock_dir = page.getPagePath("message-locks") - self.lock = lock.WriteLock(lock_dir) - - def get_next(self): - - "Return the next message 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 message number from the existing message 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 message 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' message number to a special file." - - f = open(self.next_path, "w") - try: - f.write(str(next)) - finally: - f.close() - - def write_message(self, message, next): - - "Write the given 'message' to a file with the given 'next' message number." - - f = open(os.path.join(self.path, str(next)), "w") - try: - f.write(message.as_string()) - finally: - f.close() - - def append(self, message): - - "Append the given 'message' to the store." - - self.lock.acquire() - try: - next = self.get_next() - self.write_message(message, next) - self.write_next(next + 1) - finally: - self.lock.release() - class MoinMessageError(Exception): pass diff -r 48e5623c0724 -r b9b72e0ee0a8 actions/PostMessage.py --- a/actions/PostMessage.py Fri Jan 18 00:36:13 2013 +0100 +++ b/actions/PostMessage.py Sat Jan 19 21:18:36 2013 +0100 @@ -10,8 +10,8 @@ from MoinMoin.PageEditor import PageEditor from MoinMoin.log import getLogger from MoinMoin.user import User -from MoinSupport import * -from MoinMessage import GPG, Message, MessageStore, MoinMessageError +from MoinSupport import ItemStore +from MoinMessage import GPG, Message, MoinMessageError from email.parser import Parser try: @@ -240,8 +240,8 @@ # Update a message store for the page. if to_store(update): - store = MessageStore(self.page) - store.append(update) + store = ItemStore(self.page, "messages", "message-locks") + store.append(update.as_string()) # Update the page.