Fix type hint, minor style improvements

This commit is contained in:
Zac-HD 2017-11-16 13:03:53 +11:00
parent 973f2680d6
commit 50823c4b37

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import fnmatch
import os
import re
import sys
@ -45,7 +44,7 @@ from sphinx.util.websupport import is_commentable
if False:
# For type annotation
from typing import Any, Callable, Dict, IO, Iterator, List, Pattern, Set, Tuple, Type, Union, Generator # NOQA
from typing import Any, Callable, Dict, IO, Iterator, List, Optional, Pattern, Set, Tuple, Type, Union, Generator # NOQA
from docutils import nodes # NOQA
from sphinx.application import Sphinx # NOQA
from sphinx.builders import Builder # NOQA
@ -341,17 +340,16 @@ class BuildEnvironment(object):
app.emit('env-merge-info', self, docnames, other)
def path2doc(self, filename):
# type: (unicode) -> unicode
# type: (unicode) -> Optional[unicode]
"""Return the docname for the filename if the file is document.
*filename* should be absolute or relative to the source directory.
"""
if filename.startswith(self.srcdir):
filename = filename[len(self.srcdir) + 1:]
filename = os.path.relpath(filename, self.srcdir)
for suffix in self.config.source_suffix:
if fnmatch.fnmatch(filename, '*' + suffix):
if filename.endswith(suffix):
return filename[:-len(suffix)]
# the file does not have docname
return None
def doc2path(self, docname, base=True, suffix=None):
@ -365,15 +363,13 @@ class BuildEnvironment(object):
"""
docname = docname.replace(SEP, path.sep)
if suffix is None:
candidate_suffix = None # type: unicode
# Use first candidate if there is not a file for any suffix
suffix = next(iter(self.config.source_suffix))
for candidate_suffix in self.config.source_suffix:
if path.isfile(path.join(self.srcdir, docname) +
candidate_suffix):
suffix = candidate_suffix
break
else:
# document does not exist
suffix = self.config.source_suffix[0]
if base is True:
return path.join(self.srcdir, docname) + suffix
elif base is None: