oliverthered

InkscapeExtensionUpgrade.py

Nov 12th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. '''
  3. Copyright (C) Oliver Stiewber , oliverthered, [email protected] 2020
  4.  
  5. InkscapeExtensionUpgrade.py is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with InkscapeShapeReco; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. Quick description:
  20.  
  21.    Upgrades Inkscape Extensions from 0.x to 1.x
  22.    to run
  23.    python3 InkscapeExtensionUpgrade.py [extensionrootdirectory]
  24.    
  25.    
  26.    Requires 2to3 Python script 2 to 3 upgrader
  27.        pip install 2to3
  28.  
  29. Limitations:
  30.        
  31.    2to3 issues
  32.    ----------------
  33.    
  34.    if object.attribute or not object.attribute == None:
  35.       etc...
  36.      
  37.       Doesn't get upgraded to:
  38.    
  39.          hasattr(object, attribute)
  40.    
  41.       The following is an example of a suitable substitute:
  42.    
  43.           (object.attribute if hasattr(object, attribute) else None)
  44.    
  45.    Some types aren't cast e.g.
  46.          float hello = 3456.5
  47.          vect = [1,2,"2",3]
  48.          vect[hello/0.2]
  49.          
  50.        
  51.        doesn't get converted to:
  52.           vect[int(hello/0.2)]
  53.              
  54.          
  55.        you may want to verify that floor and celing is beiung interpreited cortrectly here.
  56.        
  57.        There are also times where:
  58.            len(foobar)
  59.        Needs to be cast:
  60.            int(len(foobar))
  61.        
  62.     Upgrading in general
  63.     ----------------
  64.        
  65.         Depricated functions aren't currently upgraded
  66.        
  67.         There are no other known limitations.
  68.  
  69. '''
  70.  
  71.  
  72. import re
  73. import os
  74. import sys
  75. import shutil
  76. from tempfile import mkstemp
  77. import subprocess
  78.  
  79. class UpgradeExtension():
  80.     def __init__(self):
  81.         self.searchReplace = [
  82.         ["^([\\b]*)str[\\s]", "type=\\1str"],
  83.         ["OptionParser.add_option", "arg_parser.add_argument"],
  84.         ["action=\"store\"[\\b]*[,]?[\\s]*", ""],
  85.         [",[\\b]*,", ","],
  86.         ["type=\"inkbool\"", "type=inkex.Boolean"],
  87.         ["^([\\b]*)print (.+)", "\\1print\\(\\2\\)"],
  88.         ["type=\"(.*)\"", "type=\\1"],
  89.         ["type=string", "type=str"],
  90.         ["isinstance\\((.*)\\.tag, basestring\\)", "isinstance\\(\\1\\.tag, str\\)"],
  91.         ["isinstance\\((.*)\\.tag, unicode\\)", "isinstance\\(\\1\\.tag, str\\)"],
  92.         ["isinstance\\((.*)\\.tag, stdout\\)", "isinstance\\(\\1\\.tag, sys.stdout.buffer\\)"],
  93.         ["inkex.debug", "inkex.utils.debug"]]
  94.  
  95.     #TODO: Get rid of dest and intead create a bakup and just replace the origional,
  96.     # or just replace the oriugional and assume that the user already has a source copy.
  97.     # probably a good idea to have a warn prompt first.
  98.     def upgrade(self, source):
  99.         """Upgrades a Inkscape 4.x extension to a 1.x one
  100.    
  101.        Args:
  102.            source  (str): input filename, source will be over written.        
  103.        """
  104.        
  105.         print("Upgrading extension file: '" + source + "' to Inkscape 1.x")
  106.         subprocess.call(["2to3", "-w", "-f", "all", "-f", "buffer", "-f", "idioms", "-f", "set_literal", "-f", "ws_comma", source])        
  107.         with open(source, 'r') as fin:
  108.            
  109.             fd, name = mkstemp()            
  110.             with open(name, 'w') as fout:
  111.                
  112.                 for line in fin:
  113.                     out = line
  114.                     for pattern, replace in self.searchReplace:
  115.                         out = re.sub(pattern, replace, out)
  116.                     fout.write(out)
  117.                 fout.writelines(fin.readlines())
  118.  
  119.             os.close(fd)
  120.        
  121.         shutil.move(name, source)
  122.  
  123. def traversePath(path, filter, function):
  124.     print("Revcursing path: '" + path + "' for files ending in: '" + filter + "'")
  125.     for root, subFolders, files in os.walk(path):      
  126.         for file in files:
  127.             if file[len(file) - len(filter):] == filter:                
  128.                 function(os.path.join(root, file))
  129.     return     
  130.  
  131. upgrader = UpgradeExtension()
  132. rootPath = "."
  133. if len(sys.argv) == 2:
  134.     rootPath = sys.argv[1]
  135. traversePath(rootPath, ".py", upgrader.upgrade)
  136.  
  137. quit()
Advertisement
Add Comment
Please, Sign In to add comment