From 6d0f14e1a5b7a9a18eadffe00562f5317add494f Mon Sep 17 00:00:00 2001 From: Nicolas Elie <40382614+n-elie@users.noreply.github.com> Date: Thu, 2 Jul 2020 07:47:18 +0200 Subject: [PATCH] Generate Python stubs file for linters (#214) * Generate Python stubs file for linters * Sort members of ads namespace in generated __init__.py --- setup.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/setup.py b/setup.py index f093d89..87e8b15 100644 --- a/setup.py +++ b/setup.py @@ -168,6 +168,7 @@ class build_ext(sipdistutils.build_ext): return os.path.join(sys.prefix, 'sip', 'PyQt5') def _sip_compile(self, sip_bin, source, sbf): + pyi = os.path.join(self.build_lib, "PyQtAds", "QtAds", "ads.pyi") cmd = [sip_bin] if hasattr(self, 'sip_opts'): cmd += self.sip_opts @@ -180,11 +181,20 @@ class build_ext(sipdistutils.build_ext): "-I", self.inc_dir, "-c", self._sip_output_dir(), "-b", sbf, + "-y", pyi, "-w", "-o"] cmd += shlex.split(self.pyqt_sip_flags) # use same SIP flags as for PyQt5 cmd.append(source) self.spawn(cmd) + + if os.path.exists(pyi): + with open(pyi) as f: + content = f.readlines() + with open(pyi, "w") as f: + for line in content: + if not line.startswith("class ads"): + f.write(line) def swig_sources (self, sources, extension=None): if not self.extensions: @@ -293,6 +303,17 @@ class build_ext(sipdistutils.build_ext): ext.sources.append(out_file) sipdistutils.build_ext.build_extension(self, ext) + + import inspect + sys.path.append(os.path.join(self.build_lib, 'PyQtAds', 'QtAds')) + import ads + + with open(os.path.join(self.build_lib, 'PyQtAds', 'QtAds', '__init__.py'), 'w') as f: + f.write('from .._version import *\n') + f.write('from .ads import ads\n') + for name, member in sorted(inspect.getmembers(ads.ads)): + if not name.startswith('_'): + f.write('{0} = ads.{0}\n'.format(name)) class ProcessResourceCommand(cmd.Command):