Recommendation to deal with NVDA's configurations in add-ons.


DaVid
 

Hi there.

Today I developed a small utility to deal with NVDA's settings in our add-ons.
I really hate to write config.conf.['a1']['a2]['option'] each time I
need to access, or set a value in the configuration. But was not a
problem for me until now.
I was merging a pr in one of my add-ons, I was a little asleep, and
was very hard to find the error. Just a capital letter! And the editor
usually don't help with autocompletion if we are writting inside ''.
So, I thought... The config names and path should be declared in one
part only, and access the configurations using that code. Then I
decided to experiment with one of my mantained add-ons.

This consist on a class and a very simple descriptor, to get and
access the config. See this example:

class appConfig:
def __init__(self):
# the path to the config. currently you can't use customized paths
(for example to set it for each synthesizer) but is not hard to
implement it.
self.path = ['speechHistoryExplorer']
# if the following is true, will return the value. False, will
return the configuration name.
# is set to false to help creating the conspec.
self.returnValue = False

# now declaring some configurations, outside the constructor.
# optConfig is the descriptor that does all the ting, but it doesn't
matter to explain it.
# you can use properties too, but is too repetitive.
maxHistoryLength = OptConfig('maxHistoryLength')
trimWhitespaceFromStart = OptConfig('trimWhitespaceFromStart')
trimWhitespaceFromEnd = OptConfig('trimWhitespaceFromEnd')
beepWhenPerformingActions = OptConfig('beepWhenPerformingActions')
beepPanning = OptConfig('beepPanning')

# now instantiate the class.
appConfig = appConfig()

# now, it will create the conspec.
confspec = {
appConfig.maxHistoryLength: 'integer(default=500)',
appConfig.trimWhitespaceFromStart: 'boolean(default=false)',
appConfig.trimWhitespaceFromEnd: 'boolean(default=false)',
appConfig.beepWhenPerformingActions: 'boolean(default=true)',
appConfig.beepPanning: 'boolean(default=true)',
}
# set the conspec to the config of NVDA.
config.conf.spec[appConfig.path[0]] = confspec
# now change returnValue to True, because usually we want the value of
the config, not the name.
appConfig.returnValue = True

# Now, get or set a setting is so easy! just do the following:
# get a copnfig:
appConfig.maxHistoryLength
# set a config:
appConfig.maxHistoryLength = 250

and you can use the smart Autocomplete feature of vs code, for
example. So, you will never make a mistake when refering to a config.
And you can avoid to use those tedious ', [, etc each time you need to
access a configuration.

You can see the entire file here:
https://github.com/davidacm/SpeechHistoryExplorer/blob/SPE/addon/globalPlugins/_config.py

I'm open to suggestions.

Regards,
David.

Join nvda-addons@nvda-addons.groups.io to automatically receive all group messages.