Skip to content

Commit 9a11be3

Browse files
committed
updated the _configHelper and the add-on to use the class decorator.
1 parent a6f57a0 commit 9a11be3

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

addon/globalPlugins/beepKeyboard/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
addonHandler.initTranslation()
1111

1212

13-
from ._configHelper import *
14-
class AppConfig(BaseConfig):
15-
path = 'beepKeyboard'
16-
17-
beepUpperWithCapsLock = OptConfig('boolean(default=True)')
18-
beepCharacterWithShift = OptConfig('boolean(default=False)')
19-
beepToggleKeyChanges = OptConfig('boolean(default=False)')
20-
announceToggleStatus = OptConfig('boolean(default=True)')
21-
disableBeepingOnPasswordFields = OptConfig('boolean(default=True)')
22-
ignoredCharactersForShift = OptConfig("string(default='\\x1b\\t\\b\\r ')")
23-
beepForCharacters = OptConfig("string(default='')")
24-
shiftedCharactersTone = OptConfig('int_list(default=list(6000,10,25))')
25-
customCharactersTone = OptConfig('int_list(default=list(6000,10,25))')
26-
capsLockUpperTone = OptConfig('int_list(default=list(3000,40,50))')
27-
toggleOffTone = OptConfig('int_list(default=list(500,40,50))')
28-
toggleOnTone = OptConfig('int_list(default=list(2000, 40, 50))')
13+
from ._configHelper import configSpec, registerConfig
14+
@configSpec
15+
class AppConfig:
16+
__path__ = 'beepKeyboard'
17+
beepUpperWithCapsLock = 'boolean(default=True)'
18+
beepCharacterWithShift = 'boolean(default=False)'
19+
beepToggleKeyChanges = 'boolean(default=False)'
20+
announceToggleStatus = 'boolean(default=True)'
21+
disableBeepingOnPasswordFields = 'boolean(default=True)'
22+
ignoredCharactersForShift = "string(default='\\x1b\\t\\b\\r ')"
23+
beepForCharacters = "string(default='')"
24+
shiftedCharactersTone = 'int_list(default=list(6000,10,25))'
25+
customCharactersTone = 'int_list(default=list(6000,10,25))'
26+
capsLockUpperTone = 'int_list(default=list(3000,40,50))'
27+
toggleOffTone = 'int_list(default=list(500,40,50))'
28+
toggleOnTone = 'int_list(default=list(2000, 40, 50))'
2929
AF = registerConfig(AppConfig)
3030

3131

addon/globalPlugins/beepKeyboard/_configHelper.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,33 @@ def setConfigValue(path, optName, value):
3030

3131
def registerConfig(clsSpec, path=None):
3232
AF = clsSpec(path)
33-
config.conf.spec[AF.path[0]] = AF.createSpec()
33+
config.conf.spec[AF.__path__[0]] = AF.createSpec()
3434
AF.returnValue = True
3535
return AF
3636

3737

3838
class OptConfig:
3939
""" just a helper descriptor to create the main class to accesing config values.
40-
the option name will be taken from the declared variable. if you need to set another name, set it in the first param.
40+
the option name will be taken from the declared variable.
4141
"""
42-
def __init__(self, a, b = None):
42+
def __init__(self, desc):
4343
"""
4444
params:
45-
@a: usually the spec description. But if b is not none, a will be the name of the option.
46-
@b: the config description when is not None.
45+
@desc: the spec description.
4746
"""
48-
if b:
49-
self.name = a
50-
self.desc = b
51-
else:
52-
self.desc = a
53-
self.name = None
47+
self.desc = desc
5448

5549
def __set_name__(self, owner, name):
56-
if not self.name:
57-
self.name = name
58-
owner._confOpts.append(name)
50+
self.name = name
51+
owner.__confOpts__.append(name)
5952

6053
def __get__(self, obj, type=None):
6154
if obj.returnValue:
62-
return getConfigValue(obj.path, self.name)
55+
return getConfigValue(obj.__path__, self.name)
6356
return self.name, self.desc
6457

6558
def __set__(self, obj, value):
66-
setConfigValue(obj.path, self.name, value)
59+
setConfigValue(obj.__path__, self.name, value)
6760

6861

6962
class BaseConfig:
@@ -74,25 +67,40 @@ class BaseConfig:
7467
by default this value is False, to help to create the configuration spec first.
7568
Set it to true after creating this spec.
7669
"""
77-
path = None
70+
__path__ = None
7871
def __init__(self, path=None):
7972
self.returnValue = False
8073
if not path:
81-
path = self.__class__.path
74+
path = self.__class__.__path__
8275
if not path:
8376
raise Exception("Path for the config is not defined")
8477
if isinstance(path, list):
85-
self.path = path
78+
self.__path__ = path
8679
else:
87-
self.path = [path]
80+
self.__path__ = [path]
8881

8982
def createSpec(self):
9083
""" this method creates a config spec with the provided attributes in the class
9184
"""
9285
s = {}
93-
for k in self.__class__._confOpts:
86+
for k in self.__class__.__confOpts__:
9487
k = self.__getattribute__(k)
9588
s[k[0]] = k[1]
9689
return s
9790
# an array of the available options.
98-
_confOpts = []
91+
__confOpts__ = []
92+
93+
94+
def configSpec(cls):
95+
class ConfigSpec(BaseConfig):
96+
pass
97+
98+
for k in cls.__dict__:
99+
if k == '__path__':
100+
ConfigSpec.__path__ = cls.__path__
101+
if k.startswith("__"): continue
102+
v = getattr(cls, k)
103+
d = OptConfig(v)
104+
d.__set_name__(ConfigSpec, k)
105+
setattr(ConfigSpec, k, d)
106+
return ConfigSpec

0 commit comments

Comments
 (0)