A configuration manager using wx as it's front ent.
It all starts with a ConfManager object:
>>> from confmanager import ConfManager >>> config = ConfManager('Test')
You can use the resulting object to get and set options from any of the added secions.
To add a section, as with ConfigParser, use the add_section method:
Now this will show up with the sections method:
>>> config.sections() ['login']
To see which options are in a given section, use the get method, like so:
>>> config.options('login') 
The GUI uses a wx.Notebook object to represent the different sections of the config object. The first page of this particular configuration object will be labeled “&Login”.
By default, ConfManager will first prepend the section name with an amposand (&) to give it a shortcut key in accordance with wx, then apply Python’s String title() method to capitalise it properly. If this is undesireable you can change it with the friendly_name keyword argument:
>>> config.add_section('login', '&Login Options')
You can set values using the set method, like so:
>>> config.set('login', 'username', 'john')
The main three arguments are:
- section - The section the option should belong to.
- option - The name of the option which should be set.
- value - The option value.
If you used config.get_gui() to get a wx.Frame object which could be used to set the options from a GUI, the resulting text field would have “&Username” as it’s label. If you are not happy with this, ConfManager will let you specify a custom title:
>>> config.set('login', 'username', 'john', title = 'Your &Username')
This is great for strings, but what about other types?
|base class||Default Control|
Of course, you can use any control you like by providing it in the control argument to the set method:
>>> from wx.lib.filebrowsebutton import FileBrowseButton as FBB >>> config.set('login', 'commands', '', title = '&Initial commands to send to the server', control = FBB)
Now when you load the GUI you’ll have the standard browse button for your opperating system.
A note on providing custom controls: Ensure your control has both GetValue and SetValue methods. Without these, confmanager will fail. This is easily gotten around by subclassing:
>>> class MyChoice(wx.Choice): ... def GetValue(): ... return self.GetStrinSelection() ... def SetValue(self, value): ... return self.SetStringSelection(value) ... >>> config.set('login', 'type', 'http', title = 'Login &Type', control = MyChoice, kwargs = dict(choices = ['HTTP', 'HTTPS']))
Customise the control
If you wanted to pass extra arguments to the control, you can do that easily with the args and kwargs arguments:
config.set('login', 'timeout', 30.0, title = '&Time before the connection is dropped', args = , kwargs = dict(digits = 2))
Now when the GUI is instantiated, the control for timeout will be created as:
>>> wx.lib.agw.floatspin.FloatSpin(page, 12345, digits = 3)
Page is the page of the gui which the control should be assigned too, and what our args parameter above has done is provide a custom ID to the control. We could have achieved the same affect with:
config.set('login', 'timeout', 30.0, title = '&Time before the connection is dropped', kwargs = dict(id = 12345, digits = 2))
If you want to constrain the values a user can provide, use the validate argument, providing a lambda, like so:
config.set('login', 'name', '', title = '&Friendly Name', validate = lambda value: None if value.istitle() else 'Friendly names must be provided in title case.')
If a validation function returns None or False, it is assumed that no errors were encountered. If a value is returned, or the function causes a traceback, it is assumed there is a problem, and an error box containing either the str()’d version of the result or the exception is returned, and the GUI will not close unless the cancel button is pressed.
If you provide a help keyword argument to the set method, that argument will be put on the control using the SetHelpText method.
Excluding options from the GUI
If you provide include = False to the set method then that option will be omitted from the GUI.
Please note that if a section contains nothing but excluded options then an empty page will be created on the resulting GUI.
Retrieving Option Values
To get the value for a given option, use the get method.
>>> config.get('login', 'username') 'john'
>>> config.get('login', 'fake') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "confmanager\__init__.py", line 148, in get raise NoOptionError(option) confmanager.errors.NoOptionError: There is no option named fake. >>> config.get('login', 'fake', False) False
Getting A GUI
To get a wx.Frame which you can show or hide, use the get_gui method:
>>> config.get_gui() <confmanager.confframe.ConfFrame; proxy of <Swig Object of type 'wxFrame *' at 0x3769d68> > Pro Tip:
If you do this without a wx.App object created it will raise wx._core.PyNoAppError.
Confmanager objects have a number of useful properties. These are listed below:
- updateFunc This is a function which should be prepared to take three arguments: section, option, and value. You can use this to update things in real time as configuration options change.
This function will be called whenever the set method is used.
### Pro Tip:
The set method is called whenever the Apply or OK buttons are clicked in the config frame, not when a value is set in that frame.
config This property is the raw configuration data as an OrderedDict.
control_types This is an OrderedDict of control types stored as baseclass:control pairs.
toggle Toggles a boolean value.
remove_section Completely removes a section from the object.
remove_option Removes an option from the provided section.
get_spec Used internally to get the full specification for a given option in the provided section.
section_names A list of real:friendly section names.
title The title of the config GUI.
helpFunc A function which is bound to the help button of the config GUI.
get_dump This method returns the sections, options and their values as a raw Python dictionary which can safely be dumped with json or some other storage method.
This module allows you to import saves data into confmanager objects.
The most useful method is the parse_json method.
To use it, pass it a dictionary and a pre-configured confmanager object and it will import only thos values from the dictionary that are already present.
If you want it to import all the values it finds you can alter it’s behaviour with the create_sections and the create_options arguments which (if True) will create previously unknown sections anv values respectively.