aboutsummaryrefslogtreecommitdiff
path: root/docs/refman/generatorbase.py
blob: 08ce4920d6f782baef68e4acf06d9014fd9d99c0 (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
# Copyright 2021 The Meson development team

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABCMeta, abstractmethod
import typing as T

from .model import ReferenceManual, Function, Method, Object, ObjectType, NamedObject

_N = T.TypeVar('_N', bound=NamedObject)

class GeneratorBase(metaclass=ABCMeta):
    def __init__(self, manual: ReferenceManual) -> None:
        self.manual = manual

    @abstractmethod
    def generate(self) -> None:
        pass

    @staticmethod
    def brief(raw: _N) -> str:
        desc_lines = raw.description.split('\n')
        brief = desc_lines[0]
        if '.' in brief and '[[' not in brief:
            brief = brief[:brief.index('.')]
        return brief.strip()

    @staticmethod
    def sorted_and_filtered(raw: T.List[_N]) -> T.List[_N]:
        def key_fn(fn: NamedObject) -> str:
            if isinstance(fn, Method):
                return f'1_{fn.obj.name}.{fn.name}'
            return f'0_{fn.name}'
        return sorted([x for x in raw if not x.hidden], key=key_fn)

    @staticmethod
    def _extract_meson_version() -> str:
        from mesonbuild.coredata import version
        return version

    @property
    def functions(self) -> T.List[Function]:
        return GeneratorBase.sorted_and_filtered(self.manual.functions)

    @property
    def objects(self) -> T.List[Object]:
        return GeneratorBase.sorted_and_filtered(self.manual.objects)

    @property
    def elementary(self) -> T.List[Object]:
        return [x for x in self.objects if x.obj_type == ObjectType.ELEMENTARY]

    @property
    def builtins(self) -> T.List[Object]:
        return [x for x in self.objects if x.obj_type == ObjectType.BUILTIN]

    @property
    def returned(self) -> T.List[Object]:
        return [x for x in self.objects if x.obj_type == ObjectType.RETURNED and x.defined_by_module is None]

    @property
    def modules(self) -> T.List[Object]:
        return [x for x in self.objects if x.obj_type == ObjectType.MODULE]

    def extract_returned_by_module(self, module: Object) -> T.List[Object]:
        return [x for x in self.objects if x.obj_type == ObjectType.RETURNED and x.defined_by_module is module]