paulb@1 | 1 | #!/usr/bin/env python |
paulb@36 | 2 | # -*- coding: iso-8859-1 -*- |
paulb@1 | 3 | |
paulb@1 | 4 | """ |
paulb@116 | 5 | Interpretation of field collections from sources such as HTTP request parameter |
paulb@116 | 6 | dictionaries. |
paulb@116 | 7 | |
paul@684 | 8 | Copyright (C) 2005, 2006, 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> |
paulb@116 | 9 | |
paulb@600 | 10 | This program is free software; you can redistribute it and/or modify it under |
paulb@600 | 11 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@600 | 12 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@600 | 13 | later version. |
paulb@116 | 14 | |
paulb@600 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@600 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@600 | 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@600 | 18 | details. |
paulb@116 | 19 | |
paulb@600 | 20 | You should have received a copy of the GNU Lesser General Public License along |
paulb@600 | 21 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@116 | 22 | |
paulb@116 | 23 | -------- |
paulb@116 | 24 | |
paulb@116 | 25 | Classes which process field collections, producing instance documents. Each |
paulb@116 | 26 | field entry consists of a field name mapped to a string value, where the field |
paulb@116 | 27 | name may have the following formats: |
paulb@1 | 28 | |
paulb@142 | 29 | /name1$n1/name2 |
paulb@142 | 30 | /name1$n1/name2$n2/name3 |
paulb@142 | 31 | /name1$n1/name2$n2/name3$n3/name4 |
paulb@1 | 32 | ... |
paulb@1 | 33 | |
paulb@116 | 34 | The indexes n1, n2, n3, ... indicate the position of elements (starting from 1) |
paulb@116 | 35 | in the entire element list, whose elements may have different names. For |
paulb@116 | 36 | example: |
paulb@1 | 37 | |
paulb@142 | 38 | /zoo$1/name |
paulb@142 | 39 | /zoo$1/cage$1/name |
paulb@142 | 40 | /zoo$1/cage$2/name |
paulb@142 | 41 | /zoo$1/funding$3/contributor$1/name |
paulb@1 | 42 | |
paulb@116 | 43 | Where multiple values can be collected for a given field, the following notation |
paulb@116 | 44 | is employed: |
paulb@23 | 45 | |
paulb@142 | 46 | /package$1/categories$1/category$$value |
paulb@23 | 47 | |
paulb@116 | 48 | Some fields may contain the "=" string. This string is reserved and all text |
paulb@116 | 49 | following it is meant to specify a path into a particular document. For example: |
paulb@1 | 50 | |
paulb@142 | 51 | _action_add_animal=/zoo$1/cage$2 |
paulb@1 | 52 | """ |
paulb@1 | 53 | |
paulb@16 | 54 | import Constants |
paulb@1 | 55 | import libxml2dom |
paulb@1 | 56 | from xml.dom import EMPTY_NAMESPACE |
paulb@665 | 57 | try: |
paulb@665 | 58 | set |
paulb@665 | 59 | except NameError: |
paulb@665 | 60 | from sets import Set as set |
paulb@1 | 61 | |
paul@684 | 62 | FILE_NAMESPACE = "http://www.boddie.org.uk/ns/xmltools/file-upload" |
paul@684 | 63 | |
paulb@1 | 64 | class FieldsError(Exception): |
paulb@1 | 65 | pass |
paulb@1 | 66 | |
paulb@81 | 67 | class FieldProcessor: |
paulb@1 | 68 | |
paulb@1 | 69 | """ |
paulb@1 | 70 | A class which converts fields in the documented form to XML |
paulb@1 | 71 | instance documents. |
paulb@1 | 72 | """ |
paulb@1 | 73 | |
paulb@3 | 74 | def __init__(self, encoding="utf-8", values_are_lists=0): |
paulb@1 | 75 | |
paulb@1 | 76 | """ |
paulb@1 | 77 | Initialise the fields processor with the given 'encoding', |
paulb@1 | 78 | which is optional and which only applies to field data in |
paulb@1 | 79 | Python string form (and not Unicode objects). |
paulb@3 | 80 | |
paulb@3 | 81 | If the optional 'values_are_lists' parameter is set to true |
paulb@3 | 82 | then each actual field value will be obtained by taking the |
paulb@3 | 83 | first element from each supplied field value. |
paulb@1 | 84 | """ |
paulb@1 | 85 | |
paulb@1 | 86 | self.encoding = encoding |
paulb@3 | 87 | self.values_are_lists = values_are_lists |
paulb@1 | 88 | |
paulb@1 | 89 | def complete_documents(self, documents, fields): |
paulb@1 | 90 | |
paulb@1 | 91 | """ |
paulb@1 | 92 | Complete the given 'documents' using the 'fields' items list. |
paul@684 | 93 | |
paul@684 | 94 | Include a special entry in 'documents' for the key None, |
paul@684 | 95 | referencing a list of file upload tuples of the form |
paul@684 | 96 | (filename, content). |
paulb@1 | 97 | """ |
paulb@1 | 98 | |
paul@684 | 99 | files = [] |
paul@684 | 100 | |
paulb@1 | 101 | for field, value in fields: |
paulb@1 | 102 | |
paulb@1 | 103 | # Ignore selectors. |
paulb@1 | 104 | |
paulb@16 | 105 | if field.find(Constants.selector_indicator) != -1: |
paulb@1 | 106 | continue |
paulb@1 | 107 | |
paulb@1 | 108 | model_name, components = self._get_model_name_and_components(field) |
paulb@1 | 109 | if model_name is None: |
paulb@1 | 110 | continue |
paulb@1 | 111 | |
paulb@1 | 112 | # Get a new instance document if none has been made for the |
paulb@1 | 113 | # model. |
paulb@1 | 114 | |
paulb@1 | 115 | if not documents.has_key(model_name): |
paulb@3 | 116 | documents[model_name] = self.new_instance(model_name) |
paulb@1 | 117 | node = documents[model_name] |
paulb@1 | 118 | |
paulb@1 | 119 | # Traverse the components within the instance. |
paulb@1 | 120 | |
paulb@1 | 121 | for component in components: |
paulb@16 | 122 | t = component.split(Constants.pair_separator) |
paulb@1 | 123 | if len(t) == 1: |
paulb@21 | 124 | |
paulb@21 | 125 | # Convert from lists if necessary. |
paulb@21 | 126 | |
paulb@21 | 127 | if self.values_are_lists: |
paulb@21 | 128 | value = value[0] |
paulb@21 | 129 | |
paul@684 | 130 | self._set_attribute(node, t[0], value, files) |
paulb@1 | 131 | break |
paulb@1 | 132 | |
paulb@1 | 133 | elif len(t) == 2: |
paulb@1 | 134 | |
paulb@23 | 135 | # Convert from one-based indexing (the position() |
paulb@23 | 136 | # function) to zero-based indexing. |
paulb@1 | 137 | |
paulb@1 | 138 | name, index = t[0], int(t[1]) - 1 |
paulb@1 | 139 | if index < 0: |
paulb@1 | 140 | break |
paulb@138 | 141 | try: |
paulb@138 | 142 | node = self._enter_element(node, name, index) |
paulb@138 | 143 | except FieldsError, exc: |
paulb@138 | 144 | raise FieldsError, "In field '%s', name '%s' and index '%s' could not be added, since '%s' was found." % ( |
paulb@138 | 145 | field, name, index, exc.args[0]) |
paulb@1 | 146 | |
paulb@23 | 147 | elif len(t) == 3 and t[1] == "": |
paulb@23 | 148 | |
paulb@23 | 149 | # Multivalued fields. |
paulb@23 | 150 | |
paulb@23 | 151 | if not self.values_are_lists: |
paulb@23 | 152 | values = [value] |
paulb@23 | 153 | else: |
paulb@23 | 154 | values = value |
paulb@23 | 155 | |
paulb@23 | 156 | name = t[0] |
paulb@29 | 157 | for subvalue in values: |
paulb@29 | 158 | subnode = self._append_element(node, name) |
paul@684 | 159 | self._set_attribute(subnode, t[2], subvalue, files) |
paulb@23 | 160 | |
paul@684 | 161 | documents[None] = files |
paul@684 | 162 | |
paul@684 | 163 | def _set_attribute(self, node, name, value, files): |
paul@684 | 164 | |
paul@684 | 165 | """ |
paul@684 | 166 | Set an attribute on 'node' having the given 'name' and 'value', adding |
paul@684 | 167 | entries to the 'files' list if file upload fields are detected. |
paul@684 | 168 | """ |
paul@684 | 169 | |
paul@684 | 170 | # Convert the value to Unicode if necessary. |
paulb@23 | 171 | |
paul@684 | 172 | if type(value) == type(""): |
paul@684 | 173 | value = unicode(value, encoding=self.encoding) |
paul@684 | 174 | |
paul@684 | 175 | # Remove CR characters, ignoring non-textual parameters. |
paulb@23 | 176 | |
paul@684 | 177 | if isinstance(value, (str, unicode)): |
paul@684 | 178 | node.setAttributeNS(EMPTY_NAMESPACE, name, value.replace("\r", "")) |
paul@684 | 179 | |
paul@684 | 180 | # Handle file uploads having certain attributes. |
paulb@580 | 181 | |
paul@684 | 182 | elif hasattr(value, "content") and hasattr(value, "filename"): |
paul@684 | 183 | node.setAttributeNS(FILE_NAMESPACE, "file:" + name, str(len(files))) |
paul@684 | 184 | node.setAttributeNS(EMPTY_NAMESPACE, name, value.filename) |
paul@684 | 185 | files.append((value.filename, value.content)) |
paulb@23 | 186 | |
paulb@479 | 187 | def complete_selectors(self, selectors, fields, documents, create): |
paulb@1 | 188 | |
paulb@1 | 189 | """ |
paulb@1 | 190 | Fill in the given 'selectors' dictionary using the given |
paulb@1 | 191 | 'fields' so that it contains mappings from selector names to |
paulb@479 | 192 | parts of the specified 'documents'. If 'create' is set to a |
paulb@479 | 193 | true value, selected elements will be created if not already |
paulb@479 | 194 | present; otherwise, ignore such selectors. |
paulb@1 | 195 | """ |
paulb@1 | 196 | |
paulb@1 | 197 | for field, value in fields: |
paulb@1 | 198 | |
paulb@1 | 199 | # Process selectors only. |
paulb@1 | 200 | |
paulb@16 | 201 | selector_components = field.split(Constants.selector_indicator) |
paulb@1 | 202 | if len(selector_components) < 2: |
paulb@1 | 203 | continue |
paulb@1 | 204 | |
paulb@1 | 205 | # Get the selector name and path. |
paulb@1 | 206 | # Note that the joining of the components uses the separator, |
paulb@1 | 207 | # but the separator really should not exist in the path. |
paulb@1 | 208 | |
paulb@1 | 209 | selector_name = selector_components[0] |
paulb@16 | 210 | path = Constants.selector_indicator.join(selector_components[1:]) |
paulb@1 | 211 | |
paulb@1 | 212 | model_name, components = self._get_model_name_and_components(path) |
paulb@1 | 213 | if model_name is None: |
paulb@1 | 214 | continue |
paulb@1 | 215 | |
paulb@1 | 216 | # Go to the instance element. |
paulb@1 | 217 | |
paulb@1 | 218 | if not documents.has_key(model_name) or documents[model_name] is None: |
paulb@1 | 219 | continue |
paulb@1 | 220 | |
paulb@1 | 221 | node = documents[model_name] |
paulb@1 | 222 | |
paulb@1 | 223 | # Traverse the path to find the part of the document to be |
paulb@1 | 224 | # selected. |
paulb@1 | 225 | |
paulb@1 | 226 | for component in components: |
paulb@16 | 227 | t = component.split(Constants.pair_separator) |
paulb@1 | 228 | if len(t) == 1: |
paulb@1 | 229 | |
paulb@1 | 230 | # Select attribute. |
paulb@1 | 231 | |
paulb@1 | 232 | node = node.getAttributeNodeNS(EMPTY_NAMESPACE, t[0]) |
paulb@1 | 233 | break |
paulb@1 | 234 | |
paulb@1 | 235 | elif len(t) == 2: |
paulb@1 | 236 | |
paulb@1 | 237 | # Convert from one-based indexing (the position() function) |
paulb@1 | 238 | # to zero-based indexing. |
paulb@1 | 239 | |
paulb@1 | 240 | name, index = t[0], int(t[1]) - 1 |
paulb@1 | 241 | if index < 0: |
paulb@1 | 242 | break |
paulb@1 | 243 | |
paulb@479 | 244 | # If create is set, create selected elements. |
paulb@479 | 245 | |
paulb@479 | 246 | if create: |
paulb@479 | 247 | try: |
paulb@479 | 248 | node = self._enter_element(node, name, index) |
paulb@479 | 249 | except FieldsError, exc: |
paulb@479 | 250 | raise FieldsError, "In field '%s', name '%s' and index '%s' could not be added, since '%s' was found." % ( |
paulb@479 | 251 | field, name, index, exc.args[0]) |
paulb@479 | 252 | |
paulb@476 | 253 | # Where a node cannot be found, do not create a selector. |
paulb@1 | 254 | |
paulb@479 | 255 | else: |
paulb@479 | 256 | node = self._find_element(node, name, index) |
paulb@479 | 257 | if node is None: |
paulb@479 | 258 | break |
paulb@1 | 259 | |
paulb@1 | 260 | if not selectors.has_key(selector_name): |
paulb@1 | 261 | selectors[selector_name] = [] |
paulb@476 | 262 | if node is not None: |
paulb@476 | 263 | selectors[selector_name].append(node) |
paulb@1 | 264 | |
paulb@29 | 265 | def _append_element(self, node, name): |
paulb@29 | 266 | |
paulb@29 | 267 | """ |
paulb@29 | 268 | Within 'node' append an element with the given 'name'. |
paulb@29 | 269 | """ |
paulb@29 | 270 | |
paulb@29 | 271 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@29 | 272 | node.appendChild(new_node) |
paulb@29 | 273 | return new_node |
paulb@29 | 274 | |
paulb@1 | 275 | def _enter_element(self, node, name, index): |
paulb@1 | 276 | |
paulb@1 | 277 | """ |
paulb@1 | 278 | From 'node' enter the element with the given 'name' at the |
paulb@1 | 279 | given 'index' position amongst the child elements. Create |
paulb@1 | 280 | missing child elements if necessary. |
paulb@1 | 281 | """ |
paulb@1 | 282 | |
paulb@1 | 283 | self._ensure_elements(node, index) |
paulb@1 | 284 | |
paulb@1 | 285 | elements = node.xpath("*") |
paulb@1 | 286 | if elements[index].localName == "placeholder": |
paulb@1 | 287 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@1 | 288 | node.replaceChild(new_node, elements[index]) |
paulb@1 | 289 | else: |
paulb@1 | 290 | new_node = elements[index] |
paulb@1 | 291 | if new_node.localName != name: |
paulb@1 | 292 | raise FieldsError, (new_node.localName, name, elements, index) |
paulb@1 | 293 | |
paulb@1 | 294 | # Enter the newly-created element. |
paulb@1 | 295 | |
paulb@1 | 296 | return new_node |
paulb@1 | 297 | |
paulb@476 | 298 | def _find_element(self, node, name, index): |
paulb@476 | 299 | |
paulb@476 | 300 | """ |
paulb@476 | 301 | From 'node' find the element with the given 'name' at the |
paulb@476 | 302 | given 'index' position amongst the child elements. Return |
paulb@476 | 303 | None if no such element exists. |
paulb@476 | 304 | """ |
paulb@476 | 305 | |
paulb@476 | 306 | elements = node.xpath("*") |
paulb@476 | 307 | try: |
paulb@476 | 308 | new_node = elements[index] |
paulb@476 | 309 | if new_node.localName != name: |
paulb@476 | 310 | return None |
paulb@476 | 311 | except IndexError: |
paulb@476 | 312 | return None |
paulb@476 | 313 | return new_node |
paulb@476 | 314 | |
paulb@1 | 315 | def _get_model_name_and_components(self, field): |
paulb@1 | 316 | |
paulb@1 | 317 | """ |
paulb@1 | 318 | From 'field', return the model name and components which |
paulb@1 | 319 | describe the path within the instance document associated |
paulb@1 | 320 | with that model. |
paulb@1 | 321 | """ |
paulb@1 | 322 | |
paulb@1 | 323 | # Get the components of the field name. |
paulb@1 | 324 | # Example: /name1#n1/name2#n2/name3 |
paulb@1 | 325 | # Expected: ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 326 | |
paulb@16 | 327 | components = field.split(Constants.path_separator) |
paulb@1 | 328 | if len(components) < 2: |
paulb@1 | 329 | return None, None |
paulb@1 | 330 | |
paulb@1 | 331 | # Extract the model name from the top-level element |
paulb@1 | 332 | # specification. |
paulb@1 | 333 | # Expected: ['name1', 'n1'] |
paulb@1 | 334 | |
paulb@16 | 335 | model_name_and_index = components[1].split(Constants.pair_separator) |
paulb@1 | 336 | if len(model_name_and_index) != 2: |
paulb@1 | 337 | return None, None |
paulb@1 | 338 | |
paulb@1 | 339 | # Expected: 'name1', ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 340 | |
paulb@1 | 341 | return model_name_and_index[0], components[1:] |
paulb@1 | 342 | |
paulb@1 | 343 | def _ensure_elements(self, document, index): |
paulb@1 | 344 | |
paulb@1 | 345 | """ |
paulb@1 | 346 | In the given 'document', extend the child elements list |
paulb@1 | 347 | so that a node can be stored at the given 'index'. |
paulb@1 | 348 | """ |
paulb@1 | 349 | |
paulb@1 | 350 | elements = document.xpath("*") |
paulb@1 | 351 | i = len(elements) |
paulb@1 | 352 | while i <= index: |
paulb@1 | 353 | new_node = document.ownerDocument.createElementNS(EMPTY_NAMESPACE, "placeholder") |
paulb@1 | 354 | document.appendChild(new_node) |
paulb@1 | 355 | i += 1 |
paulb@1 | 356 | |
paulb@1 | 357 | def make_documents(self, fields): |
paulb@1 | 358 | |
paulb@1 | 359 | """ |
paulb@1 | 360 | Make a dictionary mapping model names to new documents prepared |
paulb@1 | 361 | from the given 'fields' dictionary. |
paulb@1 | 362 | """ |
paulb@1 | 363 | |
paulb@1 | 364 | documents = {} |
paulb@1 | 365 | self.complete_documents(documents, fields) |
paulb@1 | 366 | |
paulb@1 | 367 | # Fix the dictionary to return the actual document root. |
paulb@1 | 368 | |
paulb@1 | 369 | for model_name, instance_root in documents.items(): |
paulb@1 | 370 | documents[model_name] = instance_root |
paulb@1 | 371 | return documents |
paulb@1 | 372 | |
paulb@479 | 373 | def get_selectors(self, fields, documents, create=0): |
paulb@1 | 374 | |
paulb@1 | 375 | """ |
paulb@1 | 376 | Get a dictionary containing a mapping of selector names to |
paulb@479 | 377 | selected parts of the given 'documents'. If 'create' is set |
paulb@479 | 378 | to a true value, selected elements will be created if not |
paulb@479 | 379 | already present. |
paulb@1 | 380 | """ |
paulb@1 | 381 | |
paulb@1 | 382 | selectors = {} |
paulb@479 | 383 | self.complete_selectors(selectors, fields, documents, create) |
paulb@1 | 384 | return selectors |
paulb@1 | 385 | |
paulb@3 | 386 | def new_instance(self, name): |
paulb@1 | 387 | |
paulb@1 | 388 | "Return an instance root of the given 'name' in a new document." |
paulb@1 | 389 | |
paulb@1 | 390 | return libxml2dom.createDocument(EMPTY_NAMESPACE, name, None) |
paulb@1 | 391 | |
paulb@277 | 392 | # An alias for the older method name. |
paulb@277 | 393 | |
paulb@277 | 394 | new_document = new_instance |
paulb@277 | 395 | |
paulb@81 | 396 | # NOTE: Legacy name exposure. |
paulb@81 | 397 | |
paulb@81 | 398 | Fields = FieldProcessor |
paulb@81 | 399 | |
paulb@81 | 400 | class Form(FieldProcessor): |
paulb@81 | 401 | |
paulb@81 | 402 | "A collection of documents processed from form fields." |
paulb@81 | 403 | |
paulb@81 | 404 | def __init__(self, *args, **kw): |
paulb@291 | 405 | |
paulb@291 | 406 | """ |
paulb@291 | 407 | Initialise the form data container with the general 'args' and 'kw' |
paulb@291 | 408 | parameters. |
paulb@291 | 409 | """ |
paulb@291 | 410 | |
paulb@81 | 411 | FieldProcessor.__init__(self, *args, **kw) |
paulb@81 | 412 | self.parameters = {} |
paulb@81 | 413 | self.documents = {} |
paulb@665 | 414 | self.new_documents = set() |
paulb@81 | 415 | |
paulb@640 | 416 | # Activity-related attributes. |
paulb@640 | 417 | |
paulb@640 | 418 | self.current_activity = None |
paulb@640 | 419 | |
paulb@81 | 420 | def set_parameters(self, parameters): |
paulb@291 | 421 | |
paulb@291 | 422 | "Set the request 'parameters' (or fields) in the container." |
paulb@291 | 423 | |
paulb@81 | 424 | self.parameters = parameters |
paulb@81 | 425 | self.documents = self.make_documents(self.parameters.items()) |
paulb@81 | 426 | |
paulb@81 | 427 | def get_parameters(self): |
paulb@291 | 428 | |
paulb@291 | 429 | """ |
paulb@291 | 430 | Get the request parameters (or fields) from the container. Note that |
paulb@291 | 431 | these parameters comprise the raw form field values submitted in a |
paulb@291 | 432 | request rather than the structured form data. |
paulb@291 | 433 | |
paulb@291 | 434 | Return a dictionary mapping parameter names to values. |
paulb@291 | 435 | """ |
paulb@291 | 436 | |
paulb@81 | 437 | return self.parameters |
paulb@81 | 438 | |
paulb@81 | 439 | def get_documents(self): |
paulb@291 | 440 | |
paulb@291 | 441 | """ |
paulb@291 | 442 | Get the form data documents from the container, returning a dictionary |
paulb@291 | 443 | mapping document names to DOM-style document objects. |
paulb@291 | 444 | """ |
paulb@291 | 445 | |
paulb@81 | 446 | return self.documents |
paulb@81 | 447 | |
paulb@665 | 448 | # NOTE: Was get_document. |
paulb@665 | 449 | |
paulb@665 | 450 | def _get_document(self, name): |
paulb@366 | 451 | |
paulb@366 | 452 | """ |
paulb@366 | 453 | Get the form data document with the given 'name' from the container, |
paulb@366 | 454 | returning a DOM-style document object if such a document exists, or None |
paulb@366 | 455 | if no such document can be found. |
paulb@366 | 456 | """ |
paulb@366 | 457 | |
paulb@366 | 458 | return self.documents.get(name) |
paulb@366 | 459 | |
paulb@479 | 460 | def get_selectors(self, create=0): |
paulb@291 | 461 | |
paulb@291 | 462 | """ |
paulb@291 | 463 | Get the form data selectors from the container, returning a dictionary |
paulb@479 | 464 | mapping selector names to collections of selected elements. If 'create' |
paulb@479 | 465 | is set to a true value (unlike the default), the selected elements will |
paulb@479 | 466 | be created in the form data document if not already present. |
paulb@291 | 467 | """ |
paulb@291 | 468 | |
paulb@479 | 469 | return FieldProcessor.get_selectors(self, self.parameters.items(), self.documents, create) |
paulb@479 | 470 | |
paulb@479 | 471 | def get_selector(self, name, create=0): |
paulb@479 | 472 | |
paulb@479 | 473 | """ |
paulb@479 | 474 | Get the form data selectors for the given 'name', returning a collection |
paulb@479 | 475 | of selected elements. If 'create' is set to a true value (unlike the |
paulb@479 | 476 | default), the selected elements will be created in the form data |
paulb@479 | 477 | document if not already present. |
paulb@479 | 478 | """ |
paulb@479 | 479 | |
paulb@479 | 480 | parameters = [] |
paulb@479 | 481 | for parameter_name, value in parameters.items(): |
paulb@479 | 482 | if parameter_name.startswith(name + Constants.selector_indicator): |
paulb@479 | 483 | parameters.append((parameter_name, value)) |
paulb@479 | 484 | return FieldProcessor.get_selectors(self, parameters, self.documents, create) |
paulb@81 | 485 | |
paul@684 | 486 | def get_files(self): |
paul@684 | 487 | |
paul@684 | 488 | """ |
paul@684 | 489 | Get the uploaded file details as a list of tuples of the form |
paul@684 | 490 | (filename, content). |
paul@684 | 491 | """ |
paul@684 | 492 | |
paul@684 | 493 | return self.documents.get(None, []) |
paul@684 | 494 | |
paulb@120 | 495 | def new_instance(self, name): |
paulb@291 | 496 | |
paulb@291 | 497 | """ |
paulb@291 | 498 | Make a new document with the given 'name', storing it in the container |
paulb@291 | 499 | and returning the document. |
paulb@291 | 500 | """ |
paulb@291 | 501 | |
paulb@120 | 502 | doc = FieldProcessor.new_instance(self, name) |
paulb@120 | 503 | self.documents[name] = doc |
paulb@120 | 504 | return doc |
paulb@120 | 505 | |
paulb@291 | 506 | # An alias for the older method name. |
paulb@291 | 507 | |
paulb@291 | 508 | new_document = new_instance |
paulb@291 | 509 | |
paulb@650 | 510 | # NOTE: Was set_document. |
paulb@650 | 511 | |
paulb@650 | 512 | def _set_document(self, name, doc): |
paulb@291 | 513 | |
paulb@291 | 514 | """ |
paulb@291 | 515 | Store in the container under the given 'name' the supplied document |
paulb@291 | 516 | 'doc'. |
paulb@291 | 517 | """ |
paulb@291 | 518 | |
paulb@288 | 519 | self.documents[name] = doc |
paulb@288 | 520 | |
paulb@640 | 521 | # Support for activities and the main/default document. |
paulb@640 | 522 | |
paulb@640 | 523 | def set_activity(self, name): |
paulb@640 | 524 | self.current_activity = name |
paulb@640 | 525 | |
paulb@640 | 526 | def get_activity(self): |
paulb@640 | 527 | return self.current_activity |
paulb@640 | 528 | |
paulb@665 | 529 | # NOTE: Signatures are flexible to support the older methods above. |
paulb@650 | 530 | |
paulb@650 | 531 | def set_document(self, name_or_doc, doc=None): |
paulb@650 | 532 | if doc is not None: |
paulb@650 | 533 | self._set_document(name_or_doc, doc) |
paulb@650 | 534 | else: |
paulb@665 | 535 | self._set_document(self.current_activity, name_or_doc) |
paulb@640 | 536 | |
paulb@665 | 537 | def get_document(self, name=None): |
paulb@665 | 538 | if name is None: |
paulb@665 | 539 | return self._get_document(self.current_activity) |
paulb@665 | 540 | else: |
paulb@665 | 541 | return self._get_document(name) |
paulb@665 | 542 | |
paulb@665 | 543 | def is_new_document(self, name=None): |
paulb@665 | 544 | if name is None: |
paulb@665 | 545 | return self.current_activity in self.new_documents |
paulb@665 | 546 | else: |
paulb@665 | 547 | return name in self.new_documents |
paulb@640 | 548 | |
paulb@1 | 549 | if __name__ == "__main__": |
paulb@1 | 550 | |
paulb@8 | 551 | items = [ |
paulb@1 | 552 | ("_action_update", "Some value"), |
paulb@142 | 553 | ("_action_delete=/zoo$1/cage$2", "Some value"), |
paulb@476 | 554 | ("_action_nasty=/zoo$1/cage$3", "Some value"), |
paulb@142 | 555 | ("/actions$1/update$1/selected", "Some value"), # Not actually used in output documents or input. |
paulb@142 | 556 | ("/zoo$1/name", "The Zoo ???"), |
paulb@142 | 557 | ("/zoo$1/cage$1/name", "reptiles"), |
paulb@142 | 558 | ("/zoo$1/cage$1/capacity", "5"), |
paulb@142 | 559 | ("/zoo$1/cage$1/animal$1/name", "Monty"), |
paulb@142 | 560 | ("/zoo$1/cage$1/animal$1/species$1/name", "Python"), |
paulb@142 | 561 | ("/zoo$1/cage$1/animal$1/property$2/name", "texture"), |
paulb@142 | 562 | ("/zoo$1/cage$1/animal$1/property$2/value", "scaled"), |
paulb@142 | 563 | ("/zoo$1/cage$1/animal$1/property$3/name", "length"), |
paulb@142 | 564 | ("/zoo$1/cage$1/animal$1/property$3/value", "5m"), |
paulb@142 | 565 | ("/zoo$1/cage$1/animal$2/name", "Vincent"), |
paulb@142 | 566 | ("/zoo$1/cage$1/animal$2/species$1/name", "Lizard"), |
paulb@142 | 567 | ("/zoo$1/cage$1/animal$2/property$2/name", "colour"), |
paulb@142 | 568 | ("/zoo$1/cage$1/animal$2/property$2/value", "variable"), |
paulb@142 | 569 | ("/zoo$1/cage$1/animal$2/property$3/name", "length"), |
paulb@142 | 570 | ("/zoo$1/cage$1/animal$2/property$3/value", "1m"), |
paulb@142 | 571 | ("/zoo$1/cage$2/name", "mammals"), |
paulb@142 | 572 | ("/zoo$1/cage$2/capacity", "25"), |
paulb@142 | 573 | ("/zoo$1/cage$2/animal$1/name", "Simon"), |
paulb@142 | 574 | ("/zoo$1/cage$2/animal$1/species$1/name", "Giraffe"), |
paulb@142 | 575 | ("/zoo$1/cage$2/animal$2/name", "Leonard"), |
paulb@142 | 576 | ("/zoo$1/cage$2/animal$2/species$1/name", "Lion"), |
paulb@142 | 577 | ("/zoo$1/cage$2/animal$2/property$2/name", "danger"), |
paulb@142 | 578 | ("/zoo$1/cage$2/animal$2/property$2/value", "high"), |
paulb@142 | 579 | ("/zoo$1/funding$3/type", "private"), |
paulb@142 | 580 | ("/zoo$1/funding$3/contributor$1/name", "Animal Corporation"), |
paulb@142 | 581 | ("/zoo$1/funding$3/contributor$1/amount", "543210.987"), |
paulb@142 | 582 | ("/zoo$1/funding$3/contributor$1/industry$$type", "animals") |
paulb@1 | 583 | ] |
paulb@1 | 584 | |
paulb@1 | 585 | import time |
paulb@1 | 586 | import sys, cmdsyntax |
paulb@1 | 587 | |
paulb@1 | 588 | # Find the documents. |
paulb@1 | 589 | |
paulb@1 | 590 | syntax = cmdsyntax.Syntax(""" |
paulb@1 | 591 | --plain-output=OUTPUT_FILE |
paulb@1 | 592 | --instance-name=NAME |
paulb@1 | 593 | """) |
paulb@1 | 594 | |
paulb@1 | 595 | syntax_matches = syntax.get_args(sys.argv[1:]) |
paulb@1 | 596 | |
paulb@1 | 597 | try: |
paulb@1 | 598 | args = syntax_matches[0] |
paulb@1 | 599 | except IndexError: |
paulb@1 | 600 | print syntax.syntax |
paulb@1 | 601 | sys.exit(1) |
paulb@1 | 602 | |
paulb@1 | 603 | # Create an object to interpret the test data. |
paulb@1 | 604 | |
paulb@81 | 605 | fields = FieldProcessor("iso-8859-1") |
paulb@1 | 606 | |
paulb@1 | 607 | t = time.time() |
paulb@8 | 608 | documents = fields.make_documents(items) |
paulb@1 | 609 | print "Building time", time.time() - t |
paulb@1 | 610 | |
paulb@1 | 611 | t = time.time() |
paulb@66 | 612 | documents[args["instance-name"]].toStream(stream=open(args["plain-output"], "wb"), encoding="utf-8") |
paulb@1 | 613 | print "Prettyprinting time", time.time() - t |
paulb@1 | 614 | |
paulb@8 | 615 | print "Selectors", repr(fields.get_selectors(items, documents)) |
paulb@1 | 616 | |
paulb@1 | 617 | # vim: tabstop=4 expandtab shiftwidth=4 |