Code enhancements (thanks to mitya57 and tk0miya):

* use tuple instead of list
* compile regex to find copyright year
* enclose test inside try/finally
This commit is contained in:
Alexis Bienvenüe 2016-05-02 09:57:06 +02:00
parent 7a89015a54
commit 6509130ff2
2 changed files with 33 additions and 31 deletions

View File

@ -22,6 +22,7 @@ from sphinx.util.pycompat import execfile_, NoneType
from sphinx.util.i18n import format_date from sphinx.util.i18n import format_date
nonascii_re = re.compile(br'[\x80-\xff]') nonascii_re = re.compile(br'[\x80-\xff]')
copyright_year_re = re.compile(br'^((\d{4}-)?)(\d{4})(?=[ ,])')
CONFIG_SYNTAX_ERROR = "There is a syntax error in your configuration file: %s" CONFIG_SYNTAX_ERROR = "There is a syntax error in your configuration file: %s"
if PY3: if PY3:
@ -302,10 +303,9 @@ class Config(object):
# correct values of copyright year that are not coherent with # correct values of copyright year that are not coherent with
# the SOURCE_DATE_EPOCH environment variable: # the SOURCE_DATE_EPOCH environment variable:
if getenv('SOURCE_DATE_EPOCH') is not None: if getenv('SOURCE_DATE_EPOCH') is not None:
for k in ['copyright','epub_copyright']: for k in ('copyright','epub_copyright'):
if k in config: if k in config:
config[k] = re.sub('^((\d{4}-)?)(\d{4})(?=[ ,])', config[k] = copyright_year_re.sub('\g<1>%s' % format_date('%Y'),
'\g<1>%s' % format_date('%Y'),
config[k]) config[k])
def check_types(self, warn): def check_types(self, warn):

View File

@ -14,6 +14,7 @@ from util import TestApp
def test_correct_year(): def test_correct_year():
try:
# save current value of SOURCE_DATE_EPOCH # save current value of SOURCE_DATE_EPOCH
sde = os.environ.pop('SOURCE_DATE_EPOCH',None) sde = os.environ.pop('SOURCE_DATE_EPOCH',None)
@ -40,6 +41,7 @@ def test_correct_year():
app.cleanup() app.cleanup()
assert '2006-2010' in content assert '2006-2010' in content
finally:
# Restores SOURCE_DATE_EPOCH # Restores SOURCE_DATE_EPOCH
if sde == None: if sde == None:
os.environ.pop('SOURCE_DATE_EPOCH',None) os.environ.pop('SOURCE_DATE_EPOCH',None)