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, )
def test_generate_adjusted(self): block = """ x = 5 +6 if x > 7: for y in range(1,5): print "<td>%s</td>" % y """ stream = StringIO() printer = PythonPrinter(stream) printer.write_indented_block(block) printer.close() #print stream.getvalue() assert stream.getvalue() == \ """
def test_generate_adjusted(self): block = """ x = 5 +6 if x > 7: for y in range(1,5): print "<td>%s</td>" % y """ stream = StringIO() printer = PythonPrinter(stream) printer.write_indented_block(block) printer.close() #print stream.getvalue() assert stream.getvalue() == \ """
def test_backslash_line(self): block = \ """ # comment if test: if (lala + hoho) + \\ (foobar + blat) == 5: print "hi" print "more indent" """ stream = StringIO() printer = PythonPrinter(stream) printer.write_indented_block(block) printer.close() assert stream.getvalue() == \ """
def test_backslash_line(self): block = \ """ # comment if test: if (lala + hoho) + \\ (foobar + blat) == 5: print "hi" print "more indent" """ stream = StringIO() printer = PythonPrinter(stream) printer.write_indented_block(block) printer.close() assert stream.getvalue() == \ """
def test_multi_line(self): block = \ """ if test: print ''' this is a block of stuff. this is more stuff in the block. and more block. ''' do_more_stuff(g) """ stream = StringIO() printer = PythonPrinter(stream) printer.write_indented_block(block) printer.close() #print stream.getvalue() assert stream.getvalue() == \ """
def test_multi_line(self): block = \ """ if test: print ''' this is a block of stuff. this is more stuff in the block. and more block. ''' do_more_stuff(g) """ stream = StringIO() printer = PythonPrinter(stream) printer.write_indented_block(block) printer.close() #print stream.getvalue() assert stream.getvalue() == \ """
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
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
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, )