Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I assume your configurations builds a tree with nodes beings sections and leaf being configuration options.</p> <p>Given that setup you can represent a 2 depth deep configuration like network with the following classes using a declarative API:</p> <pre><code>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() </code></pre> <p>This kind of declarative API is achieved with <a href="https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">metaclasses</a> have a look at <a href="https://github.com/j2labs/dictshield/blob/master/dictshield/document.py" rel="nofollow noreferrer">dictshield</a>, and the <a href="http://code.google.com/p/appengine-ndb-experiment/source/browse/ndb/model.py#2151" rel="nofollow noreferrer">many</a> <a href="https://bitbucket.org/zzzeek/sqlalchemy/src/f2c79e9a0bde/lib/sqlalchemy/ext/declarative.py" rel="nofollow noreferrer">ORMs</a> and <a href="https://bitbucket.org/abki/graffiti/src/2280f7f39328/graffiti/elements.py" rel="nofollow noreferrer">more</a> that implements such feature.</p> <p>Given this set of class you would be able to manipulate them as follow:</p> <pre><code>&gt;&gt;&gt; configuration = NetworkConfiguration('/path/to/config/file') &gt;&gt;&gt; configuration.eth0.verbose_name 'network interface eth0' &gt;&gt;&gt; configuration.eth0.mask.set('192.168.0.255') True &gt;&gt;&gt; configuration.eth0.driver.choices ('madwifi', 'atheros', 'whatever') &gt;&gt;&gt; configuration.hostname.set('amokrane') &gt;&gt;&gt; configuration.domain.set('imazighen') &gt;&gt;&gt; configuration.wlan0.dhcp.get_value() True </code></pre> <p>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.</p> <p>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 <a href="http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html" rel="nofollow noreferrer">Python attributes and methods</a> about the subject and have deeper looks and the above links about Python ORMs since that is the used method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload