From 50823c4b37cc43ea5f5a16ccdd46aa9a0f49560d Mon Sep 17 00:00:00 2001 From: Zac-HD Date: Thu, 16 Nov 2017 13:03:53 +1100 Subject: [PATCH] Fix type hint, minor style improvements --- sphinx/environment/__init__.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py index 2cb4b4a72..5a05ce26f 100644 --- a/sphinx/environment/__init__.py +++ b/sphinx/environment/__init__.py @@ -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: