Skip to content

Commit

Permalink
fix up documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
seanbudd committed Jan 19, 2022
1 parent 4d63695 commit fde810b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 52 deletions.
40 changes: 20 additions & 20 deletions source/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def saveOnExit():
pass


class _RegKey(str, Enum):
class RegistryKey(str, Enum):
INSTALLED_COPY = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NVDA"
RUN = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
NVDA = r"SOFTWARE\NVDA"
Expand All @@ -90,17 +90,17 @@ def isInstalledCopy() -> bool:
try:
k = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
_RegKey.INSTALLED_COPY
RegistryKey.INSTALLED_COPY
)
except FileNotFoundError:
log.debug(
f"Unable to find isInstalledCopy registry key {_RegKey.INSTALLED_COPY}"
f"Unable to find isInstalledCopy registry key {RegistryKey.INSTALLED_COPY}"
"- this is not an installed copy."
)
return False
except WindowsError:
log.error(
f"Unable to open isInstalledCopy registry key {_RegKey.INSTALLED_COPY}",
f"Unable to open isInstalledCopy registry key {RegistryKey.INSTALLED_COPY}",
exc_info=True
)
return False
Expand All @@ -109,7 +109,7 @@ def isInstalledCopy() -> bool:
instDir = winreg.QueryValueEx(k, "UninstallDirectory")[0]
except FileNotFoundError:
log.debug(
f"Unable to find UninstallDirectory value for {_RegKey.INSTALLED_COPY}"
f"Unable to find UninstallDirectory value for {RegistryKey.INSTALLED_COPY}"
"- this may not be an installed copy."
)
return False
Expand All @@ -131,7 +131,7 @@ def isInstalledCopy() -> bool:

