]> icculus.org git repositories - dana/openbox.git/blob - scripts/config.py
restart and catch errors appropriately
[dana/openbox.git] / scripts / config.py
1 # Openbox's config system. Please use the defined functions instead of
2 # accessing the internal data structures directly, for the sake of us all.
3
4 import ob
5
6 def add(modulename, name, friendlyname, description, type, default,
7         **keywords):
8     """Add a variable to the configuration system.
9
10     Add a variable to the configuration system for a module.
11     modulename - The name of the module, e.g. 'focus'
12     name - The name of the variable, e.g. 'my_variable'
13     friendlyname - The user-friendly name of the variable, e.g. 'My Variable'
14     description - The detailed destription of the variable, e.g. 'Does Things'
15     type - The type of the variable, one of:
16              - 'boolean'
17              - 'enum'
18              - 'integer'
19              - 'string'
20              - 'file'
21              - 'function'
22              - 'object'
23     default - The default value for the variable, e.g. 300
24     keywords - Extra keyword=value pairs to further define the variable. These
25                can be:
26                  - For 'enum' types:
27                      - options : A list of possible options for the variable.
28                                  This *must* be set for all enum variables.
29                  - For 'integer' types:
30                      - min : The minimum value for the variable.
31                      - max : The maximum value for the variable.
32     """
33     modulename = str(modulename).lower()
34     name = str(name).lower()
35     friendlyname = str(friendlyname)
36     description = str(description)
37     type = str(type).lower()
38
39     # make sure the sub-dicts exist
40     try:
41         _settings[modulename]
42         try:
43             _settings[modulename][name]
44         except KeyError:
45             _settings[modulename][name] = {}
46     except KeyError:
47         _settings[modulename] = {}
48         _settings[modulename][name] = {}
49
50     # add the keywords first as they are used for the tests in set()
51     for key,value in zip(keywords.keys(), keywords.values()):
52         _settings[modulename][name][key] = value
53
54     _settings[modulename][name]['name'] = friendlyname
55     _settings[modulename][name]['description'] = description
56     _settings[modulename][name]['type'] = type
57     _settings[modulename][name]['default'] = default
58
59     # put it through the tests
60     try:
61         set(modulename, name, default)
62     except:
63         del _settings[modulename][name]
64         import sys
65         raise sys.exc_info()[0], sys.exc_info()[1] # re-raise it
66
67 def set(modulename, name, value):
68     """Set a variable's value.
69
70     Sets the value for a variable of the specified module.
71     modulename - The name of the module, e.g. 'focus'
72     name - The name of the variable, e.g. 'my_variable'
73     value - The new value for the variable.
74     """
75     modulename = str(modulename).lower()
76     name = str(name).lower()
77
78     # proper value checking for 'boolean's
79     if _settings[modulename][name]['type'] == 'boolean':
80         if not (value == 0 or value == 1):
81             raise ValueError, 'Attempted to set ' + name + ' to a value of '+\
82                   str(value) + ' but boolean variables can only contain 0 or'+\
83                   ' 1.'
84
85     # proper value checking for 'enum's
86     elif _settings[modulename][name]['type'] == 'enum':
87         options = _settings[modulename][name]['options']
88         if not value in options:
89             raise ValueError, 'Attempted to set ' + name + ' to a value of '+\
90                   str(value) + ' but this is not one of the possible values '+\
91                   'for this enum variable. Possible values are: ' +\
92                   str(options) + "."
93
94     # min/max checking for 'integer's
95     elif _settings[modulename][name]['type'] == 'integer':
96         try:
97             min = _settings[modulename][name]['min']
98             if value < min:
99                 raise ValueError, 'Attempted to set ' + name + ' to a value '+\
100                       ' of ' + str(value) + ' but it has a minimum value ' +\
101                       ' of ' + str(min) + '.'
102         except KeyError: pass
103         try:
104             max = _settings[modulename][name]['max']
105             if value > max:
106                 raise ValueError, 'Attempted to set ' + name + ' to a value '+\
107                       ' of ' + str(value) + ' but it has a maximum value ' +\
108                       ' of ' + str(min) + '.'
109         except KeyError: pass
110     
111     _settings[modulename][name]['value'] = value
112
113 def reset(modulename, name):
114     """Reset a variable to its default value.
115
116     Resets the value for a variable in the specified module back to its
117     original (default) value.
118     modulename - The name of the module, e.g. 'focus'
119     name - The name of the variable, e.g. 'my_variable'
120     """
121     modulename = str(modulename).lower()
122     name = str(name).lower()
123     _settings[modulename][name]['value'] = \
124                                  _settings[modulename][name]['default']
125
126 def get(modulename, name):
127     """Returns the value of a variable.
128
129     Returns the current value for a variable in the specified module.
130     modulename - The name of the module, e.g. 'focus'
131     name - The name of the variable, e.g. 'my variable'
132     """
133     modulename = str(modulename).lower()
134     name = str(name).lower()
135     return _settings[modulename][name]['value']
136
137 #---------------------------- Internals ---------------------------
138
139 """The main configuration dictionary, which holds sub-dictionaries for each
140    module.
141
142    The format for entries in here like this (for a string):
143    _settings['modulename']['varname']['name'] = 'Text Label'
144    _settings['modulename']['varname']['description'] = 'Does this'
145    _settings['modulename']['varname']['type'] = 'string'
146    _settings['modulename']['varname']['default'] = 'Foo'
147    _settings['modulename']['varname']['value'] = 'Foo'
148                              # 'value' should always be initialized to the same
149                              # value as the 'default' field!
150
151    Here's an example of an enum:
152    _settings['modulename']['varname']['name'] = 'My Enum Variable'
153    _settings['modulename']['varname']['description'] = 'Does Enum-like things.'
154    _settings['modulename']['varname']['type'] = 'enum'
155    _settings['modulename']['varname']['default'] = \
156      _settings['modulename']['varname']['value'] = [ 'Blue', 'Green', 'Pink' ]
157
158    And Here's an example of an integer with bounds:
159    _settings['modulename']['varname']['name'] = 'A Bounded Integer'
160    _settings['modulename']['varname']['description'] = 'A fierce party animal!'
161    _settings['modulename']['varname']['type'] = 'integer'
162    _settings['modulename']['varname']['default'] = \
163      _settings['modulename']['varname']['value'] = 0
164    _settings['modulename']['varname']['min'] = 0
165    _settings['modulename']['varname']['max'] = 49
166
167    Hopefully you get the idea.
168    """
169 _settings = {}
170
171 """Valid values for a variable's type."""
172 _types = [ 'boolean', # Boolean types can only hold a value of 0 or 1.
173            
174            'enum',    # Enum types hold a value from a list of possible values.
175                       # An 'options' field *must* be provided for enums,
176                       # containing a list of possible values for the variable.
177
178            'integer', # Integer types hold a single number, as well as a 'min'
179                       # and 'max' property.
180                       # If the 'min' or 'max' is ignore then bounds checking
181                       # will not be performed in that direction.
182
183            'string',  # String types hold a text string.
184
185            'file',    # File types hold a file object.
186
187            'function',# Function types hold any callable object.
188
189            'object'   # Object types can hold any python object.
190            ];
191   
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 #############################################################################
209 ### Options that can be changed to adjust the behavior of Openbox.        ###
210 #############################################################################
211
212 THEME = "/usr/local/share/openbox/styles/fieron2"
213 """The theme used to decorate everything."""
214
215 #TITLEBAR_LAYOUT = [ "icon", "title", "alldesktops", "iconify", "maximize", "close" ]
216 TITLEBAR_LAYOUT = "DITMC"
217 """The layout of the buttons/label on client titlebars, can be made up of the
218 following:
219     I - iconify button
220     L - text label
221     M - maximize button,
222     D - all-desktops button
223     C - close button
224 If no 'L' is included in the string, one will be added to the end by
225 Openbox."""
226
227 DOUBLE_CLICK_DELAY = 300
228 """The number of milliseconds in which 2 clicks are perceived as a
229 double-click."""
230
231 DRAG_THRESHOLD = 3
232 """The amount of pixels that you have to drag the mouse before motion events
233 will start occuring."""
234
235 DESKTOP_NAMES = ["one", "two", "three", "four", "five", "six", "seven", \
236                  "eight", "nine", "ten", "eleven", "twelve"]
237 """The name of each desktop."""
238
239 NUMBER_OF_DESKTOPS = 4
240 """The number of desktops/workspaces which can be scrolled between."""
241
242 #############################################################################
243
244 print "Loaded config.py"