Code

more pyxml to lxml conversion.
authoracspike <acspike@users.sourceforge.net>
Sun, 24 Jun 2007 13:17:46 +0000 (13:17 +0000)
committeracspike <acspike@users.sourceforge.net>
Sun, 24 Jun 2007 13:17:46 +0000 (13:17 +0000)
I'd like to get someone else to look at Barcode and the TeX extension.
Some of the big name extensions like pathalongpath.py still don't work but I have started converting (and somehow broke them). committing in case someone wants to help fix. :-)

15 files changed:
share/extensions/lorem_ipsum.py
share/extensions/markers_strokepaint.py
share/extensions/measure.py
share/extensions/motion.py
share/extensions/pathalongpath.py
share/extensions/pathmodifier.py
share/extensions/perspective.py
share/extensions/radiusrand.py
share/extensions/render_barcode.py
share/extensions/rtree.py
share/extensions/rubberstretch.py
share/extensions/spirograph.py
share/extensions/straightseg.py
share/extensions/summersnight.py
share/extensions/whirl.py

index d246fe0b4813a7c8bf94023a63b6f72255a7cc9d..2de7b0e60894578b2c9aa4f73b05b006af1d92d8 100644 (file)
@@ -205,37 +205,26 @@ class MyEffect(inkex.Effect):
   \r
   def addText(self, node):\r
     for i in range(self.options.num):\r
-      para=self.document.createElement('flowPara')\r
-      text=self.document.createTextNode(self.makePara())\r
-      para.appendChild(text)\r
-      node.appendChild(para)\r
-      node.appendChild(self.document.createElement('flowPara'))\r
+      para=inkex.etree.SubElement(node,inkex.addNS('flowPara','svg'))\r
+      para.text = self.makePara()\r
+      inkex.etree.SubElement(node,inkex.addNS('flowPara','svg'))\r
   \r
   def effect(self):\r
     found=0\r
     for id, node in self.selected.iteritems():\r
-      if node.tagName == 'flowRoot':\r
+      if node.tag == inkex.addNS('flowRoot','svg'):\r
         found+=1\r
         if found==1:\r
           self.addText(node)\r
     if found==0:\r
       #inkex.debug('No "flowRoot" elements selected. Unable to add text.')\r
-      svg=self.document.getElementsByTagName('svg')[0]\r
-      g=self.document.createElement('g')\r
-      g.setAttribute('inkscape:label','fill text')\r
-      g.setAttribute('inkscape:groupmode','layer')\r
-      flowRoot=self.document.createElement('flowRoot')\r
-      flowRoot.setAttribute('xml:space','preserve')\r
-      flowRegion=self.document.createElement('flowRegion')\r
-      rect=self.document.createElement('rect')\r
-      rect.setAttribute('x','0')\r
-      rect.setAttribute('y','0')\r
-      rect.setAttribute('width',svg.getAttribute('width'))\r
-      rect.setAttribute('height',svg.getAttribute('height'))\r
-      svg.appendChild(g)\r
-      g.appendChild(flowRoot)\r
-      flowRoot.appendChild(flowRegion)\r
-      flowRegion.appendChild(rect)\r
+      svg=self.document.getroot()\r
+      gattribs = {inkex.addNS('label','inkscape'):'fill text',inkex.addNS('groupmode','inkscape'):'layer'}\r
+      g=inkex.etree.SubElement(svg,inkex.addNS('g','svg'),gattribs)\r
+      flowRoot=inkex.etree.SubElement(g,inkex.addNS('flowRoot','svg'),{'xml:space':'preserve'})\r
+      flowRegion=inkex.etree.SubElement(flowRoot,inkex.addNS('flowRegion','svg'))\r
+      rattribs = {'x':'0','y':'0','width':svg.get('width'),'height':svg.get('height')}\r
+      rect=inkex.etree.SubElement(flowRegion,inkex.addNS('rect','svg'),rattribs)\r
       self.addText(flowRoot)\r
 \r
 e = MyEffect()\r
index 6d6331cdbadaffdbec872dc3f253b630a3df2e07..a6928f5e4f9f59ac0156f6659c5520e64bfbcb0d 100644 (file)
@@ -27,15 +27,14 @@ class MyEffect(inkex.Effect):
                         help="do not create a copy, modify the markers")\r
         \r
     def effect(self):\r
-        defs = self.xpathSingle('/svg//defs')\r
+        defs = self.xpathSingle('/svg:svg//svg:defs')\r
         if not defs:\r
-            defs = self.document.createElement('svg:defs')\r
-            self.document.documentElement.appendChile(defs)\r
+            defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))\r
         \r
         for id, node in self.selected.iteritems():\r
             mprops = ['marker','marker-start','marker-mid','marker-end']\r
             try:\r
-                style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
+                style = simplestyle.parseStyle(node.get('style'))\r
             except:\r
                 inkex.debug("No style attribute found for id: %s" % id)\r
                 continue\r
@@ -46,9 +45,9 @@ class MyEffect(inkex.Effect):
                 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':\r
                     marker_id = style[mprop][5:-1]\r
                     try:\r
-                        old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
+                        old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id)\r
                         if not self.options.modify:\r
-                            mnode = old_mnode.cloneNode(True)\r
+                            mnode = inkex.etree.fromstring(inkex.etree.tostring(old_mnode))\r
                         else:\r
                             mnode = old_mnode\r
                     except:\r
@@ -58,18 +57,18 @@ class MyEffect(inkex.Effect):
                     new_id = self.uniqueId(marker_id, not self.options.modify)\r
                     \r
                     style[mprop] = "url(#%s)" % new_id\r
