version 1.1.1.1, 2012/02/21 23:38:00
|
version 1.1.1.2, 2014/06/15 19:53:33
|
Line 5 import sys
|
Line 5 import sys
|
# The root of all libxml2 errors. |
# The root of all libxml2 errors. |
class libxmlError(Exception): pass |
class libxmlError(Exception): pass |
|
|
|
# Type of the wrapper class for the C objects wrappers |
|
def checkWrapper(obj): |
|
try: |
|
n = type(_obj).__name__ |
|
if n != 'PyCObject' and n != 'PyCapsule': |
|
return 1 |
|
except: |
|
return 0 |
|
return 0 |
|
|
# |
# |
# id() is sometimes negative ... |
# id() is sometimes negative ... |
# |
# |
def pos_id(o): |
def pos_id(o): |
i = id(o) |
i = id(o) |
if (i < 0): |
if (i < 0): |
return (sys.maxint - i) | return (sys.maxsize - i) |
return i |
return i |
|
|
# |
# |
Line 62 class ioWrapper:
|
Line 72 class ioWrapper:
|
def io_read(self, len = -1): |
def io_read(self, len = -1): |
if self.__io == None: |
if self.__io == None: |
return(-1) |
return(-1) |
if len < 0: | try: |
return(self.__io.read()) | if len < 0: |
return(self.__io.read(len)) | ret = self.__io.read() |
| else: |
| ret = self.__io.read(len) |
| except Exception: |
| import sys |
| e = sys.exc_info()[1] |
| print("failed to read from Python:", type(e)) |
| print("on IO:", self.__io) |
| self.__io == None |
| return(-1) |
|
|
|
return(ret) |
|
|
def io_write(self, str, len = -1): |
def io_write(self, str, len = -1): |
if self.__io == None: |
if self.__io == None: |
return(-1) |
return(-1) |
Line 79 class ioReadWrapper(ioWrapper):
|
Line 100 class ioReadWrapper(ioWrapper):
|
self._o = libxml2mod.xmlCreateInputBuffer(self, enc) |
self._o = libxml2mod.xmlCreateInputBuffer(self, enc) |
|
|
def __del__(self): |
def __del__(self): |
print "__del__" | print("__del__") |
self.io_close() |
self.io_close() |
if self._o != None: |
if self._o != None: |
libxml2mod.xmlFreeParserInputBuffer(self._o) |
libxml2mod.xmlFreeParserInputBuffer(self._o) |
Line 95 class ioWriteWrapper(ioWrapper):
|
Line 116 class ioWriteWrapper(ioWrapper):
|
def __init__(self, _obj, enc = ""): |
def __init__(self, _obj, enc = ""): |
# print "ioWriteWrapper.__init__", _obj |
# print "ioWriteWrapper.__init__", _obj |
if type(_obj) == type(''): |
if type(_obj) == type(''): |
print "write io from a string" | print("write io from a string") |
self.o = None |
self.o = None |
elif type(_obj) == types.InstanceType: | elif type(_obj).__name__ == 'PyCapsule': |
print "write io from instance of %s" % (_obj.__class__) | file = libxml2mod.outputBufferGetPythonFile(_obj) |
ioWrapper.__init__(self, _obj) | if file != None: |
self._o = libxml2mod.xmlCreateOutputBuffer(self, enc) | ioWrapper.__init__(self, file) |
| else: |
| ioWrapper.__init__(self, _obj) |
| self._o = _obj |
| # elif type(_obj) == types.InstanceType: |
| # print(("write io from instance of %s" % (_obj.__class__))) |
| # ioWrapper.__init__(self, _obj) |
| # self._o = libxml2mod.xmlCreateOutputBuffer(self, enc) |
else: |
else: |
file = libxml2mod.outputBufferGetPythonFile(_obj) |
file = libxml2mod.outputBufferGetPythonFile(_obj) |
if file != None: |
if file != None: |
Line 265 class xmlCore:
|
Line 293 class xmlCore:
|
ret = libxml2mod.parent(self._o) |
ret = libxml2mod.parent(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
def get_children(self): |
def get_children(self): |
ret = libxml2mod.children(self._o) |
ret = libxml2mod.children(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
def get_last(self): |
def get_last(self): |
ret = libxml2mod.last(self._o) |
ret = libxml2mod.last(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
def get_next(self): |
def get_next(self): |
ret = libxml2mod.next(self._o) |
ret = libxml2mod.next(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
def get_properties(self): |
def get_properties(self): |
ret = libxml2mod.properties(self._o) |
ret = libxml2mod.properties(self._o) |
if ret == None: |
if ret == None: |
Line 290 class xmlCore:
|
Line 318 class xmlCore:
|
ret = libxml2mod.prev(self._o) |
ret = libxml2mod.prev(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
def get_content(self): |
def get_content(self): |
return libxml2mod.xmlNodeGetContent(self._o) |
return libxml2mod.xmlNodeGetContent(self._o) |
getContent = get_content # why is this duplicate naming needed ? |
getContent = get_content # why is this duplicate naming needed ? |
Line 317 class xmlCore:
|
Line 345 class xmlCore:
|
ret = libxml2mod.parent(self._o) |
ret = libxml2mod.parent(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
elif attr == "properties": |
elif attr == "properties": |
ret = libxml2mod.properties(self._o) |
ret = libxml2mod.properties(self._o) |
if ret == None: |
if ret == None: |
Line 327 class xmlCore:
|
Line 355 class xmlCore:
|
ret = libxml2mod.children(self._o) |
ret = libxml2mod.children(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
elif attr == "last": |
elif attr == "last": |
ret = libxml2mod.last(self._o) |
ret = libxml2mod.last(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
elif attr == "next": |
elif attr == "next": |
ret = libxml2mod.next(self._o) |
ret = libxml2mod.next(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
elif attr == "prev": |
elif attr == "prev": |
ret = libxml2mod.prev(self._o) |
ret = libxml2mod.prev(self._o) |
if ret == None: |
if ret == None: |
return None |
return None |
return xmlNode(_obj=ret) | return nodeWrap(ret) |
elif attr == "content": |
elif attr == "content": |
return libxml2mod.xmlNodeGetContent(self._o) |
return libxml2mod.xmlNodeGetContent(self._o) |
elif attr == "name": |
elif attr == "name": |
Line 357 class xmlCore:
|
Line 385 class xmlCore:
|
else: |
else: |
return None |
return None |
return xmlDoc(_obj=ret) |
return xmlDoc(_obj=ret) |
raise AttributeError,attr | raise AttributeError(attr) |
else: |
else: |
parent = property(get_parent, None, None, "Parent node") |
parent = property(get_parent, None, None, "Parent node") |
children = property(get_children, None, None, "First child node") |
children = property(get_children, None, None, "First child node") |
Line 400 class xmlCore:
|
Line 428 class xmlCore:
|
prefixes=None, |
prefixes=None, |
with_comments=0): |
with_comments=0): |
if nodes: |
if nodes: |
nodes = map(lambda n: n._o, nodes) | nodes = [n._o for n in nodes] |
return libxml2mod.xmlC14NDocDumpMemory( |
return libxml2mod.xmlC14NDocDumpMemory( |
self.get_doc()._o, |
self.get_doc()._o, |
nodes, |
nodes, |
Line 414 class xmlCore:
|
Line 442 class xmlCore:
|
prefixes=None, |
prefixes=None, |
with_comments=0): |
with_comments=0): |
if nodes: |
if nodes: |
nodes = map(lambda n: n._o, nodes) | nodes = [n._o for n in nodes] |
return libxml2mod.xmlC14NDocSaveTo( |
return libxml2mod.xmlC14NDocSaveTo( |
self.get_doc()._o, |
self.get_doc()._o, |
nodes, |
nodes, |
Line 564 def nodeWrap(o):
|
Line 592 def nodeWrap(o):
|
def xpathObjectRet(o): |
def xpathObjectRet(o): |
otype = type(o) |
otype = type(o) |
if otype == type([]): |
if otype == type([]): |
ret = map(xpathObjectRet, o) | ret = list(map(xpathObjectRet, o)) |
return ret |
return ret |
elif otype == type(()): |
elif otype == type(()): |
ret = map(xpathObjectRet, o) | ret = list(map(xpathObjectRet, o)) |
return tuple(ret) |
return tuple(ret) |
elif otype == type('') or otype == type(0) or otype == type(0.0): |
elif otype == type('') or otype == type(0) or otype == type(0.0): |
return o |
return o |
Line 603 def registerErrorHandler(f, ctx):
|
Line 631 def registerErrorHandler(f, ctx):
|
"""Register a Python written function to for error reporting. |
"""Register a Python written function to for error reporting. |
The function is called back as f(ctx, error). """ |
The function is called back as f(ctx, error). """ |
import sys |
import sys |
if not sys.modules.has_key('libxslt'): | if 'libxslt' not in sys.modules: |
# normal behaviour when libxslt is not imported |
# normal behaviour when libxslt is not imported |
ret = libxml2mod.xmlRegisterErrorHandler(f,ctx) |
ret = libxml2mod.xmlRegisterErrorHandler(f,ctx) |
else: |
else: |
Line 682 class relaxNgValidCtxtCore:
|
Line 710 class relaxNgValidCtxtCore:
|
libxml2mod.xmlRelaxNGSetValidErrors(self._o, err_func, warn_func, arg) |
libxml2mod.xmlRelaxNGSetValidErrors(self._o, err_func, warn_func, arg) |
|
|
|
|
def _xmlTextReaderErrorFunc((f,arg),msg,severity,locator): | def _xmlTextReaderErrorFunc(xxx_todo_changeme,msg,severity,locator): |
"""Intermediate callback to wrap the locator""" |
"""Intermediate callback to wrap the locator""" |
|
(f,arg) = xxx_todo_changeme |
return f(arg,msg,severity,xmlTextReaderLocator(locator)) |
return f(arg,msg,severity,xmlTextReaderLocator(locator)) |
|
|
class xmlTextReaderCore: |
class xmlTextReaderCore: |
Line 719 class xmlTextReaderCore:
|
Line 748 class xmlTextReaderCore:
|
return arg |
return arg |
|
|
# |
# |
# The cleanup now goes though a wrappe in libxml.c | # The cleanup now goes though a wrapper in libxml.c |
# |
# |
def cleanupParser(): |
def cleanupParser(): |
libxml2mod.xmlPythonCleanupParser() |
libxml2mod.xmlPythonCleanupParser() |
|
|
|
# |
|
# The interface to xmlRegisterInputCallbacks. |
|
# Since this API does not allow to pass a data object along with |
|
# match/open callbacks, it is necessary to maintain a list of all |
|
# Python callbacks. |
|
# |
|
__input_callbacks = [] |
|
def registerInputCallback(func): |
|
def findOpenCallback(URI): |
|
for cb in reversed(__input_callbacks): |
|
o = cb(URI) |
|
if o is not None: |
|
return o |
|
libxml2mod.xmlRegisterInputCallback(findOpenCallback) |
|
__input_callbacks.append(func) |
|
|
|
def popInputCallbacks(): |
|
# First pop python-level callbacks, when no more available - start |
|
# popping built-in ones. |
|
if len(__input_callbacks) > 0: |
|
__input_callbacks.pop() |
|
if len(__input_callbacks) == 0: |
|
libxml2mod.xmlUnregisterInputCallback() |
|
|
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
# |
# |