# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
"""Astroid hooks for the Python 2 GObject introspection bindings.
Helps with understanding everything imported from 'gi.repository'
"""
# pylint:disable=import-error,import-outside-toplevel
import inspect
import itertools
import re
import sys
import warnings
from astroid import nodes
from astroid.builder import AstroidBuilder
from astroid.exceptions import AstroidBuildingError
from astroid.manager import AstroidManager
_inspected_modules = {}
_identifier_re = r"^[A-Za-z_]\w*$"
_special_methods = frozenset(
{
"__lt__",
"__le__",
"__eq__",
"__ne__",
"__ge__",
"__gt__",
"__iter__",
"__getitem__",
"__setitem__",
"__delitem__",
"__len__",
"__bool__",
"__nonzero__",
"__next__",
"__str__",
"__contains__",
"__enter__",
"__exit__",
"__repr__",
"__getattr__",
"__setattr__",
"__delattr__",
"__del__",
"__hash__",
}
)
def _gi_build_stub(parent): # noqa: C901
"""
Inspect the passed module recursively and build stubs for functions,
classes, etc.
"""
classes = {}
functions = {}
constants = {}
methods = {}
for name in dir(parent):
if name.startswith("__") and name not in _special_methods:
continue
# Check if this is a valid name in python
if not re.match(_identifier_re, name):
continue
try:
obj = getattr(parent, name)
except Exception: # pylint: disable=broad-except
# gi.module.IntrospectionModule.__getattr__() can raise all kinds of things
# like ValueError, TypeError, NotImplementedError, RepositoryError, etc
continue
if inspect.isclass(obj):
classes[name] = obj
elif inspect.isfunction(obj) or inspect.isbuiltin(obj):
functions[name] = obj
elif inspect.ismethod(obj) or inspect.ismethoddescriptor(obj):
methods[name] = obj
elif (
str(obj).startswith("