Overview
Comment:introduce ConfigList() for config: key access
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fec593dcbd35b8c2eab70f00ef9195bfe38a1b2f4f9c6ad3236a8faedca6f6f2
User & Date: mario on 2022-10-28 22:31:52
Other Links: manifest | tags
Context
2022-10-28
22:40
add some vague permissive: score for modules (error tolerancy/ignorance) check-in: d8df8b1ba2 user: mario tags: trunk
22:31
introduce ConfigList() for config: key access check-in: fec593dcbd user: mario tags: trunk
21:59
update docs check-in: 33c3bc16bb user: mario tags: trunk
Changes

Modified pluginconf/__init__.py from [40c0c39483] to [f4eabf5fc1].

368
369
370
371
372
373
374
375
376


377
378
379
380
381
382
383
384
385
386





387
388
389
390















391
392
393
394
395
396
397
368
369
370
371
372
373
374


375
376
377
378
379
380
381
382
383



384
385
386
387
388
389



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411







-
-
+
+







-
-
-
+
+
+
+
+

-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    for field in rx.keyval.findall(src):
        meta[field[0].replace("-", "_").lower()] = field[1].strip()
    meta["config"] = plugin_meta_config(meta.get("config") or "")

    return PluginMeta(meta)


# Dict wrapper
# ‾‾‾‾‾‾‾‾‾‾‾‾
# Dict/list wrappers
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
class PluginMeta(dict):
    """
    Plugin meta data, as dictionary with alternative .property access.
    Returned for each `plugin_meta()` result, and config: options.
    Non-existent .fieldnames just resolve to `""`.
    """

    def __getattr__(self, key):
        """ Return [key] for .property access, else None """
        return self.get(key, "")
    def __getattr__(self, key, default=""):
        """ Return [key] for .property access, else `""`. """
        if key == "config":
            default = OptionList()
        return self.get(key, default)

    def __hasattr__(self, key):
        """ Return [key] for .property access, else None """
        return key in self
    def __setattr__(self, key, val):
        """ Shouldn't really have this, but for parity. """
        self[key] = val

class OptionList(list):
    """
    List of config: options, with additional .name access (name= from option entry).
    """

    def __getattr__(self, key):
        """ Returns list entry with name= equaling .name access """
        for opt in self:
            if opt.name == key:
                return opt
        raise KeyError("No option name '%s' in config list" % key)


# Unpack config: structures
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
def plugin_meta_config(src):
    """
    Further breaks up the meta['config'] descriptor.
425
426
427
428
429
430
431
432


433
434
435
436
437
438
439
439
440
441
442
443
444
445

446
447
448
449
450
451
452
453
454







-
+
+







            opt[field[0]] = (field[1] or field[2] or field[3] or "").strip()
        # normalize type
        opt["type"] = config_opt_type_map.get(opt["type"], opt["type"] or "str")
        # preparse select:
        if opt.get("select"):
            opt["select"] = config_opt_parse_select(opt.get("select", ""))
        config.append(opt)
    return config

    return OptionList(PluginMeta(opt) for opt in config)

# split up `select: 1=on|2=more|3=title` or `select: foo|bar|lists`
def config_opt_parse_select(select):
    """ unpack 1|2|3 or title=lists """
    if re.search("([=:])", select):
        return dict(rx.select_dict.findall(select))
    #else:

Modified pluginconf/gui.py from [faa74dfb1b] to [70ac617c4f].

72
73
74
75
76
77
78

79

80
81
82
83
84
85
86
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87







+
-
+







    -------
    True : if changed config{} values are to be saved (the dict will be updated in any case)
    """
    plugins = kwargs.get("plugins", {})
    opt_label = kwargs.get("opt_label", False)
    theme = kwargs.get("theme", "DefaultNoMoreNagging")
    if theme:
        if "theme" in kwargs:
        del kwargs["theme"]
            del kwargs["theme"]
        sg.theme(theme)
    if files:
        plugins = read_options(files)
    layout = plugin_layout(plugins.values(), config, plugin_states, opt_label=opt_label)
    layout.append([sg.T(" ")])
    #print(repr(layout))

Modified test/basic.py from [268d6cd46f] to [1fde260c36].

9
10
11
12
13
14
15
16

17
18
19

20
21
22
23


24
25

26
27
28
29
30
31
9
10
11
12
13
14
15

16
17
18

19
20
21


22
23
24

25
26
27
28
29
30
31







-
+


-
+


-
-
+
+

-
+






import pytest
import pluginconf

@pytest.fixture
def pmd():
    return pluginconf.plugin_meta(fn=__file__)

def type_(pmd):
def type_from_dict(pmd):
    assert pmd["type"] == "test"

def version_(pmd):
def version_as_prop(pmd):
    assert pmd.version == "0.1-rc1"

def title_(pmd):
    assert pmd["title"] == "basic PMD"
def title_text(pmd):
    assert pmd.title == "basic PMD"

def doc_(pmd):
def doc_field(pmd):
    assert pmd["doc"] == "This the doc."

# Should probably migrate all to PluginMeta.property access
def test_all_as_props(pmd):
    for key, val in pmd.items():
        assert getattr(pmd, key) == val

Added test/config_listwrapper.py version [c695b3e4ba].





























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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
# type: test
# title: config list wrapper
# description: check if ConfigList() access works
# config:
#   { name: key, value: "test", description: "should be accessible per pmd.config.key.value" }
#   { name: alt, type: bool, value: 1 }
# version: 0.8+
# 
# Do all the settings!

import pytest
import pluginconf

@pytest.fixture
def pmd():
    return pluginconf.plugin_meta(fn=__file__)

def key_path(pmd):
    assert pmd.config.key.value == "test"
                   #  ^^^
                   #  name lookup

def path_type(pmd):
    assert pmd.config.alt.value == '1'

def nonexistent(pmd):
    with pytest.raises(KeyError) as exc:
        assert pmd.config.donothave.help