summaryrefslogtreecommitdiff
path: root/lib/extensions/apply_attribute.py
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2025-10-27 17:27:41 +0100
committerGitHub <noreply@github.com>2025-10-27 17:27:41 +0100
commit0f95684bff102d8f573c17e806d37c35b59562d6 (patch)
tree06a63b16a10dea712fcc9b81737b0b58fa672dee /lib/extensions/apply_attribute.py
parenta91c18c374db810357283f8197f05db1cfd87b65 (diff)
add apply attribute extension (#3983)
Diffstat (limited to 'lib/extensions/apply_attribute.py')
-rw-r--r--lib/extensions/apply_attribute.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/extensions/apply_attribute.py b/lib/extensions/apply_attribute.py
new file mode 100644
index 00000000..0d3e30e6
--- /dev/null
+++ b/lib/extensions/apply_attribute.py
@@ -0,0 +1,47 @@
+# Authors: see git history
+#
+# Copyright (c) 2025 Authors
+# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
+
+from inkex import Boolean, errormsg
+
+from ..i18n import _
+from .base import InkstitchExtension
+
+
+class ApplyAttribute(InkstitchExtension):
+ '''
+ Applies a given attribute to all selected elements
+ '''
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self, *args, **kwargs)
+ self.arg_parser.add_argument("--notebook")
+ self.arg_parser.add_argument("-n", "--namespace", dest="namespace", type=str, default='inkstitch')
+ self.arg_parser.add_argument("-k", "--key", dest="key", type=str, default='')
+ self.arg_parser.add_argument("-v", "--value", dest="value", type=str, default='')
+ self.arg_parser.add_argument("-r", "--remove", dest="remove", type=Boolean, default=False)
+
+ def effect(self):
+ self.get_elements()
+ if not self.elements:
+ errormsg(_("Please select at least one element."))
+ return
+
+ if not self.options.key:
+ errormsg(_("Please enter the attribute name."))
+ return
+
+ key = ''
+ if self.options.namespace:
+ key = f'{self.options.namespace}:'
+ key += self.options.key
+
+ if self.options.remove:
+ for element in self.elements:
+ element.node.pop(key)
+ else:
+ if not self.options.value:
+ errormsg(_("Please enter a value."))
+ return
+ for element in self.elements:
+ element.node.set(key, self.options.value)