From 181fb1093d451ecc7dac64da9a62761acd1bc7fa Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Wed, 28 Mar 2018 00:54:06 +0900 Subject: [PATCH] Now Config.read() takes a filename instead (dirname, filename) --- sphinx/application.py | 2 +- sphinx/config.py | 17 ++++++++--------- tests/test_config.py | 8 ++++---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/sphinx/application.py b/sphinx/application.py index c2da600f4..92a20e44b 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -185,7 +185,7 @@ class Sphinx(object): # read config self.tags = Tags(tags) - self.config = Config.read(self.confdir, CONFIG_FILENAME, + self.config = Config.read(path.join(self.confdir, CONFIG_FILENAME), confoverrides or {}, self.tags) self.config.check_unicode() # defer checking types until i18n has been initialized diff --git a/sphinx/config.py b/sphinx/config.py index 1656aa9a3..4e6585334 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -159,7 +159,7 @@ class Config(object): if dirname is None: config = {} # type: Dict[unicode, Any] else: - config = eval_config_file(dirname, filename, tags) + config = eval_config_file(path.join(dirname, filename), tags) else: # new style arguments: (config={}, overrides={}) if len(args) == 0: @@ -182,10 +182,10 @@ class Config(object): self.extensions = config.get('extensions', []) # type: List[unicode] @classmethod - def read(cls, confdir, filename, overrides=None, tags=None): - # type: (unicode, unicode, Dict, Tags) -> Config + def read(cls, filename, overrides=None, tags=None): + # type: (unicode, Dict, Tags) -> Config """Create a Config object from configuration file.""" - namespace = eval_config_file(confdir, filename, tags) + namespace = eval_config_file(filename, tags) return cls(namespace, overrides or {}) def check_types(self): @@ -316,15 +316,14 @@ class Config(object): return (value for value in self if value.rebuild in rebuild) -def eval_config_file(confdir, filename, tags): - # type: (unicode, unicode, Tags) -> Dict[unicode, Any] +def eval_config_file(filename, tags): + # type: (unicode, Tags) -> Dict[unicode, Any] """Evaluate a config file.""" - config_path = path.join(confdir, filename) namespace = {} # type: Dict[unicode, Any] - namespace['__file__'] = config_path + namespace['__file__'] = filename namespace['tags'] = tags - with cd(confdir): + with cd(path.dirname(filename)): # during executing config file, current dir is changed to ``confdir``. try: execfile_(filename, namespace) diff --git a/tests/test_config.py b/tests/test_config.py index f3110f6a7..7f27e3ff0 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -124,14 +124,14 @@ def test_errors_warnings(logger, tempdir): # test the error for syntax errors in the config file (tempdir / 'conf.py').write_text(u'project = \n', encoding='ascii') with pytest.raises(ConfigError) as excinfo: - Config.read(tempdir, 'conf.py', {}, None) + Config.read(tempdir / 'conf.py', {}, None) assert 'conf.py' in str(excinfo.value) # test the automatic conversion of 2.x only code in configs (tempdir / 'conf.py').write_text( u'# -*- coding: utf-8\n\nproject = u"Jägermeister"\n', encoding='utf-8') - cfg = Config.read(tempdir, 'conf.py', {}, None) + cfg = Config.read(tempdir / 'conf.py', {}, None) cfg.init_values() assert cfg.project == u'Jägermeister' assert logger.called is False @@ -143,7 +143,7 @@ def test_errors_warnings(logger, tempdir): return (tempdir / 'conf.py').write_text( u'# -*- coding: latin-1\nproject = "fooä"\n', encoding='latin-1') - cfg = Config.read(tempdir, 'conf.py', {}, None) + cfg = Config.read(tempdir / 'conf.py', {}, None) assert logger.warning.called is False cfg.check_unicode() @@ -202,7 +202,7 @@ def test_config_eol(logger, tempdir): configfile = tempdir / 'conf.py' for eol in (b'\n', b'\r\n'): configfile.write_bytes(b'project = "spam"' + eol) - cfg = Config.read(tempdir, 'conf.py', {}, None) + cfg = Config.read(tempdir / 'conf.py', {}, None) cfg.init_values() assert cfg.project == u'spam' assert logger.called is False