46
46
print "Debug-Mode ON"
48
48
print "Debug-Mode OFF"
52
for e in doc.childNodes:
53
if e.nodeType == e.ELEMENT_NODE and e.localName == "business":
56
return business_element
58
def makeIdsUnique(self, documentElement):
50
def makeIdsUnique(self, documentElement, featuretype='unknown'):
60
As each new feature is added to the face, give each id a new name,
61
then, replace each reference to it in the rest of that feature with
62
its new, uniquified id.
53
Makes id attributes unique by appending the name of the feature that
54
they came from to their previous id attribute.
55
Uses helper method appendElement()
58
As each new feature is added to the face, make each id unique.
59
Then, replace each reference to the old id in the rest of the feature
60
with its new id. Without the change to references, gradients are broken.
63
61
Hopefully this wont do something really stupid I'll regret later...
64
It ignores ids that start with "_", as inkscape doesnt start its ids
65
with "_", and if you really need your ids to be the same coming out
66
as they go in, you should change them yourself and prefix them.
67
This is probably "Bad", but it's better than unilaterally changing
68
ids that a user would have meticulously set by hand.
70
featuretype = documentElement.attributes.items()[0][1]
71
documentElement = self.appendElement(documentElement, featuretype)
64
documentElement = self.appendElement(documentElement, 'id', featuretype)
72
65
#for i in documentElement.childNodes:
73
66
#if i.nodeType == i.ELEMENT_NODE and i.localName == "g": #if it's a <g> tag
74
67
#for j in i.childNodes:
85
78
#print documentElement.toxml()
86
79
return documentElement
88
def appendElement(self, documentElement, element_to_append='id', text_to_append='appended2'):
81
def appendElement(self, documentElement, attribute_to_append='id', text_to_append='appended2'):
90
Takes documentElement, iterates over it and replaces all instances
91
of items inside replacements, replacements being a list of tuples
92
like: ('find', 'replace')
84
Takes documentElement, iterates over it and appends text_to_append to
85
the value of attribute_to_append.
93
86
Returns a documentElement with all replacements replaced.
89
It ignores attribute values that start with "_", as inkscape doesnt start
90
its attributes with "_", and if you really need your ids to be the same
91
coming out as they go in, you should change them yourself and prefix them
93
This is probably "Bad", but it's better than unilaterally changing
94
ids that a user would have meticulously set by hand.
96
#print documentElement.toxml()
97
#I'd like to note that this shouldnt be 5 indentation levels deep. This is bad.
95
98
for childnode in documentElement.childNodes:
96
99
if childnode.nodeType == childnode.ELEMENT_NODE:
97
100
if childnode.attributes.items():
98
101
for attribute in childnode.attributes.items():
99
if attribute[0] == element_to_append:
100
childnode.setAttribute(element_to_append, attribute[1]+text_to_append)
102
print 'replacing '+element_to_append+' with '+text_to_append
103
childnode = self.appendElement(childnode, element_to_append, text_to_append)
102
if attribute[0] == attribute_to_append:
103
childnode.setAttribute(attribute_to_append, attribute[1]+text_to_append)
104
if self.debug: print 'replacing '+attribute_to_append+' with '+text_to_append
105
childnode = self.appendElement(childnode, attribute_to_append, text_to_append) #Mmmm Recursive Functions! :D
104
106
return documentElement
106
108
def clearMe(self):
186
188
print new_entry.toprettyxml()
190
new_entry = self.makeIdsUnique(new_entry, featureType)
188
192
cloned = new_entry.getElementsByTagName("svg")[0]
189
gtag = self.base.documentElement.appendChild(self.makeIdsUnique(self.createGTag(path, featureType)))
193
gtag = self.base.documentElement.appendChild(self.createGTag(path, featureType))
191
195
while cloned.hasChildNodes(): # appendChild() REMOVES the element it appends!
278
282
if featureType == actual_value: # if it is the correct value
279
283
if self.debug: print "I found a feature that matches your criteria. Returning it and its parentNode"
280
return self.makeIdsUnique(feature), feature.parentNode # we return that feature and its parentNode
284
#return self.makeIdsUnique(feature), feature.parentNode # we return that feature and its parentNode
285
return feature, feature.parentNode # we return that feature and its parentNode
283
288
print "feature: %s, attribute: %s, value: %s : No match. You were looking for: %s" \
315
320
if __name__ == '__main__':
316
svgobject = ObjectStack()
321
#svgobject = ObjectStack(True)
322
svgobject = ObjectStack(False)
317
323
svgobject.clearMe()
318
324
svgobject.addFeature('../themes/cocoHead/Head/headRed.svg', "head")
319
325
#svgobject.printMe()
b'\\ No newline at end of file'