This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/reload/reload.py

32 lines
1.1 KiB
Python

#!/usr/bin/env python3
import inkex
from lxml import etree
class Reload(inkex.EffectExtension):
def effect(self):
currentDoc = self.document_path()
if currentDoc == "":
self.msg("Your document is not saved as a permanent file yet. Cannot reload.")
exit(1)
stream = open(self.document_path(), 'r')
p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
stream.close()
root = self.document.getroot()
kept = [] #required. if we delete them directly without adding new defs or namedview, inkscape will crash
for node in self.document.xpath('//*', namespaces=inkex.NSS):
if node.TAG not in ('svg', 'defs', 'namedview'):
node.delete()
elif node.TAG in ('defs', 'namedview'): #except 'svg'
kept.append(node)
children = doc.getroot().getchildren()
for child in children:
root.append(child)
for k in kept:
k.delete()
if __name__ == '__main__':
Reload().run()