Esempio n. 1
0
def generate_pyi_for_proxy(
    cls: type,
    progname: str,
    source_path: Path,
    destination_path: Path,
    ignore_output: bool,
    ignore_items: set,
):

    imports = []
    read_imports = False
    with open(source_path) as read_file:
        for line in read_file:
            if line.startswith("# ### this file stubs are generated by"):
                read_imports = True
            elif line.startswith("### end imports ###"):
                read_imports = False
                break
            elif read_imports:
                imports.append(line.rstrip())

    with open(destination_path, "w") as buf:
        printer = PythonPrinter(buf)

        printer.writeline(
            f"# ### this file stubs are generated by {progname} "
            "- do not edit ###"
        )
        for line in imports:
            buf.write(line + "\n")
        printer.writeline("### end imports ###")
        buf.write("\n\n")

        for name in dir(cls):
            if name.startswith("_") or name in ignore_items:
                continue
            meth = getattr(cls, name)
            if callable(meth):
                _generate_stub_for_meth(cls, name, printer)
            else:
                _generate_stub_for_attr(cls, name, printer)

        printer.close()

    console_scripts(
        str(destination_path),
        {"entrypoint": "zimports", "options": "-e"},
        ignore_output=ignore_output,
    )
    # note that we do not distribute pyproject.toml with the distribution
    # right now due to user complaints, so we can't refer to it here because
    # this all has to run as part of the test suite
    console_scripts(
        str(destination_path),
        {"entrypoint": "black", "options": "-l79"},
        ignore_output=ignore_output,
    )
Esempio n. 2
0
    def test_generate_normal(self):
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.writeline("import lala")
        printer.writeline("for x in foo:")
        printer.writeline("print x")
        printer.writeline(None)
        printer.writeline("print y")
        assert stream.getvalue() == \
"""import lala
Esempio n. 3
0
    def test_generate_normal(self):
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.writeline("import lala")
        printer.writeline("for x in foo:")
        printer.writeline("print x")
        printer.writeline(None)
        printer.writeline("print y")
        assert stream.getvalue() == \
"""import lala
Esempio n. 4
0
def _render_cmd_body(
    op_container: "ops.OpContainer",
    autogen_context: "AutogenContext",
) -> str:

    buf = StringIO()
    printer = PythonPrinter(buf)

    printer.writeline(
        "# ### commands auto generated by Alembic - please adjust! ###")

    has_lines = False
    for op in op_container.ops:
        lines = render_op(autogen_context, op)
        has_lines = has_lines or bool(lines)

        for line in lines:
            printer.writeline(line)

    if not has_lines:
        printer.writeline("pass")

    printer.writeline("# ### end Alembic commands ###")

    return buf.getvalue()
Esempio n. 5
0
    def test_false_unindentor(self):
        stream = StringIO()
        printer = PythonPrinter(stream)
        for line in [
                "try:", "elsemyvar = 12", "if True:", "print 'hi'", None,
                "finally:", "dosomething", None
        ]:
            printer.writeline(line)

        assert stream.getvalue() == \
"""try:
    elsemyvar = 12
    if True:
        print 'hi'
finally:
    dosomething
"""    , stream.getvalue()
Esempio n. 6
0
    def test_generate_combo(self):
        block = \
"""
                x = 5 +6
                if x > 7:
                    for y in range(1,5):
                        print "<td>%s</td>" % y
                    print "hi"
                print "there"
                foo(lala)
        """
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.writeline("import lala")
        printer.writeline("for x in foo:")
        printer.writeline("print x")
        printer.write_indented_block(block)
        printer.writeline(None)
        printer.writeline("print y")
        printer.close()
        #print "->" + stream.getvalue().replace(' ', '#') + "<-"
        assert stream.getvalue() == \
"""import lala
Esempio n. 7
0
    def test_false_unindentor(self):
        stream = StringIO()
        printer = PythonPrinter(stream)
        for line in [
            "try:",
            "elsemyvar = 12",
            "if True:",
            "print 'hi'",
            None,
            "finally:",
            "dosomething",
            None
        ]:
            printer.writeline(line)
        
        assert stream.getvalue() == \
"""try:
    elsemyvar = 12
    if True:
        print 'hi'
finally:
    dosomething
