return remaining
- def parse_config_file(self, path, final=True):
+ def parse_config_file(self, path, final=True, encoding=None):
"""Parses and loads the Python config file at the given path.
If ``final`` is ``False``, parse callbacks will not be run.
from multiple sources.
"""
config = {}
- with open(path) as f:
- exec_in(f.read(), config, config)
+ try:
+ # python 3
+ with open(path, encoding=encoding) as f:
+ exec_in(f.read(), config, config)
+ except TypeError:
+ # python 2
+ with open(path) as f:
+ exec_in(f.read(), config, config)
for name in config:
if name in self._options:
self._options[name].set(config[name])
+# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, with_statement
import datetime
def test_parse_config_file(self):
options = OptionParser()
options.define("port", default=80)
- options.parse_config_file(os.path.join(os.path.dirname(__file__),
- "options_test.cfg"))
+ options.define("username", default='foo')
+ cfg_path = os.path.join(os.path.dirname(__file__), "options_test.cfg")
+ options.parse_config_file(cfg_path, encoding="utf-8")
+
self.assertEquals(options.port, 443)
+ self.assertEqual(options.username, "李康")
def test_parse_callbacks(self):
options = OptionParser()