| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2021 The Meson development team |
| 3 |
|
| 4 |
|
| 5 |
from abc import ABCMeta, abstractmethod |
| 6 |
import typing as T |
| 7 |
|
| 8 |
from .model import ReferenceManual, Function, Method, Object, ObjectType, NamedObject |
| 9 |
|
| 10 |
_N = T.TypeVar('_N', bound=NamedObject) |
| 11 |
|
| 12 |
class GeneratorBase(metaclass=ABCMeta): |
| 13 |
def __init__(self, manual: ReferenceManual) -> None: |
| 14 |
self.manual = manual |
| 15 |
|
| 16 |
@abstractmethod |
| 17 |
def generate(self) -> None: |
| 18 |
pass |
| 19 |
|
| 20 |
@staticmethod |
| 21 |
def brief(raw: _N) -> str: |
| 22 |
desc_lines = raw.description.split('\n') |
| 23 |
brief = desc_lines[0] |
| 24 |
if '.' in brief and '[[' not in brief: |
| 25 |
brief = brief[:brief.index('.')] |
| 26 |
return brief.strip() |
| 27 |
|
| 28 |
@staticmethod |
| 29 |
def sorted_and_filtered(raw: T.List[_N]) -> T.List[_N]: |
| 30 |
def key_fn(fn: NamedObject) -> str: |
| 31 |
if isinstance(fn, Method): |
| 32 |
return f'1_{fn.obj.name}.{fn.name}' |
| 33 |
return f'0_{fn.name}' |
| 34 |
return sorted([x for x in raw if not x.hidden], key=key_fn) |
| 35 |
|
| 36 |
@staticmethod |
| 37 |
def _extract_meson_version() -> str: |
| 38 |
from mesonbuild.coredata import version |
| 39 |
return version |
| 40 |
|
| 41 |
@property |
| 42 |
def functions(self) -> T.List[Function]: |
| 43 |
return GeneratorBase.sorted_and_filtered(self.manual.functions) |
| 44 |
|
| 45 |
@property |
| 46 |
def objects(self) -> T.List[Object]: |
| 47 |
return GeneratorBase.sorted_and_filtered(self.manual.objects) |
| 48 |
|
| 49 |
@property |
| 50 |
def elementary(self) -> T.List[Object]: |
| 51 |
return [x for x in self.objects if x.obj_type == ObjectType.ELEMENTARY] |
| 52 |
|
| 53 |
@property |
| 54 |
def builtins(self) -> T.List[Object]: |
| 55 |
return [x for x in self.objects if x.obj_type == ObjectType.BUILTIN] |
| 56 |
|
| 57 |
@property |
| 58 |
def returned(self) -> T.List[Object]: |
| 59 |
return [x for x in self.objects if x.obj_type == ObjectType.RETURNED and x.defined_by_module is None] |
| 60 |
|
| 61 |
@property |
| 62 |
def modules(self) -> T.List[Object]: |
| 63 |
return [x for x in self.objects if x.obj_type == ObjectType.MODULE] |
| 64 |
|
| 65 |
def extract_returned_by_module(self, module: Object) -> T.List[Object]: |
| 66 |
return [x for x in self.objects if x.obj_type == ObjectType.RETURNED and x.defined_by_module is module] |