| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
__all__ = [ |
| 58 |
|
| 59 |
"Comment", |
| 60 |
"dump", |
| 61 |
"Element", "ElementTree", |
| 62 |
"fromstring", "fromstringlist", |
| 63 |
"iselement", "iterparse", |
| 64 |
"parse", "ParseError", |
| 65 |
"PI", "ProcessingInstruction", |
| 66 |
"QName", |
| 67 |
"SubElement", |
| 68 |
"tostring", "tostringlist", |
| 69 |
"TreeBuilder", |
| 70 |
"VERSION", |
| 71 |
"XML", |
| 72 |
"XMLParser", "XMLTreeBuilder", |
| 73 |
] |
| 74 |
|
| 75 |
VERSION = "1.3.0" |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
import sys |
| 100 |
import re |
| 101 |
import warnings |
| 102 |
|
| 103 |
|
| 104 |
class _SimpleElementPath(object): |
| 105 |
|
| 106 |
def find(self, element, tag, namespaces=None): |
| 107 |
for elem in element: |
| 108 |
if elem.tag == tag: |
| 109 |
return elem |
| 110 |
return None |
| 111 |
def findtext(self, element, tag, default=None, namespaces=None): |
| 112 |
elem = self.find(element, tag) |
| 113 |
if elem is None: |
| 114 |
return default |
| 115 |
return elem.text or "" |
| 116 |
def iterfind(self, element, tag, namespaces=None): |
| 117 |
if tag[:3] == ".//": |
| 118 |
for elem in element.iter(tag[3:]): |
| 119 |
yield elem |
| 120 |
for elem in element: |
| 121 |
if elem.tag == tag: |
| 122 |
yield elem |
| 123 |
def findall(self, element, tag, namespaces=None): |
| 124 |
return list(self.iterfind(element, tag, namespaces)) |
| 125 |
|
| 126 |
try: |
| 127 |
from . import ElementPath |
| 128 |
except ImportError: |
| 129 |
ElementPath = _SimpleElementPath() |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
class ParseError(SyntaxError): |
| 139 |
pass |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
def iselement(element): |
| 151 |
|
| 152 |
|
| 153 |
return isinstance(element, Element) or hasattr(element, "tag") |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
class Element(object): |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
tag = None |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
attrib = None |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
text = None |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
tail = None |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
def __init__(self, tag, attrib={}, **extra): |
| 208 |
attrib = attrib.copy() |
| 209 |
attrib.update(extra) |
| 210 |
self.tag = tag |
| 211 |
self.attrib = attrib |
| 212 |
self._children = [] |
| 213 |
|
| 214 |
def __repr__(self): |
| 215 |
return "<Element %s at 0x%x>" % (repr(self.tag), id(self)) |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
def makeelement(self, tag, attrib): |
| 225 |
return self.__class__(tag, attrib) |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
def copy(self): |
| 234 |
elem = self.makeelement(self.tag, self.attrib) |
| 235 |
elem.text = self.text |
| 236 |
elem.tail = self.tail |
| 237 |
elem[:] = self |
| 238 |
return elem |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
def __len__(self): |
| 248 |
return len(self._children) |
| 249 |
|
| 250 |
def __nonzero__(self): |
| 251 |
warnings.warn( |
| 252 |
"The behavior of this method will change in future versions. " |
| 253 |
"Use specific 'len(elem)' or 'elem is not None' test instead.", |
| 254 |
FutureWarning, stacklevel=2 |
| 255 |
) |
| 256 |
return len(self._children) != 0 |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
def __getitem__(self, index): |
| 266 |
return self._children[index] |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
def __setitem__(self, index, element): |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
self._children[index] = element |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
def __delitem__(self, index): |
| 290 |
del self._children[index] |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
def append(self, element): |
| 301 |
|
| 302 |
self._children.append(element) |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
def extend(self, elements): |
| 311 |
|
| 312 |
|
| 313 |
self._children.extend(elements) |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
def insert(self, index, element): |
| 321 |
|
| 322 |
self._children.insert(index, element) |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
def remove(self, element): |
| 336 |
|
| 337 |
self._children.remove(element) |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
def getchildren(self): |
| 347 |
warnings.warn( |
| 348 |
"This method will be removed in future versions. " |
| 349 |
"Use 'list(elem)' or iteration over elem instead.", |
| 350 |
DeprecationWarning, stacklevel=2 |
| 351 |
) |
| 352 |
return self._children |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
def find(self, path, namespaces=None): |
| 363 |
return ElementPath.find(self, path, namespaces) |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
def findtext(self, path, default=None, namespaces=None): |
| 378 |
return ElementPath.findtext(self, path, default, namespaces) |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
def findall(self, path, namespaces=None): |
| 390 |
return ElementPath.findall(self, path, namespaces) |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
def iterfind(self, path, namespaces=None): |
| 402 |
return ElementPath.iterfind(self, path, namespaces) |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
def clear(self): |
| 410 |
self.attrib.clear() |
| 411 |
self._children = [] |
| 412 |
self.text = self.tail = None |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
def get(self, key, default=None): |
| 425 |
return self.attrib.get(key, default) |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
def set(self, key, value): |
| 435 |
self.attrib[key] = value |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
def keys(self): |
| 446 |
return self.attrib.keys() |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
def items(self): |
| 456 |
return self.attrib.items() |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
def iter(self, tag=None): |
| 472 |
if tag == "*": |
| 473 |
tag = None |
| 474 |
if tag is None or self.tag == tag: |
| 475 |
yield self |
| 476 |
for e in self._children: |
| 477 |
for e in e.iter(tag): |
| 478 |
yield e |
| 479 |
|
| 480 |
|
| 481 |
def getiterator(self, tag=None): |
| 482 |
|
| 483 |
warnings.warn( |
| 484 |
"This method will be removed in future versions. " |
| 485 |
"Use 'elem.iter()' or 'list(elem.iter())' instead.", |
| 486 |
PendingDeprecationWarning, stacklevel=2 |
| 487 |
) |
| 488 |
return list(self.iter(tag)) |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
def itertext(self): |
| 499 |
tag = self.tag |
| 500 |
if not isinstance(tag, basestring) and tag is not None: |
| 501 |
return |
| 502 |
if self.text: |
| 503 |
yield self.text |
| 504 |
for e in self: |
| 505 |
for s in e.itertext(): |
| 506 |
yield s |
| 507 |
if e.tail: |
| 508 |
yield e.tail |
| 509 |
|
| 510 |
|
| 511 |
_Element = _ElementInterface = Element |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
def SubElement(parent, tag, attrib={}, **extra): |
| 528 |
attrib = attrib.copy() |
| 529 |
attrib.update(extra) |
| 530 |
element = parent.makeelement(tag, attrib) |
| 531 |
parent.append(element) |
| 532 |
return element |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
def Comment(text=None): |
| 547 |
element = Element(Comment) |
| 548 |
element.text = text |
| 549 |
return element |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
def ProcessingInstruction(target, text=None): |
| 562 |
element = Element(ProcessingInstruction) |
| 563 |
element.text = target |
| 564 |
if text: |
| 565 |
element.text = element.text + " " + text |
| 566 |
return element |
| 567 |
|
| 568 |
PI = ProcessingInstruction |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
class QName(object): |
| 581 |
def __init__(self, text_or_uri, tag=None): |
| 582 |
if tag: |
| 583 |
text_or_uri = "{%s}%s" % (text_or_uri, tag) |
| 584 |
self.text = text_or_uri |
| 585 |
def __str__(self): |
| 586 |
return self.text |
| 587 |
def __hash__(self): |
| 588 |
return hash(self.text) |
| 589 |
def __cmp__(self, other): |
| 590 |
if isinstance(other, QName): |
| 591 |
return cmp(self.text, other.text) |
| 592 |
return cmp(self.text, other) |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
class ElementTree(object): |
| 606 |
|
| 607 |
def __init__(self, element=None, file=None): |
| 608 |
|
| 609 |
self._root = element |
| 610 |
if file: |
| 611 |
self.parse(file) |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
def getroot(self): |
| 620 |
return self._root |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
def _setroot(self, element): |
| 630 |
|
| 631 |
self._root = element |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
def parse(self, source, parser=None): |
| 645 |
close_source = False |
| 646 |
if not hasattr(source, "read"): |
| 647 |
source = open(source, "rb") |
| 648 |
close_source = True |
| 649 |
try: |
| 650 |
if not parser: |
| 651 |
parser = XMLParser(target=TreeBuilder()) |
| 652 |
while 1: |
| 653 |
data = source.read(65536) |
| 654 |
if not data: |
| 655 |
break |
| 656 |
parser.feed(data) |
| 657 |
self._root = parser.close() |
| 658 |
return self._root |
| 659 |
finally: |
| 660 |
if close_source: |
| 661 |
source.close() |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
def iter(self, tag=None): |
| 672 |
|
| 673 |
return self._root.iter(tag) |
| 674 |
|
| 675 |
|
| 676 |
def getiterator(self, tag=None): |
| 677 |
|
| 678 |
warnings.warn( |
| 679 |
"This method will be removed in future versions. " |
| 680 |
"Use 'tree.iter()' or 'list(tree.iter())' instead.", |
| 681 |
PendingDeprecationWarning, stacklevel=2 |
| 682 |
) |
| 683 |
return list(self.iter(tag)) |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
def find(self, path, namespaces=None): |
| 695 |
|
| 696 |
if path[:1] == "/": |
| 697 |
path = "." + path |
| 698 |
warnings.warn( |
| 699 |
"This search is broken in 1.3 and earlier, and will be " |
| 700 |
"fixed in a future version. If you rely on the current " |
| 701 |
"behaviour, change it to %r" % path, |
| 702 |
FutureWarning, stacklevel=2 |
| 703 |
) |
| 704 |
return self._root.find(path, namespaces) |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
def findtext(self, path, default=None, namespaces=None): |
| 719 |
|
| 720 |
if path[:1] == "/": |
| 721 |
path = "." + path |
| 722 |
warnings.warn( |
| 723 |
"This search is broken in 1.3 and earlier, and will be " |
| 724 |
"fixed in a future version. If you rely on the current " |
| 725 |
"behaviour, change it to %r" % path, |
| 726 |
FutureWarning, stacklevel=2 |
| 727 |
) |
| 728 |
return self._root.findtext(path, default, namespaces) |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
def findall(self, path, namespaces=None): |
| 740 |
|
| 741 |
if path[:1] == "/": |
| 742 |
path = "." + path |
| 743 |
warnings.warn( |
| 744 |
"This search is broken in 1.3 and earlier, and will be " |
| 745 |
"fixed in a future version. If you rely on the current " |
| 746 |
"behaviour, change it to %r" % path, |
| 747 |
FutureWarning, stacklevel=2 |
| 748 |
) |
| 749 |
return self._root.findall(path, namespaces) |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
def iterfind(self, path, namespaces=None): |
| 762 |
|
| 763 |
if path[:1] == "/": |
| 764 |
path = "." + path |
| 765 |
warnings.warn( |
| 766 |
"This search is broken in 1.3 and earlier, and will be " |
| 767 |
"fixed in a future version. If you rely on the current " |
| 768 |
"behaviour, change it to %r" % path, |
| 769 |
FutureWarning, stacklevel=2 |
| 770 |
) |
| 771 |
return self._root.iterfind(path, namespaces) |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
def write(self, file_or_filename, |
| 788 |
|
| 789 |
encoding=None, |
| 790 |
xml_declaration=None, |
| 791 |
default_namespace=None, |
| 792 |
method=None): |
| 793 |
|
| 794 |
if not method: |
| 795 |
method = "xml" |
| 796 |
elif method not in _serialize: |
| 797 |
|
| 798 |
raise ValueError("unknown method %r" % method) |
| 799 |
if hasattr(file_or_filename, "write"): |
| 800 |
file = file_or_filename |
| 801 |
else: |
| 802 |
file = open(file_or_filename, "wb") |
| 803 |
write = file.write |
| 804 |
if not encoding: |
| 805 |
if method == "c14n": |
| 806 |
encoding = "utf-8" |
| 807 |
else: |
| 808 |
encoding = "us-ascii" |
| 809 |
elif xml_declaration or (xml_declaration is None and |
| 810 |
encoding not in ("utf-8", "us-ascii")): |
| 811 |
if method == "xml": |
| 812 |
write("<?xml version='1.0' encoding='%s'?>\n" % encoding) |
| 813 |
if method == "text": |
| 814 |
_serialize_text(write, self._root, encoding) |
| 815 |
else: |
| 816 |
qnames, namespaces = _namespaces( |
| 817 |
self._root, encoding, default_namespace |
| 818 |
) |
| 819 |
serialize = _serialize[method] |
| 820 |
serialize(write, self._root, encoding, qnames, namespaces) |
| 821 |
if file_or_filename is not file: |
| 822 |
file.close() |
| 823 |
|
| 824 |
def write_c14n(self, file): |
| 825 |
|
| 826 |
return self.write(file, method="c14n") |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
def _namespaces(elem, encoding, default_namespace=None): |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
qnames = {None: None} |
| 836 |
|
| 837 |
|
| 838 |
namespaces = {} |
| 839 |
if default_namespace: |
| 840 |
namespaces[default_namespace] = "" |
| 841 |
|
| 842 |
def encode(text): |
| 843 |
return text.encode(encoding) |
| 844 |
|
| 845 |
def add_qname(qname): |
| 846 |
|
| 847 |
try: |
| 848 |
if qname[:1] == "{": |
| 849 |
uri, tag = qname[1:].rsplit("}", 1) |
| 850 |
prefix = namespaces.get(uri) |
| 851 |
if prefix is None: |
| 852 |
prefix = _namespace_map.get(uri) |
| 853 |
if prefix is None: |
| 854 |
prefix = "ns%d" % len(namespaces) |
| 855 |
if prefix != "xml": |
| 856 |
namespaces[uri] = prefix |
| 857 |
if prefix: |
| 858 |
qnames[qname] = encode("%s:%s" % (prefix, tag)) |
| 859 |
else: |
| 860 |
qnames[qname] = encode(tag) |
| 861 |
else: |
| 862 |
if default_namespace: |
| 863 |
|
| 864 |
raise ValueError( |
| 865 |
"cannot use non-qualified names with " |
| 866 |
"default_namespace option" |
| 867 |
) |
| 868 |
qnames[qname] = encode(qname) |
| 869 |
except TypeError: |
| 870 |
_raise_serialization_error(qname) |
| 871 |
|
| 872 |
|
| 873 |
try: |
| 874 |
iterate = elem.iter |
| 875 |
except AttributeError: |
| 876 |
iterate = elem.getiterator |
| 877 |
for elem in iterate(): |
| 878 |
tag = elem.tag |
| 879 |
if isinstance(tag, QName): |
| 880 |
if tag.text not in qnames: |
| 881 |
add_qname(tag.text) |
| 882 |
elif isinstance(tag, basestring): |
| 883 |
if tag not in qnames: |
| 884 |
add_qname(tag) |
| 885 |
elif tag is not None and tag is not Comment and tag is not PI: |
| 886 |
_raise_serialization_error(tag) |
| 887 |
for key, value in elem.items(): |
| 888 |
if isinstance(key, QName): |
| 889 |
key = key.text |
| 890 |
if key not in qnames: |
| 891 |
add_qname(key) |
| 892 |
if isinstance(value, QName) and value.text not in qnames: |
| 893 |
add_qname(value.text) |
| 894 |
text = elem.text |
| 895 |
if isinstance(text, QName) and text.text not in qnames: |
| 896 |
add_qname(text.text) |
| 897 |
return qnames, namespaces |
| 898 |
|
| 899 |
def _serialize_xml(write, elem, encoding, qnames, namespaces): |
| 900 |
tag = elem.tag |
| 901 |
text = elem.text |
| 902 |
if tag is Comment: |
| 903 |
write("<!--%s-->" % _encode(text, encoding)) |
| 904 |
elif tag is ProcessingInstruction: |
| 905 |
write("<?%s?>" % _encode(text, encoding)) |
| 906 |
else: |
| 907 |
tag = qnames[tag] |
| 908 |
if tag is None: |
| 909 |
if text: |
| 910 |
write(_escape_cdata(text, encoding)) |
| 911 |
for e in elem: |
| 912 |
_serialize_xml(write, e, encoding, qnames, None) |
| 913 |
else: |
| 914 |
write("<" + tag) |
| 915 |
items = elem.items() |
| 916 |
if items or namespaces: |
| 917 |
if namespaces: |
| 918 |
for v, k in sorted(namespaces.items(), |
| 919 |
key=lambda x: x[1]): |
| 920 |
if k: |
| 921 |
k = ":" + k |
| 922 |
write(" xmlns%s=\"%s\"" % ( |
| 923 |
k.encode(encoding), |
| 924 |
_escape_attrib(v, encoding) |
| 925 |
)) |
| 926 |
for k, v in sorted(items): |
| 927 |
if isinstance(k, QName): |
| 928 |
k = k.text |
| 929 |
if isinstance(v, QName): |
| 930 |
v = qnames[v.text] |
| 931 |
else: |
| 932 |
v = _escape_attrib(v, encoding) |
| 933 |
write(" %s=\"%s\"" % (qnames[k], v)) |
| 934 |
if text or len(elem): |
| 935 |
write(">") |
| 936 |
if text: |
| 937 |
write(_escape_cdata(text, encoding)) |
| 938 |
for e in elem: |
| 939 |
_serialize_xml(write, e, encoding, qnames, None) |
| 940 |
write("</" + tag + ">") |
| 941 |
else: |
| 942 |
write(" />") |
| 943 |
if elem.tail: |
| 944 |
write(_escape_cdata(elem.tail, encoding)) |
| 945 |
|
| 946 |
HTML_EMPTY = ("area", "base", "basefont", "br", "col", "frame", "hr", |
| 947 |
"img", "input", "isindex", "link", "meta", "param") |
| 948 |
|
| 949 |
try: |
| 950 |
HTML_EMPTY = set(HTML_EMPTY) |
| 951 |
except NameError: |
| 952 |
pass |
| 953 |
|
| 954 |
def _serialize_html(write, elem, encoding, qnames, namespaces): |
| 955 |
tag = elem.tag |
| 956 |
text = elem.text |
| 957 |
if tag is Comment: |
| 958 |
write("<!--%s-->" % _escape_cdata(text, encoding)) |
| 959 |
elif tag is ProcessingInstruction: |
| 960 |
write("<?%s?>" % _escape_cdata(text, encoding)) |
| 961 |
else: |
| 962 |
tag = qnames[tag] |
| 963 |
if tag is None: |
| 964 |
if text: |
| 965 |
write(_escape_cdata(text, encoding)) |
| 966 |
for e in elem: |
| 967 |
_serialize_html(write, e, encoding, qnames, None) |
| 968 |
else: |
| 969 |
write("<" + tag) |
| 970 |
items = elem.items() |
| 971 |
if items or namespaces: |
| 972 |
if namespaces: |
| 973 |
for v, k in sorted(namespaces.items(), |
| 974 |
key=lambda x: x[1]): |
| 975 |
if k: |
| 976 |
k = ":" + k |
| 977 |
write(" xmlns%s=\"%s\"" % ( |
| 978 |
k.encode(encoding), |
| 979 |
_escape_attrib(v, encoding) |
| 980 |
)) |
| 981 |
for k, v in sorted(items): |
| 982 |
if isinstance(k, QName): |
| 983 |
k = k.text |
| 984 |
if isinstance(v, QName): |
| 985 |
v = qnames[v.text] |
| 986 |
else: |
| 987 |
v = _escape_attrib_html(v, encoding) |
| 988 |
|
| 989 |
write(" %s=\"%s\"" % (qnames[k], v)) |
| 990 |
write(">") |
| 991 |
ltag = tag.lower() |
| 992 |
if text: |
| 993 |
if ltag == "script" or ltag == "style": |
| 994 |
write(_encode(text, encoding)) |
| 995 |
else: |
| 996 |
write(_escape_cdata(text, encoding)) |
| 997 |
for e in elem: |
| 998 |
_serialize_html(write, e, encoding, qnames, None) |
| 999 |
if ltag not in HTML_EMPTY: |
| 1000 |
write("</" + tag + ">") |
| 1001 |
if elem.tail: |
| 1002 |
write(_escape_cdata(elem.tail, encoding)) |
| 1003 |
|
| 1004 |
def _serialize_text(write, elem, encoding): |
| 1005 |
for part in elem.itertext(): |
| 1006 |
write(part.encode(encoding)) |
| 1007 |
if elem.tail: |
| 1008 |
write(elem.tail.encode(encoding)) |
| 1009 |
|
| 1010 |
_serialize = { |
| 1011 |
"xml": _serialize_xml, |
| 1012 |
"html": _serialize_html, |
| 1013 |
"text": _serialize_text, |
| 1014 |
|
| 1015 |
|
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
def register_namespace(prefix, uri): |
| 1030 |
if re.match("ns\d+$", prefix): |
| 1031 |
raise ValueError("Prefix format reserved for internal use") |
| 1032 |
for k, v in _namespace_map.items(): |
| 1033 |
if k == uri or v == prefix: |
| 1034 |
del _namespace_map[k] |
| 1035 |
_namespace_map[uri] = prefix |
| 1036 |
|
| 1037 |
_namespace_map = { |
| 1038 |
|
| 1039 |
"http://www.w3.org/XML/1998/namespace": "xml", |
| 1040 |
"http://www.w3.org/1999/xhtml": "html", |
| 1041 |
"http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf", |
| 1042 |
"http://schemas.xmlsoap.org/wsdl/": "wsdl", |
| 1043 |
|
| 1044 |
"http://www.w3.org/2001/XMLSchema": "xs", |
| 1045 |
"http://www.w3.org/2001/XMLSchema-instance": "xsi", |
| 1046 |
|
| 1047 |
"http://purl.org/dc/elements/1.1/": "dc", |
| 1048 |
} |
| 1049 |
|
| 1050 |
def _raise_serialization_error(text): |
| 1051 |
raise TypeError( |
| 1052 |
"cannot serialize %r (type %s)" % (text, type(text).__name__) |
| 1053 |
) |
| 1054 |
|
| 1055 |
def _encode(text, encoding): |
| 1056 |
try: |
| 1057 |
return text.encode(encoding, "xmlcharrefreplace") |
| 1058 |
except (TypeError, AttributeError): |
| 1059 |
_raise_serialization_error(text) |
| 1060 |
|
| 1061 |
def _escape_cdata(text, encoding): |
| 1062 |
|
| 1063 |
try: |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
if "&" in text: |
| 1068 |
text = text.replace("&", "&") |
| 1069 |
if "<" in text: |
| 1070 |
text = text.replace("<", "<") |
| 1071 |
if ">" in text: |
| 1072 |
text = text.replace(">", ">") |
| 1073 |
return text.encode(encoding, "xmlcharrefreplace") |
| 1074 |
except (TypeError, AttributeError): |
| 1075 |
_raise_serialization_error(text) |
| 1076 |
|
| 1077 |
def _escape_attrib(text, encoding): |
| 1078 |
|
| 1079 |
try: |
| 1080 |
if "&" in text: |
| 1081 |
text = text.replace("&", "&") |
| 1082 |
if "<" in text: |
| 1083 |
text = text.replace("<", "<") |
| 1084 |
if ">" in text: |
| 1085 |
text = text.replace(">", ">") |
| 1086 |
if "\"" in text: |
| 1087 |
text = text.replace("\"", """) |
| 1088 |
if "\n" in text: |
| 1089 |
text = text.replace("\n", " ") |
| 1090 |
return text.encode(encoding, "xmlcharrefreplace") |
| 1091 |
except (TypeError, AttributeError): |
| 1092 |
_raise_serialization_error(text) |
| 1093 |
|
| 1094 |
def _escape_attrib_html(text, encoding): |
| 1095 |
|
| 1096 |
try: |
| 1097 |
if "&" in text: |
| 1098 |
text = text.replace("&", "&") |
| 1099 |
if ">" in text: |
| 1100 |
text = text.replace(">", ">") |
| 1101 |
if "\"" in text: |
| 1102 |
text = text.replace("\"", """) |
| 1103 |
return text.encode(encoding, "xmlcharrefreplace") |
| 1104 |
except (TypeError, AttributeError): |
| 1105 |
_raise_serialization_error(text) |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
def tostring(element, encoding=None, method=None): |
| 1121 |
class dummy: |
| 1122 |
pass |
| 1123 |
data = [] |
| 1124 |
file = dummy() |
| 1125 |
file.write = data.append |
| 1126 |
ElementTree(element).write(file, encoding, method=method) |
| 1127 |
return "".join(data) |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
def tostringlist(element, encoding=None, method=None): |
| 1142 |
class dummy: |
| 1143 |
pass |
| 1144 |
data = [] |
| 1145 |
file = dummy() |
| 1146 |
file.write = data.append |
| 1147 |
ElementTree(element).write(file, encoding, method=method) |
| 1148 |
|
| 1149 |
return data |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
def dump(elem): |
| 1161 |
|
| 1162 |
if not isinstance(elem, ElementTree): |
| 1163 |
elem = ElementTree(elem) |
| 1164 |
elem.write(sys.stdout) |
| 1165 |
tail = elem.getroot().tail |
| 1166 |
if not tail or tail[-1] != "\n": |
| 1167 |
sys.stdout.write("\n") |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
def parse(source, parser=None): |
| 1181 |
tree = ElementTree() |
| 1182 |
tree.parse(source, parser) |
| 1183 |
return tree |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
def iterparse(source, events=None, parser=None): |
| 1197 |
close_source = False |
| 1198 |
if not hasattr(source, "read"): |
| 1199 |
source = open(source, "rb") |
| 1200 |
close_source = True |
| 1201 |
try: |
| 1202 |
if not parser: |
| 1203 |
parser = XMLParser(target=TreeBuilder()) |
| 1204 |
return _IterParseIterator(source, events, parser, close_source) |
| 1205 |
except: |
| 1206 |
if close_source: |
| 1207 |
source.close() |
| 1208 |
raise |
| 1209 |
|
| 1210 |
class _IterParseIterator(object): |
| 1211 |
|
| 1212 |
def __init__(self, source, events, parser, close_source=False): |
| 1213 |
self._file = source |
| 1214 |
self._close_file = close_source |
| 1215 |
self._events = [] |
| 1216 |
self._index = 0 |
| 1217 |
self._error = None |
| 1218 |
self.root = self._root = None |
| 1219 |
self._parser = parser |
| 1220 |
|
| 1221 |
parser = self._parser._parser |
| 1222 |
append = self._events.append |
| 1223 |
if events is None: |
| 1224 |
events = ["end"] |
| 1225 |
for event in events: |
| 1226 |
if event == "start": |
| 1227 |
try: |
| 1228 |
parser.ordered_attributes = 1 |
| 1229 |
parser.specified_attributes = 1 |
| 1230 |
def handler(tag, attrib_in, event=event, append=append, |
| 1231 |
start=self._parser._start_list): |
| 1232 |
append((event, start(tag, attrib_in))) |
| 1233 |
parser.StartElementHandler = handler |
| 1234 |
except AttributeError: |
| 1235 |
def handler(tag, attrib_in, event=event, append=append, |
| 1236 |
start=self._parser._start): |
| 1237 |
append((event, start(tag, attrib_in))) |
| 1238 |
parser.StartElementHandler = handler |
| 1239 |
elif event == "end": |
| 1240 |
def handler(tag, event=event, append=append, |
| 1241 |
end=self._parser._end): |
| 1242 |
append((event, end(tag))) |
| 1243 |
parser.EndElementHandler = handler |
| 1244 |
elif event == "start-ns": |
| 1245 |
def handler(prefix, uri, event=event, append=append): |
| 1246 |
try: |
| 1247 |
uri = (uri or "").encode("ascii") |
| 1248 |
except UnicodeError: |
| 1249 |
pass |
| 1250 |
append((event, (prefix or "", uri or ""))) |
| 1251 |
parser.StartNamespaceDeclHandler = handler |
| 1252 |
elif event == "end-ns": |
| 1253 |
def handler(prefix, event=event, append=append): |
| 1254 |
append((event, None)) |
| 1255 |
parser.EndNamespaceDeclHandler = handler |
| 1256 |
else: |
| 1257 |
raise ValueError("unknown event %r" % event) |
| 1258 |
|
| 1259 |
def next(self): |
| 1260 |
try: |
| 1261 |
while 1: |
| 1262 |
try: |
| 1263 |
item = self._events[self._index] |
| 1264 |
self._index += 1 |
| 1265 |
return item |
| 1266 |
except IndexError: |
| 1267 |
pass |
| 1268 |
if self._error: |
| 1269 |
e = self._error |
| 1270 |
self._error = None |
| 1271 |
raise e |
| 1272 |
if self._parser is None: |
| 1273 |
self.root = self._root |
| 1274 |
break |
| 1275 |
|
| 1276 |
del self._events[:] |
| 1277 |
self._index = 0 |
| 1278 |
data = self._file.read(16384) |
| 1279 |
if data: |
| 1280 |
try: |
| 1281 |
self._parser.feed(data) |
| 1282 |
except SyntaxError as exc: |
| 1283 |
self._error = exc |
| 1284 |
else: |
| 1285 |
self._root = self._parser.close() |
| 1286 |
self._parser = None |
| 1287 |
except: |
| 1288 |
if self._close_file: |
| 1289 |
self._file.close() |
| 1290 |
raise |
| 1291 |
if self._close_file: |
| 1292 |
self._file.close() |
| 1293 |
raise StopIteration |
| 1294 |
|
| 1295 |
def __iter__(self): |
| 1296 |
return self |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
def XML(text, parser=None): |
| 1309 |
if not parser: |
| 1310 |
parser = XMLParser(target=TreeBuilder()) |
| 1311 |
parser.feed(text) |
| 1312 |
return parser.close() |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
def XMLID(text, parser=None): |
| 1325 |
if not parser: |
| 1326 |
parser = XMLParser(target=TreeBuilder()) |
| 1327 |
parser.feed(text) |
| 1328 |
tree = parser.close() |
| 1329 |
ids = {} |
| 1330 |
for elem in tree.iter(): |
| 1331 |
id = elem.get("id") |
| 1332 |
if id: |
| 1333 |
ids[id] = elem |
| 1334 |
return tree, ids |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
fromstring = XML |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
def fromstringlist(sequence, parser=None): |
| 1357 |
if not parser: |
| 1358 |
parser = XMLParser(target=TreeBuilder()) |
| 1359 |
for text in sequence: |
| 1360 |
parser.feed(text) |
| 1361 |
return parser.close() |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
class TreeBuilder(object): |
| 1377 |
|
| 1378 |
def __init__(self, element_factory=None): |
| 1379 |
self._data = [] |
| 1380 |
self._elem = [] |
| 1381 |
self._last = None |
| 1382 |
self._tail = None |
| 1383 |
if element_factory is None: |
| 1384 |
element_factory = Element |
| 1385 |
self._factory = element_factory |
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
def close(self): |
| 1395 |
assert len(self._elem) == 0, "missing end tags" |
| 1396 |
assert self._last is not None, "missing toplevel element" |
| 1397 |
return self._last |
| 1398 |
|
| 1399 |
def _flush(self): |
| 1400 |
if self._data: |
| 1401 |
if self._last is not None: |
| 1402 |
text = "".join(self._data) |
| 1403 |
if self._tail: |
| 1404 |
assert self._last.tail is None, "internal error (tail)" |
| 1405 |
self._last.tail = text |
| 1406 |
else: |
| 1407 |
assert self._last.text is None, "internal error (text)" |
| 1408 |
self._last.text = text |
| 1409 |
self._data = [] |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
def data(self, data): |
| 1418 |
self._data.append(data) |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
def start(self, tag, attrs): |
| 1429 |
self._flush() |
| 1430 |
self._last = elem = self._factory(tag, attrs) |
| 1431 |
if self._elem: |
| 1432 |
self._elem[-1].append(elem) |
| 1433 |
self._elem.append(elem) |
| 1434 |
self._tail = 0 |
| 1435 |
return elem |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
def end(self, tag): |
| 1445 |
self._flush() |
| 1446 |
self._last = self._elem.pop() |
| 1447 |
assert self._last.tag == tag,\ |
| 1448 |
"end tag mismatch (expected %s, got %s)" % ( |
| 1449 |
self._last.tag, tag) |
| 1450 |
self._tail = 1 |
| 1451 |
return self._last |
| 1452 |
|
| 1453 |
_sentinel = ['sentinel'] |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
class XMLParser(object): |
| 1469 |
|
| 1470 |
def __init__(self, html=_sentinel, target=None, encoding=None): |
| 1471 |
if html is not _sentinel: |
| 1472 |
warnings.warnpy3k( |
| 1473 |
"The html argument of XMLParser() is deprecated", |
| 1474 |
DeprecationWarning, stacklevel=2) |
| 1475 |
try: |
| 1476 |
from xml.parsers import expat |
| 1477 |
except ImportError: |
| 1478 |
try: |
| 1479 |
import pyexpat as expat |
| 1480 |
except ImportError: |
| 1481 |
raise ImportError( |
| 1482 |
"No module named expat; use SimpleXMLTreeBuilder instead" |
| 1483 |
) |
| 1484 |
parser = expat.ParserCreate(encoding, "}") |
| 1485 |
if target is None: |
| 1486 |
target = TreeBuilder() |
| 1487 |
|
| 1488 |
self.parser = self._parser = parser |
| 1489 |
self.target = self._target = target |
| 1490 |
self._error = expat.error |
| 1491 |
self._names = {} |
| 1492 |
|
| 1493 |
parser.DefaultHandlerExpand = self._default |
| 1494 |
parser.StartElementHandler = self._start |
| 1495 |
parser.EndElementHandler = self._end |
| 1496 |
parser.CharacterDataHandler = self._data |
| 1497 |
|
| 1498 |
parser.CommentHandler = self._comment |
| 1499 |
parser.ProcessingInstructionHandler = self._pi |
| 1500 |
|
| 1501 |
try: |
| 1502 |
self._parser.buffer_text = 1 |
| 1503 |
except AttributeError: |
| 1504 |
pass |
| 1505 |
|
| 1506 |
try: |
| 1507 |
self._parser.ordered_attributes = 1 |
| 1508 |
self._parser.specified_attributes = 1 |
| 1509 |
parser.StartElementHandler = self._start_list |
| 1510 |
except AttributeError: |
| 1511 |
pass |
| 1512 |
self._doctype = None |
| 1513 |
self.entity = {} |
| 1514 |
try: |
| 1515 |
self.version = "Expat %d.%d.%d" % expat.version_info |
| 1516 |
except AttributeError: |
| 1517 |
pass |
| 1518 |
|
| 1519 |
def _raiseerror(self, value): |
| 1520 |
err = ParseError(value) |
| 1521 |
err.code = value.code |
| 1522 |
err.position = value.lineno, value.offset |
| 1523 |
raise err |
| 1524 |
|
| 1525 |
def _fixtext(self, text): |
| 1526 |
|
| 1527 |
try: |
| 1528 |
return text.encode("ascii") |
| 1529 |
except UnicodeError: |
| 1530 |
return text |
| 1531 |
|
| 1532 |
def _fixname(self, key): |
| 1533 |
|
| 1534 |
try: |
| 1535 |
name = self._names[key] |
| 1536 |
except KeyError: |
| 1537 |
name = key |
| 1538 |
if "}" in name: |
| 1539 |
name = "{" + name |
| 1540 |
self._names[key] = name = self._fixtext(name) |
| 1541 |
return name |
| 1542 |
|
| 1543 |
def _start(self, tag, attrib_in): |
| 1544 |
fixname = self._fixname |
| 1545 |
fixtext = self._fixtext |
| 1546 |
tag = fixname(tag) |
| 1547 |
attrib = {} |
| 1548 |
for key, value in attrib_in.items(): |
| 1549 |
attrib[fixname(key)] = fixtext(value) |
| 1550 |
return self.target.start(tag, attrib) |
| 1551 |
|
| 1552 |
def _start_list(self, tag, attrib_in): |
| 1553 |
fixname = self._fixname |
| 1554 |
fixtext = self._fixtext |
| 1555 |
tag = fixname(tag) |
| 1556 |
attrib = {} |
| 1557 |
if attrib_in: |
| 1558 |
for i in range(0, len(attrib_in), 2): |
| 1559 |
attrib[fixname(attrib_in[i])] = fixtext(attrib_in[i+1]) |
| 1560 |
return self.target.start(tag, attrib) |
| 1561 |
|
| 1562 |
def _data(self, text): |
| 1563 |
return self.target.data(self._fixtext(text)) |
| 1564 |
|
| 1565 |
def _end(self, tag): |
| 1566 |
return self.target.end(self._fixname(tag)) |
| 1567 |
|
| 1568 |
def _comment(self, data): |
| 1569 |
try: |
| 1570 |
comment = self.target.comment |
| 1571 |
except AttributeError: |
| 1572 |
pass |
| 1573 |
else: |
| 1574 |
return comment(self._fixtext(data)) |
| 1575 |
|
| 1576 |
def _pi(self, target, data): |
| 1577 |
try: |
| 1578 |
pi = self.target.pi |
| 1579 |
except AttributeError: |
| 1580 |
pass |
| 1581 |
else: |
| 1582 |
return pi(self._fixtext(target), self._fixtext(data)) |
| 1583 |
|
| 1584 |
def _default(self, text): |
| 1585 |
prefix = text[:1] |
| 1586 |
if prefix == "&": |
| 1587 |
|
| 1588 |
try: |
| 1589 |
self.target.data(self.entity[text[1:-1]]) |
| 1590 |
except KeyError: |
| 1591 |
from xml.parsers import expat |
| 1592 |
err = expat.error( |
| 1593 |
"undefined entity %s: line %d, column %d" % |
| 1594 |
(text, self._parser.ErrorLineNumber, |
| 1595 |
self._parser.ErrorColumnNumber) |
| 1596 |
) |
| 1597 |
err.code = 11 |
| 1598 |
err.lineno = self._parser.ErrorLineNumber |
| 1599 |
err.offset = self._parser.ErrorColumnNumber |
| 1600 |
raise err |
| 1601 |
elif prefix == "<" and text[:9] == "<!DOCTYPE": |
| 1602 |
self._doctype = [] |
| 1603 |
elif self._doctype is not None: |
| 1604 |
|
| 1605 |
if prefix == ">": |
| 1606 |
self._doctype = None |
| 1607 |
return |
| 1608 |
text = text.strip() |
| 1609 |
if not text: |
| 1610 |
return |
| 1611 |
self._doctype.append(text) |
| 1612 |
n = len(self._doctype) |
| 1613 |
if n > 2: |
| 1614 |
type = self._doctype[1] |
| 1615 |
if type == "PUBLIC" and n == 4: |
| 1616 |
name, type, pubid, system = self._doctype |
| 1617 |
elif type == "SYSTEM" and n == 3: |
| 1618 |
name, type, system = self._doctype |
| 1619 |
pubid = None |
| 1620 |
else: |
| 1621 |
return |
| 1622 |
if pubid: |
| 1623 |
pubid = pubid[1:-1] |
| 1624 |
if hasattr(self.target, "doctype"): |
| 1625 |
self.target.doctype(name, pubid, system[1:-1]) |
| 1626 |
elif self.doctype != self._XMLParser__doctype: |
| 1627 |
|
| 1628 |
self._XMLParser__doctype(name, pubid, system[1:-1]) |
| 1629 |
self.doctype(name, pubid, system[1:-1]) |
| 1630 |
self._doctype = None |
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
def doctype(self, name, pubid, system): |
| 1640 |
"""This method of XMLParser is deprecated.""" |
| 1641 |
warnings.warn( |
| 1642 |
"This method of XMLParser is deprecated. Define doctype() " |
| 1643 |
"method on the TreeBuilder target.", |
| 1644 |
DeprecationWarning, |
| 1645 |
) |
| 1646 |
|
| 1647 |
|
| 1648 |
__doctype = doctype |
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
def feed(self, data): |
| 1656 |
try: |
| 1657 |
self._parser.Parse(data, 0) |
| 1658 |
except self._error, v: |
| 1659 |
self._raiseerror(v) |
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
def close(self): |
| 1668 |
try: |
| 1669 |
self._parser.Parse("", 1) |
| 1670 |
except self._error, v: |
| 1671 |
self._raiseerror(v) |
| 1672 |
tree = self.target.close() |
| 1673 |
del self.target, self._parser |
| 1674 |
return tree |
| 1675 |
|
| 1676 |
|
| 1677 |
XMLTreeBuilder = XMLParser |
| 1678 |
|
| 1679 |
|
| 1680 |
try: |
| 1681 |
from ElementC14N import _serialize_c14n |
| 1682 |
_serialize["c14n"] = _serialize_c14n |
| 1683 |
except ImportError: |
| 1684 |
pass |