ReplacementScript

From Inkscape Wiki
Jump to navigation Jump to search

This script can be used to do the following for all files and files in subdirs:

   sp_curve_moveto(c, p);

to

  c->moveto(p);

For more explanation, ask Johan Engelen



   # -*- coding: utf-8 -*-
   # Python
   import os, sys, re
   mydir= 'C:/inkscape/src'
   functionname='move_endpoints'
   rvalue='(?:[\w]+[->]*)+'
   patn=re.compile('sp_curve_' + functionname + '[\s]*\([\s]*(' + rvalue + ')[\s]*[,]*[\s]*(.*)')
   replacemt=r'''\1->'''+functionname+'('+r'''\2'''
   def replaceStringInFile(filePath):
       "replaces all string by a regex substitution"
       print filePath
       veranderd = False
       tempName=filePath+'~~~'
       input = open(filePath)
       s=input.read()
       while True:
           m = patn.search(s)
           if m:
               print '----'
               print m.group(0)
               print re.sub(patn, replacemt, m.group(0), 1)
               print '----'
               ok = raw_input('y/n?')
               if ok in ('y'):
                   s = re.sub(patn, replacemt, s, 1)
                   veranderd = True;
               else:
                   break
           else:
               break
       if veranderd:
           output = open(tempName,'w')
           output.write(s)
           output.close()
           input.close()
           os.remove(filePath)
           os.rename(tempName,filePath)
   def myfun(dummy, dirr, filess):
       for child in filess:
   #        if child == '20040428_xelso_ranmi.html':
           if '.cpp' == os.path.splitext(child)[1] and os.path.isfile(dirr+'/'+child):
               replaceStringInFile(dirr+'/'+child)
               print child
   os.path.walk(mydir, myfun, 3)