Files
revng-revng/python/revng/api/kind.py
Giacomo Vercesi 7231ee607a Introduce PipelineC wrapper for Python
Add the revng.api module: a wrapper around the PipelineC API.
2022-04-26 15:59:35 +02:00

42 lines
1021 B
Python

#
# This file is distributed under the MIT License. See LICENSE.md for details.
#
from typing import Dict, Optional
from ._capi import _api, ffi
from .rank import Rank
from .utils import make_python_string
class Kind:
def __init__(self, kind):
self._kind = kind
@property
def name(self) -> str:
name = _api.rp_kind_get_name(self._kind)
return make_python_string(name)
@property
def rank(self) -> Optional[Rank]:
_rank = _api.rp_kind_get_rank(self._kind)
return Rank(_rank) if _rank != ffi.NULL else None
def get_parent(self) -> Optional["Kind"]:
_kind = _api.rp_kind_get_parent(self._kind)
return Kind(_kind) if _kind != ffi.NULL else None
def as_dict(self) -> Dict[str, str]:
ret = {"name": self.name}
rank = self.rank
if rank is not None:
ret["rank"] = rank.name
parent = self.get_parent()
if parent is not None:
ret["parent"] = parent.name
return ret