# HG changeset patch # User Paul Boddie # Date 1362185288 -3600 # Node ID 093809dffbe3075d1b6ea106e6d552569df474be # Parent 4057a5c24a4e7633a40751088570f03bc776c251 Introduced metadata headers in stored form data. diff -r 4057a5c24a4e -r 093809dffbe3 MoinForms.py --- a/MoinForms.py Mon Feb 18 18:53:28 2013 +0100 +++ b/MoinForms.py Sat Mar 02 01:48:08 2013 +0100 @@ -14,6 +14,11 @@ from MoinSupport import * import re +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + __version__ = "0.1" form_field_regexp_str = r"<>" @@ -74,7 +79,7 @@ # Attempt to load the form. try: - fields = self.loadFields(number) + headers, fields = self.loadFields(number) # Absent or inaccessible forms will result in an IndexError. @@ -314,10 +319,19 @@ def storeFields(self, fields): - "Store the given 'fields' as a Python object representation." + """ + Store the given 'fields' as a Python object representation with some + metadata headers. + """ + + headers = ["Form-Page: %s" % self.pagename] + if self.attributes.has_key("fragment"): + headers.append("Form-Fragment: %s" % self.attributes["fragment"]) + + item = "%s\n\n%s" % ("\n".join(headers), repr(fields)) store = FormStore(self.access_handler) - store.append(repr(fields)) + store.append(item) def loadFields(self, number): @@ -330,13 +344,43 @@ """ Load the fields from the 'store' that are associated with the given - submission 'number'. + submission 'number', returning the metadata headers and field structure. """ - text = store[number] + f = StringIO(store[number]) + + headers = [] + lines = [] + + # Find all lines before a blank line, marking the end of any headers. + + line = f.readline() + while line.strip(): + lines.append(line) + line = f.readline() + + # Get the remaining text. + + text = f.read() + + # If there were headers, converted the recorded lines. + + if text: + for line in lines: + headers.append(line.strip().split(":", 1)) + + # Otherwise, rewind to obtain the entire item text for field data. + + else: + f.seek(0) + text = f.read() + + # Check the text and evaluate it if it is well-formed. + module = parse(text) + if checkStoredFormData(module): - return eval(text) + return headers, eval(text) else: raise MoinFormDataError, text @@ -347,13 +391,16 @@ a field definition. """ + have_child = False + for child in node.getChildNodes(): + have_child = True if isinstance(child, Const): pass elif not isinstance(child, (Dict, Discard, List, Module, Stmt)) or not checkStoredFormData(child): return False - return True + return have_child class FormAccess: