Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- '''
- Copyright (C) Oliver Stiewber , oliverthered, [email protected] 2020
- InkscapeExtensionUpgrade.py is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with InkscapeShapeReco; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- Quick description:
- Upgrades Inkscape Extensions from 0.x to 1.x
- to run
- python3 InkscapeExtensionUpgrade.py [extensionrootdirectory]
- Requires 2to3 Python script 2 to 3 upgrader
- pip install 2to3
- Limitations:
- 2to3 issues
- ----------------
- if object.attribute or not object.attribute == None:
- etc...
- Doesn't get upgraded to:
- hasattr(object, attribute)
- The following is an example of a suitable substitute:
- (object.attribute if hasattr(object, attribute) else None)
- Some types aren't cast e.g.
- float hello = 3456.5
- vect = [1,2,"2",3]
- vect[hello/0.2]
- doesn't get converted to:
- vect[int(hello/0.2)]
- you may want to verify that floor and celing is beiung interpreited cortrectly here.
- There are also times where:
- len(foobar)
- Needs to be cast:
- int(len(foobar))
- Upgrading in general
- ----------------
- Depricated functions aren't currently upgraded
- There are no other known limitations.
- '''
- import re
- import os
- import sys
- import shutil
- from tempfile import mkstemp
- import subprocess
- class UpgradeExtension():
- def __init__(self):
- self.searchReplace = [
- ["^([\\b]*)str[\\s]", "type=\\1str"],
- ["OptionParser.add_option", "arg_parser.add_argument"],
- ["action=\"store\"[\\b]*[,]?[\\s]*", ""],
- [",[\\b]*,", ","],
- ["type=\"inkbool\"", "type=inkex.Boolean"],
- ["^([\\b]*)print (.+)", "\\1print\\(\\2\\)"],
- ["type=\"(.*)\"", "type=\\1"],
- ["type=string", "type=str"],
- ["isinstance\\((.*)\\.tag, basestring\\)", "isinstance\\(\\1\\.tag, str\\)"],
- ["isinstance\\((.*)\\.tag, unicode\\)", "isinstance\\(\\1\\.tag, str\\)"],
- ["isinstance\\((.*)\\.tag, stdout\\)", "isinstance\\(\\1\\.tag, sys.stdout.buffer\\)"],
- ["inkex.debug", "inkex.utils.debug"]]
- #TODO: Get rid of dest and intead create a bakup and just replace the origional,
- # or just replace the oriugional and assume that the user already has a source copy.
- # probably a good idea to have a warn prompt first.
- def upgrade(self, source):
- """Upgrades a Inkscape 4.x extension to a 1.x one
- Args:
- source (str): input filename, source will be over written.
- """
- print("Upgrading extension file: '" + source + "' to Inkscape 1.x")
- subprocess.call(["2to3", "-w", "-f", "all", "-f", "buffer", "-f", "idioms", "-f", "set_literal", "-f", "ws_comma", source])
- with open(source, 'r') as fin:
- fd, name = mkstemp()
- with open(name, 'w') as fout:
- for line in fin:
- out = line
- for pattern, replace in self.searchReplace:
- out = re.sub(pattern, replace, out)
- fout.write(out)
- fout.writelines(fin.readlines())
- os.close(fd)
- shutil.move(name, source)
- def traversePath(path, filter, function):
- print("Revcursing path: '" + path + "' for files ending in: '" + filter + "'")
- for root, subFolders, files in os.walk(path):
- for file in files:
- if file[len(file) - len(filter):] == filter:
- function(os.path.join(root, file))
- return
- upgrader = UpgradeExtension()
- rootPath = "."
- if len(sys.argv) == 2:
- rootPath = sys.argv[1]
- traversePath(rootPath, ".py", upgrader.upgrade)
- quit()
Advertisement
Add Comment
Please, Sign In to add comment