-                    mnode.attributes.getNamedItem('id').value = new_id\r
-                    mnode.attributes.getNamedItemNS(inkex.NSS['inkscape'],'stockid').value = new_id\r
-                    defs.appendChild(mnode)\r
+                    mnode.set('id', new_id)\r
+                    mnode.set(inkex.addNS('stockid','inkscape'), new_id)\r
+                    defs.append(mnode)\r
                     \r
-                    children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)\r
+                    children = self.document.getroot().xpath('/svg:svg//svg:marker[@id="%s"]//*[@style]' % new_id,inkex.NSS)\r
                     for child in children:\r
-                        cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value)\r
+                        cstyle = simplestyle.parseStyle(child.get('style'))\r
                         if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:\r
                                 cstyle['stroke'] = stroke\r
                         if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:\r
                                 cstyle['fill'] = stroke\r
-                        child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle)\r
-            node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style)\r
+                        child.set('style',simplestyle.formatStyle(cstyle))\r
+            node.set('style',simplestyle.formatStyle(style))\r
 e = MyEffect()\r
 e.affect()\r
index 4a4435ceb105416856824a550a6a2fbd61e70c2a..b37303f1b28cf972330f05285d45c183c0c201cf 100644 (file)
@@ -110,21 +110,18 @@ class Length(inkex.Effect):
     def effect(self):
         # get number of digits
         prec = int(self.options.precision)
-        # loop over all selected pathes
+        # loop over all selected paths
         for id, node in self.selected.iteritems():
-            if node.tagName == 'path':
-                # self.group = self.document.createElement('svg:g')
-                self.group = self.document.createElement('svg:text')
-                node.parentNode.appendChild(self.group)
+            if node.tag == inkex.addNS('path','svg'):
+                self.group = inkex.etree.SubElement(node.getparent(),inkex.addNS('text','svg'))
                 
-                try:
-                    t = node.attributes.getNamedItem('transform').value
-                    self.group.setAttribute('transform', t)
-                except AttributeError:
-                    pass
+                t = node.get('transform')\r
+                if t:
+                    self.group.set('transform', t)
+
 
                 a =[]
-                p = cubicsuperpath.parsePath(node.attributes.getNamedItem('d').value)
+                p = cubicsuperpath.parsePath(node.get('d'))
                 num = 1
                 slengths, stotal = csplength(p)
                 ''' Wio: Umrechnung in unit '''
@@ -155,22 +152,20 @@ class Length(inkex.Effect):
 
 
     def addTextOnPath(self,node,x,y,text, id,dy=0):
-                #new = self.document.createElement('svg:text')
-                new = self.document.createElement('svg:textPath')
+                new = inkex.etree.SubElement(node,inkex.addNS('textPath','svg'))
                 s = {'text-align': 'center', 'vertical-align': 'bottom',
                     'text-anchor': 'middle', 'font-size': str(self.options.fontsize),
                     'fill-opacity': '1.0', 'stroke': 'none',
                     'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'}
-                new.setAttribute('style', simplestyle.formatStyle(s))
-                node.setAttribute('x', str(x))
-                node.setAttribute('y', str(y))
-                #node.setAttribute('transform','rotate(180,'+str(-x)+','+str(-y)+')')
-                new.setAttributeNS('http://www.w3.org/1999/xlink','xlink:href', '#'+id)
-                new.setAttribute('startOffset', "50%")
-                new.setAttribute('dy', str(dy)) # dubious merit
-                #new.appendChild(tp)
-                new.appendChild(self.document.createTextNode(str(text)))
-                node.appendChild(new)
+                new.set('style', simplestyle.formatStyle(s))
+                new.set(inkex.addNS('href','xlink'), '#'+id)
+                new.set('startOffset', "50%")
+                new.set('dy', str(dy)) # dubious merit
+                #new.append(tp)
+                new.text = str(text)\r
+                #node.set('transform','rotate(180,'+str(-x)+','+str(-y)+')')\r
+                node.set('x', str(x))
+                node.set('y', str(y))
 
 e = Length()
 e.affect()
index 3834c7f2b74c8563ae8b8f7556a4020351392a9d..f9533cd504ffbcbfe44b57cb6a5fbd27587560de 100755 (executable)
@@ -53,33 +53,26 @@ class Motion(inkex.Effect):
         a.append([cmd,np[:]])
             
         a.append(['Z',[]])
-        face = self.document.createElement('svg:path')
-        self.facegroup.appendChild(face)
-        face.setAttribute('d', simplepath.formatPath(a))
-        
+        face = inkex.etree.SubElement(self.facegroup,inkex.addNS('path','svg'),{'d':simplepath.formatPath(a)})
         
     def effect(self):
         self.vx = math.cos(math.radians(self.options.angle))*self.options.magnitude
         self.vy = math.sin(math.radians(self.options.angle))*self.options.magnitude
         for id, node in self.selected.iteritems():
-            if node.tagName == 'path':
-                group = self.document.createElement('svg:g')
-                self.facegroup = self.document.createElement('svg:g')
-                node.parentNode.appendChild(group)
-                group.appendChild(self.facegroup)
-                group.appendChild(node)
+            if node.tag == inkex.addNS('path','svg'):
+                group = inkex.etree.SubElement(node.getparent(),inkex.addNS('g','svg'))
+                self.facegroup = inkex.etree.SubElement(group, inkex.addNS('g','svg'))
+                group.append(node)
                 
