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 | |
paulb@665 | 8 | Copyright (C) 2005, 2006, 2007, 2008 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 | |
paulb@1 | 62 | class FieldsError(Exception): |
paulb@1 | 63 | pass |
paulb@1 | 64 | |
paulb@81 | 65 | class FieldProcessor: |
paulb@1 | 66 | |
paulb@1 | 67 | """ |
paulb@1 | 68 | A class which converts fields in the documented form to XML |
paulb@1 | 69 | instance documents. |
paulb@1 | 70 | """ |
paulb@1 | 71 | |
paulb@3 | 72 | def __init__(self, encoding="utf-8", values_are_lists=0): |
paulb@1 | 73 | |
paulb@1 | 74 | """ |
paulb@1 | 75 | Initialise the fields processor with the given 'encoding', |
paulb@1 | 76 | which is optional and which only applies to field data in |
paulb@1 | 77 | Python string form (and not Unicode objects). |
paulb@3 | 78 | |
paulb@3 | 79 | If the optional 'values_are_lists' parameter is set to true |
paulb@3 | 80 | then each actual field value will be obtained by taking the |
paulb@3 | 81 | first element from each supplied field value. |
paulb@1 | 82 | """ |
paulb@1 | 83 | |
paulb@1 | 84 | self.encoding = encoding |
paulb@3 | 85 | self.values_are_lists = values_are_lists |
paulb@1 | 86 | |
paulb@1 | 87 | def complete_documents(self, documents, fields): |
paulb@1 | 88 | |
paulb@1 | 89 | """ |
paulb@1 | 90 | Complete the given 'documents' using the 'fields' items list. |
paulb@1 | 91 | """ |
paulb@1 | 92 | |
paulb@1 | 93 | for field, value in fields: |
paulb@1 | 94 | |
paulb@1 | 95 | # Ignore selectors. |
paulb@1 | 96 | |
paulb@16 | 97 | if field.find(Constants.selector_indicator) != -1: |
paulb@1 | 98 | continue |
paulb@1 | 99 | |
paulb@1 | 100 | model_name, components = self._get_model_name_and_components(field) |
paulb@1 | 101 | if model_name is None: |
paulb@1 | 102 | continue |
paulb@1 | 103 | |
paulb@1 | 104 | # Get a new instance document if none has been made for the |
paulb@1 | 105 | # model. |
paulb@1 | 106 | |
paulb@1 | 107 | if not documents.has_key(model_name): |
paulb@3 | 108 | documents[model_name] = self.new_instance(model_name) |
paulb@1 | 109 | node = documents[model_name] |
paulb@1 | 110 | |
paulb@1 | 111 | # Traverse the components within the instance. |
paulb@1 | 112 | |
paulb@1 | 113 | for component in components: |
paulb@16 | 114 | t = component.split(Constants.pair_separator) |
paulb@1 | 115 | if len(t) == 1: |
paulb@21 | 116 | |
paulb@21 | 117 | # Convert from lists if necessary. |
paulb@21 | 118 | |
paulb@21 | 119 | if self.values_are_lists: |
paulb@21 | 120 | value = value[0] |
paulb@21 | 121 | |
paulb@21 | 122 | # Convert the value to Unicode if necessary. |
paulb@21 | 123 | |
paulb@21 | 124 | if type(value) == type(""): |
paulb@21 | 125 | value = unicode(value, encoding=self.encoding) |
paulb@21 | 126 | |
paul@674 | 127 | # Remove CR characters, ignoring non-textual parameters. |
paulb@580 | 128 | |
paul@674 | 129 | if isinstance(value, (str, unicode)): |
paul@674 | 130 | node.setAttributeNS(EMPTY_NAMESPACE, t[0], value.replace("\r", "")) |
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) |
paulb@23 | 159 | |
paulb@23 | 160 | # Convert the value to Unicode if necessary. |
paulb@23 | 161 | |
paulb@23 | 162 | if type(subvalue) == type(""): |
paulb@23 | 163 | subvalue = unicode(subvalue, encoding=self.encoding) |
paulb@23 | 164 | |
paul@674 | 165 | # Remove CR characters, ignoring non-textual parameters. |
paulb@580 | 166 | |
paul@675 | 167 | if isinstance(subvalue, (str, unicode)): |
paul@674 | 168 | subnode.setAttributeNS(EMPTY_NAMESPACE, t[2], subvalue.replace("\r", "")) |
paulb@23 | 169 | |
paulb@479 | 170 | def complete_selectors(self, selectors, fields, documents, create): |
paulb@1 | 171 | |
paulb@1 | 172 | """ |
paulb@1 | 173 | Fill in the given 'selectors' dictionary using the given |
paulb@1 | 174 | 'fields' so that it contains mappings from selector names to |
paulb@479 | 175 | parts of the specified 'documents'. If 'create' is set to a |
paulb@479 | 176 | true value, selected elements will be created if not already |
paulb@479 | 177 | present; otherwise, ignore such selectors. |
paulb@1 | 178 | """ |
paulb@1 | 179 | |
paulb@1 | 180 | for field, value in fields: |
paulb@1 | 181 | |
paulb@1 | 182 | # Process selectors only. |
paulb@1 | 183 | |
paulb@16 | 184 | selector_components = field.split(Constants.selector_indicator) |
paulb@1 | 185 | if len(selector_components) < 2: |
paulb@1 | 186 | continue |
paulb@1 | 187 | |
paulb@1 | 188 | # Get the selector name and path. |
paulb@1 | 189 | # Note that the joining of the components uses the separator, |
paulb@1 | 190 | # but the separator really should not exist in the path. |
paulb@1 | 191 | |
paulb@1 | 192 | selector_name = selector_components[0] |
paulb@16 | 193 | path = Constants.selector_indicator.join(selector_components[1:]) |
paulb@1 | 194 | |
paulb@1 | 195 | model_name, components = self._get_model_name_and_components(path) |
paulb@1 | 196 | if model_name is None: |
paulb@1 | 197 | continue |
paulb@1 | 198 | |
paulb@1 | 199 | # Go to the instance element. |
paulb@1 | 200 | |
paulb@1 | 201 | if not documents.has_key(model_name) or documents[model_name] is None: |
paulb@1 | 202 | continue |
paulb@1 | 203 | |
paulb@1 | 204 | node = documents[model_name] |
paulb@1 | 205 | |
paulb@1 | 206 | # Traverse the path to find the part of the document to be |
paulb@1 | 207 | # selected. |
paulb@1 | 208 | |
paulb@1 | 209 | for component in components: |
paulb@16 | 210 | t = component.split(Constants.pair_separator) |
paulb@1 | 211 | if len(t) == 1: |
paulb@1 | 212 | |
paulb@1 | 213 | # Select attribute. |
paulb@1 | 214 | |
paulb@1 | 215 | node = node.getAttributeNodeNS(EMPTY_NAMESPACE, t[0]) |
paulb@1 | 216 | break |
paulb@1 | 217 | |
paulb@1 | 218 | elif len(t) == 2: |
paulb@1 | 219 | |
paulb@1 | 220 | # Convert from one-based indexing (the position() function) |
paulb@1 | 221 | # to zero-based indexing. |
paulb@1 | 222 | |
paulb@1 | 223 | name, index = t[0], int(t[1]) - 1 |
paulb@1 | 224 | if index < 0: |
paulb@1 | 225 | break |
paulb@1 | 226 | |
paulb@479 | 227 | # If create is set, create selected elements. |
paulb@479 | 228 | |
paulb@479 | 229 | if create: |
paulb@479 | 230 | try: |
paulb@479 | 231 | node = self._enter_element(node, name, index) |
paulb@479 | 232 | except FieldsError, exc: |
paulb@479 | 233 | raise FieldsError, "In field '%s', name '%s' and index '%s' could not be added, since '%s' was found." % ( |
paulb@479 | 234 | field, name, index, exc.args[0]) |
paulb@479 | 235 | |
paulb@476 | 236 | # Where a node cannot be found, do not create a selector. |
paulb@1 | 237 | |
paulb@479 | 238 | else: |
paulb@479 | 239 | node = self._find_element(node, name, index) |
paulb@479 | 240 | if node is None: |
paulb@479 | 241 | break |
paulb@1 | 242 | |
paulb@1 | 243 | if not selectors.has_key(selector_name): |
paulb@1 | 244 | selectors[selector_name] = [] |
paulb@476 | 245 | if node is not None: |
paulb@476 | 246 | selectors[selector_name].append(node) |
paulb@1 | 247 | |
paulb@29 | 248 | def _append_element(self, node, name): |
paulb@29 | 249 | |
paulb@29 | 250 | """ |
paulb@29 | 251 | Within 'node' append an element with the given 'name'. |
paulb@29 | 252 | """ |
paulb@29 | 253 | |
paulb@29 | 254 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@29 | 255 | node.appendChild(new_node) |
paulb@29 | 256 | return new_node |
paulb@29 | 257 | |
paulb@1 | 258 | def _enter_element(self, node, name, index): |
paulb@1 | 259 | |
paulb@1 | 260 | """ |
paulb@1 | 261 | From 'node' enter the element with the given 'name' at the |
paulb@1 | 262 | given 'index' position amongst the child elements. Create |
paulb@1 | 263 | missing child elements if necessary. |
paulb@1 | 264 | """ |
paulb@1 | 265 | |
paulb@1 | 266 | self._ensure_elements(node, index) |
paulb@1 | 267 | |
paulb@1 | 268 | elements = node.xpath("*") |
paulb@1 | 269 | if elements[index].localName == "placeholder": |
paulb@1 | 270 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@1 | 271 | node.replaceChild(new_node, elements[index]) |
paulb@1 | 272 | else: |
paulb@1 | 273 | new_node = elements[index] |
paulb@1 | 274 | if new_node.localName != name: |
paulb@1 | 275 | raise FieldsError, (new_node.localName, name, elements, index) |
paulb@1 | 276 | |
paulb@1 | 277 | # Enter the newly-created element. |
paulb@1 | 278 | |
paulb@1 | 279 | return new_node |
paulb@1 | 280 | |
paulb@476 | 281 | def _find_element(self, node, name, index): |
paulb@476 | 282 | |
paulb@476 | 283 | """ |
paulb@476 | 284 | From 'node' find the element with the given 'name' at the |
paulb@476 | 285 | given 'index' position amongst the child elements. Return |
paulb@476 | 286 | None if no such element exists. |
paulb@476 | 287 | """ |
paulb@476 | 288 | |
paulb@476 | 289 | elements = node.xpath("*") |
paulb@476 | 290 | try: |
paulb@476 | 291 | new_node = elements[index] |
paulb@476 | 292 | if new_node.localName != name: |
paulb@476 | 293 | return None |
paulb@476 | 294 | except IndexError: |
paulb@476 | 295 | return None |
paulb@476 | 296 | return new_node |
paulb@476 | 297 | |
paulb@1 | 298 | def _get_model_name_and_components(self, field): |
paulb@1 | 299 | |
paulb@1 | 300 | """ |
paulb@1 | 301 | From 'field', return the model name and components which |
paulb@1 | 302 | describe the path within the instance document associated |
paulb@1 | 303 | with that model. |
paulb@1 | 304 | """ |
paulb@1 | 305 | |
paulb@1 | 306 | # Get the components of the field name. |
paulb@1 | 307 | # Example: /name1#n1/name2#n2/name3 |
paulb@1 | 308 | # Expected: ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 309 | |
paulb@16 | 310 | components = field.split(Constants.path_separator) |
paulb@1 | 311 | if len(components) < 2: |
paulb@1 | 312 | return None, None |
paulb@1 | 313 | |
paulb@1 | 314 | # Extract the model name from the top-level element |
paulb@1 | 315 | # specification. |
paulb@1 | 316 | # Expected: ['name1', 'n1'] |
paulb@1 | 317 | |
paulb@16 | 318 | model_name_and_index = components[1].split(Constants.pair_separator) |
paulb@1 | 319 | if len(model_name_and_index) != 2: |
paulb@1 | 320 | return None, None |
paulb@1 | 321 | |
paulb@1 | 322 | # Expected: 'name1', ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 323 | |
paulb@1 | 324 | return model_name_and_index[0], components[1:] |
paulb@1 | 325 | |
paulb@1 | 326 | def _ensure_elements(self, document, index): |
paulb@1 | 327 | |
paulb@1 | 328 | """ |
paulb@1 | 329 | In the given 'document', extend the child elements list |
paulb@1 | 330 | so that a node can be stored at the given 'index'. |
paulb@1 | 331 | """ |
paulb@1 | 332 | |
paulb@1 | 333 | elements = document.xpath("*") |
paulb@1 | 334 | i = len(elements) |
paulb@1 | 335 | while i <= index: |
paulb@1 | 336 | new_node = document.ownerDocument.createElementNS(EMPTY_NAMESPACE, "placeholder") |
paulb@1 | 337 | document.appendChild(new_node) |
paulb@1 | 338 | i += 1 |
paulb@1 | 339 | |
paulb@1 | 340 | def make_documents(self, fields): |
paulb@1 | 341 | |
paulb@1 | 342 | """ |
paulb@1 | 343 | Make a dictionary mapping model names to new documents prepared |
paulb@1 | 344 | from the given 'fields' dictionary. |
paulb@1 | 345 | """ |
paulb@1 | 346 | |
paulb@1 | 347 | documents = {} |
paulb@1 | 348 | self.complete_documents(documents, fields) |
paulb@1 | 349 | |
paulb@1 | 350 | # Fix the dictionary to return the actual document root. |
paulb@1 | 351 | |
paulb@1 | 352 | for model_name, instance_root in documents.items(): |
paulb@1 | 353 | documents[model_name] = instance_root |
paulb@1 | 354 | return documents |
paulb@1 | 355 | |
paulb@479 | 356 | def get_selectors(self, fields, documents, create=0): |
paulb@1 | 357 | |
paulb@1 | 358 | """ |
paulb@1 | 359 | Get a dictionary containing a mapping of selector names to |
paulb@479 | 360 | selected parts of the given 'documents'. If 'create' is set |
paulb@479 | 361 | to a true value, selected elements will be created if not |
paulb@479 | 362 | already present. |
paulb@1 | 363 | """ |
paulb@1 | 364 | |
paulb@1 | 365 | selectors = {} |
paulb@479 | 366 | self.complete_selectors(selectors, fields, documents, create) |
paulb@1 | 367 | return selectors |
paulb@1 | 368 | |
paulb@3 | 369 | def new_instance(self, name): |
paulb@1 | 370 | |
paulb@1 | 371 | "Return an instance root of the given 'name' in a new document." |
paulb@1 | 372 | |
paulb@1 | 373 | return libxml2dom.createDocument(EMPTY_NAMESPACE, name, None) |
paulb@1 | 374 | |
paulb@277 | 375 | # An alias for the older method name. |
paulb@277 | 376 | |
paulb@277 | 377 | new_document = new_instance |
paulb@277 | 378 | |
paulb@81 | 379 | # NOTE: Legacy name exposure. |
paulb@81 | 380 | |
paulb@81 | 381 | Fields = FieldProcessor |
paulb@81 | 382 | |
paulb@81 | 383 | class Form(FieldProcessor): |
paulb@81 | 384 | |
paulb@81 | 385 | "A collection of documents processed from form fields." |
paulb@81 | 386 | |
paulb@81 | 387 | def __init__(self, *args, **kw): |
paulb@291 | 388 | |
paulb@291 | 389 | """ |
paulb@291 | 390 | Initialise the form data container with the general 'args' and 'kw' |
paulb@291 | 391 | parameters. |
paulb@291 | 392 | """ |
paulb@291 | 393 | |
paulb@81 | 394 | FieldProcessor.__init__(self, *args, **kw) |
paulb@81 | 395 | self.parameters = {} |
paulb@81 | 396 | self.documents = {} |
paulb@665 | 397 | self.new_documents = set() |
paulb@81 | 398 | |
paulb@640 | 399 | # Activity-related attributes. |
paulb@640 | 400 | |
paulb@640 | 401 | self.current_activity = None |
paulb@640 | 402 | |
paulb@81 | 403 | def set_parameters(self, parameters): |
paulb@291 | 404 | |
paulb@291 | 405 | "Set the request 'parameters' (or fields) in the container." |
paulb@291 | 406 | |
paulb@81 | 407 | self.parameters = parameters |
paulb@81 | 408 | self.documents = self.make_documents(self.parameters.items()) |
paulb@81 | 409 | |
paulb@81 | 410 | def get_parameters(self): |
paulb@291 | 411 | |
paulb@291 | 412 | """ |
paulb@291 | 413 | Get the request parameters (or fields) from the container. Note that |
paulb@291 | 414 | these parameters comprise the raw form field values submitted in a |
paulb@291 | 415 | request rather than the structured form data. |
paulb@291 | 416 | |
paulb@291 | 417 | Return a dictionary mapping parameter names to values. |
paulb@291 | 418 | """ |
paulb@291 | 419 | |
paulb@81 | 420 | return self.parameters |
paulb@81 | 421 | |
paulb@81 | 422 | def get_documents(self): |
paulb@291 | 423 | |
paulb@291 | 424 | """ |
paulb@291 | 425 | Get the form data documents from the container, returning a dictionary |
paulb@291 | 426 | mapping document names to DOM-style document objects. |
paulb@291 | 427 | """ |
paulb@291 | 428 | |
paulb@81 | 429 | return self.documents |
paulb@81 | 430 | |
paulb@665 | 431 | # NOTE: Was get_document. |
paulb@665 | 432 | |
paulb@665 | 433 | def _get_document(self, name): |
paulb@366 | 434 | |
paulb@366 | 435 | """ |
paulb@366 | 436 | Get the form data document with the given 'name' from the container, |
paulb@366 | 437 | returning a DOM-style document object if such a document exists, or None |
paulb@366 | 438 | if no such document can be found. |
paulb@366 | 439 | """ |
paulb@366 | 440 | |
paulb@366 | 441 | return self.documents.get(name) |
paulb@366 | 442 | |
paulb@479 | 443 | def get_selectors(self, create=0): |
paulb@291 | 444 | |
paulb@291 | 445 | """ |
paulb@291 | 446 | Get the form data selectors from the container, returning a dictionary |
paulb@479 | 447 | mapping selector names to collections of selected elements. If 'create' |
paulb@479 | 448 | is set to a true value (unlike the default), the selected elements will |
paulb@479 | 449 | be created in the form data document if not already present. |
paulb@291 | 450 | """ |
paulb@291 | 451 | |
paulb@479 | 452 | return FieldProcessor.get_selectors(self, self.parameters.items(), self.documents, create) |
paulb@479 | 453 | |
paulb@479 | 454 | def get_selector(self, name, create=0): |
paulb@479 | 455 | |
paulb@479 | 456 | """ |
paulb@479 | 457 | Get the form data selectors for the given 'name', returning a collection |
paulb@479 | 458 | of selected elements. If 'create' is set to a true value (unlike the |
paulb@479 | 459 | default), the selected elements will be created in the form data |
paulb@479 | 460 | document if not already present. |
paulb@479 | 461 | """ |
paulb@479 | 462 | |
paulb@479 | 463 | parameters = [] |
paulb@479 | 464 | for parameter_name, value in parameters.items(): |
paulb@479 | 465 | if parameter_name.startswith(name + Constants.selector_indicator): |
paulb@479 | 466 | parameters.append((parameter_name, value)) |
paulb@479 | 467 | return FieldProcessor.get_selectors(self, parameters, self.documents, create) |
paulb@81 | 468 | |
paulb@120 | 469 | def new_instance(self, name): |
paulb@291 | 470 | |
paulb@291 | 471 | """ |
paulb@291 | 472 | Make a new document with the given 'name', storing it in the container |
paulb@291 | 473 | and returning the document. |
paulb@291 | 474 | """ |
paulb@291 | 475 | |
paulb@120 | 476 | doc = FieldProcessor.new_instance(self, name) |
paulb@120 | 477 | self.documents[name] = doc |
paulb@120 | 478 | return doc |
paulb@120 | 479 | |
paulb@291 | 480 | # An alias for the older method name. |
paulb@291 | 481 | |
paulb@291 | 482 | new_document = new_instance |
paulb@291 | 483 | |
paulb@650 | 484 | # NOTE: Was set_document. |
paulb@650 | 485 | |
paulb@650 | 486 | def _set_document(self, name, doc): |
paulb@291 | 487 | |
paulb@291 | 488 | """ |
paulb@291 | 489 | Store in the container under the given 'name' the supplied document |
paulb@291 | 490 | 'doc'. |
paulb@291 | 491 | """ |
paulb@291 | 492 | |
paulb@288 | 493 | self.documents[name] = doc |
paulb@288 | 494 | |
paulb@640 | 495 | # Support for activities and the main/default document. |
paulb@640 | 496 | |
paulb@640 | 497 | def set_activity(self, name): |
paulb@640 | 498 | self.current_activity = name |
paulb@640 | 499 | |
paulb@640 | 500 | def get_activity(self): |
paulb@640 | 501 | return self.current_activity |
paulb@640 | 502 | |
paulb@665 | 503 | # NOTE: Signatures are flexible to support the older methods above. |
paulb@650 | 504 | |
paulb@650 | 505 | def set_document(self, name_or_doc, doc=None): |
paulb@650 | 506 | if doc is not None: |
paulb@650 | 507 | self._set_document(name_or_doc, doc) |
paulb@650 | 508 | else: |
paulb@665 | 509 | self._set_document(self.current_activity, name_or_doc) |
paulb@640 | 510 | |
paulb@665 | 511 | def get_document(self, name=None): |
paulb@665 | 512 | if name is None: |
paulb@665 | 513 | return self._get_document(self.current_activity) |
paulb@665 | 514 | else: |
paulb@665 | 515 | return self._get_document(name) |
paulb@665 | 516 | |
paulb@665 | 517 | def is_new_document(self, name=None): |
paulb@665 | 518 | if name is None: |
paulb@665 | 519 | return self.current_activity in self.new_documents |
paulb@665 | 520 | else: |
paulb@665 | 521 | return name in self.new_documents |
paulb@640 | 522 | |
paulb@1 | 523 | if __name__ == "__main__": |
paulb@1 | 524 | |
paulb@8 | 525 | items = [ |
paulb@1 | 526 | ("_action_update", "Some value"), |
paulb@142 | 527 | ("_action_delete=/zoo$1/cage$2", "Some value"), |
paulb@476 | 528 | ("_action_nasty=/zoo$1/cage$3", "Some value"), |
paulb@142 | 529 | ("/actions$1/update$1/selected", "Some value"), # Not actually used in output documents or input. |
paulb@142 | 530 | ("/zoo$1/name", "The Zoo ???"), |
paulb@142 | 531 | ("/zoo$1/cage$1/name", "reptiles"), |
paulb@142 | 532 | ("/zoo$1/cage$1/capacity", "5"), |
paulb@142 | 533 | ("/zoo$1/cage$1/animal$1/name", "Monty"), |
paulb@142 | 534 | ("/zoo$1/cage$1/animal$1/species$1/name", "Python"), |
paulb@142 | 535 | ("/zoo$1/cage$1/animal$1/property$2/name", "texture"), |
paulb@142 | 536 | ("/zoo$1/cage$1/animal$1/property$2/value", "scaled"), |
paulb@142 | 537 | ("/zoo$1/cage$1/animal$1/property$3/name", "length"), |
paulb@142 | 538 | ("/zoo$1/cage$1/animal$1/property$3/value", "5m"), |
paulb@142 | 539 | ("/zoo$1/cage$1/animal$2/name", "Vincent"), |
paulb@142 | 540 | ("/zoo$1/cage$1/animal$2/species$1/name", "Lizard"), |
paulb@142 | 541 | ("/zoo$1/cage$1/animal$2/property$2/name", "colour"), |
paulb@142 | 542 | ("/zoo$1/cage$1/animal$2/property$2/value", "variable"), |
paulb@142 | 543 | ("/zoo$1/cage$1/animal$2/property$3/name", "length"), |
paulb@142 | 544 | ("/zoo$1/cage$1/animal$2/property$3/value", "1m"), |
paulb@142 | 545 | ("/zoo$1/cage$2/name", "mammals"), |
paulb@142 | 546 | ("/zoo$1/cage$2/capacity", "25"), |
paulb@142 | 547 | ("/zoo$1/cage$2/animal$1/name", "Simon"), |
paulb@142 | 548 | ("/zoo$1/cage$2/animal$1/species$1/name", "Giraffe"), |
paulb@142 | 549 | ("/zoo$1/cage$2/animal$2/name", "Leonard"), |
paulb@142 | 550 | ("/zoo$1/cage$2/animal$2/species$1/name", "Lion"), |
paulb@142 | 551 | ("/zoo$1/cage$2/animal$2/property$2/name", "danger"), |
paulb@142 | 552 | ("/zoo$1/cage$2/animal$2/property$2/value", "high"), |
paulb@142 | 553 | ("/zoo$1/funding$3/type", "private"), |
paulb@142 | 554 | ("/zoo$1/funding$3/contributor$1/name", "Animal Corporation"), |
paulb@142 | 555 | ("/zoo$1/funding$3/contributor$1/amount", "543210.987"), |
paulb@142 | 556 | ("/zoo$1/funding$3/contributor$1/industry$$type", "animals") |
paulb@1 | 557 | ] |
paulb@1 | 558 | |
paulb@1 | 559 | import time |
paulb@1 | 560 | import sys, cmdsyntax |
paulb@1 | 561 | |
paulb@1 | 562 | # Find the documents. |
paulb@1 | 563 | |
paulb@1 | 564 | syntax = cmdsyntax.Syntax(""" |
paulb@1 | 565 | --plain-output=OUTPUT_FILE |
paulb@1 | 566 | --instance-name=NAME |
paulb@1 | 567 | """) |
paulb@1 | 568 | |
paulb@1 | 569 | syntax_matches = syntax.get_args(sys.argv[1:]) |
paulb@1 | 570 | |
paulb@1 | 571 | try: |
paulb@1 | 572 | args = syntax_matches[0] |
paulb@1 | 573 | except IndexError: |
paulb@1 | 574 | print syntax.syntax |
paulb@1 | 575 | sys.exit(1) |
paulb@1 | 576 | |
paulb@1 | 577 | # Create an object to interpret the test data. |
paulb@1 | 578 | |
paulb@81 | 579 | fields = FieldProcessor("iso-8859-1") |
paulb@1 | 580 | |
paulb@1 | 581 | t = time.time() |
paulb@8 | 582 | documents = fields.make_documents(items) |
paulb@1 | 583 | print "Building time", time.time() - t |
paulb@1 | 584 | |
paulb@1 | 585 | t = time.time() |
paulb@66 | 586 | documents[args["instance-name"]].toStream(stream=open(args["plain-output"], "wb"), encoding="utf-8") |
paulb@1 | 587 | print "Prettyprinting time", time.time() - t |
paulb@1 | 588 | |
paulb@8 | 589 | print "Selectors", repr(fields.get_selectors(items, documents)) |
paulb@1 | 590 | |
paulb@1 | 591 | # vim: tabstop=4 expandtab shiftwidth=4 |