diff options
Diffstat (limited to 'sphinx/config.py')
| -rw-r--r-- | sphinx/config.py | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/sphinx/config.py b/sphinx/config.py index 19e299192..febc0a600 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -11,16 +11,23 @@ import os import re +import sys from os import path from sphinx.errors import ConfigError from sphinx.util.osutil import make_filename +from sphinx.util.pycompat import bytes, b, convert_with_2to3 -nonascii_re = re.compile(r'[\x80-\xff]') +nonascii_re = re.compile(b(r'[\x80-\xff]')) +CONFIG_SYNTAX_ERROR = "There is a syntax error in your configuration file: %s" +if sys.version_info >= (3, 0): + CONFIG_SYNTAX_ERROR += "\nDid you change the syntax from 2.x to 3.x?" class Config(object): - """Configuration file abstraction.""" + """ + Configuration file abstraction. + """ # the values are: (default, what needs to be rebuilt if changed) @@ -85,7 +92,7 @@ class Config(object): html_additional_pages = ({}, 'html'), html_use_modindex = (True, 'html'), # deprecated html_domain_indices = (True, 'html'), - html_add_permalinks = (True, 'html'), + html_add_permalinks = (u'\u00B6', 'html'), html_use_index = (True, 'html'), html_split_index = (False, 'html'), html_copy_source = (True, 'html'), @@ -120,6 +127,7 @@ class Config(object): epub_identifier = ('unknown', 'html'), epub_scheme = ('unknown', 'html'), epub_uid = ('unknown', 'env'), + epub_cover = ((), 'env'), epub_pre_files = ([], 'env'), epub_post_files = ([], 'env'), epub_exclude_files = ([], 'env'), @@ -151,6 +159,17 @@ class Config(object): # manpage options man_pages = ([], None), + + # Texinfo options + texinfo_documents = ([], None), + texinfo_appendices = ([], None), + texinfo_elements = ({}, None), + texinfo_domain_indices = (True, None), + + # linkcheck options + linkcheck_ignore = ([], None), + linkcheck_timeout = (None, None), + linkcheck_workers = (5, None), ) def __init__(self, dirname, filename, overrides, tags): @@ -163,12 +182,30 @@ class Config(object): config['tags'] = tags olddir = os.getcwd() try: + # we promise to have the config dir as current dir while the + # config file is executed + os.chdir(dirname) + # get config source + f = open(config_file, 'rb') + try: + source = f.read() + finally: + f.close() try: - os.chdir(dirname) - execfile(config['__file__'], config) + # compile to a code object, handle syntax errors + try: + code = compile(source, config_file, 'exec') + except SyntaxError: + if convert_with_2to3: + # maybe the file uses 2.x syntax; try to refactor to + # 3.x syntax using 2to3 + source = convert_with_2to3(config_file) + code = compile(source, config_file, 'exec') + else: + raise + exec code in config except SyntaxError, err: - raise ConfigError('There is a syntax error in your ' - 'configuration file: ' + str(err)) + raise ConfigError(CONFIG_SYNTAX_ERROR % err) finally: os.chdir(olddir) @@ -182,10 +219,11 @@ class Config(object): # check all string values for non-ASCII characters in bytestrings, # since that can result in UnicodeErrors all over the place for name, value in self._raw_config.iteritems(): - if isinstance(value, str) and nonascii_re.search(value): + if isinstance(value, bytes) and nonascii_re.search(value): warn('the config value %r is set to a string with non-ASCII ' 'characters; this can lead to Unicode errors occurring. ' - 'Please use Unicode strings, e.g. u"Content".' % name) + 'Please use Unicode strings, e.g. %r.' % (name, u'Content') + ) def init_values(self): config = self._raw_config |
