1.1 --- a/MoinForms.py Mon Feb 18 18:53:28 2013 +0100
1.2 +++ b/MoinForms.py Sat Mar 02 01:48:08 2013 +0100
1.3 @@ -14,6 +14,11 @@
1.4 from MoinSupport import *
1.5 import re
1.6
1.7 +try:
1.8 + from cStringIO import StringIO
1.9 +except ImportError:
1.10 + from StringIO import StringIO
1.11 +
1.12 __version__ = "0.1"
1.13
1.14 form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>"
1.15 @@ -74,7 +79,7 @@
1.16 # Attempt to load the form.
1.17
1.18 try:
1.19 - fields = self.loadFields(number)
1.20 + headers, fields = self.loadFields(number)
1.21
1.22 # Absent or inaccessible forms will result in an IndexError.
1.23
1.24 @@ -314,10 +319,19 @@
1.25
1.26 def storeFields(self, fields):
1.27
1.28 - "Store the given 'fields' as a Python object representation."
1.29 + """
1.30 + Store the given 'fields' as a Python object representation with some
1.31 + metadata headers.
1.32 + """
1.33 +
1.34 + headers = ["Form-Page: %s" % self.pagename]
1.35 + if self.attributes.has_key("fragment"):
1.36 + headers.append("Form-Fragment: %s" % self.attributes["fragment"])
1.37 +
1.38 + item = "%s\n\n%s" % ("\n".join(headers), repr(fields))
1.39
1.40 store = FormStore(self.access_handler)
1.41 - store.append(repr(fields))
1.42 + store.append(item)
1.43
1.44 def loadFields(self, number):
1.45
1.46 @@ -330,13 +344,43 @@
1.47
1.48 """
1.49 Load the fields from the 'store' that are associated with the given
1.50 - submission 'number'.
1.51 + submission 'number', returning the metadata headers and field structure.
1.52 """
1.53
1.54 - text = store[number]
1.55 + f = StringIO(store[number])
1.56 +
1.57 + headers = []
1.58 + lines = []
1.59 +
1.60 + # Find all lines before a blank line, marking the end of any headers.
1.61 +
1.62 + line = f.readline()
1.63 + while line.strip():
1.64 + lines.append(line)
1.65 + line = f.readline()
1.66 +
1.67 + # Get the remaining text.
1.68 +
1.69 + text = f.read()
1.70 +
1.71 + # If there were headers, converted the recorded lines.
1.72 +
1.73 + if text:
1.74 + for line in lines:
1.75 + headers.append(line.strip().split(":", 1))
1.76 +
1.77 + # Otherwise, rewind to obtain the entire item text for field data.
1.78 +
1.79 + else:
1.80 + f.seek(0)
1.81 + text = f.read()
1.82 +
1.83 + # Check the text and evaluate it if it is well-formed.
1.84 +
1.85 module = parse(text)
1.86 +
1.87 if checkStoredFormData(module):
1.88 - return eval(text)
1.89 + return headers, eval(text)
1.90 else:
1.91 raise MoinFormDataError, text
1.92
1.93 @@ -347,13 +391,16 @@
1.94 a field definition.
1.95 """
1.96
1.97 + have_child = False
1.98 +
1.99 for child in node.getChildNodes():
1.100 + have_child = True
1.101 if isinstance(child, Const):
1.102 pass
1.103 elif not isinstance(child, (Dict, Discard, List, Module, Stmt)) or not checkStoredFormData(child):
1.104 return False
1.105
1.106 - return True
1.107 + return have_child
1.108
1.109 class FormAccess:
1.110