X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=scripts%2Fconfig.py;h=97a455966121f6adc787c046bec1bebb066ebfdd;hb=216a04bdd057c03a719a0908cd003503b4f73fdb;hp=a0f3b603ae3d7bc0a33c2b4042bdd6c6428f8166;hpb=50002f2ceb4234145f3977bb14752dc930ada26c;p=chaz%2Fopenbox diff --git a/scripts/config.py b/scripts/config.py index a0f3b603..97a45596 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -1,3 +1,210 @@ +# Openbox's config system. Please use the defined functions instead of +# accessing the internal data structures directly, for the sake of us all. + +import ob + +def add(modulename, name, friendlyname, description, type, default, + **keywords): + """Add a variable to the configuration system. + + Add a variable to the configuration system for a module. + modulename - The name of the module, e.g. 'focus' + name - The name of the variable, e.g. 'my_variable' + friendlyname - The user-friendly name of the variable, e.g. 'My Variable' + description - The detailed destription of the variable, e.g. 'Does Things' + type - The type of the variable, one of: + - 'boolean' + - 'enum' + - 'integer' + - 'string' + - 'file' + - 'function' + - 'object' + default - The default value for the variable, e.g. 300 + keywords - Extra keyword=value pairs to further define the variable. These + can be: + - For 'enum' types: + - options : A list of possible options for the variable. + This *must* be set for all enum variables. + - For 'integer' types: + - min : The minimum value for the variable. + - max : The maximum value for the variable. + """ + modulename = str(modulename).lower() + name = str(name).lower() + friendlyname = str(friendlyname) + description = str(description) + type = str(type).lower() + + # make sure the sub-dicts exist + try: + _settings[modulename] + try: + _settings[modulename][name] + except KeyError: + _settings[modulename][name] = {} + except KeyError: + _settings[modulename] = {} + _settings[modulename][name] = {} + + # add the keywords first as they are used for the tests in set() + for key,value in zip(keywords.keys(), keywords.values()): + _settings[modulename][name][key] = value + + _settings[modulename][name]['name'] = friendlyname + _settings[modulename][name]['description'] = description + _settings[modulename][name]['type'] = type + _settings[modulename][name]['default'] = default + + # put it through the tests + try: + set(modulename, name, default) + except: + del _settings[modulename][name] + import sys + raise sys.exc_info()[0], sys.exc_info()[1] # re-raise it + +def set(modulename, name, value): + """Set a variable's value. + + Sets the value for a variable of the specified module. + modulename - The name of the module, e.g. 'focus' + name - The name of the variable, e.g. 'my_variable' + value - The new value for the variable. + """ + modulename = str(modulename).lower() + name = str(name).lower() + + # proper value checking for 'boolean's + if _settings[modulename][name]['type'] == 'boolean': + if not (value == 0 or value == 1): + raise ValueError, 'Attempted to set ' + name + ' to a value of '+\ + str(value) + ' but boolean variables can only contain 0 or'+\ + ' 1.' + + # proper value checking for 'enum's + elif _settings[modulename][name]['type'] == 'enum': + options = _settings[modulename][name]['options'] + if not value in options: + raise ValueError, 'Attempted to set ' + name + ' to a value of '+\ + str(value) + ' but this is not one of the possible values '+\ + 'for this enum variable. Possible values are: ' +\ + str(options) + "." + + # min/max checking for 'integer's + elif _settings[modulename][name]['type'] == 'integer': + try: + min = _settings[modulename][name]['min'] + if value < min: + raise ValueError, 'Attempted to set ' + name + ' to a value '+\ + ' of ' + str(value) + ' but it has a minimum value ' +\ + ' of ' + str(min) + '.' + except KeyError: pass + try: + max = _settings[modulename][name]['max'] + if value > max: + raise ValueError, 'Attempted to set ' + name + ' to a value '+\ + ' of ' + str(value) + ' but it has a maximum value ' +\ + ' of ' + str(min) + '.' + except KeyError: pass + + _settings[modulename][name]['value'] = value + +def reset(modulename, name): + """Reset a variable to its default value. + + Resets the value for a variable in the specified module back to its + original (default) value. + modulename - The name of the module, e.g. 'focus' + name - The name of the variable, e.g. 'my_variable' + """ + modulename = str(modulename).lower() + name = str(name).lower() + _settings[modulename][name]['value'] = \ + _settings[modulename][name]['default'] + +def get(modulename, name): + """Returns the value of a variable. + + Returns the current value for a variable in the specified module. + modulename - The name of the module, e.g. 'focus' + name - The name of the variable, e.g. 'my variable' + """ + modulename = str(modulename).lower() + name = str(name).lower() + return _settings[modulename][name]['value'] + +#---------------------------- Internals --------------------------- + +"""The main configuration dictionary, which holds sub-dictionaries for each + module. + + The format for entries in here like this (for a string): + _settings['modulename']['varname']['name'] = 'Text Label' + _settings['modulename']['varname']['description'] = 'Does this' + _settings['modulename']['varname']['type'] = 'string' + _settings['modulename']['varname']['default'] = 'Foo' + _settings['modulename']['varname']['value'] = 'Foo' + # 'value' should always be initialized to the same + # value as the 'default' field! + + Here's an example of an enum: + _settings['modulename']['varname']['name'] = 'My Enum Variable' + _settings['modulename']['varname']['description'] = 'Does Enum-like things.' + _settings['modulename']['varname']['type'] = 'enum' + _settings['modulename']['varname']['default'] = \ + _settings['modulename']['varname']['value'] = [ 'Blue', 'Green', 'Pink' ] + + And Here's an example of an integer with bounds: + _settings['modulename']['varname']['name'] = 'A Bounded Integer' + _settings['modulename']['varname']['description'] = 'A fierce party animal!' + _settings['modulename']['varname']['type'] = 'integer' + _settings['modulename']['varname']['default'] = \ + _settings['modulename']['varname']['value'] = 0 + _settings['modulename']['varname']['min'] = 0 + _settings['modulename']['varname']['max'] = 49 + + Hopefully you get the idea. + """ +_settings = {} + +"""Valid values for a variable's type.""" +_types = [ 'boolean', # Boolean types can only hold a value of 0 or 1. + + 'enum', # Enum types hold a value from a list of possible values. + # An 'options' field *must* be provided for enums, + # containing a list of possible values for the variable. + + 'integer', # Integer types hold a single number, as well as a 'min' + # and 'max' property. + # If the 'min' or 'max' is ignore then bounds checking + # will not be performed in that direction. + + 'string', # String types hold a text string. + + 'file', # File types hold a file object. + + 'function',# Function types hold any callable object. + + 'object' # Object types can hold any python object. + ]; + + + + + + + + + + + + + + + + + ############################################################################# ### Options that can be changed to adjust the behavior of Openbox. ### ############################################################################# @@ -6,7 +213,7 @@ THEME = "/usr/local/share/openbox/styles/fieron2" """The theme used to decorate everything.""" #TITLEBAR_LAYOUT = [ "icon", "title", "alldesktops", "iconify", "maximize", "close" ] -TITLEBAR_LAYOUT = "NTIMC" +TITLEBAR_LAYOUT = "DITMC" """The layout of the buttons/label on client titlebars, can be made up of the following: I - iconify button @@ -32,65 +239,6 @@ DESKTOP_NAMES = ["one", "two", "three", "four", "five", "six", "seven", \ NUMBER_OF_DESKTOPS = 4 """The number of desktops/workspaces which can be scrolled between.""" -DEFAULT_ICON_WIDTH = 16 -"""The width of the default icon.""" -DEFAULT_ICON_HEIGHT = 16 -"""The height of the default icon.""" -DEFAULT_ICON = \ -"\377\377\377\0SSS\377\251\251\251\377\251\251\251\377\251\251\251\377\251"+\ -"\251\251\377\251\251\251\377\251\251\251\377\251\251\251\377\251\251\251"+\ -"\377\251\251\251\377\251\251\251\377\216\216\216\377bbb\377\366\366\366\0"+\ -"\377\377\377\0\377\377\377\0SSS\377\377\377\377\377\377\377\377\377\377\377"+\ -"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\376\376\376\377"+\ -"\376\376\376\377\373\373\373\377\371\371\370\377\302\302\302\377\244\244"+\ -"\244\377EEE\377\377\377\377\0\377\377\377\0SSS\377\377\377\377\377\377\377"+\ -"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\376\376\376\377"+\ -"\374\374\374\377\372\372\372\377\367\367\367\377\365\365\365\377\265\265"+\ -"\265\377``_\377\30\30\30\377\377\377\377\0\377\377\377\0SSS\377\377\377\377"+\ -"\377\377\377\377\377\377\377\377\377\376\376\376\377\374\374\374\377\372"+\ -"\372\372\377\367\367\367\377\365\365\365\377\363\363\362\377\361\361\360"+\ -"\377\356\356\355\377\354\354\351\377jjj\377\377\377\377\0\377\377\377\0S"+\ -"SS\377\377\377\377\377\377\377\377\377\375\375\375\377\373\373\373\377\371"+\ -"\371\370\377\366\366\366\377\364\364\363\377\362\362\361\377\357\357\356"+\ -"\377\355\355\354\377\352\352\351\377\350\350\347\377mmm\377\377\377\377\0"+\ -"\377\377\377\0SSS\377\376\376\376\377\373\373\373\377\371\371\370\377\366"+\ -"\366\366\377\364\364\363\377\362\362\361\377\357\357\356\377\355\355\354"+\ -"\377\352\352\351\377\350\350\347\377\346\346\343\377\344\344\341\377lll\377"+\ -"\377\377\377\0\377\377\377\0SSS\377\375\375\375\377\370\370\367\377\365\365"+\ -"\364\377\363\363\362\377\360\360\357\377\356\356\355\377\354\354\351\377"+\ -"\352\352\347\377\347\347\344\377\345\345\342\377\342\342\337\377\340\340"+\ -"\335\377lll\377\377\377\377\0\377\377\377\0SSS\377\373\373\373\377\363\363"+\ -"\362\377\360\360\357\377\356\356\355\377\354\354\351\377\351\351\350\377"+\ -"\347\347\344\377\345\345\342\377\342\342\337\377\340\340\335\377\336\336"+\ -"\332\377\333\333\330\377llk\377\377\377\377\0\377\377\377\0SSS\377\372\372"+\ -"\372\377\357\357\356\377\355\355\352\377\353\353\350\377\350\350\345\377"+\ -"\346\346\343\377\343\343\340\377\341\341\336\377\337\337\333\377\335\335"+\ -"\331\377\332\332\326\377\330\330\324\377kkj\377\377\377\377\0\377\377\377"+\ -"\0SSS\377\370\370\370\377\353\353\350\377\350\350\345\377\346\346\343\377"+\ -"\343\343\340\377\341\341\336\377\337\337\333\377\335\335\331\377\332\332"+\ -"\326\377\330\330\324\377\325\325\321\377\323\323\317\377kkk\377\377\377\377"+\ -"\0\377\377\377\0SSS\377\367\367\367\377\347\347\344\377\345\345\342\377\342"+\ -"\342\337\377\340\340\335\377\336\336\332\377\333\333\327\377\331\331\325"+\ -"\377\326\326\322\377\324\324\320\377\322\322\315\377\320\320\313\377jjj\377"+\ -"\377\377\377\0\377\377\377\0SSS\377\366\366\365\377\342\342\337\377\340\340"+\ -"\335\377\336\336\332\377\333\333\330\377\331\331\325\377\326\326\322\377"+\ -"\324\324\320\377\322\322\315\377\320\320\313\377\315\315\310\377\314\314"+\ -"\307\377llk\377\377\377\377\0\377\377\377\0SSS\377\364\364\364\377\337\337"+\ -"\333\377\334\334\331\377\332\332\326\377\330\330\324\377\326\326\321\377"+\ -"\323\323\317\377\321\321\314\377\316\316\311\377\315\315\307\377\314\314"+\ -"\306\377\314\314\306\377jjj\377\377\377\377\0\377\377\377\0SSS\377\344\344"+\ -"\343\377\316\316\312\377\312\312\306\377\304\304\300\377\302\302\276\377"+\ -"\301\301\274\377\274\274\267\377\267\267\263\377\266\266\261\377\265\265"+\ -"\261\377\265\265\260\377\263\263\256\377[[[\377\377\377\377\0\377\377\377"+\ -"\0SSS\377AAA\377;;;\377998\377554\377554\377552\377110\377..-\377..-\377"+\ -"---\377--,\377+++\377333\377\377\377\377\0\377\377\377\0\377\377\377\0\377"+\ -"\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"+\ -"\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"+\ -"\0\377\377\377\0\377\377\377\0\377\377\377\0" -"""The icon which will be used when an application doesn't supply one. This - was generated by making the icon in gimp (with an alpha channel!) and saving - it as a C source file.""" - ############################################################################# print "Loaded config.py"