CONFIG_IN_LOCAL_APPDATA_SUBKEY = "configInLocalAppData"
"""
#6864: The name of the subkey stored under NVDA_REGKEY where the value is stored
#6864: The name of the subkey stored under RegistryKey.NVDA where the value is stored
which will make an installed NVDA load the user configuration either from the local or from
the roaming application data profile.
The registry value is unset by default.
Expand All @@ -143,7 +143,7 @@ def isInstalledCopy() -> bool:

def getInstalledUserConfigPath() -> Optional[str]:
try:
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, _RegKey.NVDA)
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegistryKey.NVDA)
except WindowsError:
log.error("Could not find nvda registry key", exc_info=True)
configInLocalAppData = False
Expand Down Expand Up @@ -253,20 +253,20 @@ def getStartAfterLogon() -> bool:
"""
if (
easeOfAccess.canConfigTerminateOnDesktopSwitch
and easeOfAccess.willAutoStart(winreg.HKEY_CURRENT_USER)
and easeOfAccess.willAutoStart(easeOfAccess.AutoStartContext.AFTER_LOGON)
):
return True
try:
k = winreg.OpenKey(winreg.HKEY_CURRENT_USER, _RegKey.RUN)
k = winreg.OpenKey(winreg.HKEY_CURRENT_USER, RegistryKey.RUN)
except FileNotFoundError:
log.debugWarning(
f"Unable to find run registry key {_RegKey.RUN}",
f"Unable to find run registry key {RegistryKey.RUN}",
exc_info=True
)
return False
except WindowsError:
log.error(
f"Unable to open run registry key {_RegKey.RUN}",
f"Unable to open run registry key {RegistryKey.RUN}",
exc_info=True
)
return False
Expand Down Expand Up @@ -318,15 +318,15 @@ def setStartAfterLogon(enable: bool) -> None:
if getStartAfterLogon() == enable:
return
if easeOfAccess.canConfigTerminateOnDesktopSwitch:
easeOfAccess.setAutoStart(winreg.HKEY_CURRENT_USER, enable)
easeOfAccess.setAutoStart(easeOfAccess.AutoStartContext.AFTER_LOGON, enable)
if enable:
return
# We're disabling, so ensure the run key is cleared,
# as it might have been set by an old version.
run = False
else:
run = enable
k = winreg.OpenKey(winreg.HKEY_CURRENT_USER, _RegKey.RUN, 0, winreg.KEY_WRITE)
k = winreg.OpenKey(winreg.HKEY_CURRENT_USER, RegistryKey.RUN, 0, winreg.KEY_WRITE)
if run:
winreg.SetValueEx(k, "nvda", None, winreg.REG_SZ, sys.argv[0])
else:
Expand Down Expand Up @@ -359,26 +359,26 @@ def getStartOnLogonScreen() -> bool:
Then, checks a NVDA registry key to see if NVDA
has been registered to start on logon by earlier NVDA versions.
"""
if easeOfAccess.willAutoStart(winreg.HKEY_LOCAL_MACHINE):
if easeOfAccess.willAutoStart(easeOfAccess.AutoStartContext.ON_LOGON):
return True
try:
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, _RegKey.NVDA)
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegistryKey.NVDA)
except FileNotFoundError:
log.debugWarning(f"Could not find NVDA reg key {_RegKey.NVDA}", exc_info=True)
log.debugWarning(f"Could not find NVDA reg key {RegistryKey.NVDA}", exc_info=True)
except WindowsError:
log.error(f"Failed to open NVDA reg key {_RegKey.NVDA}", exc_info=True)
log.error(f"Failed to open NVDA reg key {RegistryKey.NVDA}", exc_info=True)
try:
return bool(winreg.QueryValueEx(k, "startOnLogonScreen")[0])
except FileNotFoundError:
log.debug(f"Could not find startOnLogonScreen value for {_RegKey.NVDA} - likely unset.")
log.debug(f"Could not find startOnLogonScreen value for {RegistryKey.NVDA} - likely unset.")
return False
except WindowsError:
log.error(f"Failed to query startOnLogonScreen value for {_RegKey.NVDA}", exc_info=True)
log.error(f"Failed to query startOnLogonScreen value for {RegistryKey.NVDA}", exc_info=True)
return False


def _setStartOnLogonScreen(enable: bool) -> None:
easeOfAccess.setAutoStart(winreg.HKEY_LOCAL_MACHINE, enable)
easeOfAccess.setAutoStart(easeOfAccess.AutoStartContext.AFTER_LOGON, enable)


def setSystemConfigToCurrentConfig():
Expand Down
64 changes: 41 additions & 23 deletions source/easeOfAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""Utilities for working with the Windows Ease of Access Center.
"""

from enum import Enum, IntEnum
from typing import List
from logHandler import log
import winreg
Expand All @@ -15,16 +16,29 @@

# Windows >= 8
canConfigTerminateOnDesktopSwitch: bool = winVersion.getWinVer() >= winVersion.WIN8
_APP_KEY_NAME = "nvda_nvda_v1"

ROOT_KEY = r"Software\Microsoft\Windows NT\CurrentVersion\Accessibility"
APP_KEY_NAME = "nvda_nvda_v1"
APP_KEY_PATH = r"%s\ATs\%s" % (ROOT_KEY, APP_KEY_NAME)

class RegistryKey(str, Enum):
ROOT = r"Software\Microsoft\Windows NT\CurrentVersion\Accessibility"
TEMP = r"Software\Microsoft\Windows NT\CurrentVersion\AccessibilityTemp"
APP = r"%s\ATs\%s" % (ROOT, _APP_KEY_NAME)


class AutoStartContext(IntEnum):
"""Registry HKEY used for tracking when NVDA starts automatically"""
ON_LOGON = winreg.HKEY_LOCAL_MACHINE
AFTER_LOGON = winreg.HKEY_CURRENT_USER


