Creates a new table in the given database from the given XML-string. The XML must be in the format generated by Table.xml. If the table already exists, raises a TableError. The given table parameter can be used to rename the table. The given field function can be use
(database, xml, table=None, field=lambda s: s.replace(".", "-"))
| 1451 | return xml |
| 1452 | |
| 1453 | def parse_xml(database, xml, table=None, field=lambda s: s.replace(".", "-")): |
| 1454 | """ Creates a new table in the given database from the given XML-string. |
| 1455 | The XML must be in the format generated by Table.xml. |
| 1456 | If the table already exists, raises a TableError. |
| 1457 | The given table parameter can be used to rename the table. |
| 1458 | The given field function can be used to rename field names. |
| 1459 | """ |
| 1460 | def _attr(node, attribute, default=""): |
| 1461 | return node.getAttribute(attribute) or default |
| 1462 | # parseString() will decode entities, no need for decode_entities(). |
| 1463 | from xml.dom.minidom import parseString |
| 1464 | dom = parseString(encode_utf8(xml)) |
| 1465 | a = dom.getElementsByTagName("table") |
| 1466 | b = dom.getElementsByTagName("query") |
| 1467 | if len(a) > 0: |
| 1468 | table = table or _attr(a[0], "name", "") |
| 1469 | if len(b) > 0: |
| 1470 | table = table or _attr(b[0], "table", "") |
| 1471 | # Parse field information (i.e., field name, field type, etc.) |
| 1472 | fields, schema, rows = [], [], [] |
| 1473 | for f in dom.getElementsByTagName("field"): |
| 1474 | fields.append(_attr(f, "name")) |
| 1475 | schema.append(_field( |
| 1476 | name = field(_attr(f, "name")), |
| 1477 | type = _attr(f, "type") == STRING and STRING(int(_attr(f, "length", 255))) or _attr(f, "type"), |
| 1478 | default = _attr(f, "default", None), |
| 1479 | index = _attr(f, "index", False), |
| 1480 | optional = _attr(f, "optional", True) != "no" |
| 1481 | )) |
| 1482 | # Integer primary key is always auto-increment. |
| 1483 | # The id's in the new table will differ from those in the XML. |
| 1484 | if _attr(f, "index") == PRIMARY and _attr(f, "type") == INTEGER: |
| 1485 | fields.pop() |
| 1486 | # Parse row data. |
| 1487 | for r in dom.getElementsByTagName("row"): |
| 1488 | rows.append({}) |
| 1489 | for i, f in enumerate(fields): |
| 1490 | v = _attr(r, f, None) |
| 1491 | if schema[i][1] == BOOLEAN: |
| 1492 | rows[-1][f] = (0,1)[v!="no"] |
| 1493 | else: |
| 1494 | rows[-1][f] = v |
| 1495 | # Create table if not exists and insert rows. |
| 1496 | if database.connected is False: |
| 1497 | database.connect() |
| 1498 | if table in database: |
| 1499 | raise TableError, "table '%s' already exists" % table |
| 1500 | database.create(table, fields=schema) |
| 1501 | for r in rows: |
| 1502 | database[table].insert(r, commit=False) |
| 1503 | database.commit() |
| 1504 | return database[table] |
| 1505 | |
| 1506 | #### JSON PARSER ################################################################################### |
| 1507 |
no test coverage detected
searching dependent graphs…