-                try:
-                    t = node.attributes.getNamedItem('transform').value
-                    group.setAttribute('transform', t)
-                    node.attributes.getNamedItem('transform').value=""
-                except AttributeError:
-                    pass
-
-                s = node.attributes.getNamedItem('style').value
-                self.facegroup.setAttribute('style', s)
+                t = node.get('transform')
+                if t:
+                    group.set('transform', t)
+                    node.set('transform','')
+                    
+                s = node.get('style')
+                self.facegroup.set('style', s)
 
-                p = simplepath.parsePath(node.attributes.getNamedItem('d').value)
+                p = simplepath.parsePath(node.get('d'))
                 for cmd,params in p:
                     tees = []
                     if cmd == 'C':
index af42a2dee48e47cda2b564b2acf11a64b025f76e..3901069c9089976c00f7c429ee1fe54235758db8 100644 (file)
@@ -34,7 +34,7 @@ they move and rotate, deforming the pattern.
 import inkex, cubicsuperpath, bezmisc\r
 import pathmodifier \r
 \r
-import copy, math, re, random, xml.xpath\r
+import copy, math, re, random\r
 \r
 def flipxy(path):\r
     for pathcomp in path:\r
@@ -212,15 +212,15 @@ class PathAlongPath(pathmodifier.Diffeo):
         dx=width+self.options.space\r
 \r
         for id, node in self.patterns.iteritems():\r
-            if node.tagName == 'path':\r
-                d = node.attributes.getNamedItem('d')\r
-                p0 = cubicsuperpath.parsePath(d.value)\r
+            if node.tag == inkex.addNS('path','svg'):\r
+                d = node.get('d')\r
+                p0 = cubicsuperpath.parsePath(d)\r
                 if self.options.vertical:\r
                     flipxy(p0)\r
 \r
                 newp=[]\r
                 for skelnode in self.skeletons.itervalues(): \r
-                    self.curSekeleton=cubicsuperpath.parsePath(skelnode.getAttribute('d'))\r
+                    self.curSekeleton=cubicsuperpath.parsePath(skelnode.get('d'))\r
                     if self.options.vertical:\r
                         flipxy(self.curSekeleton)\r
                     for comp in self.curSekeleton:\r
@@ -261,7 +261,7 @@ class PathAlongPath(pathmodifier.Diffeo):
                             flipxy(p)\r
                         newp+=p\r
 \r
-                d.value = cubicsuperpath.formatPath(newp)\r
+                node.set('d', cubicsuperpath.formatPath(newp))\r
 \r
 e = PathAlongPath()\r
 e.affect()\r
