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

Small refactoring for RegistryEntries. #731

Merged
merged 2 commits into from
Jan 4, 2025
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
51 changes: 20 additions & 31 deletions comtypes/server/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,6 @@ def _get_serverdll():
class RegistryEntries(object):
def __init__(self, cls):
self._cls = cls
self._table = []

def _add(self, rootkey: int, subkey: str, name: str, value: str) -> None:
self._table.append((rootkey, subkey, name, value))

def _get_full_classname(self, cls):
"""Return <modulename>.<classname> for 'cls'."""
Expand Down Expand Up @@ -285,36 +281,36 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
)
if reg_desc:
reg_desc = reg_desc.replace(".", " ")
self._add(HKCR, f"CLSID\\{reg_clsid}", "", reg_desc)
yield (HKCR, f"CLSID\\{reg_clsid}", "", reg_desc)

reg_progid = getattr(cls, "_reg_progid_", None)
if reg_progid:
# for ProgIDFromCLSID:
self._add(HKCR, f"CLSID\\{reg_clsid}\\ProgID", "", reg_progid) # 1
yield (HKCR, f"CLSID\\{reg_clsid}\\ProgID", "", reg_progid) # 1

# for CLSIDFromProgID
if reg_desc:
self._add(HKCR, reg_progid, "", reg_desc) # 2
self._add(HKCR, f"{reg_progid}\\CLSID", "", reg_clsid) # 3
yield (HKCR, reg_progid, "", reg_desc) # 2
yield (HKCR, f"{reg_progid}\\CLSID", "", reg_clsid) # 3

reg_novers_progid = getattr(cls, "_reg_novers_progid_", None)
if reg_novers_progid:
self._add(
yield (
HKCR,
f"CLSID\\{reg_clsid}\\VersionIndependentProgID", # 1a
"",
reg_novers_progid,
)
if reg_desc:
self._add(HKCR, reg_novers_progid, "", reg_desc) # 2a
self._add(HKCR, f"{reg_novers_progid}\\CurVer", "", reg_progid) #
self._add(HKCR, f"{reg_novers_progid}\\CLSID", "", reg_clsid) # 3a
yield (HKCR, reg_novers_progid, "", reg_desc) # 2a
yield (HKCR, f"{reg_novers_progid}\\CurVer", "", reg_progid) #
yield (HKCR, f"{reg_novers_progid}\\CLSID", "", reg_clsid) # 3a

clsctx = getattr(cls, "_reg_clsctx_", 0)
clsctx: int = getattr(cls, "_reg_clsctx_", 0)
localsvr_ctx = bool(clsctx & comtypes.CLSCTX_LOCAL_SERVER)
inprocsvr_ctx = bool(clsctx & comtypes.CLSCTX_INPROC_SERVER)

if clsctx & comtypes.CLSCTX_LOCAL_SERVER and not hasattr(
sys, "frozendllhandle"
):
if localsvr_ctx and not hasattr(sys, "frozendllhandle"):
exe = sys.executable
if " " in exe:
exe = f'"{exe}"'
Expand All @@ -324,31 +320,26 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
script = os.path.abspath(sys.modules[cls.__module__].__file__)
if " " in script:
script = f'"{script}"'
self._add(
HKCR, rf"CLSID\{reg_clsid}\LocalServer32", "", f"{exe} {script}"
)
yield (HKCR, rf"CLSID\{reg_clsid}\LocalServer32", "", f"{exe} {script}")
else:
self._add(HKCR, rf"CLSID\{reg_clsid}\LocalServer32", "", f"{exe}")
yield (HKCR, rf"CLSID\{reg_clsid}\LocalServer32", "", f"{exe}")

# Register InprocServer32 only when run from script or from
# py2exe dll server, not from py2exe exe server.
if clsctx & comtypes.CLSCTX_INPROC_SERVER and getattr(sys, "frozen", None) in (
None,
"dll",
):
self._add(HKCR, rf"CLSID\{reg_clsid}\InprocServer32", "", _get_serverdll())
if inprocsvr_ctx and getattr(sys, "frozen", None) in (None, "dll"):
yield (HKCR, rf"CLSID\{reg_clsid}\InprocServer32", "", _get_serverdll())
# only for non-frozen inproc servers the PythonPath/PythonClass is needed.
if (
not hasattr(sys, "frozendllhandle")
or not comtypes.server.inprocserver._clsid_to_class
):
self._add(
yield (
HKCR,
rf"CLSID\{reg_clsid}\InprocServer32",
"PythonClass",
self._get_full_classname(cls),
)
self._add(
yield (
HKCR,
rf"CLSID\{reg_clsid}\InprocServer32",
"PythonPath",
Expand All @@ -357,7 +348,7 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:

reg_threading = getattr(cls, "_reg_threading_", None)
if reg_threading is not None:
self._add(
yield (
HKCR,
rf"CLSID\{reg_clsid}\InprocServer32",
"ThreadingModel",
Expand All @@ -366,9 +357,7 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:

reg_tlib = getattr(cls, "_reg_typelib_", None)
if reg_tlib is not None:
self._add(HKCR, rf"CLSID\{reg_clsid}\Typelib", "", reg_tlib[0])

yield from self._table
yield (HKCR, rf"CLSID\{reg_clsid}\Typelib", "", reg_tlib[0])


################################################################
Expand Down
Loading