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