Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove for_stub=True conditional branch from the tools.codegenerator.ImportedNamespaces.getvalue #489

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions comtypes/tools/codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,21 +1388,6 @@ def add(self, name1, name2=None, symbols=None):
... 'Decimal', 'GUID', 'COMMETHOD', 'DISPMETHOD', 'IUnknown',
... 'dispid', 'CoClass', 'BSTR', 'DISPPROPERTY'
... }
>>> print(imports.getvalue(for_stub=True))
from ctypes import *
import datetime
from decimal import Decimal as Decimal
from comtypes import (
BSTR as BSTR,
CoClass as CoClass,
COMMETHOD as COMMETHOD,
dispid as dispid,
DISPMETHOD as DISPMETHOD,
DISPPROPERTY as DISPPROPERTY,
GUID as GUID,
IUnknown as IUnknown,
)
import ctypes.wintypes
"""
if name2 is None:
import_ = name1
Expand Down Expand Up @@ -1448,25 +1433,19 @@ def get_symbols(self) -> Set[str]:
names.add(key)
return names

def _make_line(self, from_, imports, for_stub):
if for_stub:
import_ = ", ".join("%s as %s" % (n, n) for n in imports)
else:
import_ = ", ".join(imports)
def _make_line(self, from_, imports):
import_ = ", ".join(imports)
code = "from %s import %s" % (from_, import_)
if len(code) <= 80:
return code
if for_stub:
import_ = "\n".join(" %s as %s," % (n, n) for n in imports)
else:
wrapper = textwrap.TextWrapper(
subsequent_indent=" ", initial_indent=" ", break_long_words=False
)
import_ = "\n".join(wrapper.wrap(import_))
wrapper = textwrap.TextWrapper(
subsequent_indent=" ", initial_indent=" ", break_long_words=False
)
import_ = "\n".join(wrapper.wrap(import_))
code = "from %s import (\n%s\n)" % (from_, import_)
return code

def getvalue(self, for_stub=False):
def getvalue(self):
ns = {}
lines = []
for key, val in self.data.items():
Expand All @@ -1481,7 +1460,7 @@ def getvalue(self, for_stub=False):
lines.append("import %s" % key)
else:
names = sorted(val, key=lambda s: s.lower())
lines.append(self._make_line(key, names, for_stub=for_stub))
lines.append(self._make_line(key, names))
return "\n".join(lines)


Expand Down