Пример #1
0
Файл: node.py Проект: tony/mkapi
def is_member(obj: Any, name: str = "", sourcefiles: List[str] = None) -> int:
    """Returns an integer thats indicates if `obj` is a member or not.

    * $-1$ : Is not a member.
    * $>0$ : Is a member. If the value is larger than 0, `obj` is defined
        in different file and the value is corresponding to the index of unique
        source files of superclasses.

    Args:
        name: Object name.
        obj: Object
        sourcefiles: Parent source files. If the parent is a class,
            those of the superclasses should be included in the order
            of `mro()`.
    """
    if name == "":
        name = obj.__name__
    obj = get_origin(obj)
    if name in ["__func__", "__self__", "__base__", "__bases__"]:
        return -1
    if name.startswith("_"):
        if not name.startswith("__") or not name.endswith("__"):
            return -1
    if not get_kind(obj):
        return -1
    try:
        sourcefile = inspect.getsourcefile(obj)
    except TypeError:
        return -1
    if not sourcefiles:
        return 0
    for sourcefile_index, parent_sourcefile in enumerate(sourcefiles):
        if sourcefile == parent_sourcefile:
            return sourcefile_index
    return -1
Пример #2
0
 def get_kind(self) -> str:
     if inspect.ismodule(self.obj):
         if self.sourcefile.endswith("__init__.py"):
             return "package"
         else:
             return "module"
     if isinstance(self.obj, property):
         if self.obj.fset:
             return "readwrite_property"
         else:
             return "readonly_property"
     return get_kind(get_origin(self.obj))
Пример #3
0
 def __post_init__(self):
     obj = get_origin(self.obj)
     self.sourcefile, self.lineno = get_sourcefile_and_lineno(obj)
     prefix, name = split_prefix_and_name(obj)
     qualname = get_qualname(obj)
     kind = self.get_kind()
     signature = get_signature(obj)
     self.object = Object(
         prefix=prefix, name=name, qualname=qualname, kind=kind, signature=signature,
     )
     self.docstring = get_docstring(obj)
     self.obj = obj
     self.members = self.get_members()
     for member in self.members:
         member.parent = self
Пример #4
0
 def get_kind(self) -> str:
     if inspect.ismodule(self.obj):
         if self.sourcefile.endswith("__init__.py"):
             return "package"
         else:
             return "module"
     abstract = is_abstract(self.obj)
     if isinstance(self.obj, property):
         if self.obj.fset:
             kind = "readwrite property"
         else:
             kind = "readonly property"
     else:
         kind = get_kind(get_origin(self.obj))
     if abstract:
         return "abstract " + kind
     else:
         return kind