Created
November 25, 2015 17:27
-
-
Save pdehaan/ed6f9fbd85b0e0ff47a9 to your computer and use it in GitHub Desktop.
Playing w/ Python 2's ConfigParser module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [DEFAULT] | |
| envs: dev,stage, pre-prod | |
| base_url: hello.firefox.com | |
| [SectionOne] | |
| Status: Single | |
| Name: Derek | |
| Value: Yes | |
| Age: 30 | |
| Single: True | |
| foo : buzz | |
| envs: dev,stage | |
| version_url: %(base_url)s/__version__ | |
| [SectionTwo] | |
| FavoriteColor=Green | |
| skip = false | |
| [stage] | |
| root = call.stage.mozaws.net | |
| root/config.js = %(root)s/config.js | |
| loop_server = loop.stage.mozaws.net | |
| loop_server/push_config = %(loop_server)s/push-server-config | |
| [prod] | |
| root = hello.firefox.com | |
| root/config.js = %(root)s/config.js | |
| loop_server = loop.services.mozilla.com | |
| loop_server/push_config = %(loop_server)s/push-server-config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ConfigParser | |
| import csv | |
| def parseCsv(input): | |
| values = [] | |
| parser = csv.reader([input]) | |
| for fields in parser: | |
| for i, field in enumerate(fields): | |
| values.append(field.strip()) | |
| return values | |
| def dumpConfig(config): | |
| print("---") | |
| for section in config.sections(): | |
| print("\n[%s]" % section) | |
| for option in config.options(section): | |
| print(' %s => %s' % (option, config.get(section, option))) | |
| Config = ConfigParser.ConfigParser() | |
| Config.read("./config.ini") | |
| print("sections: %s" % Config.sections()) | |
| print("[SectionOne] options: %s" % Config.options("SectionOne")) | |
| print("") | |
| for key in Config.options('SectionOne'): | |
| print("key %s => %s" % (key, Config.get('SectionOne', key))) | |
| print("SectionOne `envs` => %s" % parseCsv(Config.get('SectionOne', 'envs'))) | |
| print("SectionTwo `envs` => %s" % parseCsv(Config.get('SectionTwo', 'envs'))) | |
| print("") | |
| dumpConfig(Config) | |
| """ | |
| for env in Config.get('SectionOne', 'envs').split(','): | |
| print("got env %s" % env) | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ python config.py | |
| sections: ['SectionOne', 'SectionTwo', 'stage', 'prod'] | |
| [SectionOne] options: ['status', 'name', 'value', 'age', 'single', 'foo', 'envs', 'version_url', 'base_url'] | |
| key status => Single | |
| key name => Derek | |
| key value => Yes | |
| key age => 30 | |
| key single => True | |
| key foo => buzz | |
| key envs => dev,stage | |
| key version_url => hello.firefox.com/__version__ | |
| key base_url => hello.firefox.com | |
| SectionOne `envs` => ['dev', 'stage'] | |
| SectionTwo `envs` => ['dev', 'stage', 'pre-prod'] | |
| --- | |
| [SectionOne] | |
| status => Single | |
| name => Derek | |
| value => Yes | |
| age => 30 | |
| single => True | |
| foo => buzz | |
| envs => dev,stage | |
| version_url => hello.firefox.com/__version__ | |
| base_url => hello.firefox.com | |
| [SectionTwo] | |
| favoritecolor => Green | |
| skip => false | |
| envs => dev,stage, pre-prod | |
| base_url => hello.firefox.com | |
| [stage] | |
| root => call.stage.mozaws.net | |
| root/config.js => call.stage.mozaws.net/config.js | |
| loop_server => loop.stage.mozaws.net | |
| loop_server/push_config => loop.stage.mozaws.net/push-server-config | |
| envs => dev,stage, pre-prod | |
| base_url => hello.firefox.com | |
| [prod] | |
| root => hello.firefox.com | |
| root/config.js => hello.firefox.com/config.js | |
| loop_server => loop.services.mozilla.com | |
| loop_server/push_config => loop.services.mozilla.com/push-server-config | |
| envs => dev,stage, pre-prod | |
| base_url => hello.firefox.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment