def _gi_build_stub(parent):
classes = {}<EOL>functions = {}<EOL>constants = {}<EOL>methods = {}<EOL>for name in dir(parent):<EOL><INDENT>if name.startswith("<STR_LIT>"):<EOL><INDENT>continue<EOL><DEDENT>if not re.match(_identifier_re, name):<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>obj = getattr(parent, name)<EOL><DEDENT>except:<EOL><INDENT>continue<EOL><DEDENT>if inspect.isclass(obj):<EOL><INDENT>classes[name] = obj<EOL><DEDENT>elif inspect.isfunction(obj) or inspect.isbuiltin(obj):<EOL><INDENT>functions[name] = obj<EOL><DEDENT>elif inspect.ismethod(obj) or inspect.ismethoddescriptor(obj):<EOL><INDENT>methods[name] = obj<EOL><DEDENT>elif (<EOL>str(obj).startswith("<STR_LIT>")<EOL>or str(obj).startswith("<STR_LIT>")<EOL>or str(obj).startswith("<STR_LIT>")<EOL>or inspect.isdatadescriptor(obj)<EOL>):<EOL><INDENT>constants[name] = <NUM_LIT:0><EOL><DEDENT>elif isinstance(obj, (int, str)):<EOL><INDENT>constants[name] = obj<EOL><DEDENT>elif callable(obj):<EOL><INDENT>functions[name] = obj<EOL><DEDENT>else:<EOL><INDENT>constants[name] = <NUM_LIT:0><EOL><DEDENT><DEDENT>ret = "<STR_LIT>"<EOL>if constants:<EOL><INDENT>ret += "<STR_LIT>" % parent.__name__<EOL><DEDENT>for name in sorted(constants):<EOL><INDENT>if name[<NUM_LIT:0>].isdigit():<EOL><INDENT>continue<EOL><DEDENT>val = constants[name]<EOL>strval = str(val)<EOL>if isinstance(val, str):<EOL><INDENT>strval = '<STR_LIT>' % str(val).replace("<STR_LIT:\\>", "<STR_LIT>")<EOL><DEDENT>ret += "<STR_LIT>" % (name, strval)<EOL><DEDENT>if ret:<EOL><INDENT>ret += "<STR_LIT>"<EOL><DEDENT>if functions:<EOL><INDENT>ret += "<STR_LIT>" % parent.__name__<EOL><DEDENT>for name in sorted(functions):<EOL><INDENT>ret += "<STR_LIT>" % name<EOL>ret += "<STR_LIT>"<EOL><DEDENT>if ret:<EOL><INDENT>ret += "<STR_LIT>"<EOL><DEDENT>if methods:<EOL><INDENT>ret += "<STR_LIT>" % parent.__name__<EOL><DEDENT>for name in sorted(methods):<EOL><INDENT>ret += "<STR_LIT>" % name<EOL>ret += "<STR_LIT>"<EOL><DEDENT>if ret:<EOL><INDENT>ret += "<STR_LIT>"<EOL><DEDENT>if classes:<EOL><INDENT>ret += "<STR_LIT>" % parent.__name__<EOL><DEDENT>for name, obj in sorted(classes.items()):<EOL><INDENT>base = "<STR_LIT:object>"<EOL>if issubclass(obj, Exception):<EOL><INDENT>base = "<STR_LIT>"<EOL><DEDENT>ret += "<STR_LIT>" % (name, base)<EOL>classret = _gi_build_stub(obj)<EOL>if not classret:<EOL><INDENT>classret = "<STR_LIT>"<EOL><DEDENT>for line in classret.splitlines():<EOL><INDENT>ret += "<STR_LIT:U+0020>" + line + "<STR_LIT:\n>"<EOL><DEDENT>ret += "<STR_LIT:\n>"<EOL><DEDENT>return ret<EOL>
Inspect the passed module recursively and build stubs for functions,
classes, etc.