def isRegistered() -> bool:
try:
winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, APP_KEY_PATH, 0,
winreg.KEY_READ | winreg.KEY_WOW64_64KEY)
winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
RegistryKey.APP,
0,
winreg.KEY_READ | winreg.KEY_WOW64_64KEY
)
return True
except FileNotFoundError:
log.debug("Unable to find AT registry key")
Expand All @@ -36,8 +50,8 @@ def isRegistered() -> bool:
def notify(signal):
if not isRegistered():
return
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows NT\CurrentVersion\AccessibilityTemp") as rkey:
winreg.SetValueEx(rkey, APP_KEY_NAME, None, winreg.REG_DWORD, signal)
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, RegistryKey.TEMP) as rkey:
winreg.SetValueEx(rkey, _APP_KEY_NAME, None, winreg.REG_DWORD, signal)
keys = []
# The user might be holding unwanted modifiers.
for vk in winUser.VK_SHIFT, winUser.VK_CONTROL, winUser.VK_MENU:
Expand All @@ -63,27 +77,31 @@ def notify(signal):
winUser.SendInput(inputs)


def willAutoStart(hkey: 'winreg._KeyType') -> bool:
return (APP_KEY_NAME in _getAutoStartConfiguration(hkey))
def willAutoStart(autoStartContext: AutoStartContext) -> bool:
return (_APP_KEY_NAME in _getAutoStartConfiguration(autoStartContext))


def _getAutoStartConfiguration(hkey: 'winreg._KeyType') -> List[str]:
def _getAutoStartConfiguration(autoStartContext: AutoStartContext) -> List[str]:
try:
k = winreg.OpenKey(hkey, ROOT_KEY, 0,
winreg.KEY_READ | winreg.KEY_WOW64_64KEY)
k = winreg.OpenKey(
autoStartContext.value,
RegistryKey.ROOT,
0,
winreg.KEY_READ | winreg.KEY_WOW64_64KEY
)
except FileNotFoundError:
log.debug("Unable to find existing auto start registry key")
log.debug(f"Unable to find existing {autoStartContext} {RegistryKey.ROOT}")
return []
except WindowsError:
log.error("Unable to open auto start registry key for reading", exc_info=True)
log.error(f"Unable to open {autoStartContext} {RegistryKey.ROOT} for reading", exc_info=True)
return []

try:
conf: List[str] = winreg.QueryValueEx(k, "Configuration")[0].split(",")
except FileNotFoundError:
log.debug("Unable to find auto start configuration")
log.debug(f"Unable to find {autoStartContext} {RegistryKey.ROOT} configuration")
except WindowsError:
log.error("Unable to query auto start configuration", exc_info=True)
log.error(f"Unable to query {autoStartContext} {RegistryKey.ROOT} configuration", exc_info=True)
else:
if not conf[0]:
# "".split(",") returns [""], so remove the empty string.
Expand All @@ -92,23 +110,23 @@ def _getAutoStartConfiguration(hkey: 'winreg._KeyType') -> List[str]:
return []


def setAutoStart(hkey: 'winreg._KeyType', enable: bool) -> None:
def setAutoStart(autoStartContext: AutoStartContext, enable: bool) -> None:
"""Raises `Union[WindowsError, FileNotFoundError]`"""
conf = _getAutoStartConfiguration(hkey)
currentlyEnabled = APP_KEY_NAME in conf
conf = _getAutoStartConfiguration(autoStartContext)
currentlyEnabled = _APP_KEY_NAME in conf
changed = False

if enable and not currentlyEnabled:
conf.append(APP_KEY_NAME)
conf.append(_APP_KEY_NAME)
changed = True
elif not enable and currentlyEnabled:
conf.remove(APP_KEY_NAME)
conf.remove(_APP_KEY_NAME)
changed = True

