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
- # *************************************************************
- # 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.
- """
- fin = open(source, 'r')
- fd, name = mkstemp()
- fout = open(name, 'w')
- for line in fin:
- out = line
- for pattern, replace in self.searchReplace:
- out = re.sub(pattern, replace, out)
- fout.write(out)
- try:
- fout.writelines(fin.readlines())
- except Exception as E:
- raise E
- fin.close()
- fout.close()
- os.close(fd)
- shutil.move(name, source)
- upgrader = UpgradeExtension()
- if len(sys.argv) == 2:
- upgrader.upgrade(sys.argv[1])
- quit()
Advertisement
Add Comment
Please, Sign In to add comment