modm_data.owl.stmicro.device

 1# Copyright 2022, Niklas Hauser
 2# SPDX-License-Identifier: MPL-2.0
 3
 4from ...utils import cache_path
 5import modm_data.owl.stmicro as owl
 6import json
 7
 8
 9_OWL_FILES = None
10_OWL_MAPPING = None
11_OWL_MAPPING_FILE = cache_path("stmicro-owl.json")
12
13
14def owls():
15    global _OWL_FILES
16    if _OWL_FILES is None:
17        _OWL_FILES = cache_path("stmicro-owl/").glob("*.owl")
18        _OWL_FILES = [ds.stem for ds in _OWL_FILES]
19    return _OWL_FILES
20
21
22def owl_devices():
23    global _OWL_MAPPING, _OWL_MAPPING_FILE
24    if _OWL_MAPPING is None:
25        if not _OWL_MAPPING_FILE.exists():
26            _OWL_MAPPING = {}
27            for ds in owls():
28                if ds.startswith("DS"):
29                    owl.store.load(ds)
30                    for name in set(i.name for i in owl.Device.instances()):
31                        _OWL_MAPPING[name] = ds
32
33            _OWL_MAPPING_FILE.parent.mkdir(parents=True, exist_ok=True)
34            with _OWL_MAPPING_FILE.open('w', encoding='utf-8') as fh:
35                json.dump(_OWL_MAPPING, fh, indent=4)
36        else:
37            with _OWL_MAPPING_FILE.open('r', encoding='utf-8') as fh:
38                _OWL_MAPPING = json.load(fh)
39    return _OWL_MAPPING
40
41
42def owl_device(device):
43    return owl_devices().get(device.string)
44
45
46def load_owl_device(device) -> bool:
47    if (dev := owl_devices().get(device.string)) is not None:
48        owl.store.load(dev)
49        return True
50    return False
def owls():
16def owls():
17    global _OWL_FILES
18    if _OWL_FILES is None:
19        _OWL_FILES = cache_path("stmicro-owl/").glob("*.owl")
20        _OWL_FILES = [ds.stem for ds in _OWL_FILES]
21    return _OWL_FILES
def owl_devices():
24def owl_devices():
25    global _OWL_MAPPING, _OWL_MAPPING_FILE
26    if _OWL_MAPPING is None:
27        if not _OWL_MAPPING_FILE.exists():
28            _OWL_MAPPING = {}
29            for ds in owls():
30                if ds.startswith("DS"):
31                    owl.store.load(ds)
32                    for name in set(i.name for i in owl.Device.instances()):
33                        _OWL_MAPPING[name] = ds
34
35            _OWL_MAPPING_FILE.parent.mkdir(parents=True, exist_ok=True)
36            with _OWL_MAPPING_FILE.open('w', encoding='utf-8') as fh:
37                json.dump(_OWL_MAPPING, fh, indent=4)
38        else:
39            with _OWL_MAPPING_FILE.open('r', encoding='utf-8') as fh:
40                _OWL_MAPPING = json.load(fh)
41    return _OWL_MAPPING
def owl_device(device):
44def owl_device(device):
45    return owl_devices().get(device.string)
def load_owl_device(device) -> bool:
48def load_owl_device(device) -> bool:
49    if (dev := owl_devices().get(device.string)) is not None:
50        owl.store.load(dev)
51        return True
52    return False