@@ -30,40 +30,33 @@ def setConfigValue(path, optName, value):
30
30
31
31
def registerConfig (clsSpec , path = None ):
32
32
AF = clsSpec (path )
33
- config .conf .spec [AF .path [0 ]] = AF .createSpec ()
33
+ config .conf .spec [AF .__path__ [0 ]] = AF .createSpec ()
34
34
AF .returnValue = True
35
35
return AF
36
36
37
37
38
38
class OptConfig :
39
39
""" 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.
41
41
"""
42
- def __init__ (self , a , b = None ):
42
+ def __init__ (self , desc ):
43
43
"""
44
44
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.
47
46
"""
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
54
48
55
49
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 )
59
52
60
53
def __get__ (self , obj , type = None ):
61
54
if obj .returnValue :
62
- return getConfigValue (obj .path , self .name )
55
+ return getConfigValue (obj .__path__ , self .name )
63
56
return self .name , self .desc
64
57
65
58
def __set__ (self , obj , value ):
66
- setConfigValue (obj .path , self .name , value )
59
+ setConfigValue (obj .__path__ , self .name , value )
67
60
68
61
69
62
class BaseConfig :
@@ -74,25 +67,40 @@ class BaseConfig:
74
67
by default this value is False, to help to create the configuration spec first.
75
68
Set it to true after creating this spec.
76
69
"""
77
- path = None
70
+ __path__ = None
78
71
def __init__ (self , path = None ):
79
72
self .returnValue = False
80
73
if not path :
81
- path = self .__class__ .path
74
+ path = self .__class__ .__path__
82
75
if not path :
83
76
raise Exception ("Path for the config is not defined" )
84
77
if isinstance (path , list ):
85
- self .path = path
78
+ self .__path__ = path
86
79
else :
87
- self .path = [path ]
80
+ self .__path__ = [path ]
88
81
89
82
def createSpec (self ):
90
83
""" this method creates a config spec with the provided attributes in the class
91
84
"""
92
85
s = {}
93
- for k in self .__class__ ._confOpts :
86
+ for k in self .__class__ .__confOpts__ :
94
87
k = self .__getattribute__ (k )
95
88
s [k [0 ]] = k [1 ]
96
89
return s
97
90
# 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