Esempio n. 1
0
def generate_pyi(import_name, outpath, options):
    """
    Generates a .pyi file.
    """
    plainname = import_name.split(".")[-1]
    outfilepath = os.path.join(outpath, plainname + ".pyi")
    top = __import__(import_name)
    obj = getattr(top, plainname)
    if not getattr(obj, "__file__", None) or os.path.isdir(obj.__file__):
        raise ModuleNotFoundError("We do not accept a namespace as module "
                                  "{plainname}".format(**locals()))
    module = sys.modules[import_name]

    outfile = io.StringIO()
    fmt = Formatter(outfile)
    fmt.print(get_license_text())  # which has encoding, already
    need_imports = not USE_PEP563
    if USE_PEP563:
        fmt.print("from __future__ import annotations")
        fmt.print()
    fmt.print(
        dedent('''\
        """
        This file contains the exact signatures for all functions in module
        {import_name}, except for defaults which are replaced by "...".
        """
        '''.format(**locals())))
    HintingEnumerator(fmt).module(import_name)
    fmt.print()
    fmt.print("# eof")
    # Postprocess: resolve the imports
    with open(outfilepath, "w") as realfile:
        wr = Writer(realfile)
        outfile.seek(0)
        while True:
            line = outfile.readline()
            if not line:
                break
            line = line.rstrip()
            # we remove the IMPORTS marker and insert imports if needed
            if line == "IMPORTS":
                if need_imports:
                    for mod_name in find_imports(outfile.getvalue()):
                        imp = "PySide2." + mod_name
                        if imp != import_name:
                            wr.print("import " + imp)
                wr.print("import " + import_name)
                wr.print()
                wr.print()
            else:
                wr.print(line)
    logger.info("Generated: {outfilepath}".format(**locals()))
    if is_py3 and (options.check or is_ci):
        # Python 3: We can check the file directly if the syntax is ok.
        subprocess.check_output([sys.executable, outfilepath])
Esempio n. 2
0
def generate_pyi(import_name, outpath, options):
    """
    Generates a .pyi file.

    Returns 1 If the result is valid, -1 if the result existed already
    and was skipped, else 0.

    This function will get called during a PySide build, and many concurrent
    process might try to create .pyi files. We let only one process at a
    time work on these files, but it will still be different processes which
    do the work.
    """
    pid = os.getpid()
    plainname = import_name.split(".")[-1]
    outfilepath = os.path.join(outpath, plainname + ".pyi")
    if options.skip and os.path.exists(outfilepath):
        logger.debug("{pid}:Skipped existing: {op}".format(
            op=os.path.basename(outfilepath), **locals()))
        return -1

    try:
        top = __import__(import_name)
        obj = getattr(top, plainname)
        if not getattr(obj, "__file__", None) or os.path.isdir(obj.__file__):
            raise ImportError(
                "We do not accept a namespace as module {plainname}".format(
                    **locals()))
        module = sys.modules[import_name]

        outfile = io.StringIO()
        fmt = Formatter(outfile)
        enu = HintingEnumerator(fmt)
        fmt.print(get_license_text())  # which has encoding, already
        need_imports = not USE_PEP563
        if USE_PEP563:
            fmt.print("from __future__ import annotations")
            fmt.print()
        fmt.print(
            dedent('''\
            """
            This file contains the exact signatures for all functions in module
            {import_name}, except for defaults which are replaced by "...".
            """
            '''.format(**locals())))
        enu.module(import_name)
        fmt.print()
        fmt.print("# eof")

    except ImportError as e:
        logger.debug(
            "{pid}:Import problem with module {plainname}: {e}".format(
                **locals()))
        return 0

    with open(outfilepath, "w") as realfile:
        wr = Writer(realfile)
        outfile.seek(0)
        while True:
            line = outfile.readline()
            if not line:
                break
            line = line.rstrip()
            # we remove the IMPORTS marker and insert imports if needed
            if line == "IMPORTS":
                if need_imports:
                    for mod_name in find_imports(outfile.getvalue()):
                        imp = "PySide2." + mod_name
                        if imp != import_name:
                            wr.print("import " + imp)
                wr.print("import " + import_name)
                wr.print()
                wr.print()
            else:
                wr.print(line)
    logger.info("Generated: {outfilepath}".format(**locals()))
    if sys.version_info[0] == 3:
        # Python 3: We can check the file directly if the syntax is ok.
        subprocess.check_output([sys.executable, outfilepath])
    return 1