1.1 --- a/MoinSupport.py Sat Jan 19 23:12:05 2013 +0100
1.2 +++ b/MoinSupport.py Sun Jan 20 01:13:24 2013 +0100
1.3 @@ -9,6 +9,7 @@
1.4 """
1.5
1.6 from DateSupport import *
1.7 +from ItemSupport import ItemDirectoryStore
1.8 from MoinMoin.Page import Page
1.9 from MoinMoin.util import lock
1.10 from MoinMoin import config, search, wikiutil
1.11 @@ -872,7 +873,7 @@
1.12
1.13 # Content storage support.
1.14
1.15 -class ItemStore:
1.16 +class ItemStore(ItemDirectoryStore):
1.17
1.18 "A page-specific item store."
1.19
1.20 @@ -880,78 +881,8 @@
1.21
1.22 "Initialise an item store for the given 'page'."
1.23
1.24 + ItemDirectoryStore.__init__(self, page.getPagePath(item_dir_name), page.getPagePath(lock_dir_name))
1.25 self.page = page
1.26 - self.path = page.getPagePath(item_dir_name)
1.27 - self.next_path = os.path.join(self.path, "next")
1.28 - lock_dir = page.getPagePath(lock_dir_name)
1.29 - self.lock = lock.WriteLock(lock_dir)
1.30 -
1.31 - def get_next(self):
1.32 -
1.33 - "Return the next item number."
1.34 -
1.35 - next = self.read_next()
1.36 - if not next:
1.37 - next = self.deduce_next()
1.38 - self.write_next(next)
1.39 - return next
1.40 -
1.41 - def deduce_next(self):
1.42 -
1.43 - "Deduce the next item number from the existing item files."
1.44 -
1.45 - return max([int(filename) for filename in os.listdir(self.path) if filename.isdigit()] or [0]) + 1
1.46 -
1.47 - def read_next(self):
1.48 -
1.49 - "Read the next item number from a special file."
1.50 -
1.51 - if not os.path.exists(self.next_path):
1.52 - return 0
1.53 -
1.54 - f = open(self.next_path)
1.55 - try:
1.56 - try:
1.57 - return int(f.read())
1.58 - except ValueError:
1.59 - return 0
1.60 - finally:
1.61 - f.close()
1.62 -
1.63 - def write_next(self, next):
1.64 -
1.65 - "Write the 'next' item number to a special file."
1.66 -
1.67 - f = open(self.next_path, "w")
1.68 - try:
1.69 - f.write(str(next))
1.70 - finally:
1.71 - f.close()
1.72 -
1.73 - def write_item(self, item, next):
1.74 -
1.75 - "Write the given 'item' to a file with the given 'next' item number."
1.76 -
1.77 - f = open(os.path.join(self.path, str(next)), "w")
1.78 - try:
1.79 - f.write(item)
1.80 - finally:
1.81 - f.close()
1.82 -
1.83 - def append(self, item):
1.84 -
1.85 - "Append the given 'item' to the store."
1.86 -
1.87 - if not self.can_write():
1.88 - return
1.89 -
1.90 - self.lock.acquire()
1.91 - try:
1.92 - next = self.get_next()
1.93 - self.write_item(item, next)
1.94 - self.write_next(next + 1)
1.95 - finally:
1.96 - self.lock.release()
1.97
1.98 def can_write(self):
1.99
1.100 @@ -963,4 +894,15 @@
1.101 user = self.page.request.user
1.102 return user and user.may.write(self.page.page_name)
1.103
1.104 + # High-level methods.
1.105 +
1.106 + def append(self, item):
1.107 +
1.108 + "Append the given 'item' to the store."
1.109 +
1.110 + if not self.can_write():
1.111 + return
1.112 +
1.113 + ItemDirectoryStore.append(self, item)
1.114 +
1.115 # vim: tabstop=4 expandtab shiftwidth=4