I am writing configuration program for my own Linux distribution. The configuration is divided into sections: general, networking, session, etc. - which groups similar options. E.g. in section general there are computer name, description, workgroup, language options.
I think every section should be presented by the class, and each option should have corresponding property (getter and maybe setter).
Moreover it would be nice for generalization if there was function to test if given option is enabled (i.e. if system meets requirements for this option) i.e.:
class General(object):
@property
def name(self):
return self.get_computer_name()
@name.setter
def name(self, name):
self.set_computer_name(name)
def is_option_enabled(self, option):
return True_or_False
What is more I need these options (and sections too) link together with corresponding translated name and description (using gettext).
I know that hard coding presentation layer in classes is not good idea...
I need something like design pattern, general idea/template how to implement this so it was high quality, easy to manage and extend.
I am going to create several front ends for these classes (text, gui, web) so I don't want every time to repeat same code.
I am thinking very hard, but unfortunately I don't have idea how to do this or have some doubts...
Thank you :)
解决方案
I assume your configurations builds a tree with nodes beings sections and leaf being configuration options.
Given that setup you can represent a 2 depth deep configuration like network with the following classes using a declarative API:
class InterfaceConfiguration(Configuration):
mask = IPField()
dns = IPField()
IP = IPField()
dhcp = BooleanField()
driver = ChoiceField(choices=('madwifi', 'atheros', 'whatever'))
class NetworkConfiguration(Configuration):
eth0 = InterfaceConfiguration(verbose_name='network interface eth0')
eth1 = InterfaceConfiguration(verbose_name='network interface eth1')
wlan0 = InterfaceConfiguration(verboxe_name='wireless network interface wlan0')
hostname = StringField()
domain = StringField()
This kind of declarative API is achieved with metaclasses have a look at dictshield, and the many ORMs and more that implements such feature.
Given this set of class you would be able to manipulate them as follow:
>>> configuration = NetworkConfiguration('/path/to/config/file')
>>> configuration.eth0.verbose_name
'network interface eth0'
>>> configuration.eth0.mask.set('192.168.0.255')
True
>>> configuration.eth0.driver.choices
('madwifi', 'atheros', 'whatever')
>>> configuration.hostname.set('amokrane')
>>> configuration.domain.set('imazighen')
>>> configuration.wlan0.dhcp.get_value()
True
This kind of API is simpler to implement and doesn't require specific python construction (see below) and provide the ability to have other methods besides get and set.
If you don't need other methods besides get/set you can use python descriptors to implement the different kind of fields, I recommand you read the article Python attributes and methods about the subject and have deeper looks and the above links about Python ORMs since that is the used method.