aboutsummaryrefslogtreecommitdiff
path: root/tools/python/mwm/find_feature.py
blob: af94cd91299639f89034d7d7f3b9548ae5263be7 (plain) (blame)
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
import json
from typing import List

from mwm import EnumAsStrEncoder
from mwm import Feature
from mwm import Mwm
from mwm import readable_type


def find_features(path: str, typ: str, string: str) -> List[Feature]:
    features = []
    index = int(string) if typ == "id" else None
    for feature in Mwm(path):
        found = False
        if typ == "n":
            for value in feature.names().values():
                if string in value:
                    found = True
                    break
        elif typ in ("t", "et"):
            for t in feature.types():
                readable_type_ = readable_type(t)
                if readable_type_ == string:
                    found = True
                    break
                elif typ == "t" and string in readable_type_:
                    found = True
                    break
        elif typ == "m":
            for f in feature.metadata():
                if string in f.name:
                    found = True
                    break
        elif typ == "id" and index == feature.index():
            found = True

        if found:
            features.append(feature)

    return features


def find_and_print_features(path: str, typ: str, string: str):
    for feature in find_features(path, typ, string):
        print(
            json.dumps(
                feature.to_json(),
                ensure_ascii=False,
                sort_keys=True,
                cls=EnumAsStrEncoder,
            )
        )