Move read_config to milla.util
parent
3408919faa
commit
65ebf76b45
|
@ -1,56 +0,0 @@
|
||||||
try:
|
|
||||||
import configparser
|
|
||||||
except ImportError:
|
|
||||||
import ConfigParser as configparser
|
|
||||||
|
|
||||||
def read_config(filename):
|
|
||||||
'''Parse an ini file into a nested dictionary
|
|
||||||
|
|
||||||
:param string filename: Path to the ini file to read
|
|
||||||
:returns: A dictionary whose keys correspond to the section and
|
|
||||||
option, joined with a dot character (.)
|
|
||||||
|
|
||||||
For example, consider the following ini file::
|
|
||||||
|
|
||||||
[xmen]
|
|
||||||
storm = Ororo Monroe
|
|
||||||
cyclops = Scott Summers
|
|
||||||
|
|
||||||
[avengers]
|
|
||||||
hulk = Bruce Banner
|
|
||||||
iron_man = Tony Stark
|
|
||||||
|
|
||||||
The resulting dictionary would look like this::
|
|
||||||
|
|
||||||
{
|
|
||||||
'xmen.storm': 'Ororo Monroe',
|
|
||||||
'xmen.cyclops': 'Scott Summers',
|
|
||||||
'avengers.hulk': 'Bruce Banner',
|
|
||||||
'avengers.iron_man': 'Tony Stark',
|
|
||||||
}
|
|
||||||
|
|
||||||
Thus, the option values for any section can be obtained as follows::
|
|
||||||
|
|
||||||
config['xmen.storm']
|
|
||||||
|
|
||||||
This dictionary can be used to configure an :py:class:`~milla.Application`
|
|
||||||
instance by using the ``update`` method::
|
|
||||||
|
|
||||||
app = milla.Application(router)
|
|
||||||
app.config.update(config)
|
|
||||||
'''
|
|
||||||
|
|
||||||
with open(filename) as f:
|
|
||||||
# ConfigParser API changed in Python 3.2
|
|
||||||
if hasattr(configparser.ConfigParser, 'read_file'):
|
|
||||||
cparser = configparser.ConfigParser()
|
|
||||||
cparser.read_file(f)
|
|
||||||
else:
|
|
||||||
cparser = configparser.SafeConfigParser()
|
|
||||||
cparser.readfp(f)
|
|
||||||
|
|
||||||
config = {}
|
|
||||||
for section in cparser.sections():
|
|
||||||
for option in cparser.options(section):
|
|
||||||
config['.'.join((section, option))] = cparser.get(section, option)
|
|
||||||
return config
|
|
|
@ -11,19 +11,19 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
'''Module milla.uti
|
'''Convenience utility functions
|
||||||
|
|
||||||
Please give me a docstring!
|
|
||||||
|
|
||||||
:Created: Mar 30, 2011
|
:Created: Mar 30, 2011
|
||||||
:Author: dustin
|
:Author: dustin
|
||||||
:Updated: $Date$
|
|
||||||
:Updater: $Author$
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from wsgiref.handlers import format_date_time
|
from wsgiref.handlers import format_date_time
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
|
try:
|
||||||
|
import configparser
|
||||||
|
except ImportError:
|
||||||
|
import ConfigParser as configparser
|
||||||
|
|
||||||
def asbool(val):
|
def asbool(val):
|
||||||
'''Test a value for truth
|
'''Test a value for truth
|
||||||
|
@ -66,3 +66,56 @@ def http_date(date):
|
||||||
|
|
||||||
stamp = time.mktime(date.timetuple())
|
stamp = time.mktime(date.timetuple())
|
||||||
return format_date_time(stamp)
|
return format_date_time(stamp)
|
||||||
|
|
||||||
|
|
||||||
|
def read_config(filename):
|
||||||
|
'''Parse an ini file into a nested dictionary
|
||||||
|
|
||||||
|
:param string filename: Path to the ini file to read
|
||||||
|
:returns: A dictionary whose keys correspond to the section and
|
||||||
|
option, joined with a dot character (.)
|
||||||
|
|
||||||
|
For example, consider the following ini file::
|
||||||
|
|
||||||
|
[xmen]
|
||||||
|
storm = Ororo Monroe
|
||||||
|
cyclops = Scott Summers
|
||||||
|
|
||||||
|
[avengers]
|
||||||
|
hulk = Bruce Banner
|
||||||
|
iron_man = Tony Stark
|
||||||
|
|
||||||
|
The resulting dictionary would look like this::
|
||||||
|
|
||||||
|
{
|
||||||
|
'xmen.storm': 'Ororo Monroe',
|
||||||
|
'xmen.cyclops': 'Scott Summers',
|
||||||
|
'avengers.hulk': 'Bruce Banner',
|
||||||
|
'avengers.iron_man': 'Tony Stark',
|
||||||
|
}
|
||||||
|
|
||||||
|
Thus, the option values for any section can be obtained as follows::
|
||||||
|
|
||||||
|
config['xmen.storm']
|
||||||
|
|
||||||
|
This dictionary can be used to configure an :py:class:`~milla.Application`
|
||||||
|
instance by using the ``update`` method::
|
||||||
|
|
||||||
|
app = milla.Application(router)
|
||||||
|
app.config.update(config)
|
||||||
|
'''
|
||||||
|
|
||||||
|
with open(filename) as f:
|
||||||
|
# ConfigParser API changed in Python 3.2
|
||||||
|
if hasattr(configparser.ConfigParser, 'read_file'):
|
||||||
|
cparser = configparser.ConfigParser()
|
||||||
|
cparser.read_file(f)
|
||||||
|
else:
|
||||||
|
cparser = configparser.SafeConfigParser()
|
||||||
|
cparser.readfp(f)
|
||||||
|
|
||||||
|
config = {}
|
||||||
|
for section in cparser.sections():
|
||||||
|
for option in cparser.options(section):
|
||||||
|
config['.'.join((section, option))] = cparser.get(section, option)
|
||||||
|
return config
|
||||||
|
|
Loading…
Reference in New Issue