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