1.1 --- a/MoinSupport.py Sat Jan 19 16:08:29 2013 +0100
1.2 +++ b/MoinSupport.py Sat Jan 19 16:18:37 2013 +0100
1.3 @@ -10,11 +10,13 @@
1.4
1.5 from DateSupport import *
1.6 from MoinMoin.Page import Page
1.7 +from MoinMoin.util import lock
1.8 from MoinMoin import config, search, wikiutil
1.9 from StringIO import StringIO
1.10 from shlex import shlex
1.11 import re
1.12 import time
1.13 +import os
1.14
1.15 # Moin 1.9 request parameters.
1.16
1.17 @@ -868,4 +870,83 @@
1.18 else:
1.19 return title
1.20
1.21 +# Content storage support.
1.22 +
1.23 +class ItemStore:
1.24 +
1.25 + "A page-specific item store."
1.26 +
1.27 + def __init__(self, page, item_dir_name="items", lock_dir_name="item-locks"):
1.28 +
1.29 + "Initialise an item store for the given 'page'."
1.30 +
1.31 + self.path = page.getPagePath(item_dir_name)
1.32 + self.next_path = os.path.join(self.path, "next")
1.33 + lock_dir = page.getPagePath(lock_dir_name)
1.34 + self.lock = lock.WriteLock(lock_dir)
1.35 +
1.36 + def get_next(self):
1.37 +
1.38 + "Return the next item number."
1.39 +
1.40 + next = self.read_next()
1.41 + if not next:
1.42 + next = self.deduce_next()
1.43 + self.write_next(next)
1.44 + return next
1.45 +
1.46 + def deduce_next(self):
1.47 +
1.48 + "Deduce the next item number from the existing item files."
1.49 +
1.50 + return max([int(filename) for filename in os.listdir(self.path) if filename.isdigit()] or [0]) + 1
1.51 +
1.52 + def read_next(self):
1.53 +
1.54 + "Read the next item number from a special file."
1.55 +
1.56 + if not os.path.exists(self.next_path):
1.57 + return 0
1.58 +
1.59 + f = open(self.next_path)
1.60 + try:
1.61 + try:
1.62 + return int(f.read())
1.63 + except ValueError:
1.64 + return 0
1.65 + finally:
1.66 + f.close()
1.67 +
1.68 + def write_next(self, next):
1.69 +
1.70 + "Write the 'next' item number to a special file."
1.71 +
1.72 + f = open(self.next_path, "w")
1.73 + try:
1.74 + f.write(str(next))
1.75 + finally:
1.76 + f.close()
1.77 +
1.78 + def write_item(self, item, next):
1.79 +
1.80 + "Write the given 'item' to a file with the given 'next' item number."
1.81 +
1.82 + f = open(os.path.join(self.path, str(next)), "w")
1.83 + try:
1.84 + f.write(item.as_string())
1.85 + finally:
1.86 + f.close()
1.87 +
1.88 + def append(self, item):
1.89 +
1.90 + "Append the given 'item' to the store."
1.91 +
1.92 + self.lock.acquire()
1.93 + try:
1.94 + next = self.get_next()
1.95 + self.write_item(item, next)
1.96 + self.write_next(next + 1)
1.97 + finally:
1.98 + self.lock.release()
1.99 +
1.100 # vim: tabstop=4 expandtab shiftwidth=4