Skip to content

Commit

Permalink
abstraction over ElementTree API, works with both lxml and py3k's xml…
Browse files Browse the repository at this point in the history
….etree
  • Loading branch information
skrat committed Nov 1, 2011
1 parent 9573eca commit d4032b8
Show file tree
Hide file tree
Showing 24 changed files with 313 additions and 43 deletions.
28 changes: 15 additions & 13 deletions collada/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import types
import zipfile
from datetime import datetime
from lxml import etree as ElementTree

from collada import animation
from collada import asset
Expand All @@ -34,11 +33,16 @@
from collada import light
from collada import material
from collada import scene
from collada import schema
from collada.common import E, tag
from collada.common import DaeError
from collada.util import IndexedList
from collada.util import basestring, BytesIO
from collada.util import IndexedList
from collada.xmlutil import etree as ElementTree

try:
from collada import schema
except ImportError: # no lxml
schema = None


class Collada(object):
Expand Down Expand Up @@ -114,7 +118,7 @@ def __init__(self, filename=None, ignore=None, aux_file_loader=None, zip_filenam
self.scene = None
"""The default scene. This is either an instance of :class:`collada.scene.Scene` or `None`."""

if validate_output:
if validate_output and schema:
self.validator = schema.ColladaValidator()
else:
self.validator = None
Expand Down Expand Up @@ -185,13 +189,12 @@ def __init__(self, filename=None, ignore=None, aux_file_loader=None, zip_filenam
if aux_file_loader is not None:
self.getFileData = aux_file_loader

etree_parser = ElementTree.XMLParser(remove_comments=True,
remove_blank_text=True)
etree_parser = ElementTree.XMLParser()
try:
self.xmlnode = ElementTree.ElementTree(element=None,
file=BytesIO(data), parser=etree_parser)
except ElementTree.XMLSyntaxError as e:
raise DaeMalformedError("XML Syntax Parsing Error: %s" % e)
file=BytesIO(data))
except ElementTree.ParseError as e:
raise DaeMalformedError("XML Parsing Error: %s" % e)

self._loadAssetInfo()
self._loadImages()
Expand Down Expand Up @@ -456,9 +459,8 @@ def save(self):
self.assetInfo.save()
assetnode = self.xmlnode.getroot().find(tag('asset'))
if assetnode is not None:
self.xmlnode.getroot().replace(assetnode, self.assetInfo.xmlnode)
else:
self.xmlnode.getroot().insert(0, self.assetInfo.xmlnode)
self.xmlnode.getroot().remove(assetnode)
self.xmlnode.getroot().insert(0, self.assetInfo.xmlnode)

library_loc = 0
for i, node in enumerate(self.xmlnode.getroot()):
Expand Down Expand Up @@ -510,7 +512,7 @@ def write(self, fp):
self.save()
if isinstance(fp, basestring):
fp = open(fp, 'wb')
self.xmlnode.write(fp, pretty_print=True)
self.xmlnode.write(fp)

def __str__(self):
return '<Collada geometries=%d>' % (len(self.geometries))
Expand Down
2 changes: 1 addition & 1 deletion collada/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import numpy
import datetime
import dateutil.parser
from lxml import etree as ElementTree

from collada.common import DaeObject, E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.util import _correctValInNode
from collada.xmlutil import etree as ElementTree


class UP_AXIS:
Expand Down
2 changes: 1 addition & 1 deletion collada/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"""Contains objects for representing cameras"""

import numpy
from lxml import etree as ElementTree

from collada.common import DaeObject, E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError
from collada.xmlutil import etree as ElementTree


class Camera(DaeObject):
Expand Down
3 changes: 1 addition & 2 deletions collada/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from lxml import etree
from lxml.builder import ElementMaker
from collada.xmlutil import etree, ElementMaker


COLLADA_NS = 'http://www.collada.org/2005/11/COLLADASchema'
Expand Down
2 changes: 1 addition & 1 deletion collada/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
experimental. More support will be added in version 0.4.**"""

import numpy
from lxml import etree as ElementTree

from collada import source
from collada.common import DaeObject, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.geometry import Geometry
from collada.util import checkSource
from collada.xmlutil import etree as ElementTree


class Controller(DaeObject):
Expand Down
4 changes: 2 additions & 2 deletions collada/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

"""Contains objects for representing a geometry."""

from lxml import etree as ElementTree
import numpy
import types

from collada import source
from collada import triangleset
from collada import lineset
Expand All @@ -24,6 +23,7 @@
from collada.common import DaeObject, E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.xmlutil import etree as ElementTree


class Geometry(DaeObject):
Expand Down
3 changes: 2 additions & 1 deletion collada/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"""Contains objects for representing lights."""

import numpy
from lxml import etree as ElementTree

from collada.common import DaeObject, E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.util import _correctValInNode
from collada.xmlutil import etree as ElementTree


class Light(DaeObject):
Expand Down
2 changes: 1 addition & 1 deletion collada/lineset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"""Module containing classes and functions for the <lines> primitive."""

import numpy
from lxml import etree as ElementTree

from collada import primitive
from collada.util import toUnitVec, checkSource
from collada.common import E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.xmlutil import etree as ElementTree


class Line(object):
Expand Down
8 changes: 5 additions & 3 deletions collada/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

import copy
import numpy
from lxml import etree as ElementTree

from collada.common import DaeObject, E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.util import falmostEqual, StringIO
from collada.xmlutil import etree as ElementTree

try:
import Image as pil
Expand Down Expand Up @@ -642,7 +642,8 @@ def _loadShadingParam( collada, localscope, node ):
else:
return None
else:
raise DaeUnsupportedError('Unknown shading param definition ' + vnode.tag)
raise DaeUnsupportedError('Unknown shading param definition ' + \
vnode.tag)
return value

def _fixColorValues(self):
Expand Down Expand Up @@ -670,7 +671,8 @@ def save(self):
for param in self.params:
param.save()
if param.xmlnode not in profilenode.getchildren():
profilenode.insert(profilenode.index(tecnode), param.xmlnode)
profilenode.insert(list(profilenode).index(tecnode),
param.xmlnode)

deletenodes = []
for oldparam in profilenode.findall( tag('newparam') ):
Expand Down
4 changes: 2 additions & 2 deletions collada/polygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"""Module containing classes and functions for the <polygons> primitive."""

import numpy
from lxml import etree as ElementTree

from collada import primitive
from collada import polylist
from collada import triangleset
from collada.util import toUnitVec, checkSource
from collada.common import E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.util import toUnitVec, checkSource
from collada.xmlutil import etree as ElementTree


class Polygons(polylist.Polylist):
Expand Down
2 changes: 1 addition & 1 deletion collada/polylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"""Module containing classes and functions for the <polylist> primitive."""

import numpy
from lxml import etree as ElementTree

from collada import primitive
from collada import triangleset
from collada.common import E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.util import toUnitVec, checkSource, xrange
from collada.xmlutil import etree as ElementTree


class Polygon(object):
Expand Down
4 changes: 2 additions & 2 deletions collada/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

import copy
import numpy
from lxml import etree as ElementTree

from collada.util import toUnitVec
from collada.common import DaeObject, E, tag
from collada.common import DaeError, DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.util import toUnitVec
from collada.xmlutil import etree as ElementTree


class DaeInstanceNotLoadedError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion collada/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"""Module for managing data sources defined in geometry tags."""

import numpy
from lxml import etree as ElementTree

from collada.common import DaeObject, E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, DaeMalformedError
from collada.xmlutil import etree as ElementTree

class InputList(object):
"""Used for defining input sources to a geometry."""
Expand Down
5 changes: 4 additions & 1 deletion collada/tests/test_asset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import datetime
from lxml.etree import fromstring, tostring

import collada
from collada.util import unittest
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestAsset(unittest.TestCase):
Expand Down
6 changes: 4 additions & 2 deletions collada/tests/test_camera.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from lxml.etree import fromstring, tostring

import collada
from collada.common import DaeMalformedError
from collada.util import unittest
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestCamera(unittest.TestCase):
Expand Down
7 changes: 5 additions & 2 deletions collada/tests/test_collada.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import os
import numpy
import dateutil.parser
from lxml.etree import fromstring, tostring

import collada
from collada.util import unittest, BytesIO
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestCollada(unittest.TestCase):
Expand Down Expand Up @@ -268,7 +271,7 @@ def test_collada_saving(self):

loaded_mesh.save()

strdata = tostring(loaded_mesh.xmlnode)
strdata = tostring(loaded_mesh.xmlnode.getroot())
indata = BytesIO(strdata)
loaded_mesh2 = collada.Collada(indata, validate_output=True)

Expand Down
5 changes: 4 additions & 1 deletion collada/tests/test_geometry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy
from lxml.etree import fromstring, tostring

import collada
from collada.util import unittest
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestGeometry(unittest.TestCase):
Expand Down
6 changes: 4 additions & 2 deletions collada/tests/test_light.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from lxml.etree import fromstring, tostring

import collada
from collada.util import unittest
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestLight(unittest.TestCase):
Expand Down
5 changes: 4 additions & 1 deletion collada/tests/test_material.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import sys
from lxml.etree import fromstring, tostring

import collada
from collada.util import unittest
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestMaterial(unittest.TestCase):
Expand Down
5 changes: 4 additions & 1 deletion collada/tests/test_scene.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy
from lxml.etree import fromstring, tostring

import collada
from collada.util import unittest
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestScene(unittest.TestCase):
Expand Down
5 changes: 4 additions & 1 deletion collada/tests/test_source.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy
from lxml.etree import fromstring, tostring

import collada
from collada.util import unittest
from collada.xmlutil import etree

fromstring = etree.fromstring
tostring = etree.tostring


class TestSource(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion collada/triangleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"""Module containing classes and functions for the <triangles> primitive."""

import numpy
from lxml import etree as ElementTree

from collada import primitive
from collada.common import E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, \
DaeMalformedError, DaeUnsupportedError
from collada.util import toUnitVec, checkSource, normalize_v3, dot_v3, xrange
from collada.xmlutil import etree as ElementTree


class Triangle(object):
Expand Down
Loading

0 comments on commit d4032b8

Please sign in to comment.