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@116 | 22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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@1 | 161 | def complete_selectors(self, selectors, fields, documents): |
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@1 | 166 | parts of the specified 'documents'. |
paulb@1 | 167 | """ |
paulb@1 | 168 | |
paulb@1 | 169 | for field, value in fields: |
paulb@1 | 170 | |
paulb@1 | 171 | # Process selectors only. |
paulb@1 | 172 | |
paulb@16 | 173 | selector_components = field.split(Constants.selector_indicator) |
paulb@1 | 174 | if len(selector_components) < 2: |
paulb@1 | 175 | continue |
paulb@1 | 176 | |
paulb@1 | 177 | # Get the selector name and path. |
paulb@1 | 178 | # Note that the joining of the components uses the separator, |
paulb@1 | 179 | # but the separator really should not exist in the path. |
paulb@1 | 180 | |
paulb@1 | 181 | selector_name = selector_components[0] |
paulb@16 | 182 | path = Constants.selector_indicator.join(selector_components[1:]) |
paulb@1 | 183 | |
paulb@1 | 184 | model_name, components = self._get_model_name_and_components(path) |
paulb@1 | 185 | if model_name is None: |
paulb@1 | 186 | continue |
paulb@1 | 187 | |
paulb@1 | 188 | # Go to the instance element. |
paulb@1 | 189 | |
paulb@1 | 190 | if not documents.has_key(model_name) or documents[model_name] is None: |
paulb@1 | 191 | continue |
paulb@1 | 192 | |
paulb@1 | 193 | node = documents[model_name] |
paulb@1 | 194 | |
paulb@1 | 195 | # Traverse the path to find the part of the document to be |
paulb@1 | 196 | # selected. |
paulb@1 | 197 | |
paulb@1 | 198 | for component in components: |
paulb@16 | 199 | t = component.split(Constants.pair_separator) |
paulb@1 | 200 | if len(t) == 1: |
paulb@1 | 201 | |
paulb@1 | 202 | # Select attribute. |
paulb@1 | 203 | |
paulb@1 | 204 | node = node.getAttributeNodeNS(EMPTY_NAMESPACE, t[0]) |
paulb@1 | 205 | break |
paulb@1 | 206 | |
paulb@1 | 207 | elif len(t) == 2: |
paulb@1 | 208 | |
paulb@1 | 209 | # Convert from one-based indexing (the position() function) |
paulb@1 | 210 | # to zero-based indexing. |
paulb@1 | 211 | |
paulb@1 | 212 | name, index = t[0], int(t[1]) - 1 |
paulb@1 | 213 | if index < 0: |
paulb@1 | 214 | break |
paulb@1 | 215 | |
paulb@1 | 216 | # NOTE: Controversial creation of potentially non-existent |
paulb@1 | 217 | # NOTE: nodes. |
paulb@1 | 218 | |
paulb@138 | 219 | try: |
paulb@138 | 220 | node = self._enter_element(node, name, index) |
paulb@138 | 221 | except FieldsError, exc: |
paulb@138 | 222 | raise FieldsError, "In field '%s', name '%s' and index '%s' could not be added, since '%s' was found." % ( |
paulb@138 | 223 | field, name, index, exc.args[0]) |
paulb@1 | 224 | |
paulb@1 | 225 | if not selectors.has_key(selector_name): |
paulb@1 | 226 | selectors[selector_name] = [] |
paulb@1 | 227 | selectors[selector_name].append(node) |
paulb@1 | 228 | |
paulb@29 | 229 | def _append_element(self, node, name): |
paulb@29 | 230 | |
paulb@29 | 231 | """ |
paulb@29 | 232 | Within 'node' append an element with the given 'name'. |
paulb@29 | 233 | """ |
paulb@29 | 234 | |
paulb@29 | 235 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@29 | 236 | node.appendChild(new_node) |
paulb@29 | 237 | return new_node |
paulb@29 | 238 | |
paulb@1 | 239 | def _enter_element(self, node, name, index): |
paulb@1 | 240 | |
paulb@1 | 241 | """ |
paulb@1 | 242 | From 'node' enter the element with the given 'name' at the |
paulb@1 | 243 | given 'index' position amongst the child elements. Create |
paulb@1 | 244 | missing child elements if necessary. |
paulb@1 | 245 | """ |
paulb@1 | 246 | |
paulb@1 | 247 | self._ensure_elements(node, index) |
paulb@1 | 248 | |
paulb@1 | 249 | elements = node.xpath("*") |
paulb@1 | 250 | if elements[index].localName == "placeholder": |
paulb@1 | 251 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@1 | 252 | node.replaceChild(new_node, elements[index]) |
paulb@1 | 253 | else: |
paulb@1 | 254 | new_node = elements[index] |
paulb@1 | 255 | if new_node.localName != name: |
paulb@1 | 256 | raise FieldsError, (new_node.localName, name, elements, index) |
paulb@1 | 257 | |
paulb@1 | 258 | # Enter the newly-created element. |
paulb@1 | 259 | |
paulb@1 | 260 | return new_node |
paulb@1 | 261 | |
paulb@1 | 262 | def _get_model_name_and_components(self, field): |
paulb@1 | 263 | |
paulb@1 | 264 | """ |
paulb@1 | 265 | From 'field', return the model name and components which |
paulb@1 | 266 | describe the path within the instance document associated |
paulb@1 | 267 | with that model. |
paulb@1 | 268 | """ |
paulb@1 | 269 | |
paulb@1 | 270 | # Get the components of the field name. |
paulb@1 | 271 | # Example: /name1#n1/name2#n2/name3 |
paulb@1 | 272 | # Expected: ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 273 | |
paulb@16 | 274 | components = field.split(Constants.path_separator) |
paulb@1 | 275 | if len(components) < 2: |
paulb@1 | 276 | return None, None |
paulb@1 | 277 | |
paulb@1 | 278 | # Extract the model name from the top-level element |
paulb@1 | 279 | # specification. |
paulb@1 | 280 | # Expected: ['name1', 'n1'] |
paulb@1 | 281 | |
paulb@16 | 282 | model_name_and_index = components[1].split(Constants.pair_separator) |
paulb@1 | 283 | if len(model_name_and_index) != 2: |
paulb@1 | 284 | return None, None |
paulb@1 | 285 | |
paulb@1 | 286 | # Expected: 'name1', ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 287 | |
paulb@1 | 288 | return model_name_and_index[0], components[1:] |
paulb@1 | 289 | |
paulb@1 | 290 | def _ensure_elements(self, document, index): |
paulb@1 | 291 | |
paulb@1 | 292 | """ |
paulb@1 | 293 | In the given 'document', extend the child elements list |
paulb@1 | 294 | so that a node can be stored at the given 'index'. |
paulb@1 | 295 | """ |
paulb@1 | 296 | |
paulb@1 | 297 | elements = document.xpath("*") |
paulb@1 | 298 | i = len(elements) |
paulb@1 | 299 | while i <= index: |
paulb@1 | 300 | new_node = document.ownerDocument.createElementNS(EMPTY_NAMESPACE, "placeholder") |
paulb@1 | 301 | document.appendChild(new_node) |
paulb@1 | 302 | i += 1 |
paulb@1 | 303 | |
paulb@1 | 304 | def make_documents(self, fields): |
paulb@1 | 305 | |
paulb@1 | 306 | """ |
paulb@1 | 307 | Make a dictionary mapping model names to new documents prepared |
paulb@1 | 308 | from the given 'fields' dictionary. |
paulb@1 | 309 | """ |
paulb@1 | 310 | |
paulb@1 | 311 | documents = {} |
paulb@1 | 312 | self.complete_documents(documents, fields) |
paulb@1 | 313 | |
paulb@1 | 314 | # Fix the dictionary to return the actual document root. |
paulb@1 | 315 | |
paulb@1 | 316 | for model_name, instance_root in documents.items(): |
paulb@1 | 317 | documents[model_name] = instance_root |
paulb@1 | 318 | return documents |
paulb@1 | 319 | |
paulb@1 | 320 | def get_selectors(self, fields, documents): |
paulb@1 | 321 | |
paulb@1 | 322 | """ |
paulb@1 | 323 | Get a dictionary containing a mapping of selector names to |
paulb@1 | 324 | selected parts of the given 'documents'. |
paulb@1 | 325 | """ |
paulb@1 | 326 | |
paulb@1 | 327 | selectors = {} |
paulb@1 | 328 | self.complete_selectors(selectors, fields, documents) |
paulb@1 | 329 | return selectors |
paulb@1 | 330 | |
paulb@3 | 331 | def new_instance(self, name): |
paulb@1 | 332 | |
paulb@1 | 333 | "Return an instance root of the given 'name' in a new document." |
paulb@1 | 334 | |
paulb@1 | 335 | return libxml2dom.createDocument(EMPTY_NAMESPACE, name, None) |
paulb@1 | 336 | |
paulb@277 | 337 | # An alias for the older method name. |
paulb@277 | 338 | |
paulb@277 | 339 | new_document = new_instance |
paulb@277 | 340 | |
paulb@81 | 341 | # NOTE: Legacy name exposure. |
paulb@81 | 342 | |
paulb@81 | 343 | Fields = FieldProcessor |
paulb@81 | 344 | |
paulb@81 | 345 | class Form(FieldProcessor): |
paulb@81 | 346 | |
paulb@81 | 347 | "A collection of documents processed from form fields." |
paulb@81 | 348 | |
paulb@81 | 349 | def __init__(self, *args, **kw): |
paulb@291 | 350 | |
paulb@291 | 351 | """ |
paulb@291 | 352 | Initialise the form data container with the general 'args' and 'kw' |
paulb@291 | 353 | parameters. |
paulb@291 | 354 | """ |
paulb@291 | 355 | |
paulb@81 | 356 | FieldProcessor.__init__(self, *args, **kw) |
paulb@81 | 357 | self.parameters = {} |
paulb@81 | 358 | self.documents = {} |
paulb@81 | 359 | |
paulb@81 | 360 | def set_parameters(self, parameters): |
paulb@291 | 361 | |
paulb@291 | 362 | "Set the request 'parameters' (or fields) in the container." |
paulb@291 | 363 | |
paulb@81 | 364 | self.parameters = parameters |
paulb@81 | 365 | self.documents = self.make_documents(self.parameters.items()) |
paulb@81 | 366 | |
paulb@81 | 367 | def get_parameters(self): |
paulb@291 | 368 | |
paulb@291 | 369 | """ |
paulb@291 | 370 | Get the request parameters (or fields) from the container. Note that |
paulb@291 | 371 | these parameters comprise the raw form field values submitted in a |
paulb@291 | 372 | request rather than the structured form data. |
paulb@291 | 373 | |
paulb@291 | 374 | Return a dictionary mapping parameter names to values. |
paulb@291 | 375 | """ |
paulb@291 | 376 | |
paulb@81 | 377 | return self.parameters |
paulb@81 | 378 | |
paulb@81 | 379 | def get_documents(self): |
paulb@291 | 380 | |
paulb@291 | 381 | """ |
paulb@291 | 382 | Get the form data documents from the container, returning a dictionary |
paulb@291 | 383 | mapping document names to DOM-style document objects. |
paulb@291 | 384 | """ |
paulb@291 | 385 | |
paulb@81 | 386 | return self.documents |
paulb@81 | 387 | |
paulb@81 | 388 | def get_selectors(self): |
paulb@291 | 389 | |
paulb@291 | 390 | """ |
paulb@291 | 391 | Get the form data selectors from the container, returning a dictionary |
paulb@291 | 392 | mapping selector names to collections of selected elements. |
paulb@291 | 393 | """ |
paulb@291 | 394 | |
paulb@81 | 395 | return FieldProcessor.get_selectors(self, self.parameters.items(), self.documents) |
paulb@81 | 396 | |
paulb@120 | 397 | def new_instance(self, name): |
paulb@291 | 398 | |
paulb@291 | 399 | """ |
paulb@291 | 400 | Make a new document with the given 'name', storing it in the container |
paulb@291 | 401 | and returning the document. |
paulb@291 | 402 | """ |
paulb@291 | 403 | |
paulb@120 | 404 | doc = FieldProcessor.new_instance(self, name) |
paulb@120 | 405 | self.documents[name] = doc |
paulb@120 | 406 | return doc |
paulb@120 | 407 | |
paulb@291 | 408 | # An alias for the older method name. |
paulb@291 | 409 | |
paulb@291 | 410 | new_document = new_instance |
paulb@291 | 411 | |
paulb@288 | 412 | def set_document(self, name, doc): |
paulb@291 | 413 | |
paulb@291 | 414 | """ |
paulb@291 | 415 | Store in the container under the given 'name' the supplied document |
paulb@291 | 416 | 'doc'. |
paulb@291 | 417 | """ |
paulb@291 | 418 | |
paulb@288 | 419 | self.documents[name] = doc |
paulb@288 | 420 | |
paulb@1 | 421 | if __name__ == "__main__": |
paulb@1 | 422 | |
paulb@8 | 423 | items = [ |
paulb@1 | 424 | ("_action_update", "Some value"), |
paulb@142 | 425 | ("_action_delete=/zoo$1/cage$2", "Some value"), |
paulb@142 | 426 | ("/actions$1/update$1/selected", "Some value"), # Not actually used in output documents or input. |
paulb@142 | 427 | ("/zoo$1/name", "The Zoo ???"), |
paulb@142 | 428 | ("/zoo$1/cage$1/name", "reptiles"), |
paulb@142 | 429 | ("/zoo$1/cage$1/capacity", "5"), |
paulb@142 | 430 | ("/zoo$1/cage$1/animal$1/name", "Monty"), |
paulb@142 | 431 | ("/zoo$1/cage$1/animal$1/species$1/name", "Python"), |
paulb@142 | 432 | ("/zoo$1/cage$1/animal$1/property$2/name", "texture"), |
paulb@142 | 433 | ("/zoo$1/cage$1/animal$1/property$2/value", "scaled"), |
paulb@142 | 434 | ("/zoo$1/cage$1/animal$1/property$3/name", "length"), |
paulb@142 | 435 | ("/zoo$1/cage$1/animal$1/property$3/value", "5m"), |
paulb@142 | 436 | ("/zoo$1/cage$1/animal$2/name", "Vincent"), |
paulb@142 | 437 | ("/zoo$1/cage$1/animal$2/species$1/name", "Lizard"), |
paulb@142 | 438 | ("/zoo$1/cage$1/animal$2/property$2/name", "colour"), |
paulb@142 | 439 | ("/zoo$1/cage$1/animal$2/property$2/value", "variable"), |
paulb@142 | 440 | ("/zoo$1/cage$1/animal$2/property$3/name", "length"), |
paulb@142 | 441 | ("/zoo$1/cage$1/animal$2/property$3/value", "1m"), |
paulb@142 | 442 | ("/zoo$1/cage$2/name", "mammals"), |
paulb@142 | 443 | ("/zoo$1/cage$2/capacity", "25"), |
paulb@142 | 444 | ("/zoo$1/cage$2/animal$1/name", "Simon"), |
paulb@142 | 445 | ("/zoo$1/cage$2/animal$1/species$1/name", "Giraffe"), |
paulb@142 | 446 | ("/zoo$1/cage$2/animal$2/name", "Leonard"), |
paulb@142 | 447 | ("/zoo$1/cage$2/animal$2/species$1/name", "Lion"), |
paulb@142 | 448 | ("/zoo$1/cage$2/animal$2/property$2/name", "danger"), |
paulb@142 | 449 | ("/zoo$1/cage$2/animal$2/property$2/value", "high"), |
paulb@142 | 450 | ("/zoo$1/funding$3/type", "private"), |
paulb@142 | 451 | ("/zoo$1/funding$3/contributor$1/name", "Animal Corporation"), |
paulb@142 | 452 | ("/zoo$1/funding$3/contributor$1/amount", "543210.987"), |
paulb@142 | 453 | ("/zoo$1/funding$3/contributor$1/industry$$type", "animals") |
paulb@1 | 454 | ] |
paulb@1 | 455 | |
paulb@1 | 456 | import time |
paulb@1 | 457 | import sys, cmdsyntax |
paulb@1 | 458 | |
paulb@1 | 459 | # Find the documents. |
paulb@1 | 460 | |
paulb@1 | 461 | syntax = cmdsyntax.Syntax(""" |
paulb@1 | 462 | --plain-output=OUTPUT_FILE |
paulb@1 | 463 | --instance-name=NAME |
paulb@1 | 464 | """) |
paulb@1 | 465 | |
paulb@1 | 466 | syntax_matches = syntax.get_args(sys.argv[1:]) |
paulb@1 | 467 | |
paulb@1 | 468 | try: |
paulb@1 | 469 | args = syntax_matches[0] |
paulb@1 | 470 | except IndexError: |
paulb@1 | 471 | print syntax.syntax |
paulb@1 | 472 | sys.exit(1) |
paulb@1 | 473 | |
paulb@1 | 474 | # Create an object to interpret the test data. |
paulb@1 | 475 | |
paulb@81 | 476 | fields = FieldProcessor("iso-8859-1") |
paulb@1 | 477 | |
paulb@1 | 478 | t = time.time() |
paulb@8 | 479 | documents = fields.make_documents(items) |
paulb@1 | 480 | print "Building time", time.time() - t |
paulb@1 | 481 | |
paulb@1 | 482 | t = time.time() |
paulb@66 | 483 | documents[args["instance-name"]].toStream(stream=open(args["plain-output"], "wb"), encoding="utf-8") |
paulb@1 | 484 | print "Prettyprinting time", time.time() - t |
paulb@1 | 485 | |
paulb@8 | 486 | print "Selectors", repr(fields.get_selectors(items, documents)) |
paulb@1 | 487 | |
paulb@1 | 488 | # vim: tabstop=4 expandtab shiftwidth=4 |