|
From: Nicolas M. <nic...@le...> - 2011-06-20 15:16:45
|
Hello,
I have a problem understanding subsections with configobj.
Here is the structure of my config file :
[__main_section_1__]
key1 = string
key2 = string
key3 = string
[[__subsection_1__]]
sub_key_1 = string
sub_key_2 = string
(So there is an unlimited number of main section in which I have some
subsections ; at least one subsection by main section)
What I need to do is a loop on each subsection to do some job. I can't
do it (I'm new with Python so please excuse me if my question seems silly).
something like :
for subsections in main_section:
my_code
Here is a structure sample of my current config file :
ConfigObj({'opghl': {'acc_user': 'openerp', 'prod_user': 'openerp',
'acc_host': 'ghl-dev.lan.pcsol.be', 'db_prod': {'prod_db_name': 'ghl',
'acc_db_name': 'ghl'}, 'db_prod_2': {'prod_db_name': 'ghl2',
'acc_db_name': 'ghl2'}}, 'test': {'acc_user': 'openerp-test',
'prod_user': 'openerp-test', 'acc_host': 'test-dev.lan.pcsol.be',
'db_prod-test': {'prod_db_name': 'ghl-test', 'acc_db_name': 'ghl-test'},
'db_prod_2-test': {'prod_db_name': 'ghl2-test', 'acc_db_name':
'ghl2-test'}}})
==> I would want to list all the subsection of opghl or test. In that
case it would be
- for opghl : db_prod and db_prod_2
- for test : db_prod-test, db_prod_2-test
Thank you a lot for your help!
sylock
|
|
From: Nicolas M. <nic...@le...> - 2011-06-20 15:29:46
|
I just find the answer by reading this : http://www.voidspace.org.uk/python/articles/configobj_for_data_persistence.shtml#creating-a-configspec So here is what I did : for sub in (keys for keys in config['opghl'] if isinstance(config['opghl'][keys],dict)): print sub Then it prints me : db_prod db_prod_2 (See my first mail below) But I was wondering if you can't implement a method to do it more easily or maybe to document it better in the official doc : http://www.voidspace.org.uk/python/configobj.html Is this the better way to achieve mu goal? Or there is a more pythonic way? Regards, sylock On 06/20/2011 04:49 PM, Nicolas Michel wrote: > Hello, > > I have a problem understanding subsections with configobj. > > Here is the structure of my config file : > > [__main_section_1__] > key1 = string > key2 = string > key3 = string > [[__subsection_1__]] > sub_key_1 = string > sub_key_2 = string > > (So there is an unlimited number of main section in which I have some > subsections ; at least one subsection by main section) > > What I need to do is a loop on each subsection to do some job. I can't > do it (I'm new with Python so please excuse me if my question seems silly). > > something like : > for subsections in main_section: > my_code > > Here is a structure sample of my current config file : > > ConfigObj({'opghl': {'acc_user': 'openerp', 'prod_user': 'openerp', > 'acc_host': 'ghl-dev.lan.pcsol.be', 'db_prod': {'prod_db_name': 'ghl', > 'acc_db_name': 'ghl'}, 'db_prod_2': {'prod_db_name': 'ghl2', > 'acc_db_name': 'ghl2'}}, 'test': {'acc_user': 'openerp-test', > 'prod_user': 'openerp-test', 'acc_host': 'test-dev.lan.pcsol.be', > 'db_prod-test': {'prod_db_name': 'ghl-test', 'acc_db_name': 'ghl-test'}, > 'db_prod_2-test': {'prod_db_name': 'ghl2-test', 'acc_db_name': > 'ghl2-test'}}}) > > ==> I would want to list all the subsection of opghl or test. In that > case it would be > - for opghl : db_prod and db_prod_2 > - for test : db_prod-test, db_prod_2-test > > Thank you a lot for your help! > sylock > > ------------------------------------------------------------------------------ > EditLive Enterprise is the world's most technically advanced content > authoring tool. Experience the power of Track Changes, Inline Image > Editing and ensure content is compliant with Accessibility Checking. > http://p.sf.net/sfu/ephox-dev2dev > _______________________________________________ > Configobj-develop mailing list > Con...@li... > https://lists.sourceforge.net/lists/listinfo/configobj-develop |
|
From: Michael F. <fuz...@vo...> - 2011-06-25 19:36:44
|
On 20/06/2011 15:49, Nicolas Michel wrote:
> Hello,
>
> I have a problem understanding subsections with configobj.
>
> Here is the structure of my config file :
>
> [__main_section_1__]
> key1 = string
> key2 = string
> key3 = string
> [[__subsection_1__]]
> sub_key_1 = string
> sub_key_2 = string
>
> (So there is an unlimited number of main section in which I have some
> subsections ; at least one subsection by main section)
>
> What I need to do is a loop on each subsection to do some job. I can't
> do it (I'm new with Python so please excuse me if my question seems silly).
>
> something like :
> for subsections in main_section:
> my_code
>
> Here is a structure sample of my current config file :
>
> ConfigObj({'opghl': {'acc_user': 'openerp', 'prod_user': 'openerp',
> 'acc_host': 'ghl-dev.lan.pcsol.be', 'db_prod': {'prod_db_name': 'ghl',
> 'acc_db_name': 'ghl'}, 'db_prod_2': {'prod_db_name': 'ghl2',
> 'acc_db_name': 'ghl2'}}, 'test': {'acc_user': 'openerp-test',
> 'prod_user': 'openerp-test', 'acc_host': 'test-dev.lan.pcsol.be',
> 'db_prod-test': {'prod_db_name': 'ghl-test', 'acc_db_name': 'ghl-test'},
> 'db_prod_2-test': {'prod_db_name': 'ghl2-test', 'acc_db_name':
> 'ghl2-test'}}})
>
> ==> I would want to list all the subsection of opghl or test. In that
> case it would be
> - for opghl : db_prod and db_prod_2
> - for test : db_prod-test, db_prod_2-test
>
Hello Nicolas,
Sorry for the late reply. You can tell what sub-sections are in a
section with the '.sections' attribute. This is a list of strings that
you can loop over.
Here's an example:
>>> from configobj import ConfigObj
>>> c = ConfigObj({'opghl': {'acc_user': 'openerp', 'prod_user':
'openerp',
... 'acc_host': 'ghl-dev.lan.pcsol.be', 'db_prod': {'prod_db_name': 'ghl',
... 'acc_db_name': 'ghl'}, 'db_prod_2': {'prod_db_name': 'ghl2',
... 'acc_db_name': 'ghl2'}}, 'test': {'acc_user': 'openerp-test',
... 'prod_user': 'openerp-test', 'acc_host': 'test-dev.lan.pcsol.be',
... 'db_prod-test': {'prod_db_name': 'ghl-test', 'acc_db_name':
'ghl-test'},
... 'db_prod_2-test': {'prod_db_name': 'ghl2-test', 'acc_db_name':
... 'ghl2-test'}}})
>>> c.sections
['test', 'opghl']
>>> test = c['test']
>>> test.sections
['db_prod_2-test', 'db_prod-test']
>>> for section in test.sections:
... print test[section]
...
{'acc_db_name': 'ghl2-test', 'prod_db_name': 'ghl2-test'}
{'acc_db_name': 'ghl-test', 'prod_db_name': 'ghl-test'}
The '.sections' attribute is documented here:
http://www.voidspace.org.uk/python/configobj.html#section-attributes
I hope this is helpful.
All the best,
Michael Foord
> Thank you a lot for your help!
> sylock
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
|