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