~gryc-ueusp/memaker/gryc-mercilessrefactoring

« back to all changes in this revision

Viewing changes to src/motor.py

  • Committer: Gryc Ueusp
  • Date: 2008-01-07 10:21:03 UTC
  • Revision ID: gryc.ueusp@gmail.com-20080107102103-2xbj2uymzwyn2mm2
Intermediate commit that uniquifies ids but doesnt change their references thereby breaking gradients and such. Some useless comments and code are still left in there so I can use them for reference.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
            print "Debug-Mode ON"
47
47
        else:
48
48
            print "Debug-Mode OFF"
49
 
    
50
 
    def findIds(doc):
51
 
        id_attribute = None
52
 
        for e in doc.childNodes:
53
 
            if e.nodeType == e.ELEMENT_NODE and e.localName == "business":
54
 
                business_element = e
55
 
                break
56
 
        return business_element
57
 
    
58
 
    def makeIdsUnique(self, documentElement):
 
49
 
 
50
    def makeIdsUnique(self, documentElement, featuretype='unknown'):
59
51
        """
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.
 
52
        What it does:
 
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()
 
56
        
 
57
        What it should do:
 
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.
69
62
        """
70
 
        featuretype = documentElement.attributes.items()[0][1]
71
 
        documentElement = self.appendElement(documentElement, featuretype)
 
63
 
 
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
87
80
 
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'):
89
82
        """
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')
 
83
        What it does:
 
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.
 
87
        
 
88
        What it should do:
 
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 
 
92
        with "_".
 
93
        This is probably "Bad", but it's better than unilaterally changing
 
94
        ids that a user would have meticulously set by hand.
94
95
        """
 
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)
101
 
                            if self.debug:
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
105
107
 
106
108
    def clearMe(self):
185
187
            if self.debug:
186
188
                print new_entry.toprettyxml()
187
189
 
 
190
            new_entry = self.makeIdsUnique(new_entry, featureType)
 
191
 
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))
190
194
 
191
195
            while cloned.hasChildNodes():                       # appendChild() REMOVES the element it appends! 
192
196
                if self.debug:
277
281
 
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
281
286
                    else:
282
287
                        if self.debug:
283
288
                            print "feature: %s, attribute: %s, value: %s : No match. You were looking for: %s" \
313
318
        self.base = tmp
314
319
 
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'