Skip to content

Commit dcbad59

Browse files
authored
Merge 57535a1 into 9c5130f
2 parents 9c5130f + 57535a1 commit dcbad59

8 files changed

Lines changed: 71 additions & 35 deletions

File tree

source/addonHandler/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import zipfile
2222
from configobj import ConfigObj
2323
from configobj.validate import Validator
24-
24+
from .packaging import initializeModulePackagePaths
2525
import config
2626
import languageHandler
2727
from logHandler import log
@@ -168,6 +168,7 @@ def initialize():
168168
getAvailableAddons(refresh=True, isFirstLoad=True)
169169
state.cleanupRemovedDisabledAddons()
170170
state.save()
171+
initializeModulePackagePaths()
171172

172173

173174
def terminate():

source/addonHandler/packaging.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# A part of NonVisual Desktop Access (NVDA)
2+
# Copyright (C) 2009-2022 NV Access Limited, Rui Batista, Zahari Yurukov, Leonard de Ruijter
3+
# This file is covered by the GNU General Public License.
4+
# See the file COPYING for more details.
5+
6+
"""Functions to add python modules within addon directories to python module paths.
7+
"""
8+
9+
import os.path
10+
from typing import Optional
11+
from types import ModuleType
12+
import globalVars
13+
import config
14+
15+
16+
def initializeModulePackagePaths():
17+
"""Initializes the module package paths for drivers and plugins.
18+
This ensures that drivers (such as braille display drivers) or plugins (such as app modules)
19+
can be discovered by NVDA.
20+
"""
21+
import appModules
22+
import brailleDisplayDrivers
23+
import globalPlugins
24+
import synthDrivers
25+
import visionEnhancementProviders
26+
modules = [
27+
appModules,
28+
brailleDisplayDrivers,
29+
globalPlugins,
30+
synthDrivers,
31+
visionEnhancementProviders,
32+
]
33+
for module in modules:
34+
addDirsToPythonPackagePath(module)
35+
36+
37+
def addDirsToPythonPackagePath(module: ModuleType, subdir: Optional[str] = None):
38+
"""Add add-on and scratchpath directories for a module to the search path (__path__) of a Python package.
39+
C{subdir} is added to each directory. It defaults to the name of the Python package.
40+
@param module: The root module of the package.
41+
@param subdir: The subdirectory to be used, C{None} for the name of C{module}.
42+
"""
43+
if config.isAppX or globalVars.appArgs.disableAddons:
44+
return
45+
from . import getRunningAddons
46+
for addon in getRunningAddons():
47+
addon.addToPackagePath(module)
48+
if globalVars.appArgs.secure or not config.conf['development']['enableScratchpadDir']:
49+
return
50+
if not subdir:
51+
subdir = module.__name__
52+
fullPath = os.path.join(config.getScratchpadDir(), subdir)
53+
if fullPath in module.__path__:
54+
return
55+
# Ensure this directory exists otherwise pkgutil.iter_importers may emit None for missing paths.
56+
if not os.path.isdir(fullPath):
57+
os.makedirs(fullPath)
58+
# Insert this path at the beginning of the module's search paths.
59+
# The module's search paths may not be a mutable list, so replace it with a new one
60+
pathList = [fullPath]
61+
pathList.extend(module.__path__)
62+
module.__path__ = pathList

source/appModuleHandler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ def reloadAppModules():
361361
def initialize():
362362
"""Initializes the appModule subsystem.
363363
"""
364-
config.addConfigDirsToPythonPackagePath(appModules)
365364
if not initialize._alreadyInitialized:
366365
initialize._alreadyInitialized = True
367366

source/braille.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,6 @@ def func(cls):
23932393

23942394
def initialize():
23952395
global handler
2396-
config.addConfigDirsToPythonPackagePath(brailleDisplayDrivers)
23972396
log.info("Using liblouis version %s" % louis.version())
23982397
import serial
23992398
log.info("Using pySerial version %s"%serial.VERSION)

source/config/__init__.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ def __getattr__(attrName: str) -> Any:
7474
if attrName == "RUN_REGKEY" and NVDAState._allowDeprecatedAPI():
7575
log.warning("RUN_REGKEY is deprecated, use RegistryKey.RUN instead.")
7676
return RegistryKey.RUN.value
77+
if attrName == "addConfigDirsToPythonPackagePath" and NVDAState._allowDeprecatedAPI():
78+
log.warning(
79+
"addConfigDirsToPythonPackagePath is deprecated, "
80+
"use addonHandler.packaging.addDirsToPythonPackagePath instead."
81+
)
82+
from addonHandler.packaging import addDirsToPythonPackagePath
83+
return addDirsToPythonPackagePath
7784
raise AttributeError(f"module {repr(__name__)} has no attribute {repr(attrName)}")
7885

7986

@@ -474,35 +481,6 @@ def setStartOnLogonScreen(enable: bool) -> None:
474481
raise RuntimeError("Slave failed to set startOnLogonScreen")
475482

476483

477-
def addConfigDirsToPythonPackagePath(module, subdir=None):
478-
"""Add the configuration directories to the module search path (__path__) of a Python package.
479-
C{subdir} is added to each configuration directory. It defaults to the name of the Python package.
480-
@param module: The root module of the package.
481-
@type module: module
482-
@param subdir: The subdirectory to be used, C{None} for the name of C{module}.
483-
@type subdir: str
484-
"""
485-
if isAppX or globalVars.appArgs.disableAddons:
486-
return
487-
# FIXME: this should not be coupled to the config module....
488-
import addonHandler
489-
for addon in addonHandler.getRunningAddons():
490-
addon.addToPackagePath(module)
491-
if globalVars.appArgs.secure or not conf['development']['enableScratchpadDir']:
492-
return
493-
if not subdir:
494-
subdir = module.__name__
495-
fullPath=os.path.join(getScratchpadDir(),subdir)
496-
# Ensure this directory exists otherwise pkgutil.iter_importers may emit None for missing paths.
497-
if not os.path.isdir(fullPath):
498-
os.makedirs(fullPath)
499-
# Insert this path at the beginning of the module's search paths.
500-
# The module's search paths may not be a mutable list, so replace it with a new one
501-
pathList=[fullPath]
502-
pathList.extend(module.__path__)
503-
module.__path__=pathList
504-
505-
506484
def _transformSpec(spec: ConfigObj):
507485
"""To make the spec less verbose, transform the spec:
508486
- Add default="default" to all featureFlag items. This is required so that the key can be read,

source/globalPluginHandler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def listPlugins():
2727
yield plugin
2828

2929
def initialize():
30-
config.addConfigDirsToPythonPackagePath(globalPlugins)
3130
for plugin in listPlugins():
3231
try:
3332
runningPlugins.add(plugin())

source/synthDriverHandler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ def _get_initialSettingsRingSetting(self):
375375

376376

377377
def initialize():
378-
config.addConfigDirsToPythonPackagePath(synthDrivers)
379378
config.post_configProfileSwitch.register(handlePostConfigProfileSwitch)
380379

381380

source/vision/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
def initialize() -> None:
2222
global handler
23-
config.addConfigDirsToPythonPackagePath(visionEnhancementProviders)
2423
handler = VisionHandler()
2524

2625

0 commit comments

Comments
 (0)