if changed:
k = winreg.OpenKey(
hkey,
ROOT_KEY,
autoStartContext.value,
RegistryKey.ROOT,
0,
winreg.KEY_READ | winreg.KEY_WRITE | winreg.KEY_WOW64_64KEY
)
Expand Down
25 changes: 16 additions & 9 deletions source/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def createShortcut(path,targetPath=None,arguments=None,iconLocation=None,working

def getStartMenuFolder(noDefault=False):
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, config._RegKey.NVDA) as k:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, config.RegistryKey.NVDA) as k:
return winreg.QueryValueEx(k,u"Start Menu Folder")[0]
except WindowsError:
return defaultStartMenuFolder if not noDefault else None
Expand Down Expand Up @@ -243,7 +243,7 @@ def registerInstallation(installDir,startMenuFolder,shouldCreateDesktopShortcut,
winreg.SetValueEx(k,name,None,winreg.REG_SZ,value.format(installDir=installDir))
with winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\nvda.exe",0,winreg.KEY_WRITE) as k:
winreg.SetValueEx(k,"",None,winreg.REG_SZ,os.path.join(installDir,"nvda.exe"))
with winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, config._RegKey.NVDA, 0, winreg.KEY_WRITE) as k:
with winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, config.RegistryKey, 0, winreg.KEY_WRITE) as k:
winreg.SetValueEx(k,"startMenuFolder",None,winreg.REG_SZ,startMenuFolder)
if configInLocalAppData:
winreg.SetValueEx(k,config.CONFIG_IN_LOCAL_APPDATA_SUBKEY,None,winreg.REG_DWORD,int(configInLocalAppData))
Expand Down Expand Up @@ -412,9 +412,12 @@ def isDesktopShortcutInstalled():

def unregisterInstallation(keepDesktopShortcut=False):
try:
winreg.DeleteKeyEx(winreg.HKEY_LOCAL_MACHINE, easeOfAccess.APP_KEY_PATH,
winreg.KEY_WOW64_64KEY)
easeOfAccess.setAutoStart(winreg.HKEY_LOCAL_MACHINE, False)
winreg.DeleteKeyEx(
winreg.HKEY_LOCAL_MACHINE,
easeOfAccess.RegistryKey.APP,
winreg.KEY_WOW64_64KEY
)
easeOfAccess.setAutoStart(easeOfAccess.AutoStartContext.ON_LOGON, False)
except WindowsError:
pass
wsh=_getWSH()
Expand All @@ -439,7 +442,7 @@ def unregisterInstallation(keepDesktopShortcut=False):
except WindowsError:
pass
try:
winreg.DeleteKey(winreg.HKEY_LOCAL_MACHINE, config._RegKey.NVDA)
winreg.DeleteKey(winreg.HKEY_LOCAL_MACHINE, config.RegistryKey)
except WindowsError:
pass
unregisterAddonFileAssociation()
Expand Down Expand Up @@ -553,7 +556,7 @@ def tryCopyFile(sourceFilePath,destFilePath):
def install(shouldCreateDesktopShortcut=True,shouldRunAtLogon=True):
prevInstallPath=getInstallPath(noDefault=True)
try:
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, config._RegKey.NVDA)
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, config.RegistryKey)
configInLocalAppData = bool(winreg.QueryValueEx(k, config.CONFIG_IN_LOCAL_APPDATA_SUBKEY)[0])
except WindowsError:
configInLocalAppData = False
Expand Down Expand Up @@ -615,8 +618,12 @@ def createPortableCopy(destPath,shouldCopyUserConfig=True):
removeOldLibFiles(destPath,rebootOK=True)

def registerEaseOfAccess(installDir):
with winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, easeOfAccess.APP_KEY_PATH, 0,
winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_64KEY) as appKey:
with winreg.CreateKeyEx(
winreg.HKEY_LOCAL_MACHINE,
easeOfAccess.RegistryKey.APP,
0,
winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_64KEY
) as appKey:
winreg.SetValueEx(appKey, "ApplicationName", None, winreg.REG_SZ,
versionInfo.name)
winreg.SetValueEx(appKey, "Description", None, winreg.REG_SZ,
Expand Down

0 comments on commit fde810b

Please sign in to comment.