Skip to content

Commit d65f4de

Browse files
authored
Fixup FolderId usage and change log (#12986)
Fixup of #12943 Summary of the issue: Changelog entries were missing for #12943 UpperCamelCase is the standard for class names, including enums, however FOLDERID was used as the casing for the enum, which mirrors the windows constants. There is a similar issue for the enum members (which used UpperCamelCase instead of CAP_SNAKE_CASE). If we want to change our practice to be consistent with what an Enum represents, we should update codingStandards.md Also the enum type was not being fully leveraged by SHGetKnownFolderPath, requiring .value to be used unnecessarily when calling the function. Description of how this pull request fixes the issue: Updates the casing of the enum and it's members. Updates the change log, and fixes up some earlier entries.
1 parent 9f8fff8 commit d65f4de

5 files changed

Lines changed: 29 additions & 19 deletions

File tree

‎source/config/__init__.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def getInstalledUserConfigPath():
103103
except WindowsError:
104104
configInLocalAppData=False
105105
configParent = shlobj.SHGetKnownFolderPath(
106-
shlobj.FOLDERID.LocalAppData.value if configInLocalAppData else shlobj.FOLDERID.RoamingAppData.value
106+
shlobj.FolderId.LOCAL_APP_DATA if configInLocalAppData else shlobj.FolderId.ROAMING_APP_DATA
107107
)
108108
try:
109109
return os.path.join(configParent, "nvda")
@@ -1151,4 +1151,3 @@ def exit(self):
11511151

11521152
def __exit__(self, excType, excVal, traceback):
11531153
self.exit()
1154-

‎source/fileUtils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _suspendWow64RedirectionForFileInfoRetrieval(func):
5555
"""
5656
@wraps(func)
5757
def funcWrapper(filePath, *attributes):
58-
nativeSys32 = shlobj.SHGetKnownFolderPath(shlobj.FOLDERID.System.value)
58+
nativeSys32 = shlobj.SHGetKnownFolderPath(shlobj.FolderId.SYSTEM)
5959
if (
6060
systemUtils.hasSyswow64Dir()
6161
# `os.path.commonpath` is necessary to perform case-insensitive comparisons

‎source/shlobj.py‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,42 @@
1313

1414
import comtypes
1515
import ctypes
16-
import enum
16+
from enum import Enum
1717
import functools
18-
import typing
18+
from typing import Optional, Union
1919

2020

21-
class FOLDERID(str, enum.Enum):
21+
class FolderId(str, Enum):
2222
"""Contains guids of known folders from Knownfolders.h. Full list is availabe at:
2323
https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid"""
2424
#: The file system directory that serves as a common repository for application-specific data.
2525
#: A typical path is C:\Documents and Settings\username\Application Data.
26-
RoamingAppData = "{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}"
26+
ROAMING_APP_DATA = "{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}"
2727
#: The file system directory that serves as a data repository for local (nonroaming) applications.
2828
#: A typical path is C:\Documents and Settings\username\Local Settings\Application Data.
29-
LocalAppData = "{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"
29+
LOCAL_APP_DATA = "{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"
3030
#: The file system directory that contains application data for all users.
3131
#: A typical path is C:\Documents and Settings\All Users\Application Data.
3232
#: This folder is used for application data that is not user specific.
33-
ProgramData = "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}"
33+
PROGRAM_DATA = "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}"
3434
# The Windows System folder.
3535
# A typical path is C:\Windows\System32.
36-
System = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}"
37-
SystemX86 = "{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}"
36+
SYSTEM = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}"
37+
SYSTEM_X86 = "{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}"
3838

3939

4040
@functools.lru_cache(maxsize=128)
41-
def SHGetKnownFolderPath(folderGuid: str, dwFlags: int = 0, hToken: typing.Optional[int] = None) -> str:
41+
def SHGetKnownFolderPath(
42+
folderGuid: Union[FolderId, str],
43+
dwFlags: int = 0,
44+
hToken: Optional[int] = None
45+
) -> str:
4246
"""Wrapper for `SHGetKnownFolderPath` which caches the results
4347
to avoid calling the win32 function unnecessarily."""
48+
if isinstance(folderGuid, FolderId):
49+
folderGuid = folderGuid.value
4450
guid = comtypes.GUID(folderGuid)
51+
4552
pathPointer = ctypes.c_wchar_p()
4653
res = ctypes.windll.shell32.SHGetKnownFolderPath(
4754
comtypes.byref(guid),
@@ -50,7 +57,7 @@ def SHGetKnownFolderPath(folderGuid: str, dwFlags: int = 0, hToken: typing.Optio
5057
ctypes.byref(pathPointer)
5158
)
5259
if res != 0:
53-
raise RuntimeError(f"SHGetKnownFolderPath failed with erro code {res}")
60+
raise RuntimeError(f"SHGetKnownFolderPath failed with error code {res}")
5461
path = pathPointer.value
5562
ctypes.windll.ole32.CoTaskMemFree(pathPointer)
5663
return path

‎source/systemUtils.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
import winKernel
1010
import shellapi
1111
import winUser
12-
import os
1312
import functools
1413
import shlobj
1514

1615

1716
@functools.lru_cache(maxsize=1)
1817
def hasSyswow64Dir() -> bool:
1918
"""Returns `True` if the current system has separate system32 directories for 32-bit processes."""
20-
nativeSys32 = shlobj.SHGetKnownFolderPath(shlobj.FOLDERID.System.value)
21-
Syswow64Sys32 = shlobj.SHGetKnownFolderPath(shlobj.FOLDERID.SystemX86.value)
19+
nativeSys32 = shlobj.SHGetKnownFolderPath(shlobj.FolderId.SYSTEM)
20+
Syswow64Sys32 = shlobj.SHGetKnownFolderPath(shlobj.FolderId.SYSTEM_X86)
2221
return nativeSys32 != Syswow64Sys32
2322

2423

‎user_docs/en/changes.t2t‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ What's New in NVDA
4040
- ``constants.SVE*`` usages should be replaced with ``SpeechVoiceEvents.*``
4141
-
4242
- The ``soffice`` appModule has the following classes and functions removed ``JAB_OOTableCell``, ``JAB_OOTable``, ``gridCoordStringToNumbers``. (#12849)
43-
- core.CallCancelled becomes exceptions.CallCancelled.
44-
- All constants starting with RPC from core and logHandler are moved into RPCConstants.RPC enum
45-
- It is recommended that mouseHandler.doPrimaryClick and mouseHandler.doSecondaryClick functions should be used to click the mouse to perform a logical action such as activating (primary) or secondary (show context menu), rather than using executeMouseEvent and specifying the left or right mouse button specifically. This ensures code will honor the Windows user setting for swapping the primary mouse button. (#12642)
43+
- ``core.CallCancelled`` is now ``exceptions.CallCancelled``. (#12940)
44+
- All constants starting with RPC from ``core`` and ``logHandler`` are moved into ``RPCConstants.RPC`` enum. (#12940)
45+
- It is recommended that ``mouseHandler.doPrimaryClick`` and ``mouseHandler.doSecondaryClick`` functions should be used to click the mouse to perform a logical action such as activating (primary) or secondary (show context menu),
46+
rather than using executeMouseEvent and specifying the left or right mouse button specifically.
47+
This ensures code will honor the Windows user setting for swapping the primary mouse button. (#12642)
48+
- ``config.getSystemConfigPath`` has been removed - there is no replacement. (#12943)
49+
- ``shlobj.SHGetFolderPath`` has been removed - please use ``shlobj.SHGetKnownFolderPath`` instead. (#12943)
50+
- ``shlobj`` constants have been removed. A new enum has been created, ``shlobj.FolderId`` for usage with ``SHGetKnownFolderPath``. (#12943)
4651
-
4752

4853

0 commit comments

Comments
 (0)