2020-08-17 15:21:46 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import inkex
|
|
|
|
|
import sys
|
|
|
|
|
|
2021-06-02 23:30:37 +02:00
|
|
|
|
class EditAttributes(inkex.EffectExtension):
|
2021-04-15 17:03:47 +02:00
|
|
|
|
def add_arguments(self, pars):
|
|
|
|
|
pars.add_argument("-a", "--attributeName", help="attribute name to set")
|
|
|
|
|
pars.add_argument("-v", "--attributeValue", help="attribute value to set")
|
|
|
|
|
pars.add_argument("-m", "--mode", default="set", help="mode of operation")
|
2020-08-17 15:21:46 +02:00
|
|
|
|
|
|
|
|
|
def effect(self):
|
2021-04-15 17:03:47 +02:00
|
|
|
|
if not self.options.attributeName: # if attributeName is not given
|
|
|
|
|
inkex.errormsg("Attribute name not given")
|
|
|
|
|
return
|
|
|
|
|
if not self.options.attributeValue: # required to make proper behaviour
|
|
|
|
|
inkex.errormsg("Please define proper attribute value")
|
|
|
|
|
return
|
2020-08-17 15:21:46 +02:00
|
|
|
|
|
|
|
|
|
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":
|
2021-04-15 17:03:47 +02:00
|
|
|
|
el.set(self.options.attributeName, self.options.attributeValue)
|
2020-08-17 15:21:46 +02:00
|
|
|
|
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:
|
2020-08-20 13:12:34 +02:00
|
|
|
|
inkex.errormsg("Invalid mode: " + self.options.mode)
|
2020-08-17 15:21:46 +02:00
|
|
|
|
|
2020-08-31 21:25:41 +02:00
|
|
|
|
if __name__ == '__main__':
|
2021-06-02 23:30:37 +02:00
|
|
|
|
EditAttributes().run()
|