index a27c547c19789259613da30867341d405690e049..f10a86b494d251e77304c253de5580d6c16e201f 100644 (file)
@@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 barraud@math.univ-lille1.fr\r
 '''\r
 import inkex, cubicsuperpath, bezmisc, simplestyle\r
-import copy, math, re, random, xml.xpath\r
+import copy, math, re, random\r
 \r
 def parseTransform(transf,mat=[[1.0,0.0,0.0],[0.0,1.0,0.0]]):\r
     if transf=="":\r
@@ -84,9 +84,9 @@ def composeTransform(M1,M2):
     return [[a11,a12,v1],[a21,a22,v2]]\r
 \r
 def applyTransformToNode(mat,node):\r
-    m=parseTransform(node.getAttributeNS(None,"transform"))\r
+    m=parseTransform(node.get("transform"))\r
     newtransf=formatTransform(composeTransform(mat,m))\r
-    node.setAttributeNS(None,"transform", newtransf)\r
+    node.set("transform", newtransf)\r
 \r
 def applyTransformToPoint(mat,pt):\r
     x=mat[0][0]*pt[0]+mat[0][1]*pt[1]+mat[0][2]\r
@@ -95,15 +95,18 @@ def applyTransformToPoint(mat,pt):
     pt[1]=y\r
 \r
 def fuseTransform(node):\r
-    m=parseTransform(node.getAttributeNS(None,"transform"))\r
-    d = node.getAttributeNS(None,'d')\r
+    t = node.get("transform")\r
+    if t == None:\r
+        return\r
+    m=parseTransform(t)\r
+    d = node.get('d')\r
     p=cubicsuperpath.parsePath(d)\r
     for comp in p:\r
         for ctl in comp:\r
             for pt in ctl:\r
                 applyTransformToPoint(m,pt)\r
-    node.setAttributeNS(None,'d', cubicsuperpath.formatPath(p))\r
-    node.removeAttributeNS(None,"transform")\r
+    node.set('d', cubicsuperpath.formatPath(p))\r
+    del node.attribs["transform"]\r
 \r
 \r
 def boxunion(b1,b2):\r
@@ -134,83 +137,83 @@ class PathModifier(inkex.Effect):
 #-- Selectionlists manipulation --\r
 ##################################\r
     def computeBBox(self, aList):\r
-       bbox=None\r
-       for id, node in aList.iteritems():\r
-           if node.tagName == 'path':\r
-               d = node.attributes.getNamedItem('d')\r
-               p = cubicsuperpath.parsePath(d.value)\r
-               bbox=boxunion(roughBBox(p),bbox)\r
-       return bbox\r
+        bbox=None\r
+        for id, node in aList.iteritems():\r
+            if node.tag == inkex.addNS('path','svg') or node.tag == 'path':\r
+                d = node.get('d')\r
+                p = cubicsuperpath.parsePath(d)\r
+                bbox=boxunion(roughBBox(p),bbox)\r
+        return bbox\r
 \r
     def duplicateNodes(self, aList):\r
         clones={}\r
         for id,node in aList.iteritems():\r
-            clone=node.cloneNode(True)\r
-    #!!!--> should it be given an id?\r
-    #seems to work without this!?!\r
-            clone.setAttributeNS(None,"id", self.uniqueId(node.tagName))\r
-            node.parentNode.appendChild(clone)\r
-            clones[clone.getAttributeNS(None,"id")]=clone\r
+            clone=inkex.etree.fromstring(inkex.etree.tostring(node))\r
+            #!!!--> should it be given an id?\r
+            #seems to work without this!?!\r
+            clone.set("id", self.uniqueId(node.tag))\r
+            node.getparent().append(clone)\r
+            clones[clone.get("id")]=clone\r
         return(clones)\r
 \r
     def uniqueId(self, prefix):\r
         id="%s%04i"%(prefix,random.randint(0,9999))\r
-        while len(xml.xpath.Evaluate('//*[@id="%s"]' % id,self.document)):\r
+        while len(self.document.getroot().xpath('//*[@id="%s"]' % id,inkex.NSS)):\r
             id="%s%04i"%(prefix,random.randint(0,9999))\r
         return(id)\r
 \r
     def expandGroups(self,aList,transferTransform=True):\r
         for id, node in aList.items():      \r
-            if node.tagName == 'g':\r
-                mat=parseTransform(node.getAttributeNS(None,"transform"))\r
-                for child in node.childNodes:\r
-                    if child.nodeType==child.ELEMENT_NODE:\r
-                        if transferTransform:\r
-                            applyTransformToNode(mat,child)\r
-                        aList.update(self.expandGroups({child.getAttribute('id'):child}))\r
+            if node.tag == inkex.addNS('g','svg'):\r
+                mat=parseTransform(node.get("transform"))\r
+                for child in node:\r
+                    if transferTransform:\r
+                        applyTransformToNode(mat,child)\r
+                    aList.update(self.expandGroups({child.get('id'):child}))\r
                 if transferTransform:\r
-                    node.removeAttribute("transform")\r
+                    del node.attribs["transform"]\r
                 del aList[id]\r
-       return(aList)\r
+        return(aList)\r
 \r
     def expandGroupsUnlinkClones(self,aList,transferTransform=True,doReplace=True):\r
         for id in aList.keys()[:]:     \r
             node=aList[id]\r
-            if node.tagName == 'g':\r
+            if node.tag == inkex.addNS('g','svg'):\r
                 self.expandGroups(aList,transferTransform)\r
                 self.expandGroupsUnlinkClones(aList,transferTransform,doReplace)\r
-               #Hum... not very efficient if there are many clones of groups...\r
-            elif node.tagName == 'use':\r
-                refid=node.getAttributeNS(inkex.NSS[u'xlink'],'href')\r
+                #Hum... not very efficient if there are many clones of groups...\r
+            elif node.tag == inkex.addNS('use','svg'):\r
+                refid=node.get(inkex.addNS('href','xlink'))\r
                 path = '//*[@id="%s"]' % refid[1:]\r
-                refnode = xml.xpath.Evaluate(path,self.document)[0]\r
-                newnode=refnode.cloneNode(True)\r
-               self.recursNewIds(newnode)\r
-\r
-               if node.hasAttributeNS(None,u'style'):\r
-                   style=simplestyle.parseStyle(node.getAttributeNS(None,u'style'))\r
-                   refstyle=simplestyle.parseStyle(refnode.getAttributeNS(None,u'style'))\r
-                   style.update(refstyle)\r
-                   newnode.setAttributeNS(None,'style',simplestyle.formatStyle(style))\r
-                applyTransformToNode(parseTransform(node.getAttributeNS(None,'transform')),newnode)\r
-                if doReplace:\r
-                    parent=node.parentNode\r
-                    parent.insertBefore(newnode,node)\r
-                    parent.removeChild(node)\r
-                del aList[id]\r
-               newid=newnode.getAttributeNS(None,'id')\r
+                refnode = self.document.getroot().xpath(path,inkex.NSS)\r
+                newnode=inkex.etree.fromstring(inkex.etree.tostring(refnode))\r
+                self.recursNewIds(newnode)\r
+\r
+                s = node.get('style')\r
+                if s:\r
+                    style=simplestyle.parseStyle(s)\r
+                    refstyle=simplestyle.parseStyle(refnode.get('style'))\r
+                    style.update(refstyle)\r
+                    newnode.set('style',simplestyle.formatStyle(style))\r
+                    applyTransformToNode(parseTransform(node.get('transform')),newnode)\r
+                    if doReplace:\r
+                        parent=node.getparent()\r
+                        parent.insert(node.index,newnode)\r
+                        parent.remove(node)\r
+                    del aList[id]\r
+                newid=newnode.get('id')\r
                 aList.update(self.expandGroupsUnlinkClones({newid:newnode},transferTransform,doReplace))\r
         return aList\r
     \r
     def recursNewIds(self,node):\r
-       if node.nodeType==node.ELEMENT_NODE and node.hasAttributeNS(None,u'id'):\r
-           node.setAttributeNS(None,u'id',self.uniqueId(node.tagName))\r
-       for child in node.childNodes:\r
-           self.recursNewIds(child)\r
-               \r
+        if node.get('id'):\r
+            node.set('id',self.uniqueId(node.tag))\r
+        for child in node:\r
+            self.recursNewIds(child)\r
+            \r
 \r
        \r
-\r
+# Had not been rewritten for ElementTree\r
 #     def makeClonesReal(self,aList,doReplace=True,recursivelytransferTransform=True):\r
 #         for id in aList.keys():     \r
 #             node=aList[id]\r
@@ -246,17 +249,18 @@ class PathModifier(inkex.Effect):
 ################################\r
 \r
     def rectToPath(self,node,doReplace=True):\r
-        if node.tagName == 'rect':\r
-            x =float(node.getAttributeNS(None,u'x'))\r
-            y =float(node.getAttributeNS(None,u'y'))\r
+        if node.tag == inkex.addNS('rect','svg'):\r
+            x =float(node.get('x'))\r
+            y =float(node.get('y'))\r
+            #FIXME: no exception anymore and sometimes just one\r
             try:\r
-                rx=float(node.getAttributeNS(None,u'rx'))\r
-                ry=float(node.getAttributeNS(None,u'ry'))\r
+                rx=float(node.get('rx'))\r
+                ry=float(node.get('ry'))\r
             except:\r
                 rx=0\r
                 ry=0\r
-            w =float(node.getAttributeNS(None,u'width' ))\r
-            h =float(node.getAttributeNS(None,u'height'))\r
+            w =float(node.get('width' ))\r
+            h =float(node.get('height'))\r
             d ='M %f,%f '%(x+rx,y)\r
             d+='L %f,%f '%(x+w-rx,y)\r
             d+='A %f,%f,%i,%i,%i,%f,%f '%(rx,ry,0,0,1,x+w,y+ry)\r
@@ -267,33 +271,39 @@ class PathModifier(inkex.Effect):
             d+='L %f,%f '%(x,y+ry)\r
             d+='A %f,%f,%i,%i,%i,%f,%f '%(rx,ry,0,0,1,x+rx,y)\r
 \r
-            newnode=self.document.createElement('path')\r
-            newnode.setAttributeNS(None,'d',d)\r
-            newnode.setAttributeNS(None,'id', self.uniqueId('path'))\r
-            newnode.setAttributeNS(None,'style',node.getAttributeNS(None,u'style'))\r
-            newnode.setAttributeNS(None,'transform',node.getAttributeNS(None,u'transform'))\r
-            fuseTransform(newnode)\r
+            newnode=inkex.etree.Element('path')\r
+            newnode.set('d',d)\r
+            newnode.set('id', self.uniqueId('path'))\r
+            newnode.set('style',node.get('style'))\r
+            nnt = node.get('transform')\r
+            if nnt:\r
+                newnode.set('transform',nnt)\r
+                fuseTransform(newnode)\r
             if doReplace:\r
-                parent=node.parentNode\r
-                parent.insertBefore(newnode,node)\r
-                parent.removeChild(node)\r
+                parent=node.getparent()\r
+                parent.insert(node.index,newnode)\r
+                parent.remove(node)\r
             return newnode\r
 \r
     def objectToPath(self,node,doReplace=True):\r
-    #--TODO: support other object types!!!!\r
-    #--TODO: make sure cubicsuperpath supports A and Q commands... \r
-        if node.tagName == 'rect':\r
-           return(self.rectToPath(node,doReplace))\r
-        elif node.tagName == 'path':\r
-            attributes = node.attributes.keys()\r
-            for uri,attName in attributes:\r
-               if uri in [inkex.NSS[u'sodipodi'],inkex.NSS[u'inkscape']]:\r
-#                if attName not in ["d","id","style","transform"]:\r
-                    node.removeAttributeNS(uri,attName)\r
+        #--TODO: support other object types!!!!\r
+        #--TODO: make sure cubicsuperpath supports A and Q commands... \r
+        if node.tag == inkex.addNS('rect','svg'):\r
+            return(self.rectToPath(node,doReplace))\r
+        elif node.tag == inkex.addNS('path','svg') or node.tag == 'path':\r
+            attributes = node.keys()\r
+            for attName in attributes:\r
+                uri = None\r
+                if attName[0] == '{':\r
+                    uri,attName = attName.split('}')\r
+                    uri = uri[1:]\r
+                if uri in [inkex.NSS[u'sodipodi'],inkex.NSS[u'inkscape']]:\r
+                    #if attName not in ["d","id","style","transform"]:\r
+                    del node.attribs[inkex.addNS(attName,uri)]\r
             fuseTransform(node)\r
             return node\r
         else:\r
-            inkex.debug("Please first convert objects to paths!...(got '%s')"%node.tagName)\r
+            inkex.debug("Please first convert objects to paths!...(got '%s')"%node.tag)\r
             return None\r
 \r
     def objectsToPaths(self,aList,doReplace=True):\r
@@ -301,27 +311,27 @@ class PathModifier(inkex.Effect):
         for id,node in aList.items():\r
             newnode=self.objectToPath(node,self.document)\r
             del aList[id]\r
-           aList[newnode.getAttributeNS(None,u'id')]=newnode\r
+            aList[newnode.get('id')]=newnode\r
 \r
 \r
 ################################\r
 #-- Action ----------\r
 ################################\r
-           \r
+        \r
     #-- overwrite this method in subclasses...\r
     def effect(self):\r
         #self.duplicateNodes(self.selected)\r
         self.expandGroupsUnlinkClones(self.selected, True)\r
-       self.objectsToPaths(self.selected, True)\r
+        self.objectsToPaths(self.selected, True)\r
         self.bbox=self.computeBBox(self.selected)\r
         for id, node in self.selected.iteritems():\r
-            if node.tagName == 'path':\r
-                d = node.attributes.getNamedItem('d')\r
-                p = cubicsuperpath.parsePath(d.value)\r
+            if node.tag == inkex.addNS('path','svg'):\r
+                d = node.get('d')\r
+                p = cubicsuperpath.parsePath(d)\r
 \r
                #do what ever you want with p!\r
 \r
-               d.value = cubicsuperpath.formatPath(p)\r
+               node.set('d',cubicsuperpath.formatPath(p))\r
 \r
 \r
 class Diffeo(PathModifier):\r
@@ -355,20 +365,20 @@ class Diffeo(PathModifier):
 \r
     def effect(self):\r
         #self.duplicateNodes(self.selected)\r
-       self.expandGroupsUnlinkClones(self.selected, True)\r
+        self.expandGroupsUnlinkClones(self.selected, True)\r
         self.expandGroups(self.selected, True)\r
-       self.objectsToPaths(self.selected, True)\r
+        self.objectsToPaths(self.selected, True)\r
         self.bbox=self.computeBBox(self.selected)\r
         for id, node in self.selected.iteritems():\r
-            if node.tagName == 'path':\r
-                d = node.attributes.getNamedItem('d')\r
-                p = cubicsuperpath.parsePath(d.value)\r
+            if node.tag == inkex.addNS('path','svg'):\r
+                d = node.get('d')\r
+                p = cubicsuperpath.parsePath(d)\r
 \r
                 for sub in p:\r
                     for ctlpt in sub:\r
                         self.applyDiffeo(ctlpt[1],(ctlpt[0],ctlpt[2]))\r
 \r
-               d.value = cubicsuperpath.formatPath(p)\r
+                node.set('d',cubicsuperpath.formatPath(p))\r
 \r
 #e = Diffeo()\r
 #e.affect()\r
index 014a7b779c4807ab8426dfebfc686aa03db98a2a..84bd9c768e835f0c7cd1a0b9163562a448980679 100755 (executable)
@@ -56,8 +56,8 @@ class Project(inkex.Effect):
         #obj is selected second
         obj = self.selected[self.options.ids[0]]
         envelope = self.selected[self.options.ids[1]]
-        if (obj.tagName == 'path' or obj.tagName == 'g') and envelope.tagName == 'path':
-            path = cubicsuperpath.parsePath(envelope.attributes.getNamedItem('d').value)
+        if (obj.tag == inkex.addNS('path','svg') or obj.tag == inkex.addNS('g','svg')) and envelope.tag == inkex.addNS('path','svg'):
+            path = cubicsuperpath.parsePath(envelope.get('d'))
             dp = zeros((4,2), dtype=float64)
             for i in range(4):
                 dp[i][0] = path[0][i][1][0]
@@ -71,7 +71,7 @@ class Project(inkex.Effect):
                 _,f,err = os.popen3("inkscape --query-%s --query-id=%s %s" % (query,id,file))
                 q[query] = float(f.read())
                 f.close()
-               err.close()
+                err.close()
             sp = array([[q['x'], q['y']+q['height']],[q['x'], q['y']],[q['x']+q['width'], q['y']],[q['x']+q['width'], q['y']+q['height']]], dtype=float64)
 
         solmatrix = zeros((8,8), dtype=float64)
@@ -92,30 +92,29 @@ class Project(inkex.Effect):
 
         res = solve(solmatrix, free_term)
         projmatrix = array([[res[0],res[1],res[2]],[res[3],res[4],res[5]],[res[6],res[7],1.0]],dtype=float64)
-        if obj.tagName == "path":
+        if obj.tag == inkex.addNS("path",'svg'):
             self.process_path(obj,projmatrix)
-        if obj.tagName == "g":
+        if obj.tag == inkex.addNS("g",'svg'):
             self.process_group(obj,projmatrix)
 
 
     def process_group(self,group,m):
-        for node in group.childNodes:
-            if node.nodeType==node.ELEMENT_NODE:
-                if node.tagName == 'path':
-                     self.process_path(node,m)
-                if node.tagName == 'g':
-                     self.process_group(node,m)    
+        for node in group:
+            if node.tag == inkex.addNS('path','svg'):
+                self.process_path(node,m)
+            if node.tagName == inkex.addNS('g','svg'):
+                self.process_group(node,m)    
 
 
     def process_path(self,path,m):
-        d = path.attributes.getNamedItem('d')
-        p = cubicsuperpath.parsePath(d.value)
+        d = path.get('d')
+        p = cubicsuperpath.parsePath(d)
         for subs in p:
             for csp in subs:
                 csp[0] = self.project_point(csp[0],m)
                 csp[1] = self.project_point(csp[1],m)
                 csp[2] = self.project_point(csp[2],m)
-        d.value = cubicsuperpath.formatPath(p)
+        path.set('d',cubicsuperpath.formatPath(p))
 
 
 
index 0dcba40cf20eb9982d43ab3db719227587b00d31..5eb73965b2ef87b39b5308bf3167a26e688486bd 100755 (executable)
@@ -50,9 +50,9 @@ class RadiusRandomize(inkex.Effect):
                         help="Use normal distribution")
     def effect(self):
         for id, node in self.selected.iteritems():
-            if node.tagName == 'path':
-                d = node.attributes.getNamedItem('d')
-                p = cubicsuperpath.parsePath(d.value)
+            if node.tag == inkex.addNS('path','svg'):
+                d = node.get('d')
+                p = cubicsuperpath.parsePath(d)
                 for subpath in p:
                     for csp in subpath:
                         if self.options.end:
@@ -66,7 +66,7 @@ class RadiusRandomize(inkex.Effect):
                         if self.options.ctrl:
                             csp[0]=randomize(csp[0], self.options.radius, self.options.norm)
                             csp[2]=randomize(csp[2], self.options.radius, self.options.norm)
-                d.value = cubicsuperpath.formatPath(p)
+                node.set('d',cubicsuperpath.formatPath(p))
 
 e = RadiusRandomize()
 e.affect()
index c35507a95941773370f9cb8e74391910cb77c89e..590e55b86c519b385f89f6edc29c108459f361a0 100644 (file)
@@ -48,7 +48,7 @@ class InsertBarcode(inkex.Effect):
                if object:
                        barcode = object.generate()
                        if barcode:
-                               self.current_layer.appendChild(barcode)
+                               self.current_layer.append(barcode)
                        else:
                                sys.stderr.write("No barcode was generated\n")
                else:
index 5d8ac9f3d6372f55ac8ff8a43cb00ae042297432..a703565574e8af235a1e5625a673ce8781707b24 100755 (executable)
@@ -44,19 +44,18 @@ class RTreeTurtle(inkex.Effect):
                         dest="minimum", default=4.0,
                         help="minimum branch size")
     def effect(self):
-        new = self.document.createElement('svg:path')
         s = {'stroke-linejoin': 'miter', 'stroke-width': '1.0px', 
             'stroke-opacity': '1.0', 'fill-opacity': '1.0', 
             'stroke': '#000000', 'stroke-linecap': 'butt', 
             'fill': 'none'}
-        new.setAttribute('style', simplestyle.formatStyle(s))
         t = pturtle.pTurtle()
         t.pu()
         t.setpos(self.view_center)
         t.pd()
         rtree(t, self.options.size, self.options.minimum)
-        new.setAttribute('d', t.getPath())
-        self.current_layer.appendChild(new)
+        
+        attribs = {'d':t.getPath(),'style':simplestyle.formatStyle(s)}
+        inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), attribs)
 
 e = RTreeTurtle()
 e.affect()
index ae52d1c844225c610a5cd01abedc70b1633403c8..1ea533cf0c8dd7fb63c0796a18d890f49b807101 100644 (file)
@@ -33,45 +33,45 @@ class RubberStretch(pathmodifier.Diffeo):
                         dest="curve", default=0.5)\r
 \r
     def applyDiffeo(self,bpt,vects=()):\r
-       for v in vects:\r
-           v[0]-=bpt[0]\r
-           v[1]-=bpt[1]\r
-           v[1]*=-1\r
-       bpt[1]*=-1\r
-       a=self.options.ratio/100\r
-       b=min(self.options.curve/100,0.99)\r
-       x0= (self.bbox[0]+self.bbox[1])/2\r
-       y0=-(self.bbox[2]+self.bbox[3])/2\r
-       w,h=(self.bbox[1]-self.bbox[0])/2,(self.bbox[3]-self.bbox[2])/2\r
-       \r
-       x,y=(bpt[0]-x0),(bpt[1]-y0)\r
-       sx=(1+b*(x/w+1)*(x/w-1))*2**(-a)\r
-       sy=(1+b*(y/h+1)*(y/h-1))*2**(-a)\r
-       bpt[0]=x0+x*sy\r
-       bpt[1]=y0+y/sx\r
-       for v in vects:\r
-           dx,dy=v\r
-           dXdx=sy\r
-           dXdy= x*2*b*y/h/h*2**(-a)\r
-           dYdx=-y*2*b*x/w/w*2**(-a)/sx/sx\r
-           dYdy=1/sx\r
-           v[0]=dXdx*dx+dXdy*dy\r
-           v[1]=dYdx*dx+dYdy*dy\r
-\r
-       #--spherify\r
-#      s=((x*x+y*y)/(w*w+h*h))**(-a/2)\r
-#      bpt[0]=x0+s*x\r
-#      bpt[1]=y0+s*y\r
-#      for v in vects:\r
-#          dx,dy=v\r
-#          v[0]=(1-a/2/(x*x+y*y)*2*x*x)*s*dx+( -a/2/(x*x+y*y)*2*y*x)*s*dy\r
-#          v[1]=( -a/2/(x*x+y*y)*2*x*y)*s*dx+(1-a/2/(x*x+y*y)*2*y*y)*s*dy\r
-\r
-       for v in vects:\r
-           v[0]+=bpt[0]\r
-           v[1]+=bpt[1]\r
-           v[1]*=-1\r
-       bpt[1]*=-1\r
+        for v in vects:\r
+            v[0]-=bpt[0]\r
+            v[1]-=bpt[1]\r
+            v[1]*=-1\r
+        bpt[1]*=-1\r
+        a=self.options.ratio/100\r
+        b=min(self.options.curve/100,0.99)\r
+        x0= (self.bbox[0]+self.bbox[1])/2\r
+        y0=-(self.bbox[2]+self.bbox[3])/2\r
+        w,h=(self.bbox[1]-self.bbox[0])/2,(self.bbox[3]-self.bbox[2])/2\r
+        \r
+        x,y=(bpt[0]-x0),(bpt[1]-y0)\r
+            sx=(1+b*(x/w+1)*(x/w-1))*2**(-a)\r
+        sy=(1+b*(y/h+1)*(y/h-1))*2**(-a)\r
+        bpt[0]=x0+x*sy\r
+        bpt[1]=y0+y/sx\r
+        for v in vects:\r
+            dx,dy=v\r
+            dXdx=sy\r
+            dXdy= x*2*b*y/h/h*2**(-a)\r
+            dYdx=-y*2*b*x/w/w*2**(-a)/sx/sx\r
+            dYdy=1/sx\r
+            v[0]=dXdx*dx+dXdy*dy\r
+            v[1]=dYdx*dx+dYdy*dy\r
+    \r
+        #--spherify\r
+        #s=((x*x+y*y)/(w*w+h*h))**(-a/2)\r
+        #bpt[0]=x0+s*x\r
+        #bpt[1]=y0+s*y\r
+        #for v in vects:\r
+        #    dx,dy=v\r
+        #    v[0]=(1-a/2/(x*x+y*y)*2*x*x)*s*dx+( -a/2/(x*x+y*y)*2*y*x)*s*dy\r
+        #    v[1]=( -a/2/(x*x+y*y)*2*x*y)*s*dx+(1-a/2/(x*x+y*y)*2*y*y)*s*dy\r
+    \r
+        for v in vects:\r
+            v[0]+=bpt[0]\r
+            v[1]+=bpt[1]\r
+            v[1]*=-1\r
+        bpt[1]*=-1\r
 \r
 e = RubberStretch()\r
 e.affect()\r
index 82b83a31b0f003f99e4f583bc908b1054b2e508d..8ed297849707771ea3d6a014cc726e64e29e4e50 100644 (file)
@@ -66,9 +66,9 @@ class Spirograph(inkex.Effect):
         
         rotation = - math.pi * self.options.rotation / 180;
         
-        new = self.document.createElement('svg:path')
+        new = inkex.etree.Element(inkex.addNS('path','svg'))
         s = { 'stroke': '#000000', 'fill': 'none' }
-        new.setAttribute('style', simplestyle.formatStyle(s))
+        new.set('style', simplestyle.formatStyle(s))
         
         pathString = ''
         maxPointCount = 1000
@@ -104,8 +104,8 @@ class Spirograph(inkex.Effect):
                         pathString += ' C ' + str(x + dx) + ',' + str(y + dy) + ' '
                     
         
-        new.setAttribute('d', pathString)
-        self.current_layer.appendChild(new)
+        new.set('d', pathString)
+        self.current_layer.append(new)
 
 e = Spirograph()
 e.affect()
index dcec88e2c76510091b178c5253e9a270c1f0cf81..071768e272caccf7c7a4d58e999af25b2008f4fa 100755 (executable)
@@ -37,9 +37,9 @@ class SegmentStraightener(inkex.Effect):
                         help="straightening behavior for cubic segments")
     def effect(self):
         for id, node in self.selected.iteritems():
-            if node.tagName == 'path':
-                d = node.attributes.getNamedItem('d')
-                p = simplepath.parsePath(d.value)
+            if node.tag == inkex.addNS('path', 'svg'):
+                d = node.get('d')
+                p = simplepath.parsePath(d)
                 last = []
                 subPathStart = []
                 for cmd,params in p:
@@ -63,7 +63,7 @@ class SegmentStraightener(inkex.Effect):
                         last = subPathStart[:]
                     else:
                         last = params[-2:]
-                d.value = simplepath.formatPath(p)
+                node.set('d',simplepath.formatPath(p))
 
 e = SegmentStraightener()
 e.affect()
index 803b2b37253149f944da382388155fa728a2e2dc..1e283e10d184ac8a2b6de0b6ab8d883038da3109 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+dfa#!/usr/bin/env python
 """
 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
 
@@ -31,9 +31,9 @@ class Project(inkex.Effect):
         #obj is selected second
         obj = self.selected[self.options.ids[0]]
         trafo = self.selected[self.options.ids[1]]
-        if obj.tagName == 'path' and trafo.tagName == 'path':
+        if obj.tag == inkex.addNS('path','svg) and trafo.tag == inkex.addNS('path','svg'):
             #distil trafo into four node points
-            trafo = cubicsuperpath.parsePath(trafo.attributes.getNamedItem('d').value)
+            trafo = cubicsuperpath.parsePath(trafo.get('d'))
             trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
 
             #vectors pointing away from the trafo origin
@@ -53,14 +53,14 @@ class Project(inkex.Effect):
                 err.close()
 
             #process path
-            d = obj.attributes.getNamedItem('d')
-            p = cubicsuperpath.parsePath(d.value)
+            d = obj.get('d')
+            p = cubicsuperpath.parsePath(d)
             for subs in p:
                 for csp in subs:
                     csp[0] = self.trafopoint(csp[0])
                     csp[1] = self.trafopoint(csp[1])
                     csp[2] = self.trafopoint(csp[2])
-            d.value = cubicsuperpath.formatPath(p)
+            obj.set('d',cubicsuperpath.formatPath(p))
 
     def trafopoint(self,(x,y)):
         #Transform algorithm thanks to Jose Hevia (freon)
index 3e9df6c1681644c35db02399fd51ba7f48939560..0be38e0d584fc17579ce26c138391c3a3cb0de25 100644 (file)
@@ -35,9 +35,9 @@ class Whirl(inkex.Effect):
             if self.options.rotation == True:
                 rotation = 1
             whirl = self.options.whirl / 1000
-            if node.tagName == 'path':
-                d = node.attributes.getNamedItem('d')
-                p = cubicsuperpath.parsePath(d.value)
+            if node.tag == inkex.addNS('path','svg'):
+                d = node.get('d')
+                p = cubicsuperpath.parsePath(d)
                 for sub in p:
                     for csp in sub:
                         for point in csp:
@@ -51,7 +51,7 @@ class Whirl(inkex.Effect):
                                 point[1] = (dist * math.sin(theta))
                             point[0] += self.view_center[0]
                             point[1] += self.view_center[1]
-                d.value = cubicsuperpath.formatPath(p)
+                node.set('d',cubicsuperpath.formatPath(p))
 
 e = Whirl()
 e.affect()