Collection of mostly command line tools / PHP scripts. Somewhat out of date.

⌈⌋ ⎇ branch:  scripts + snippets


Check-in [6b16bde070]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:separate handler for notebook/page sectioning
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6b16bde07047d1056ad9a7ca616e02d7f3412371
User & Date: mario 2022-10-11 07:12:08
Context
2022-10-11
07:12
hex_color redundant with inkex.Color check-in: c284f16d06 user: mario tags: trunk
07:12
separate handler for notebook/page sectioning check-in: 6b16bde070 user: mario tags: trunk
2022-10-10
07:45
translatable=no for combo boxes check-in: 33dbf985e5 user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to inkscape/pmd2inks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
# encoding: utf-8
# api: python
##type: cli
# category: convert
# title: plugin meta to .inx
# description: Convert plugin description to Inkscape .inx
# license: PD
# version: 0.6
# state: beta
# depends: pluginconf (>= 0.7.6)
# config: -
# pylint: disable=missing-docstring, invalid-name, no-else-return
# doc: https://gitlab.com/inkscape/extensions/-/blob/master/inkscape.extension.rng
#
# Quickly converts a PMD comment block to .inx descriptor.








|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
# encoding: utf-8
# api: python
##type: cli
# category: convert
# title: plugin meta to .inx
# description: Convert plugin description to Inkscape .inx
# license: PD
# version: 0.7
# state: beta
# depends: pluginconf (>= 0.7.6)
# config: -
# pylint: disable=missing-docstring, invalid-name, no-else-return
# doc: https://gitlab.com/inkscape/extensions/-/blob/master/inkscape.extension.rng
#
# Quickly converts a PMD comment block to .inx descriptor.
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
        "float": "float",
        "bool": "inkex.Boolean",
        "color": "inkex.Color",
    }
}
bool_true_match = re.compile(r"^(1|yes|true)", re.I).match


class state:

    tab = ""
    page = ""





























def enum_item(k, v, _add="", xml="option"):
    return f"""<{xml} {_add} value="{k}">{v}</{xml}>"""

def param(o):

    # arguments
    raw_type = o.get("type")
    _type = raw_type or "str"
    _type = map["type"].get(_type, "string")
    _name = o.get("name", "_0")
    _val = o.get("value", "")
    _desc = o.get("description", "…")
    _tab = o.get("category") or o.get("class") or o.get("page")
    # optional attributes via {_add}
    _prefix = ""
    _add = ""
    for prop in "mode", "appearance", "translatable":
        if map[prop].get(raw_type) and not o.get(prop):
            o[prop] = map[prop][raw_type]
        if o.get(prop):
            _add = _add + f"{prop}=\"{o[prop]}\" "
    if o.get("help"):
        _add += f"gui-description=\"{o['help']}\""
    if bool_true_match(o.get("hidden", "")):
        _add += 'gui-hidden="true" '
    # value mapping
    if _type == "bool":
        _val = "true" if bool_true_match( _val) else "false"
    # raw output
    if _tab:
        if _tab != state.tab:
            if state.tab:
                _prefix = """</page>"""
            _prefix += f"""
                 <page name="{_tab}" gui-text="{o.get("tab_desc") or _tab}">
            """
        state.tab = _tab
    if "xml" in o:
        return o["xml"]
    elif "label" in o or _type == "label":
        text = o.get("label") or _desc
        if re.match(r"^[#{$]", text):
            text = meta[re.sub(r"^[#{$]+|[:}$]*$", "", text)] # in-place reference
        return f"""
            {_prefix}
            <label {_add}>{text}</label>
        """
    # variants
    elif _type == "select":
        _option_add = 'translatable="no" ' if not bool_true_match(o.get("translatable", "yes")) else ''
        ls = "\n            ".join([enum_item(k, v, _option_add) for k, v in o.get("select", {}).items()])
        return f"""
            {_prefix}
            <param name="{_name}" type="optiongroup" {_add} gui-text="{_desc}">
            {ls}
            </param>
        """
    elif _type == "notebook":
        state.page = _name
        state.tab = ""
        return f"""
            {_prefix}
            <param name="{_name}" type="{_type}" {_add} gui-text="{_desc}">
        """
    elif _type in ("int", "float"):
        return f"""
            {_prefix}
            <param name="{_name}" type="{_type}" min="{o.get("min",0)}" max="{o.get("max",100)}" precision="{o.get("precision",1)}" {_add} gui-text="{_desc}">{_val}</param>
        """
    else:
        return f"""
            {_prefix}
            <param name="{_name}" type="{_type}" {_add} gui-text="{_desc}">{_val}</param>
        """

def inx_export(m, xml="output"):
    if not "inx_export" in m:
        return ""
    _ext, _mime, _type, _tip = re.split(r"\s*,\s*", m["inx_export"])







>
|
>

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>









<














|
<
<
<
<
<
<
|

|




|
<






|
<





<
|
|
<



|
<



|
<







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
145
146
147
148

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163






164
165
166
167
168
169
170
171

172
173
174
175
176
177
178

