-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibxml2class.py
More file actions
8529 lines (7424 loc) · 314 KB
/
libxml2class.py
File metadata and controls
8529 lines (7424 loc) · 314 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Functions from module HTMLparser
#
def htmlCreateMemoryParserCtxt(buffer, size):
"""Create a parser context for an HTML in-memory document. """
ret = libxml2mod.htmlCreateMemoryParserCtxt(buffer, size)
if ret is None:raise parserError('htmlCreateMemoryParserCtxt() failed')
return parserCtxt(_obj=ret)
def htmlHandleOmittedElem(val):
"""Set and return the previous value for handling HTML omitted
tags. """
ret = libxml2mod.htmlHandleOmittedElem(val)
return ret
def htmlIsScriptAttribute(name):
"""Check if an attribute is of content type Script """
ret = libxml2mod.htmlIsScriptAttribute(name)
return ret
def htmlNewParserCtxt():
"""Allocate and initialize a new parser context. """
ret = libxml2mod.htmlNewParserCtxt()
if ret is None:raise parserError('htmlNewParserCtxt() failed')
return parserCtxt(_obj=ret)
def htmlParseDoc(cur, encoding):
"""parse an HTML in-memory document and build a tree. """
ret = libxml2mod.htmlParseDoc(cur, encoding)
if ret is None:raise parserError('htmlParseDoc() failed')
return xmlDoc(_obj=ret)
def htmlParseFile(filename, encoding):
"""parse an HTML file and build a tree. Automatic support for
ZLIB/Compress compressed document is provided by default if
found at compile-time. """
ret = libxml2mod.htmlParseFile(filename, encoding)
if ret is None:raise parserError('htmlParseFile() failed')
return xmlDoc(_obj=ret)
def htmlReadDoc(cur, URL, encoding, options):
"""parse an XML in-memory document and build a tree. """
ret = libxml2mod.htmlReadDoc(cur, URL, encoding, options)
if ret is None:raise treeError('htmlReadDoc() failed')
return xmlDoc(_obj=ret)
def htmlReadFd(fd, URL, encoding, options):
"""parse an XML from a file descriptor and build a tree. """
ret = libxml2mod.htmlReadFd(fd, URL, encoding, options)
if ret is None:raise treeError('htmlReadFd() failed')
return xmlDoc(_obj=ret)
def htmlReadFile(filename, encoding, options):
"""parse an XML file from the filesystem or the network. """
ret = libxml2mod.htmlReadFile(filename, encoding, options)
if ret is None:raise treeError('htmlReadFile() failed')
return xmlDoc(_obj=ret)
def htmlReadMemory(buffer, size, URL, encoding, options):
"""parse an XML in-memory document and build a tree. """
ret = libxml2mod.htmlReadMemory(buffer, size, URL, encoding, options)
if ret is None:raise treeError('htmlReadMemory() failed')
return xmlDoc(_obj=ret)
#
# Functions from module HTMLtree
#
def htmlIsBooleanAttr(name):
"""Determine if a given attribute is a boolean attribute. """
ret = libxml2mod.htmlIsBooleanAttr(name)
return ret
def htmlNewDoc(URI, ExternalID):
"""Creates a new HTML document """
ret = libxml2mod.htmlNewDoc(URI, ExternalID)
if ret is None:raise treeError('htmlNewDoc() failed')
return xmlDoc(_obj=ret)
def htmlNewDocNoDtD(URI, ExternalID):
"""Creates a new HTML document without a DTD node if @URI and
@ExternalID are None """
ret = libxml2mod.htmlNewDocNoDtD(URI, ExternalID)
if ret is None:raise treeError('htmlNewDocNoDtD() failed')
return xmlDoc(_obj=ret)
#
# Functions from module SAX2
#
def SAXDefaultVersion(version):
"""Set the default version of SAX used globally by the
library. By default, during initialization the default is
set to 2. Note that it is generally a better coding style
to use xmlSAXVersion() to set up the version explicitly for
a given parsing context. """
ret = libxml2mod.xmlSAXDefaultVersion(version)
return ret
def defaultSAXHandlerInit():
"""Initialize the default SAX2 handler """
libxml2mod.xmlDefaultSAXHandlerInit()
def docbDefaultSAXHandlerInit():
"""Initialize the default SAX handler """
libxml2mod.docbDefaultSAXHandlerInit()
def htmlDefaultSAXHandlerInit():
"""Initialize the default SAX handler """
libxml2mod.htmlDefaultSAXHandlerInit()
#
# Functions from module catalog
#
def catalogAdd(type, orig, replace):
"""Add an entry in the catalog, it may overwrite existing but
different entries. If called before any other catalog
routine, allows to override the default shared catalog put
in place by xmlInitializeCatalog(); """
ret = libxml2mod.xmlCatalogAdd(type, orig, replace)
return ret
def catalogCleanup():
"""Free up all the memory associated with catalogs """
libxml2mod.xmlCatalogCleanup()
def catalogConvert():
"""Convert all the SGML catalog entries as XML ones """
ret = libxml2mod.xmlCatalogConvert()
return ret
def catalogDump(out):
"""Dump all the global catalog content to the given file. """
if out is not None: out.flush()
libxml2mod.xmlCatalogDump(out)
def catalogGetPublic(pubID):
"""Try to lookup the catalog reference associated to a public
ID DEPRECATED, use xmlCatalogResolvePublic() """
ret = libxml2mod.xmlCatalogGetPublic(pubID)
return ret
def catalogGetSystem(sysID):
"""Try to lookup the catalog reference associated to a system
ID DEPRECATED, use xmlCatalogResolveSystem() """
ret = libxml2mod.xmlCatalogGetSystem(sysID)
return ret
def catalogRemove(value):
"""Remove an entry from the catalog """
ret = libxml2mod.xmlCatalogRemove(value)
return ret
def catalogResolve(pubID, sysID):
"""Do a complete resolution lookup of an External Identifier """
ret = libxml2mod.xmlCatalogResolve(pubID, sysID)
return ret
def catalogResolvePublic(pubID):
"""Try to lookup the catalog reference associated to a public
ID """
ret = libxml2mod.xmlCatalogResolvePublic(pubID)
return ret
def catalogResolveSystem(sysID):
"""Try to lookup the catalog resource for a system ID """
ret = libxml2mod.xmlCatalogResolveSystem(sysID)
return ret
def catalogResolveURI(URI):
"""Do a complete resolution lookup of an URI """
ret = libxml2mod.xmlCatalogResolveURI(URI)
return ret
def catalogSetDebug(level):
"""Used to set the debug level for catalog operation, 0
disable debugging, 1 enable it """
ret = libxml2mod.xmlCatalogSetDebug(level)
return ret
def initializeCatalog():
"""Do the catalog initialization. this function is not thread
safe, catalog initialization should preferably be done once
at startup """
libxml2mod.xmlInitializeCatalog()
def loadACatalog(filename):
"""Load the catalog and build the associated data structures.
This can be either an XML Catalog or an SGML Catalog It
will recurse in SGML CATALOG entries. On the other hand XML
Catalogs are not handled recursively. """
ret = libxml2mod.xmlLoadACatalog(filename)
if ret is None:raise treeError('xmlLoadACatalog() failed')
return catalog(_obj=ret)
def loadCatalog(filename):
"""Load the catalog and makes its definitions effective for
the default external entity loader. It will recurse in SGML
CATALOG entries. this function is not thread safe, catalog
initialization should preferably be done once at startup """
ret = libxml2mod.xmlLoadCatalog(filename)
return ret
def loadCatalogs(pathss):
"""Load the catalogs and makes their definitions effective for
the default external entity loader. this function is not
thread safe, catalog initialization should preferably be
done once at startup """
libxml2mod.xmlLoadCatalogs(pathss)
def loadSGMLSuperCatalog(filename):
"""Load an SGML super catalog. It won't expand CATALOG or
DELEGATE references. This is only needed for manipulating
SGML Super Catalogs like adding and removing CATALOG or
DELEGATE entries. """
ret = libxml2mod.xmlLoadSGMLSuperCatalog(filename)
if ret is None:raise treeError('xmlLoadSGMLSuperCatalog() failed')
return catalog(_obj=ret)
def newCatalog(sgml):
"""create a new Catalog. """
ret = libxml2mod.xmlNewCatalog(sgml)
if ret is None:raise treeError('xmlNewCatalog() failed')
return catalog(_obj=ret)
def parseCatalogFile(filename):
"""parse an XML file and build a tree. It's like
xmlParseFile() except it bypass all catalog lookups. """
ret = libxml2mod.xmlParseCatalogFile(filename)
if ret is None:raise parserError('xmlParseCatalogFile() failed')
return xmlDoc(_obj=ret)
#
# Functions from module chvalid
#
def isBaseChar(ch):
"""This function is DEPRECATED. Use xmlIsBaseChar_ch or
xmlIsBaseCharQ instead """
ret = libxml2mod.xmlIsBaseChar(ch)
return ret
def isBlank(ch):
"""This function is DEPRECATED. Use xmlIsBlank_ch or
xmlIsBlankQ instead """
ret = libxml2mod.xmlIsBlank(ch)
return ret
def isChar(ch):
"""This function is DEPRECATED. Use xmlIsChar_ch or xmlIsCharQ
instead """
ret = libxml2mod.xmlIsChar(ch)
return ret
def isCombining(ch):
"""This function is DEPRECATED. Use xmlIsCombiningQ instead """
ret = libxml2mod.xmlIsCombining(ch)
return ret
def isDigit(ch):
"""This function is DEPRECATED. Use xmlIsDigit_ch or
xmlIsDigitQ instead """
ret = libxml2mod.xmlIsDigit(ch)
return ret
def isExtender(ch):
"""This function is DEPRECATED. Use xmlIsExtender_ch or
xmlIsExtenderQ instead """
ret = libxml2mod.xmlIsExtender(ch)
return ret
def isIdeographic(ch):
"""This function is DEPRECATED. Use xmlIsIdeographicQ instead """
ret = libxml2mod.xmlIsIdeographic(ch)
return ret
def isPubidChar(ch):
"""This function is DEPRECATED. Use xmlIsPubidChar_ch or
xmlIsPubidCharQ instead """
ret = libxml2mod.xmlIsPubidChar(ch)
return ret
#
# Functions from module debugXML
#
def boolToText(boolval):
"""Convenient way to turn bool into text """
ret = libxml2mod.xmlBoolToText(boolval)
return ret
def debugDumpString(output, str):
"""Dumps informations about the string, shorten it if necessary """
if output is not None: output.flush()
libxml2mod.xmlDebugDumpString(output, str)
def shellPrintXPathError(errorType, arg):
"""Print the xpath error to libxml default error channel """
libxml2mod.xmlShellPrintXPathError(errorType, arg)
#
# Functions from module dict
#
def dictCleanup():
"""Free the dictionary mutex. Do not call unless sure the
library is not in use anymore ! """
libxml2mod.xmlDictCleanup()
def initializeDict():
"""Do the dictionary mutex initialization. this function is
deprecated """
ret = libxml2mod.xmlInitializeDict()
return ret
#
# Functions from module encoding
#
def addEncodingAlias(name, alias):
"""Registers an alias @alias for an encoding named @name.
Existing alias will be overwritten. """
ret = libxml2mod.xmlAddEncodingAlias(name, alias)
return ret
def cleanupCharEncodingHandlers():
"""Cleanup the memory allocated for the char encoding support,
it unregisters all the encoding handlers and the aliases. """
libxml2mod.xmlCleanupCharEncodingHandlers()
def cleanupEncodingAliases():
"""Unregisters all aliases """
libxml2mod.xmlCleanupEncodingAliases()
def delEncodingAlias(alias):
"""Unregisters an encoding alias @alias """
ret = libxml2mod.xmlDelEncodingAlias(alias)
return ret
def encodingAlias(alias):
"""Lookup an encoding name for the given alias. """
ret = libxml2mod.xmlGetEncodingAlias(alias)
return ret
def initCharEncodingHandlers():
"""Initialize the char encoding support, it registers the
default encoding supported. NOTE: while public, this
function usually doesn't need to be called in normal
processing. """
libxml2mod.xmlInitCharEncodingHandlers()
#
# Functions from module entities
#
def cleanupPredefinedEntities():
"""Cleanup up the predefined entities table. Deprecated call """
libxml2mod.xmlCleanupPredefinedEntities()
def initializePredefinedEntities():
"""Set up the predefined entities. Deprecated call """
libxml2mod.xmlInitializePredefinedEntities()
def predefinedEntity(name):
"""Check whether this name is an predefined entity. """
ret = libxml2mod.xmlGetPredefinedEntity(name)
if ret is None:raise treeError('xmlGetPredefinedEntity() failed')
return xmlEntity(_obj=ret)
#
# Functions from module globals
#
def cleanupGlobals():
"""Additional cleanup for multi-threading """
libxml2mod.xmlCleanupGlobals()
def initGlobals():
"""Additional initialisation for multi-threading """
libxml2mod.xmlInitGlobals()
def thrDefDefaultBufferSize(v):
ret = libxml2mod.xmlThrDefDefaultBufferSize(v)
return ret
def thrDefDoValidityCheckingDefaultValue(v):
ret = libxml2mod.xmlThrDefDoValidityCheckingDefaultValue(v)
return ret
def thrDefGetWarningsDefaultValue(v):
ret = libxml2mod.xmlThrDefGetWarningsDefaultValue(v)
return ret
def thrDefIndentTreeOutput(v):
ret = libxml2mod.xmlThrDefIndentTreeOutput(v)
return ret
def thrDefKeepBlanksDefaultValue(v):
ret = libxml2mod.xmlThrDefKeepBlanksDefaultValue(v)
return ret
def thrDefLineNumbersDefaultValue(v):
ret = libxml2mod.xmlThrDefLineNumbersDefaultValue(v)
return ret
def thrDefLoadExtDtdDefaultValue(v):
ret = libxml2mod.xmlThrDefLoadExtDtdDefaultValue(v)
return ret
def thrDefParserDebugEntities(v):
ret = libxml2mod.xmlThrDefParserDebugEntities(v)
return ret
def thrDefPedanticParserDefaultValue(v):
ret = libxml2mod.xmlThrDefPedanticParserDefaultValue(v)
return ret
def thrDefSaveNoEmptyTags(v):
ret = libxml2mod.xmlThrDefSaveNoEmptyTags(v)
return ret
def thrDefSubstituteEntitiesDefaultValue(v):
ret = libxml2mod.xmlThrDefSubstituteEntitiesDefaultValue(v)
return ret
def thrDefTreeIndentString(v):
ret = libxml2mod.xmlThrDefTreeIndentString(v)
return ret
#
# Functions from module nanoftp
#
def nanoFTPCleanup():
"""Cleanup the FTP protocol layer. This cleanup proxy
informations. """
libxml2mod.xmlNanoFTPCleanup()
def nanoFTPInit():
"""Initialize the FTP protocol layer. Currently it just checks
for proxy informations, and get the hostname """
libxml2mod.xmlNanoFTPInit()
def nanoFTPProxy(host, port, user, passwd, type):
"""Setup the FTP proxy informations. This can also be done by
using ftp_proxy ftp_proxy_user and ftp_proxy_password
environment variables. """
libxml2mod.xmlNanoFTPProxy(host, port, user, passwd, type)
def nanoFTPScanProxy(URL):
"""(Re)Initialize the FTP Proxy context by parsing the URL and
finding the protocol host port it indicates. Should be like
ftp://myproxy/ or ftp://myproxy:3128/ A None URL cleans up
proxy informations. """
libxml2mod.xmlNanoFTPScanProxy(URL)
#
# Functions from module nanohttp
#
def nanoHTTPCleanup():
"""Cleanup the HTTP protocol layer. """
libxml2mod.xmlNanoHTTPCleanup()
def nanoHTTPInit():
"""Initialize the HTTP protocol layer. Currently it just
checks for proxy informations """
libxml2mod.xmlNanoHTTPInit()
def nanoHTTPScanProxy(URL):
"""(Re)Initialize the HTTP Proxy context by parsing the URL
and finding the protocol host port it indicates. Should be
like http://myproxy/ or http://myproxy:3128/ A None URL
cleans up proxy informations. """
libxml2mod.xmlNanoHTTPScanProxy(URL)
#
# Functions from module parser
#
def createDocParserCtxt(cur):
"""Creates a parser context for an XML in-memory document. """
ret = libxml2mod.xmlCreateDocParserCtxt(cur)
if ret is None:raise parserError('xmlCreateDocParserCtxt() failed')
return parserCtxt(_obj=ret)
def initParser():
"""Initialization function for the XML parser. This is not
reentrant. Call once before processing in case of use in
multithreaded programs. """
libxml2mod.xmlInitParser()
def keepBlanksDefault(val):
"""Set and return the previous value for default blanks text
nodes support. The 1.x version of the parser used an
heuristic to try to detect ignorable white spaces. As a
result the SAX callback was generating
xmlSAX2IgnorableWhitespace() callbacks instead of
characters() one, and when using the DOM output text nodes
containing those blanks were not generated. The 2.x and
later version will switch to the XML standard way and
ignorableWhitespace() are only generated when running the
parser in validating mode and when the current element
doesn't allow CDATA or mixed content. This function is
provided as a way to force the standard behavior on 1.X
libs and to switch back to the old mode for compatibility
when running 1.X client code on 2.X . Upgrade of 1.X code
should be done by using xmlIsBlankNode() commodity function
to detect the "empty" nodes generated. This value also
affect autogeneration of indentation when saving code if
blanks sections are kept, indentation is not generated. """
ret = libxml2mod.xmlKeepBlanksDefault(val)
return ret
def lineNumbersDefault(val):
"""Set and return the previous value for enabling line numbers
in elements contents. This may break on old application and
is turned off by default. """
ret = libxml2mod.xmlLineNumbersDefault(val)
return ret
def newParserCtxt():
"""Allocate and initialize a new parser context. """
ret = libxml2mod.xmlNewParserCtxt()
if ret is None:raise parserError('xmlNewParserCtxt() failed')
return parserCtxt(_obj=ret)
def parseDTD(ExternalID, SystemID):
"""Load and parse an external subset. """
ret = libxml2mod.xmlParseDTD(ExternalID, SystemID)
if ret is None:raise parserError('xmlParseDTD() failed')
return xmlDtd(_obj=ret)
def parseDoc(cur):
"""parse an XML in-memory document and build a tree. """
ret = libxml2mod.xmlParseDoc(cur)
if ret is None:raise parserError('xmlParseDoc() failed')
return xmlDoc(_obj=ret)
def parseEntity(filename):
"""parse an XML external entity out of context and build a
tree. [78] extParsedEnt ::= TextDecl? content This
correspond to a "Well Balanced" chunk """
ret = libxml2mod.xmlParseEntity(filename)
if ret is None:raise parserError('xmlParseEntity() failed')
return xmlDoc(_obj=ret)
def parseFile(filename):
"""parse an XML file and build a tree. Automatic support for
ZLIB/Compress compressed document is provided by default if
found at compile-time. """
ret = libxml2mod.xmlParseFile(filename)
if ret is None:raise parserError('xmlParseFile() failed')
return xmlDoc(_obj=ret)
def parseMemory(buffer, size):
"""parse an XML in-memory block and build a tree. """
ret = libxml2mod.xmlParseMemory(buffer, size)
if ret is None:raise parserError('xmlParseMemory() failed')
return xmlDoc(_obj=ret)
def pedanticParserDefault(val):
"""Set and return the previous value for enabling pedantic
warnings. """
ret = libxml2mod.xmlPedanticParserDefault(val)
return ret
def readDoc(cur, URL, encoding, options):
"""parse an XML in-memory document and build a tree. """
ret = libxml2mod.xmlReadDoc(cur, URL, encoding, options)
if ret is None:raise treeError('xmlReadDoc() failed')
return xmlDoc(_obj=ret)
def readFd(fd, URL, encoding, options):
"""parse an XML from a file descriptor and build a tree. NOTE
that the file descriptor will not be closed when the reader
is closed or reset. """
ret = libxml2mod.xmlReadFd(fd, URL, encoding, options)
if ret is None:raise treeError('xmlReadFd() failed')
return xmlDoc(_obj=ret)
def readFile(filename, encoding, options):
"""parse an XML file from the filesystem or the network. """
ret = libxml2mod.xmlReadFile(filename, encoding, options)
if ret is None:raise treeError('xmlReadFile() failed')
return xmlDoc(_obj=ret)
def readMemory(buffer, size, URL, encoding, options):
"""parse an XML in-memory document and build a tree. """
ret = libxml2mod.xmlReadMemory(buffer, size, URL, encoding, options)
if ret is None:raise treeError('xmlReadMemory() failed')
return xmlDoc(_obj=ret)
def recoverDoc(cur):
"""parse an XML in-memory document and build a tree. In the
case the document is not Well Formed, a attempt to build a
tree is tried anyway """
ret = libxml2mod.xmlRecoverDoc(cur)
if ret is None:raise treeError('xmlRecoverDoc() failed')
return xmlDoc(_obj=ret)
def recoverFile(filename):
"""parse an XML file and build a tree. Automatic support for
ZLIB/Compress compressed document is provided by default if
found at compile-time. In the case the document is not Well
Formed, it attempts to build a tree anyway """
ret = libxml2mod.xmlRecoverFile(filename)
if ret is None:raise treeError('xmlRecoverFile() failed')
return xmlDoc(_obj=ret)
def recoverMemory(buffer, size):
"""parse an XML in-memory block and build a tree. In the case
the document is not Well Formed, an attempt to build a tree
is tried anyway """
ret = libxml2mod.xmlRecoverMemory(buffer, size)
if ret is None:raise treeError('xmlRecoverMemory() failed')
return xmlDoc(_obj=ret)
def substituteEntitiesDefault(val):
"""Set and return the previous value for default entity
support. Initially the parser always keep entity references
instead of substituting entity values in the output. This
function has to be used to change the default parser
behavior SAX::substituteEntities() has to be used for
changing that on a file by file basis. """
ret = libxml2mod.xmlSubstituteEntitiesDefault(val)
return ret
#
# Functions from module parserInternals
#
def checkLanguageID(lang):
"""Checks that the value conforms to the LanguageID
production: NOTE: this is somewhat deprecated, those
productions were removed from the XML Second edition. [33]
LanguageID ::= Langcode ('-' Subcode)* [34] Langcode ::=
ISO639Code | IanaCode | UserCode [35] ISO639Code ::=
([a-z] | [A-Z]) ([a-z] | [A-Z]) [36] IanaCode ::= ('i' |
'I') '-' ([a-z] | [A-Z])+ [37] UserCode ::= ('x' | 'X') '-'
([a-z] | [A-Z])+ [38] Subcode ::= ([a-z] | [A-Z])+ The
current REC reference the sucessors of RFC 1766, currently
5646 http://www.rfc-editor.org/rfc/rfc5646.txt langtag
= language ["-" script] ["-" region] *("-" variant) *("-"
extension) ["-" privateuse] language = 2*3ALPHA
; shortest ISO 639 code ["-" extlang] ; sometimes
followed by ; extended language subtags / 4ALPHA
; or reserved for future use / 5*8ALPHA ; or
registered language subtag extlang = 3ALPHA
; selected ISO 639 codes *2("-" 3ALPHA) ; permanently
reserved script = 4ALPHA ; ISO 15924
code region = 2ALPHA ; ISO 3166-1 code
/ 3DIGIT ; UN M.49 code variant =
5*8alphanum ; registered variants / (DIGIT
3alphanum) extension = singleton 1*("-" (2*8alphanum))
; Single alphanumerics ; "x" reserved for private use
singleton = DIGIT ; 0 - 9 / %x41-57
; A - W / %x59-5A ; Y - Z / %x61-77
; a - w / %x79-7A ; y - z it sounds right to
still allow Irregular i-xxx IANA and user codes too The
parser below doesn't try to cope with extension or
privateuse that could be added but that's not interoperable
anyway """
ret = libxml2mod.xmlCheckLanguageID(lang)
return ret
def copyChar(len, out, val):
"""append the char value in the array """
ret = libxml2mod.xmlCopyChar(len, out, val)
return ret
def copyCharMultiByte(out, val):
"""append the char value in the array """
ret = libxml2mod.xmlCopyCharMultiByte(out, val)
return ret
def createEntityParserCtxt(URL, ID, base):
"""Create a parser context for an external entity Automatic
support for ZLIB/Compress compressed document is provided
by default if found at compile-time. """
ret = libxml2mod.xmlCreateEntityParserCtxt(URL, ID, base)
if ret is None:raise parserError('xmlCreateEntityParserCtxt() failed')
return parserCtxt(_obj=ret)
def createFileParserCtxt(filename):
"""Create a parser context for a file content. Automatic
support for ZLIB/Compress compressed document is provided
by default if found at compile-time. """
ret = libxml2mod.xmlCreateFileParserCtxt(filename)
if ret is None:raise parserError('xmlCreateFileParserCtxt() failed')
return parserCtxt(_obj=ret)
def createMemoryParserCtxt(buffer, size):
"""Create a parser context for an XML in-memory document. """
ret = libxml2mod.xmlCreateMemoryParserCtxt(buffer, size)
if ret is None:raise parserError('xmlCreateMemoryParserCtxt() failed')
return parserCtxt(_obj=ret)
def createURLParserCtxt(filename, options):
"""Create a parser context for a file or URL content.
Automatic support for ZLIB/Compress compressed document is
provided by default if found at compile-time and for file
accesses """
ret = libxml2mod.xmlCreateURLParserCtxt(filename, options)
if ret is None:raise parserError('xmlCreateURLParserCtxt() failed')
return parserCtxt(_obj=ret)
def htmlCreateFileParserCtxt(filename, encoding):
"""Create a parser context for a file content. Automatic
support for ZLIB/Compress compressed document is provided
by default if found at compile-time. """
ret = libxml2mod.htmlCreateFileParserCtxt(filename, encoding)
if ret is None:raise parserError('htmlCreateFileParserCtxt() failed')
return parserCtxt(_obj=ret)
def htmlInitAutoClose():
"""Initialize the htmlStartCloseIndex for fast lookup of
closing tags names. This is not reentrant. Call
xmlInitParser() once before processing in case of use in
multithreaded programs. """
libxml2mod.htmlInitAutoClose()
def isLetter(c):
"""Check whether the character is allowed by the production
[84] Letter ::= BaseChar | Ideographic """
ret = libxml2mod.xmlIsLetter(c)
return ret
def namePop(ctxt):
"""Pops the top element name from the name stack """
if ctxt is None: ctxt__o = None
else: ctxt__o = ctxt._o
ret = libxml2mod.namePop(ctxt__o)
return ret
def namePush(ctxt, value):
"""Pushes a new element name on top of the name stack """
if ctxt is None: ctxt__o = None
else: ctxt__o = ctxt._o
ret = libxml2mod.namePush(ctxt__o, value)
return ret
def nodePop(ctxt):
"""Pops the top element node from the node stack """
if ctxt is None: ctxt__o = None
else: ctxt__o = ctxt._o
ret = libxml2mod.nodePop(ctxt__o)
if ret is None:raise treeError('nodePop() failed')
return xmlNode(_obj=ret)
def nodePush(ctxt, value):
"""Pushes a new element node on top of the node stack """
if ctxt is None: ctxt__o = None
else: ctxt__o = ctxt._o
if value is None: value__o = None
else: value__o = value._o
ret = libxml2mod.nodePush(ctxt__o, value__o)
return ret
#
# Functions from module python
#
def SAXParseFile(SAX, URI, recover):
"""Interface to parse an XML file or resource pointed by an
URI to build an event flow to the SAX object """
libxml2mod.xmlSAXParseFile(SAX, URI, recover)
def createInputBuffer(file, encoding):
"""Create a libxml2 input buffer from a Python file """
ret = libxml2mod.xmlCreateInputBuffer(file, encoding)
if ret is None:raise treeError('xmlCreateInputBuffer() failed')
return inputBuffer(_obj=ret)
def createOutputBuffer(file, encoding):
"""Create a libxml2 output buffer from a Python file """
ret = libxml2mod.xmlCreateOutputBuffer(file, encoding)
if ret is None:raise treeError('xmlCreateOutputBuffer() failed')
return outputBuffer(_obj=ret)
def createPushParser(SAX, chunk, size, URI):
"""Create a progressive XML parser context to build either an
event flow if the SAX object is not None, or a DOM tree
otherwise. """
ret = libxml2mod.xmlCreatePushParser(SAX, chunk, size, URI)
if ret is None:raise parserError('xmlCreatePushParser() failed')
return parserCtxt(_obj=ret)
def debugMemory(activate):
"""Switch on the generation of line number for elements nodes.
Also returns the number of bytes allocated and not freed by
libxml2 since memory debugging was switched on. """
ret = libxml2mod.xmlDebugMemory(activate)
return ret
def dumpMemory():
"""dump the memory allocated in the file .memdump """
libxml2mod.xmlDumpMemory()
def htmlCreatePushParser(SAX, chunk, size, URI):
"""Create a progressive HTML parser context to build either an
event flow if the SAX object is not None, or a DOM tree
otherwise. """
ret = libxml2mod.htmlCreatePushParser(SAX, chunk, size, URI)
if ret is None:raise parserError('htmlCreatePushParser() failed')
return parserCtxt(_obj=ret)
def htmlSAXParseFile(SAX, URI, encoding):
"""Interface to parse an HTML file or resource pointed by an
URI to build an event flow to the SAX object """
libxml2mod.htmlSAXParseFile(SAX, URI, encoding)
def memoryUsed():
"""Returns the total amount of memory allocated by libxml2 """
ret = libxml2mod.xmlMemoryUsed()
return ret
def newNode(name):
"""Create a new Node """
ret = libxml2mod.xmlNewNode(name)
if ret is None:raise treeError('xmlNewNode() failed')
return xmlNode(_obj=ret)
def pythonCleanupParser():
"""Cleanup function for the XML library. It tries to reclaim
all parsing related global memory allocated for the library
processing. It doesn't deallocate any document related
memory. Calling this function should not prevent reusing
the library but one should call xmlCleanupParser() only
when the process has finished using the library or XML
document built with it. """
libxml2mod.xmlPythonCleanupParser()
def setEntityLoader(resolver):
"""Set the entity resolver as a python function """
ret = libxml2mod.xmlSetEntityLoader(resolver)
return ret
#
# Functions from module relaxng
#
def relaxNGCleanupTypes():
"""Cleanup the default Schemas type library associated to
RelaxNG """
libxml2mod.xmlRelaxNGCleanupTypes()
def relaxNGInitTypes():
"""Initilize the default type libraries. """
ret = libxml2mod.xmlRelaxNGInitTypes()
return ret
def relaxNGNewMemParserCtxt(buffer, size):
"""Create an XML RelaxNGs parse context for that memory buffer
expected to contain an XML RelaxNGs file. """
ret = libxml2mod.xmlRelaxNGNewMemParserCtxt(buffer, size)
if ret is None:raise parserError('xmlRelaxNGNewMemParserCtxt() failed')
return relaxNgParserCtxt(_obj=ret)
def relaxNGNewParserCtxt(URL):
"""Create an XML RelaxNGs parse context for that file/resource
expected to contain an XML RelaxNGs file. """
ret = libxml2mod.xmlRelaxNGNewParserCtxt(URL)
if ret is None:raise parserError('xmlRelaxNGNewParserCtxt() failed')
return relaxNgParserCtxt(_obj=ret)
#
# Functions from module tree
#
def buildQName(ncname, prefix, memory, len):
"""Builds the QName @prefix:@ncname in @memory if there is
enough space and prefix is not None nor empty, otherwise
allocate a new string. If prefix is None or empty it
returns ncname. """
ret = libxml2mod.xmlBuildQName(ncname, prefix, memory, len)
return ret
def compressMode():
"""get the default compression mode used, ZLIB based. """
ret = libxml2mod.xmlGetCompressMode()
return ret
def isXHTML(systemID, publicID):
"""Try to find if the document correspond to an XHTML DTD """
ret = libxml2mod.xmlIsXHTML(systemID, publicID)
return ret
def newComment(content):
"""Creation of a new node containing a comment. """
ret = libxml2mod.xmlNewComment(content)
if ret is None:raise treeError('xmlNewComment() failed')
return xmlNode(_obj=ret)
def newDoc(version):
"""Creates a new XML document """
ret = libxml2mod.xmlNewDoc(version)
if ret is None:raise treeError('xmlNewDoc() failed')
return xmlDoc(_obj=ret)
def newPI(name, content):
"""Creation of a processing instruction element. Use
xmlDocNewPI preferably to get string interning """
ret = libxml2mod.xmlNewPI(name, content)
if ret is None:raise treeError('xmlNewPI() failed')
return xmlNode(_obj=ret)
def newText(content):
"""Creation of a new text node. """
ret = libxml2mod.xmlNewText(content)
if ret is None:raise treeError('xmlNewText() failed')
return xmlNode(_obj=ret)
def newTextLen(content, len):
"""Creation of a new text node with an extra parameter for the
content's length """
ret = libxml2mod.xmlNewTextLen(content, len)
if ret is None:raise treeError('xmlNewTextLen() failed')
return xmlNode(_obj=ret)
def setCompressMode(mode):
"""set the default compression mode used, ZLIB based Correct
values: 0 (uncompressed) to 9 (max compression) """
libxml2mod.xmlSetCompressMode(mode)
def validateNCName(value, space):
"""Check that a value conforms to the lexical space of NCName """
ret = libxml2mod.xmlValidateNCName(value, space)
return ret
def validateNMToken(value, space):
"""Check that a value conforms to the lexical space of NMToken """
ret = libxml2mod.xmlValidateNMToken(value, space)
return ret
def validateName(value, space):
"""Check that a value conforms to the lexical space of Name """
ret = libxml2mod.xmlValidateName(value, space)
return ret
def validateQName(value, space):
"""Check that a value conforms to the lexical space of QName """
ret = libxml2mod.xmlValidateQName(value, space)
return ret
#
# Functions from module uri
#
def URIEscape(str):
"""Escaping routine, does not do validity checks ! It will try
to escape the chars needing this, but this is heuristic
based it's impossible to be sure. """
ret = libxml2mod.xmlURIEscape(str)
return ret
def URIEscapeStr(str, list):
"""This routine escapes a string to hex, ignoring reserved
characters (a-z) and the characters in the exception list. """
ret = libxml2mod.xmlURIEscapeStr(str, list)
return ret
def URIUnescapeString(str, len, target):
"""Unescaping routine, but does not check that the string is
an URI. The output is a direct unsigned char translation of
%XX values (no encoding) Note that the length of the result
can only be smaller or same size as the input string. """
ret = libxml2mod.xmlURIUnescapeString(str, len, target)
return ret
def buildRelativeURI(URI, base):
"""Expresses the URI of the reference in terms relative to the
base. Some examples of this operation include: base =
"http://site1.com/docs/book1.html" URI input
URI returned docs/pic1.gif pic1.gif
docs/img/pic1.gif img/pic1.gif img/pic1.gif
../img/pic1.gif http://site1.com/docs/pic1.gif pic1.gif
http://site2.com/docs/pic1.gif
http://site2.com/docs/pic1.gif base = "docs/book1.html"
URI input URI returned docs/pic1.gif
pic1.gif docs/img/pic1.gif img/pic1.gif
img/pic1.gif ../img/pic1.gif
http://site1.com/docs/pic1.gif
http://site1.com/docs/pic1.gif Note: if the URI reference
is really wierd or complicated, it may be worthwhile to
first convert it into a "nice" one by calling xmlBuildURI
(using 'base') before calling this routine, since this
routine (for reasonable efficiency) assumes URI has already
been through some validation. """
ret = libxml2mod.xmlBuildRelativeURI(URI, base)
return ret
def buildURI(URI, base):
"""Computes he final URI of the reference done by checking
that the given URI is valid, and building the final URI
using the base URI. This is processed according to section
5.2 of the RFC 2396 5.2. Resolving Relative References to
Absolute Form """