##// END OF EJS Templates
summary: Avoid to re-fetch Node object
johbo -
r774:d9e392db default
parent child Browse files
Show More
@@ -63,14 +63,16 b' class SummaryController(BaseRepoControll'
63 @cache_region('long_term')
63 @cache_region('long_term')
64 def _generate_readme(cache_key):
64 def _generate_readme(cache_key):
65 readme_data = None
65 readme_data = None
66 readme_file = None
66 readme_node = None
67 readme_filename = None
67 commit = self._get_landing_commit_or_none(db_repo)
68 commit = self._get_landing_commit_or_none(db_repo)
68 if commit:
69 if commit:
69 log.debug("Searching for a README file.")
70 log.debug("Searching for a README file.")
70 readme_file = ReadmeFinder(default_renderer).search(commit)
71 readme_node = ReadmeFinder(default_renderer).search(commit)
71 if readme_file:
72 if readme_node:
72 readme_data = self._render_readme_or_none(commit, readme_file)
73 readme_data = self._render_readme_or_none(commit, readme_node)
73 return readme_data, readme_file
74 readme_filename = readme_node.path
75 return readme_data, readme_filename
74
76
75 invalidator_context = CacheKey.repo_context_cache(
77 invalidator_context = CacheKey.repo_context_cache(
76 _generate_readme, repo_name, CacheKey.CACHE_TYPE_README)
78 _generate_readme, repo_name, CacheKey.CACHE_TYPE_README)
@@ -93,13 +95,13 b' class SummaryController(BaseRepoControll'
93 log.exception(
95 log.exception(
94 "Problem getting commit when trying to render the README.")
96 "Problem getting commit when trying to render the README.")
95
97
96 def _render_readme_or_none(self, commit, readme_file):
98 def _render_readme_or_none(self, commit, readme_node):
97 log.debug(
99 log.debug(
98 'Found README file `%s` rendering...', readme_file)
100 'Found README file `%s` rendering...', readme_node.path)
99 renderer = MarkupRenderer()
101 renderer = MarkupRenderer()
100 node = commit.get_node(readme_file)
101 try:
102 try:
102 return renderer.render(node.content, filename=readme_file)
103 return renderer.render(
104 readme_node.content, filename=readme_node.file)
103 except Exception:
105 except Exception:
104 log.exception(
106 log.exception(
105 "Exception while trying to render the README")
107 "Exception while trying to render the README")
@@ -987,7 +987,7 b' class ReadmeFinder:'
987 matches = self._match_readmes(nodes)
987 matches = self._match_readmes(nodes)
988 matches = self._sort_according_to_priority(matches)
988 matches = self._sort_according_to_priority(matches)
989 if matches:
989 if matches:
990 return matches[0].path
990 return matches[0]
991
991
992 paths = self._match_paths(nodes)
992 paths = self._match_paths(nodes)
993 paths = self._sort_paths_according_to_priority(paths)
993 paths = self._sort_paths_according_to_priority(paths)
@@ -54,29 +54,29 b' class CommitUtility:'
54 def test_no_matching_file_returns_none(commit_util):
54 def test_no_matching_file_returns_none(commit_util):
55 commit = commit_util.commit_with_files(['LIESMICH'])
55 commit = commit_util.commit_with_files(['LIESMICH'])
56 finder = ReadmeFinder(default_renderer='rst')
56 finder = ReadmeFinder(default_renderer='rst')
57 filename = finder.search(commit)
57 filenode = finder.search(commit)
58 assert filename is None
58 assert filenode is None
59
59
60
60
61 def test_matching_file_returns_the_file_name(commit_util):
61 def test_matching_file_returns_the_file_name(commit_util):
62 commit = commit_util.commit_with_files(['README'])
62 commit = commit_util.commit_with_files(['README'])
63 finder = ReadmeFinder(default_renderer='rst')
63 finder = ReadmeFinder(default_renderer='rst')
64 filename = finder.search(commit)
64 filenode = finder.search(commit)
65 assert filename == 'README'
65 assert filenode.path == 'README'
66
66
67
67
68 def test_matching_file_with_extension(commit_util):
68 def test_matching_file_with_extension(commit_util):
69 commit = commit_util.commit_with_files(['README.rst'])
69 commit = commit_util.commit_with_files(['README.rst'])
70 finder = ReadmeFinder(default_renderer='rst')
70 finder = ReadmeFinder(default_renderer='rst')
71 filename = finder.search(commit)
71 filenode = finder.search(commit)
72 assert filename == 'README.rst'
72 assert filenode.path == 'README.rst'
73
73
74
74
75 def test_prefers_readme_without_extension(commit_util):
75 def test_prefers_readme_without_extension(commit_util):
76 commit = commit_util.commit_with_files(['README.rst', 'Readme'])
76 commit = commit_util.commit_with_files(['README.rst', 'Readme'])
77 finder = ReadmeFinder()
77 finder = ReadmeFinder()
78 filename = finder.search(commit)
78 filenode = finder.search(commit)
79 assert filename == 'Readme'
79 assert filenode.path == 'Readme'
80
80
81
81
82 @pytest.mark.parametrize('renderer, expected', [
82 @pytest.mark.parametrize('renderer, expected', [
@@ -87,20 +87,20 b' def test_prefers_renderer_extensions(com'
87 commit = commit_util.commit_with_files(
87 commit = commit_util.commit_with_files(
88 ['readme.rst', 'readme.md', 'readme.txt'])
88 ['readme.rst', 'readme.md', 'readme.txt'])
89 finder = ReadmeFinder(default_renderer=renderer)
89 finder = ReadmeFinder(default_renderer=renderer)
90 filename = finder.search(commit)
90 filenode = finder.search(commit)
91 assert filename == expected
91 assert filenode.path == expected
92
92
93
93
94 def test_finds_readme_in_subdirectory(commit_util):
94 def test_finds_readme_in_subdirectory(commit_util):
95 commit = commit_util.commit_with_files(['doc/README.rst', 'LIESMICH'])
95 commit = commit_util.commit_with_files(['doc/README.rst', 'LIESMICH'])
96 finder = ReadmeFinder()
96 finder = ReadmeFinder()
97 filename = finder.search(commit)
97 filenode = finder.search(commit)
98 assert filename == 'doc/README.rst'
98 assert filenode.path == 'doc/README.rst'
99
99
100
100
101 def test_prefers_subdirectory_with_priority(commit_util):
101 def test_prefers_subdirectory_with_priority(commit_util):
102 commit = commit_util.commit_with_files(
102 commit = commit_util.commit_with_files(
103 ['Doc/Readme.rst', 'Docs/Readme.rst'])
103 ['Doc/Readme.rst', 'Docs/Readme.rst'])
104 finder = ReadmeFinder()
104 finder = ReadmeFinder()
105 filename = finder.search(commit)
105 filenode = finder.search(commit)
106 assert filename == 'Doc/Readme.rst'
106 assert filenode.path == 'Doc/Readme.rst'
General Comments 0
You need to be logged in to leave comments. Login now