| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2021 The Meson development team |
| 3 |
|
| 4 |
from .generatorbase import GeneratorBase |
| 5 |
from .model import ReferenceManual, Object, Function, DataTypeInfo, Type, ObjectType |
| 6 |
|
| 7 |
from mesonbuild import mlog |
| 8 |
import typing as T |
| 9 |
|
| 10 |
def my_nested() -> T.ContextManager[None]: |
| 11 |
prefix = '|' * mlog.get_log_depth() |
| 12 |
return mlog.nested(prefix) |
| 13 |
|
| 14 |
class GeneratorPrint(GeneratorBase): |
| 15 |
def _types_to_string(self, typ: Type) -> str: |
| 16 |
def _data_type_to_str(dt: DataTypeInfo) -> str: |
| 17 |
if dt.holds: |
| 18 |
return f'{dt.data_type.name}[{self._types_to_string(dt.holds)}]' |
| 19 |
return dt.data_type.name |
| 20 |
return ' | '.join([_data_type_to_str(x) for x in typ.resolved]) |
| 21 |
|
| 22 |
def _generate_function(self, func: Function) -> None: |
| 23 |
mlog.log() |
| 24 |
mlog.log('Function', mlog.bold(func.name)) |
| 25 |
with my_nested(): |
| 26 |
desc = func.description |
| 27 |
if '\n' in desc: |
| 28 |
desc = desc[:desc.index('\n')] |
| 29 |
mlog.log('Description:', mlog.bold(desc)) |
| 30 |
mlog.log('Return type:', mlog.bold(self._types_to_string(func.returns))) |
| 31 |
mlog.log('Pos args: ', mlog.bold(str([x.name for x in func.posargs]))) |
| 32 |
mlog.log('Opt args: ', mlog.bold(str([x.name for x in func.optargs]))) |
| 33 |
mlog.log('Varargs: ', mlog.bold(func.varargs.name if func.varargs is not None else 'null')) |
| 34 |
mlog.log('Kwargs: ', mlog.bold(str(list(func.kwargs.keys())))) |
| 35 |
|
| 36 |
def _generate_object(self, obj: Object) -> None: |
| 37 |
tags = [] |
| 38 |
tags += [{ |
| 39 |
ObjectType.ELEMENTARY: mlog.yellow('[elementary]'), |
| 40 |
ObjectType.BUILTIN: mlog.green('[builtin]'), |
| 41 |
ObjectType.MODULE: mlog.blue('[module]'), |
| 42 |
ObjectType.RETURNED: mlog.cyan('[returned]'), |
| 43 |
}[obj.obj_type]] |
| 44 |
if obj.is_container: |
| 45 |
tags += [mlog.red('[container]')] |
| 46 |
mlog.log() |
| 47 |
mlog.log('Object', mlog.bold(obj.name), *tags) |
| 48 |
with my_nested(): |
| 49 |
desc = obj.description |
| 50 |
if '\n' in desc: |
| 51 |
desc = desc[:desc.index('\n')] |
| 52 |
mlog.log('Description:', mlog.bold(desc)) |
| 53 |
mlog.log('Returned by:', mlog.bold(str([x.name for x in obj.returned_by]))) |
| 54 |
mlog.log('Methods:') |
| 55 |
with my_nested(): |
| 56 |
for m in obj.methods: |
| 57 |
self._generate_function(m) |
| 58 |
|
| 59 |
def generate(self) -> None: |
| 60 |
mlog.log('\n\n', mlog.bold('=== Functions ==='), '\n') |
| 61 |
for f in self.functions: |
| 62 |
self._generate_function(f) |
| 63 |
mlog.log('\n\n', mlog.bold('=== Elementary ==='), '\n') |
| 64 |
for obj in self.elementary: |
| 65 |
self._generate_object(obj) |
| 66 |
mlog.log('\n\n', mlog.bold('=== Builtins ==='), '\n') |
| 67 |
for obj in self.builtins: |
| 68 |
self._generate_object(obj) |
| 69 |
mlog.log('\n\n', mlog.bold('=== Returned objects ==='), '\n') |
| 70 |
for obj in self.returned: |
| 71 |
self._generate_object(obj) |
| 72 |
mlog.log('\n\n', mlog.bold('=== Modules ==='), '\n') |
| 73 |
for obj in self.modules: |
| 74 |
self._generate_object(obj) |
| 75 |
for mod_obj in self.extract_returned_by_module(obj): |
| 76 |
self._generate_object(mod_obj) |