Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import os
- import sys
- import shutil
- from tempfile import mkstemp
- import subprocess
- # *************************************************************
- # The inkscape extension
- # *************************************************************
- 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\\)"]]
- #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
Advertisement