Classes to group some actions in Python

柔情痞子 提交于 2019-12-24 15:14:02

问题


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 :)


回答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.




回答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 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.



来源:https://stackoverflow.com/questions/8999943/classes-to-group-some-actions-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!