paulb@1 | 1 | #!/usr/bin/env python |
paulb@36 | 2 | # -*- coding: iso-8859-1 -*- |
paulb@1 | 3 | |
paulb@1 | 4 | """ |
paulb@1 | 5 | Classes which process field collections, producing instance |
paulb@1 | 6 | documents. Each field entry consists of a field name mapped |
paulb@1 | 7 | to a string value, where the field name may have the following |
paulb@1 | 8 | formats: |
paulb@1 | 9 | |
paulb@1 | 10 | /name1#n1/name2 |
paulb@1 | 11 | /name1#n1/name2#n2/name3 |
paulb@1 | 12 | /name1#n1/name2#n2/name3#n3/name4 |
paulb@1 | 13 | ... |
paulb@1 | 14 | |
paulb@1 | 15 | The indexes n1, n2, n3, ... indicate the position of elements |
paulb@1 | 16 | (starting from 1) in the entire element list, whose elements |
paulb@1 | 17 | may have different names. For example: |
paulb@1 | 18 | |
paulb@1 | 19 | /zoo#1/name |
paulb@3 | 20 | /zoo#1/cage#1/name |
paulb@3 | 21 | /zoo#1/cage#2/name |
paulb@1 | 22 | /zoo#1/funding#3/contributor#1/name |
paulb@1 | 23 | |
paulb@23 | 24 | Where multiple values can be collected for a given field, the |
paulb@23 | 25 | following notation is employed: |
paulb@23 | 26 | |
paulb@23 | 27 | /package#1/categories#1/category##value |
paulb@23 | 28 | |
paulb@1 | 29 | Some fields may contain the "=" string. This string is |
paulb@1 | 30 | reserved and all text following it is meant to specify a path |
paulb@1 | 31 | into a particular document. For example: |
paulb@1 | 32 | |
paulb@1 | 33 | _action_add_animal=/zoo#1/cage#2 |
paulb@1 | 34 | """ |
paulb@1 | 35 | |
paulb@16 | 36 | import Constants |
paulb@1 | 37 | import libxml2dom |
paulb@1 | 38 | from xml.dom import EMPTY_NAMESPACE |
paulb@1 | 39 | |
paulb@1 | 40 | class FieldsError(Exception): |
paulb@1 | 41 | pass |
paulb@1 | 42 | |
paulb@81 | 43 | class FieldProcessor: |
paulb@1 | 44 | |
paulb@1 | 45 | """ |
paulb@1 | 46 | A class which converts fields in the documented form to XML |
paulb@1 | 47 | instance documents. |
paulb@1 | 48 | """ |
paulb@1 | 49 | |
paulb@3 | 50 | def __init__(self, encoding="utf-8", values_are_lists=0): |
paulb@1 | 51 | |
paulb@1 | 52 | """ |
paulb@1 | 53 | Initialise the fields processor with the given 'encoding', |
paulb@1 | 54 | which is optional and which only applies to field data in |
paulb@1 | 55 | Python string form (and not Unicode objects). |
paulb@3 | 56 | |
paulb@3 | 57 | If the optional 'values_are_lists' parameter is set to true |
paulb@3 | 58 | then each actual field value will be obtained by taking the |
paulb@3 | 59 | first element from each supplied field value. |
paulb@1 | 60 | """ |
paulb@1 | 61 | |
paulb@1 | 62 | self.encoding = encoding |
paulb@3 | 63 | self.values_are_lists = values_are_lists |
paulb@1 | 64 | |
paulb@1 | 65 | def complete_documents(self, documents, fields): |
paulb@1 | 66 | |
paulb@1 | 67 | """ |
paulb@1 | 68 | Complete the given 'documents' using the 'fields' items list. |
paulb@1 | 69 | """ |
paulb@1 | 70 | |
paulb@1 | 71 | for field, value in fields: |
paulb@1 | 72 | |
paulb@1 | 73 | # Ignore selectors. |
paulb@1 | 74 | |
paulb@16 | 75 | if field.find(Constants.selector_indicator) != -1: |
paulb@1 | 76 | continue |
paulb@1 | 77 | |
paulb@1 | 78 | model_name, components = self._get_model_name_and_components(field) |
paulb@1 | 79 | if model_name is None: |
paulb@1 | 80 | continue |
paulb@1 | 81 | |
paulb@1 | 82 | # Get a new instance document if none has been made for the |
paulb@1 | 83 | # model. |
paulb@1 | 84 | |
paulb@1 | 85 | if not documents.has_key(model_name): |
paulb@3 | 86 | documents[model_name] = self.new_instance(model_name) |
paulb@1 | 87 | node = documents[model_name] |
paulb@1 | 88 | |
paulb@1 | 89 | # Traverse the components within the instance. |
paulb@1 | 90 | |
paulb@1 | 91 | for component in components: |
paulb@16 | 92 | t = component.split(Constants.pair_separator) |
paulb@1 | 93 | if len(t) == 1: |
paulb@21 | 94 | |
paulb@21 | 95 | # Convert from lists if necessary. |
paulb@21 | 96 | |
paulb@21 | 97 | if self.values_are_lists: |
paulb@21 | 98 | value = value[0] |
paulb@21 | 99 | |
paulb@21 | 100 | # Convert the value to Unicode if necessary. |
paulb@21 | 101 | |
paulb@21 | 102 | if type(value) == type(""): |
paulb@21 | 103 | value = unicode(value, encoding=self.encoding) |
paulb@21 | 104 | |
paulb@1 | 105 | node.setAttributeNS(EMPTY_NAMESPACE, t[0], value) |
paulb@1 | 106 | break |
paulb@1 | 107 | |
paulb@1 | 108 | elif len(t) == 2: |
paulb@1 | 109 | |
paulb@23 | 110 | # Convert from one-based indexing (the position() |
paulb@23 | 111 | # function) to zero-based indexing. |
paulb@1 | 112 | |
paulb@1 | 113 | name, index = t[0], int(t[1]) - 1 |
paulb@1 | 114 | if index < 0: |
paulb@1 | 115 | break |
paulb@1 | 116 | node = self._enter_element(node, name, index) |
paulb@1 | 117 | |
paulb@23 | 118 | elif len(t) == 3 and t[1] == "": |
paulb@23 | 119 | |
paulb@23 | 120 | # Multivalued fields. |
paulb@23 | 121 | |
paulb@23 | 122 | if not self.values_are_lists: |
paulb@23 | 123 | values = [value] |
paulb@23 | 124 | else: |
paulb@23 | 125 | values = value |
paulb@23 | 126 | |
paulb@23 | 127 | name = t[0] |
paulb@29 | 128 | for subvalue in values: |
paulb@29 | 129 | subnode = self._append_element(node, name) |
paulb@23 | 130 | |
paulb@23 | 131 | # Convert the value to Unicode if necessary. |
paulb@23 | 132 | |
paulb@23 | 133 | if type(subvalue) == type(""): |
paulb@23 | 134 | subvalue = unicode(subvalue, encoding=self.encoding) |
paulb@23 | 135 | |
paulb@23 | 136 | subnode.setAttributeNS(EMPTY_NAMESPACE, t[2], subvalue) |
paulb@23 | 137 | |
paulb@1 | 138 | def complete_selectors(self, selectors, fields, documents): |
paulb@1 | 139 | |
paulb@1 | 140 | """ |
paulb@1 | 141 | Fill in the given 'selectors' dictionary using the given |
paulb@1 | 142 | 'fields' so that it contains mappings from selector names to |
paulb@1 | 143 | parts of the specified 'documents'. |
paulb@1 | 144 | """ |
paulb@1 | 145 | |
paulb@1 | 146 | for field, value in fields: |
paulb@1 | 147 | |
paulb@1 | 148 | # Process selectors only. |
paulb@1 | 149 | |
paulb@16 | 150 | selector_components = field.split(Constants.selector_indicator) |
paulb@1 | 151 | if len(selector_components) < 2: |
paulb@1 | 152 | continue |
paulb@1 | 153 | |
paulb@1 | 154 | # Get the selector name and path. |
paulb@1 | 155 | # Note that the joining of the components uses the separator, |
paulb@1 | 156 | # but the separator really should not exist in the path. |
paulb@1 | 157 | |
paulb@1 | 158 | selector_name = selector_components[0] |
paulb@16 | 159 | path = Constants.selector_indicator.join(selector_components[1:]) |
paulb@1 | 160 | |
paulb@1 | 161 | model_name, components = self._get_model_name_and_components(path) |
paulb@1 | 162 | if model_name is None: |
paulb@1 | 163 | continue |
paulb@1 | 164 | |
paulb@1 | 165 | # Go to the instance element. |
paulb@1 | 166 | |
paulb@1 | 167 | if not documents.has_key(model_name) or documents[model_name] is None: |
paulb@1 | 168 | continue |
paulb@1 | 169 | |
paulb@1 | 170 | node = documents[model_name] |
paulb@1 | 171 | |
paulb@1 | 172 | # Traverse the path to find the part of the document to be |
paulb@1 | 173 | # selected. |
paulb@1 | 174 | |
paulb@1 | 175 | for component in components: |
paulb@16 | 176 | t = component.split(Constants.pair_separator) |
paulb@1 | 177 | if len(t) == 1: |
paulb@1 | 178 | |
paulb@1 | 179 | # Select attribute. |
paulb@1 | 180 | |
paulb@1 | 181 | node = node.getAttributeNodeNS(EMPTY_NAMESPACE, t[0]) |
paulb@1 | 182 | break |
paulb@1 | 183 | |
paulb@1 | 184 | elif len(t) == 2: |
paulb@1 | 185 | |
paulb@1 | 186 | # Convert from one-based indexing (the position() function) |
paulb@1 | 187 | # to zero-based indexing. |
paulb@1 | 188 | |
paulb@1 | 189 | name, index = t[0], int(t[1]) - 1 |
paulb@1 | 190 | if index < 0: |
paulb@1 | 191 | break |
paulb@1 | 192 | |
paulb@1 | 193 | # NOTE: Controversial creation of potentially non-existent |
paulb@1 | 194 | # NOTE: nodes. |
paulb@1 | 195 | |
paulb@1 | 196 | node = self._enter_element(node, name, index) |
paulb@1 | 197 | |
paulb@1 | 198 | if not selectors.has_key(selector_name): |
paulb@1 | 199 | selectors[selector_name] = [] |
paulb@1 | 200 | selectors[selector_name].append(node) |
paulb@1 | 201 | |
paulb@29 | 202 | def _append_element(self, node, name): |
paulb@29 | 203 | |
paulb@29 | 204 | """ |
paulb@29 | 205 | Within 'node' append an element with the given 'name'. |
paulb@29 | 206 | """ |
paulb@29 | 207 | |
paulb@29 | 208 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@29 | 209 | node.appendChild(new_node) |
paulb@29 | 210 | return new_node |
paulb@29 | 211 | |
paulb@1 | 212 | def _enter_element(self, node, name, index): |
paulb@1 | 213 | |
paulb@1 | 214 | """ |
paulb@1 | 215 | From 'node' enter the element with the given 'name' at the |
paulb@1 | 216 | given 'index' position amongst the child elements. Create |
paulb@1 | 217 | missing child elements if necessary. |
paulb@1 | 218 | """ |
paulb@1 | 219 | |
paulb@1 | 220 | self._ensure_elements(node, index) |
paulb@1 | 221 | |
paulb@1 | 222 | elements = node.xpath("*") |
paulb@1 | 223 | if elements[index].localName == "placeholder": |
paulb@1 | 224 | new_node = node.ownerDocument.createElementNS(EMPTY_NAMESPACE, name) |
paulb@1 | 225 | node.replaceChild(new_node, elements[index]) |
paulb@1 | 226 | else: |
paulb@1 | 227 | new_node = elements[index] |
paulb@1 | 228 | if new_node.localName != name: |
paulb@1 | 229 | raise FieldsError, (new_node.localName, name, elements, index) |
paulb@1 | 230 | |
paulb@1 | 231 | # Enter the newly-created element. |
paulb@1 | 232 | |
paulb@1 | 233 | return new_node |
paulb@1 | 234 | |
paulb@1 | 235 | def _get_model_name_and_components(self, field): |
paulb@1 | 236 | |
paulb@1 | 237 | """ |
paulb@1 | 238 | From 'field', return the model name and components which |
paulb@1 | 239 | describe the path within the instance document associated |
paulb@1 | 240 | with that model. |
paulb@1 | 241 | """ |
paulb@1 | 242 | |
paulb@1 | 243 | # Get the components of the field name. |
paulb@1 | 244 | # Example: /name1#n1/name2#n2/name3 |
paulb@1 | 245 | # Expected: ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 246 | |
paulb@16 | 247 | components = field.split(Constants.path_separator) |
paulb@1 | 248 | if len(components) < 2: |
paulb@1 | 249 | return None, None |
paulb@1 | 250 | |
paulb@1 | 251 | # Extract the model name from the top-level element |
paulb@1 | 252 | # specification. |
paulb@1 | 253 | # Expected: ['name1', 'n1'] |
paulb@1 | 254 | |
paulb@16 | 255 | model_name_and_index = components[1].split(Constants.pair_separator) |
paulb@1 | 256 | if len(model_name_and_index) != 2: |
paulb@1 | 257 | return None, None |
paulb@1 | 258 | |
paulb@1 | 259 | # Expected: 'name1', ['', 'name1#n1', 'name2#n2', 'name3'] |
paulb@1 | 260 | |
paulb@1 | 261 | return model_name_and_index[0], components[1:] |
paulb@1 | 262 | |
paulb@1 | 263 | def _ensure_elements(self, document, index): |
paulb@1 | 264 | |
paulb@1 | 265 | """ |
paulb@1 | 266 | In the given 'document', extend the child elements list |
paulb@1 | 267 | so that a node can be stored at the given 'index'. |
paulb@1 | 268 | """ |
paulb@1 | 269 | |
paulb@1 | 270 | elements = document.xpath("*") |
paulb@1 | 271 | i = len(elements) |
paulb@1 | 272 | while i <= index: |
paulb@1 | 273 | new_node = document.ownerDocument.createElementNS(EMPTY_NAMESPACE, "placeholder") |
paulb@1 | 274 | document.appendChild(new_node) |
paulb@1 | 275 | i += 1 |
paulb@1 | 276 | |
paulb@1 | 277 | def make_documents(self, fields): |
paulb@1 | 278 | |
paulb@1 | 279 | """ |
paulb@1 | 280 | Make a dictionary mapping model names to new documents prepared |
paulb@1 | 281 | from the given 'fields' dictionary. |
paulb@1 | 282 | """ |
paulb@1 | 283 | |
paulb@1 | 284 | documents = {} |
paulb@1 | 285 | self.complete_documents(documents, fields) |
paulb@1 | 286 | |
paulb@1 | 287 | # Fix the dictionary to return the actual document root. |
paulb@1 | 288 | |
paulb@1 | 289 | for model_name, instance_root in documents.items(): |
paulb@1 | 290 | documents[model_name] = instance_root |
paulb@1 | 291 | return documents |
paulb@1 | 292 | |
paulb@1 | 293 | def get_selectors(self, fields, documents): |
paulb@1 | 294 | |
paulb@1 | 295 | """ |
paulb@1 | 296 | Get a dictionary containing a mapping of selector names to |
paulb@1 | 297 | selected parts of the given 'documents'. |
paulb@1 | 298 | """ |
paulb@1 | 299 | |
paulb@1 | 300 | selectors = {} |
paulb@1 | 301 | self.complete_selectors(selectors, fields, documents) |
paulb@1 | 302 | return selectors |
paulb@1 | 303 | |
paulb@3 | 304 | def new_instance(self, name): |
paulb@1 | 305 | |
paulb@1 | 306 | "Return an instance root of the given 'name' in a new document." |
paulb@1 | 307 | |
paulb@1 | 308 | return libxml2dom.createDocument(EMPTY_NAMESPACE, name, None) |
paulb@1 | 309 | |
paulb@81 | 310 | # NOTE: Legacy name exposure. |
paulb@81 | 311 | |
paulb@81 | 312 | Fields = FieldProcessor |
paulb@81 | 313 | |
paulb@81 | 314 | class Form(FieldProcessor): |
paulb@81 | 315 | |
paulb@81 | 316 | "A collection of documents processed from form fields." |
paulb@81 | 317 | |
paulb@81 | 318 | def __init__(self, *args, **kw): |
paulb@81 | 319 | FieldProcessor.__init__(self, *args, **kw) |
paulb@81 | 320 | self.parameters = {} |
paulb@81 | 321 | self.documents = {} |
paulb@81 | 322 | |
paulb@81 | 323 | def set_parameters(self, parameters): |
paulb@81 | 324 | self.parameters = parameters |
paulb@81 | 325 | self.documents = self.make_documents(self.parameters.items()) |
paulb@81 | 326 | |
paulb@81 | 327 | def get_parameters(self): |
paulb@81 | 328 | return self.parameters |
paulb@81 | 329 | |
paulb@81 | 330 | def get_documents(self): |
paulb@81 | 331 | return self.documents |
paulb@81 | 332 | |
paulb@81 | 333 | def get_selectors(self): |
paulb@81 | 334 | return FieldProcessor.get_selectors(self, self.parameters.items(), self.documents) |
paulb@81 | 335 | |
paulb@1 | 336 | if __name__ == "__main__": |
paulb@1 | 337 | |
paulb@8 | 338 | items = [ |
paulb@1 | 339 | ("_action_update", "Some value"), |
paulb@1 | 340 | ("_action_delete=/zoo#1/cage#2", "Some value"), |
paulb@1 | 341 | ("/actions#1/update#1/selected", "Some value"), # Not actually used in output documents or input. |
paulb@1 | 342 | ("/zoo#1/name", "The Zoo ???"), |
paulb@1 | 343 | ("/zoo#1/cage#1/name", "reptiles"), |
paulb@1 | 344 | ("/zoo#1/cage#1/capacity", "5"), |
paulb@1 | 345 | ("/zoo#1/cage#1/animal#1/name", "Monty"), |
paulb@1 | 346 | ("/zoo#1/cage#1/animal#1/species#1/name", "Python"), |
paulb@1 | 347 | ("/zoo#1/cage#1/animal#1/property#2/name", "texture"), |
paulb@1 | 348 | ("/zoo#1/cage#1/animal#1/property#2/value", "scaled"), |
paulb@1 | 349 | ("/zoo#1/cage#1/animal#1/property#3/name", "length"), |
paulb@1 | 350 | ("/zoo#1/cage#1/animal#1/property#3/value", "5m"), |
paulb@1 | 351 | ("/zoo#1/cage#1/animal#2/name", "Vincent"), |
paulb@1 | 352 | ("/zoo#1/cage#1/animal#2/species#1/name", "Lizard"), |
paulb@1 | 353 | ("/zoo#1/cage#1/animal#2/property#2/name", "colour"), |
paulb@1 | 354 | ("/zoo#1/cage#1/animal#2/property#2/value", "variable"), |
paulb@1 | 355 | ("/zoo#1/cage#1/animal#2/property#3/name", "length"), |
paulb@1 | 356 | ("/zoo#1/cage#1/animal#2/property#3/value", "1m"), |
paulb@1 | 357 | ("/zoo#1/cage#2/name", "mammals"), |
paulb@1 | 358 | ("/zoo#1/cage#2/capacity", "25"), |
paulb@1 | 359 | ("/zoo#1/cage#2/animal#1/name", "Simon"), |
paulb@1 | 360 | ("/zoo#1/cage#2/animal#1/species#1/name", "Giraffe"), |
paulb@1 | 361 | ("/zoo#1/cage#2/animal#2/name", "Leonard"), |
paulb@1 | 362 | ("/zoo#1/cage#2/animal#2/species#1/name", "Lion"), |
paulb@1 | 363 | ("/zoo#1/cage#2/animal#2/property#2/name", "danger"), |
paulb@1 | 364 | ("/zoo#1/cage#2/animal#2/property#2/value", "high"), |
paulb@1 | 365 | ("/zoo#1/funding#3/type", "private"), |
paulb@1 | 366 | ("/zoo#1/funding#3/contributor#1/name", "Animal Corporation"), |
paulb@23 | 367 | ("/zoo#1/funding#3/contributor#1/amount", "543210.987"), |
paulb@23 | 368 | ("/zoo#1/funding#3/contributor#1/industry##type", "animals") |
paulb@1 | 369 | ] |
paulb@1 | 370 | |
paulb@1 | 371 | import time |
paulb@1 | 372 | import sys, cmdsyntax |
paulb@1 | 373 | |
paulb@1 | 374 | # Find the documents. |
paulb@1 | 375 | |
paulb@1 | 376 | syntax = cmdsyntax.Syntax(""" |
paulb@1 | 377 | --plain-output=OUTPUT_FILE |
paulb@1 | 378 | --instance-name=NAME |
paulb@1 | 379 | """) |
paulb@1 | 380 | |
paulb@1 | 381 | syntax_matches = syntax.get_args(sys.argv[1:]) |
paulb@1 | 382 | |
paulb@1 | 383 | try: |
paulb@1 | 384 | args = syntax_matches[0] |
paulb@1 | 385 | except IndexError: |
paulb@1 | 386 | print syntax.syntax |
paulb@1 | 387 | sys.exit(1) |
paulb@1 | 388 | |
paulb@1 | 389 | # Create an object to interpret the test data. |
paulb@1 | 390 | |
paulb@81 | 391 | fields = FieldProcessor("iso-8859-1") |
paulb@1 | 392 | |
paulb@1 | 393 | t = time.time() |
paulb@8 | 394 | documents = fields.make_documents(items) |
paulb@1 | 395 | print "Building time", time.time() - t |
paulb@1 | 396 | |
paulb@1 | 397 | t = time.time() |
paulb@66 | 398 | documents[args["instance-name"]].toStream(stream=open(args["plain-output"], "wb"), encoding="utf-8") |
paulb@1 | 399 | print "Prettyprinting time", time.time() - t |
paulb@1 | 400 | |
paulb@8 | 401 | print "Selectors", repr(fields.get_selectors(items, documents)) |
paulb@1 | 402 | |
paulb@1 | 403 | # vim: tabstop=4 expandtab shiftwidth=4 |