179
180
181
182
183

184
185

186
187
188
189

190
191
192
193

194
195
196
197
198
199
200
        "float": "float",
        "bool": "inkex.Boolean",
        "color": "inkex.Color",
    }
}
bool_true_match = re.compile(r"^(1|yes|true)", re.I).match

notebook = None
class new_notebook:
    """ nestable notebook pages / tabs """
    tab = ""
    param = ""
    prev = None
    curr = None
    
    def __init__(self, param, o):
        self.param = param
        self.nest("", o)
        self.curr = self
        self.prev = notebook

    def nest(self, name, o):
        if name == self.tab:
            return
        if self.tab:
            yield from self.end(name)
        if name:
            yield f"""
                 <page name="{name}" gui-text="{o.get("tab_desc") or name}">
            """
        self.tab = name

    def end(self, name, all=True):
        yield """</page>"""
        if self.param and not name:
            self.param = ""
            yield """</param>"""
            notebook = self.prev
        if all and self.prev:
            self.prev.end(name, all)

def enum_item(k, v, _add="", xml="option"):
    return f"""<{xml} {_add} value="{k}">{v}</{xml}>"""

def param(o):
    global notebook
    # arguments
    raw_type = o.get("type")
    _type = raw_type or "str"
    _type = map["type"].get(_type, "string")
    _name = o.get("name", "_0")
    _val = o.get("value", "")
    _desc = o.get("description", "…")
    _tab = o.get("category") or o.get("class") or o.get("page")
    # optional attributes via {_add}

    _add = ""
    for prop in "mode", "appearance", "translatable":
        if map[prop].get(raw_type) and not o.get(prop):
            o[prop] = map[prop][raw_type]
        if o.get(prop):
            _add = _add + f"{prop}=\"{o[prop]}\" "
    if o.get("help"):
        _add += f"gui-description=\"{o['help']}\""
    if bool_true_match(o.get("hidden", "")):
        _add += 'gui-hidden="true" '
    # value mapping
    if _type == "bool":
        _val = "true" if bool_true_match( _val) else "false"
    # raw output
    if notebook:






        yield from notebook.nest(_tab, o)
    if "xml" in o:
        yield o["xml"]
    elif "label" in o or _type == "label":
        text = o.get("label") or _desc
        if re.match(r"^[#{$]", text):
            text = meta[re.sub(r"^[#{$]+|[:}$]*$", "", text)] # in-place reference
        yield f"""

            <label {_add}>{text}</label>
        """
    # variants
    elif _type == "select":
        _option_add = 'translatable="no" ' if not bool_true_match(o.get("translatable", "yes")) else ''
        ls = "\n            ".join([enum_item(k, v, _option_add) for k, v in o.get("select", {}).items()])
        yield f"""

            <param name="{_name}" type="optiongroup" {_add} gui-text="{_desc}">
            {ls}
            </param>
        """
    elif _type == "notebook":

        notebook = new_notebook(_name, o)
        yield f"""

            <param name="{_name}" type="{_type}" {_add} gui-text="{_desc}">
        """
    elif _type in ("int", "float"):
        yield f"""

            <param name="{_name}" type="{_type}" min="{o.get("min",0)}" max="{o.get("max",100)}" precision="{o.get("precision",1)}" {_add} gui-text="{_desc}">{_val}</param>
        """
    else:
        yield f"""

            <param name="{_name}" type="{_type}" {_add} gui-text="{_desc}">{_val}</param>
        """

def inx_export(m, xml="output"):
    if not "inx_export" in m:
        return ""
    _ext, _mime, _type, _tip = re.split(r"\s*,\s*", m["inx_export"])
201
202
203
204
205
206
207
208
209
210
211

212


213
214
215
216
217
218
219
    elif _type not in ("int", "float"):
        _val = '"' + _val + '"'
    return f"""        pars.add_argument("-"+"-{_name}", type={_type}, dest="{_name}", default={_val}, help="{o.get("description","")}")"""


def generate_inx(m):
    """ roughly convert builtin plugin description to .inx """
    params = [
        param(o) for o in m["config"]
    ]
    if state.tab:

        params.append("</page></param>")


    args = list(
        filter(
            None,
            [add_arg(o) for o in m["config"] if o["type"] != "label"]
        )
    )
    return xml_template.format(







|
|
<
<
>
|
>
>







219
220
221
222
223
224
225
226
227


228
229
230
231
232
233
234
235
236
237
238
    elif _type not in ("int", "float"):
        _val = '"' + _val + '"'
    return f"""        pars.add_argument("-"+"-{_name}", type={_type}, dest="{_name}", default={_val}, help="{o.get("description","")}")"""


def generate_inx(m):
    """ roughly convert builtin plugin description to .inx """
    params = []
    for o in m["config"]:


        for line in param(o):
            params.append(line)
    if notebook.curr:
        params = params + list(notebook.end("", True))
    args = list(
        filter(
            None,
            [add_arg(o) for o in m["config"] if o["type"] != "label"]
        )
    )
    return xml_template.format(