Skip to content Skip to sidebar Skip to footer

Classes To Group Some Actions In Python

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

Solution 1:

I'd say that your project looks quite ambitious.

And using a class for every section doesn't seems a good idea to me, because it basically means that adding, removing or modifying sections will require changes to the code, it's not dynamic, and implies the release of a new version incomptable with the previous one.

It would be better if every section will be treated in the same way.

For the core I think you'll need:

  1. A protected file with the declaration of the acceptable sections and options
  2. A configuration holder with access to such file.
  3. A loader for your config files:
    • Read/parse your config files.
    • Build your configuration holder.
  4. Commands with access to your configuration holder:
    • An abstract BaseCommand that implements this access,
    • Subclasses for every actual command: Get, Add, Remove.

Then every front-ends will just be an interface for those commands.

Solution 2:

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 manyORMs 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.

Post a Comment for "Classes To Group Some Actions In Python"