chiark / gitweb /
99e5ca6aabc358d81dce5671ae29ee1c2c9d3c6d
[cura.git] / Cura / fabmetheus_utilities / xml_simple_writer.py
1 """
2 XML tag writer utilities.
3
4 """
5
6
7 from __future__ import absolute_import
8 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
9 import __init__
10
11 import cStringIO
12
13
14 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
15 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
16 __date__ = '$Date: 2008/21/04 $'
17 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
18
19
20 def addBeginEndInnerXMLTag(attributes, depth, innerText, localName, output, text=''):
21         'Add the begin and end xml tag and the inner text if any.'
22         if len( innerText ) > 0:
23                 addBeginXMLTag(attributes, depth, localName, output, text)
24                 output.write( innerText )
25                 addEndXMLTag(depth, localName, output)
26         else:
27                 addClosedXMLTag(attributes, depth, localName, output, text)
28
29 def addBeginXMLTag(attributes, depth, localName, output, text=''):
30         'Add the begin xml tag.'
31         depthStart = '\t' * depth
32         output.write('%s<%s%s>%s\n' % (depthStart, localName, getAttributesString(attributes), text))
33
34 def addClosedXMLTag(attributes, depth, localName, output, text=''):
35         'Add the closed xml tag.'
36         depthStart = '\t' * depth
37         attributesString = getAttributesString(attributes)
38         if len(text) > 0:
39                 output.write('%s<%s%s >%s</%s>\n' % (depthStart, localName, attributesString, text, localName))
40         else:
41                 output.write('%s<%s%s />\n' % (depthStart, localName, attributesString))
42
43 def addEndXMLTag(depth, localName, output):
44         'Add the end xml tag.'
45         depthStart = '\t' * depth
46         output.write('%s</%s>\n' % (depthStart, localName))
47
48 def addXMLFromLoopComplexZ(attributes, depth, loop, output, z):
49         'Add xml from loop.'
50         addBeginXMLTag(attributes, depth, 'path', output)
51         for pointComplexIndex in xrange(len(loop)):
52                 pointComplex = loop[pointComplexIndex]
53                 addXMLFromXYZ(depth + 1, pointComplexIndex, output, pointComplex.real, pointComplex.imag, z)
54         addEndXMLTag(depth, 'path', output)
55
56 def addXMLFromObjects(depth, objects, output):
57         'Add xml from objects.'
58         for object in objects:
59                 object.addXML(depth, output)
60
61 def addXMLFromVertexes(depth, output, vertexes):
62         'Add xml from loop.'
63         for vertexIndex in xrange(len(vertexes)):
64                 vertex = vertexes[vertexIndex]
65                 addXMLFromXYZ(depth + 1, vertexIndex, output, vertex.x, vertex.y, vertex.z)
66
67 def addXMLFromXYZ(depth, index, output, x, y, z):
68         'Add xml from x, y & z.'
69         attributes = {'index' : str(index)}
70         if x != 0.0:
71                 attributes['x'] = str(x)
72         if y != 0.0:
73                 attributes['y'] = str(y)
74         if z != 0.0:
75                 attributes['z'] = str(z)
76         addClosedXMLTag(attributes, depth, 'vertex', output)
77
78 def compareAttributeKeyAscending(key, otherKey):
79         'Get comparison in order to sort attribute keys in ascending order, with the id key first and name second.'
80         if key == 'id':
81                 return - 1
82         if otherKey == 'id':
83                 return 1
84         if key == 'name':
85                 return - 1
86         if otherKey == 'name':
87                 return 1
88         if key < otherKey:
89                 return - 1
90         return int(key > otherKey)
91
92 def getAttributesString(attributes):
93         'Add the closed xml tag.'
94         attributesString = ''
95         attributesKeys = attributes.keys()
96         attributesKeys.sort(compareAttributeKeyAscending)
97         for attributesKey in attributesKeys:
98                 valueString = str(attributes[attributesKey])
99                 if "'" in valueString:
100                         attributesString += ' %s="%s"' % (attributesKey, valueString)
101                 else:
102                         attributesString += " %s='%s'" % (attributesKey, valueString)
103         return attributesString
104
105 def getBeginGeometryXMLOutput(elementNode=None):
106         'Get the beginning of the string representation of this boolean geometry object info.'
107         output = getBeginXMLOutput()
108         attributes = {}
109         if elementNode != None:
110                 documentElement = elementNode.getDocumentElement()
111                 attributes = documentElement.attributes
112         addBeginXMLTag(attributes, 0, 'fabmetheus', output)
113         return output
114
115 def getBeginXMLOutput():
116         'Get the beginning of the string representation of this object info.'
117         output = cStringIO.StringIO()
118         output.write("<?xml version='1.0' ?>\n")
119         return output
120
121 def getDictionaryWithoutList(dictionary, withoutList):
122         'Get the dictionary without the keys in the list.'
123         dictionaryWithoutList = {}
124         for key in dictionary:
125                 if key not in withoutList:
126                         dictionaryWithoutList[key] = dictionary[key]
127         return dictionaryWithoutList
128
129 def getEndGeometryXMLString(output):
130         'Get the string representation of this object info.'
131         addEndXMLTag(0, 'fabmetheus', output)
132         return output.getvalue()