"""    , stream.getvalue()
Esempio n. 8
0
    def test_generate_combo(self):
        block = \
"""
                x = 5 +6
                if x > 7:
                    for y in range(1,5):
                        print "<td>%s</td>" % y
                    print "hi"
                print "there"
                foo(lala)
        """
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.writeline("import lala")
        printer.writeline("for x in foo:")
        printer.writeline("print x")
        printer.write_indented_block(block)
        printer.writeline(None)
        printer.writeline("print y")
        printer.close()
        #print "->" + stream.getvalue().replace(' ', '#') + "<-"
        assert stream.getvalue() == \
"""import lala
Esempio n. 9
0
def _render_cmd_body(op_container, autogen_context):

    buf = StringIO()
    printer = PythonPrinter(buf)

    printer.writeline("### commands auto generated by Alembic - " "please adjust! ###")

    if not op_container.ops:
        printer.writeline("pass")
    else:
        for op in op_container.ops:
            lines = render_op(autogen_context, op)

            for line in lines:
                printer.writeline(line)

    printer.writeline("### end Alembic commands ###")

    return buf.getvalue()
Esempio n. 10
0
def _render_cmd_body(op_container, autogen_context):
    buf = StringIO()
    printer = PythonPrinter(buf)

    printer.writeline("### commands auto generated by Alembic - "
                      "please adjust! ###")

    if not op_container.ops:
        printer.writeline("pass")
    else:
        for op in op_container.ops:
            lines = render_op(autogen_context, op)

            for line in lines:
                printer.writeline(line)

    printer.writeline("### end Alembic commands ###")

    return buf.getvalue()
Esempio n. 11
0
def _render_cmd_body(fn, diffs, autogen_context):

    buf = StringIO()
    printer = PythonPrinter(buf)

    printer.writeline("### commands auto generated by Alembic - "
                      "please adjust! ###")

    for line in fn(diffs, autogen_context):
        printer.writeline(line)

    printer.writeline("### end Alembic commands ###")

    return buf.getvalue()
Esempio n. 12
0
def _render_cmd_body(fn, diffs, autogen_context):

    buf = StringIO()
    printer = PythonPrinter(buf)

    printer.writeline(
        "### commands auto generated by Alembic - "
        "please adjust! ###"
    )

    for line in fn(diffs, autogen_context):
        printer.writeline(line)

    printer.writeline("### end Alembic commands ###")

    return buf.getvalue()
Esempio n. 13
0
def generate_pyi_for_proxy(
    cls: type,
    progname: str,
    source_path: Path,
    destination_path: Path,
    ignore_output: bool,
    ignore_items: set,
):
    if sys.version_info < (3, 9):
        raise RuntimeError("This script must be run with Python 3.9 or higher")

    # When using an absolute path on windows, this will generate the correct
    # relative path that shall be written to the top comment of the pyi file.
    if Path(progname).is_absolute():
        progname = Path(progname).relative_to(Path().cwd()).as_posix()

    imports = []
    read_imports = False
    with open(source_path) as read_file:
        for line in read_file:
            if line.startswith("# ### this file stubs are generated by"):
                read_imports = True
            elif line.startswith("### end imports ###"):
                read_imports = False
                break
            elif read_imports:
                imports.append(line.rstrip())

    with open(destination_path, "w") as buf:
        printer = PythonPrinter(buf)

        printer.writeline(f"# ### this file stubs are generated by {progname} "
                          "- do not edit ###")
        for line in imports:
            buf.write(line + "\n")
        printer.writeline("### end imports ###")
        buf.write("\n\n")

        for name in dir(cls):
            if name.startswith("_") or name in ignore_items:
                continue
            meth = getattr(cls, name)
            if callable(meth):
                _generate_stub_for_meth(cls, name, printer)
            else:
                _generate_stub_for_attr(cls, name, printer)

        printer.close()

    console_scripts(
        str(destination_path),
        {
            "entrypoint": "zimports",
            "options": "-e"
        },
        ignore_output=ignore_output,
    )
    # note that we do not distribute pyproject.toml with the distribution
    # right now due to user complaints, so we can't refer to it here because
    # this all has to run as part of the test suite
    console_scripts(
        str(destination_path),
        {
            "entrypoint": "black",
            "options": "-l79"
        },
        ignore_output=ignore_output,
    )