Added attributes importer, editorand cleaner extensions

This commit is contained in:
Mario Voigt 2020-08-17 15:21:46 +02:00
parent b22f862b94
commit b8cd24d689
8 changed files with 175 additions and 2 deletions

View File

@ -1,5 +1,4 @@
#! /usr/bin/env python #! /usr/bin/env python3
# coding=utf-8
# #
# #
# Este script dibuja el perfil exterior de corte la caja en un solo # Este script dibuja el perfil exterior de corte la caja en un solo

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import inkex import inkex
import fablabchemnitz_apolloniangasket_func import fablabchemnitz_apolloniangasket_func
from lxml import etree from lxml import etree

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Remove Obsolete Attributes</name>
<id>fablabchemnitz.deattributes_cleaner</id>
<param name="introduction" type="description">Remove attributes &quot;sodipodi:absref&quot;, &quot;sodipodi:docbase&quot; and &quot;sodipodi:docname&quot; from all elements that contain them. These attributes contain absolute paths to resources which can pose a security issue.</param>
<param name="removeAbsref" type="bool" gui-text="Remove sodipodi:absref">true</param>
<param name="removeDocbase" type="bool" gui-text="Remove sodipodi:docbase">true</param>
<param name="removeDocname" type="bool" gui-text="Remove sodipodi:docname">true</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="Attributes"/>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">fablabchemnitz_attributes_cleaner.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,44 @@
#!/usr/bin/env python3
"""
Removes attributes sodipodi:absref, sodipodi:docbase and sodipodi:docname from all elements that contain them.
full names of attributes
sodipodi:absref
{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}absref
sodipodi:docbase
{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}docbase
sodipodi:docname
{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}docname
element.attrib.pop("{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}absref", None)
"""
import inkex
import sys
class AbsrefRemover(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("-a", "--removeAbsref", type=inkex.Boolean, default=True, help="Remove sodipodi:absref")
self.arg_parser.add_argument("-b", "--removeDocbase", type=inkex.Boolean, default=True, help="Remove sodipodi:docbase")
self.arg_parser.add_argument("-n", "--removeDocname", type=inkex.Boolean, default=True, help="Remove sodipodi:docname")
def effect(self):
if self.options.removeAbsref:
elements = self.document.xpath("//*[@sodipodi:absref]", namespaces=inkex.NSS)
for element in elements:
element.attrib.pop("{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}absref", None)
if self.options.removeDocbase:
elements = self.document.xpath("//*[@sodipodi:docbase]", namespaces=inkex.NSS)
for element in elements:
element.attrib.pop("{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}docbase", None)
if self.options.removeDocname:
elements = self.document.xpath("//*[@sodipodi:docname]", namespaces=inkex.NSS)
for element in elements:
element.attrib.pop("{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}docname", None)
if __name__ == "__main__":
AbsrefRemover().run()

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Edit Attributes</name>
<id>fablabchemnitz.de.attributes_editor</id>
<param name="introduction" type="description">Edit value of attribute on selected elements.</param>
<param name="tip" type="description">For namespaces use {namespaceUrl}attributeName</param>
<param name="attributeName" type="string" gui-text="Name:"></param>
<param name="attributeValue" type="string" gui-text="Value: "></param>
<param name="mode" type="optiongroup" appearance="combo" gui-text="Operation on attribute">
<option value="set">Set</option>
<option value="append">Append</option>
<option value="prefix">Prefix</option>
<option value="subtract">Remove content</option>
<option value="remove">Remove attribute</option>
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="Attributes" />
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">fablabchemnitz_attributes_editor.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,38 @@
#!/usr/bin/env python3
import inkex
import sys
class AttribEditor(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("-a", "--attributeName", help="attribute name to set")
self.arg_parser.add_argument("-v", "--attributeValue", help="attribute value to set")
self.arg_parser.add_argument("-m", "--mode", default="set", help="mode of operation")
def effect(self):
if not self.options.attributeName: # if attributeName is not given
self.OptionParser.error("Attribute name not given")
elements = self.svg.selected.values()
for el in elements:
currentAtt = el.attrib.get(self.options.attributeName)
if currentAtt is None:
currentAtt = ""
if self.options.mode == "set":
el.attrib[self.options.attributeName] = self.options.attributeValue
elif self.options.mode == "append":
el.attrib[self.options.attributeName] = currentAtt + self.options.attributeValue
elif self.options.mode == "prefix":
el.attrib[self.options.attributeName] = self.options.attributeValue + currentAtt
elif self.options.mode == "subtract":
el.attrib[self.options.attributeName] = currentAtt.replace(self.options.attributeValue, "")
elif self.options.mode == "remove":
if self.options.attributeName in el.attrib:
del el.attrib[self.options.attributeName]
else:
inkex.errormsg(_("Invalid mode: " + self.options.mode))
if __name__ == "__main__":
AttribEditor().run()

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Import Attributes</name>
<id>fablabchemnitz.de.attributes_import</id>
<param name="introduction" type="description"> Uses lines in text file to edit attributes of elements.</param>
<_param name="usage" type="description">Line: 'elementID,attributeName,attributeValue'. </param>
<param name="namespaceTip" type="description"> For namespaces use {namespaceUrl}attributeName</param>
<param name="data" type="path" _gui-text="Data file:" mode="file" filetypes="txt,csv"/>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="Attributes" />
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">fablabchemnitz_attributes_import.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python3
import inkex
class AttribImport(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("--data", default="", help="data file")
def effect(self):
with open(self.options.data, 'r') as f:
lines = f.read().splitlines()
for line in lines:
#split on , max 2+1 = 3 items
parts = line.split(",", 2)
if len(parts) >= 3:
id = parts[0]
attribute = parts[1]
value = parts[2]
try:
node = self.svg.getElementById(id)
if node is not None:
try:
node.set(attribute, value)
except AttributeError:
inkex.utils.debug("Unknown Attribute")
except AttributeError:
inkex.utils.debug("element with id '" + id + "' not found in current selection.")
if __name__ == '__main__':
AttribImport().run()