summaryrefslogtreecommitdiffstats
path: root/share/extensions/inkex/elements/_utils.py
blob: 56e1e129c3e86a93a45095b667cc7b6b998f8b76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 Martin Owens <doctormo@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
Useful utilities specifically for elements (that aren't base classes)

.. versionadded:: 1.1
    Most of the methods in this module were moved from inkex.utils.
"""

from collections import defaultdict
import re

# a dictionary of all of the xmlns prefixes in a standard inkscape doc
NSS = {
    "sodipodi": "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
    "cc": "http://creativecommons.org/ns#",
    "ccOLD": "http://web.resource.org/cc/",
    "svg": "http://www.w3.org/2000/svg",
    "dc": "http://purl.org/dc/elements/1.1/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "inkscape": "http://www.inkscape.org/namespaces/inkscape",
    "xlink": "http://www.w3.org/1999/xlink",
    "xml": "http://www.w3.org/XML/1998/namespace",
}
SSN = dict((b, a) for (a, b) in NSS.items())


def addNS(tag, ns=None):  # pylint: disable=invalid-name
    """Add a known namespace to a name for use with lxml"""
    if tag.startswith("{") and ns:
        _, tag = removeNS(tag)
    if not tag.startswith("{"):
        tag = tag.replace("__", ":")
        if ":" in tag:
            (ns, tag) = tag.rsplit(":", 1)
        ns = NSS.get(ns, None) or ns
        if ns is not None:
            return f"{{{ns}}}{tag}"
    return tag


def removeNS(name):  # pylint: disable=invalid-name
    """The reverse of addNS, finds any namespace and returns tuple (ns, tag)"""
    if name[0] == "{":
        (url, tag) = name[1:].split("}", 1)
        return SSN.get(url, "svg"), tag
    if ":" in name:
        return name.rsplit(":", 1)
    return "svg", name


def splitNS(name):  # pylint: disable=invalid-name
    """Like removeNS, but returns a url instead of a prefix"""
    (prefix, tag) = removeNS(name)
    return (NSS[prefix], tag)


def natural_sort_key(key, _nsre=re.compile("([0-9]+)")):
    """Helper for a natural sort, see
    https://stackoverflow.com/a/16090640/3298143"""
    return [int(text) if text.isdigit() else text.lower() for text in _nsre.split(key)]


class ChildToProperty(property):
    """Use when you have a singleton child element who's text
    content is the canonical value for the property"""

    def __init__(self, tag, prepend=False):
        super().__init__()
        self.tag = tag
        self.prepend = prepend

    def __get__(self, obj, klass=None):
        elem = obj.findone(self.tag)
        return elem.text if elem is not None else None

    def __set__(self, obj, value):
        elem = obj.get_or_create(self.tag, prepend=self.prepend)
        elem.text = value

    def __delete__(self, obj):
        obj.remove_all(self.tag)

    @property
    def __doc__(self):
        return f"Get, set or delete the {self.tag} property."


class CloningVat:
    """
    When modifying defs, sometimes we want to know if every backlink would have
    needed changing, or it was just some of them.

    This tracks the def elements, their promises and creates clones if needed.
    """

    def __init__(self, svg):
        self.svg = svg
        self.tracks = defaultdict(set)
        self.set_ids = defaultdict(list)

    def track(self, elem, parent, set_id=None, **kwargs):
        """Track the element and connected parent"""
        elem_id = elem.get("id")
        parent_id = parent.get("id")
        self.tracks[elem_id].add(parent_id)
        self.set_ids[elem_id].append((set_id, kwargs))

    def process(self, process, types=(), make_clones=True, **kwargs):
        """
        Process each tracked item if the backlinks match the parents

        Optionally make clones, process the clone and set the new id.
        """
        for elem_id in list(self.tracks):
            parents = self.tracks[elem_id]
            elem = self.svg.getElementById(elem_id)
            backlinks = {blk.get("id") for blk in elem.backlinks(*types)}
            if backlinks == parents:
                # No need to clone, we're processing on-behalf of all parents
                process(elem, **kwargs)
            elif make_clones:
                clone = elem.copy()
                elem.getparent().append(clone)
                clone.set_random_id()
                for update, upkw in self.set_ids.get(elem_id, ()):
                    update(elem.get("id"), clone.get("id"), **upkw)
                process(clone, **kwargs)