# HG changeset patch # User Johannes Bornhold # Date 2016-09-07 08:31:20 # Node ID 95f8a91c8171c7447f9365a5291c046aeb961312 # Parent 5902f223d86d31bc41ff39a70bb056f8d2491e42 summary: Move logic to find a readme out Created ReadmeFinder inside of model.repo. The ReadmeFinder will be evolved, so that it uses a smarter algorithm to actually find the readme file. diff --git a/rhodecode/controllers/summary.py b/rhodecode/controllers/summary.py --- a/rhodecode/controllers/summary.py +++ b/rhodecode/controllers/summary.py @@ -44,6 +44,8 @@ from rhodecode.lib.vcs.backends.base imp from rhodecode.lib.vcs.exceptions import ( CommitError, EmptyRepositoryError, NodeDoesNotExistError) from rhodecode.model.db import Statistics, CacheKey, User +from rhodecode.model.repo import ReadmeFinder + log = logging.getLogger(__name__) @@ -63,26 +65,21 @@ class SummaryController(BaseRepoControll readme_data = None readme_file = None try: - # gets the landing revision or tip if fails + # Find the landing commit commit = db_repo.get_landing_commit() if isinstance(commit, EmptyCommit): raise EmptyRepositoryError() - renderer = MarkupRenderer() - for f in renderer.pick_readme_order(default_renderer): - try: - node = commit.get_node(f) - except NodeDoesNotExistError: - continue + + readme_file = ReadmeFinder(default_renderer).search(commit) - if not node.is_file(): - continue - - readme_file = f + # Render the readme if one was found + if readme_file: + renderer = MarkupRenderer() + node = commit.get_node(readme_file) log.debug('Found README file `%s` rendering...', readme_file) - readme_data = renderer.render(node.content, - filename=f) - break + readme_data = renderer.render( + node.content, filename=readme_file) except CommitError: log.exception( "Problem getting commit when trying to render the README.") diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -40,11 +40,13 @@ from rhodecode.lib.auth import HasUserGr from rhodecode.lib.caching_query import FromCache from rhodecode.lib.exceptions import AttachedForksError from rhodecode.lib.hooks_base import log_delete_repository +from rhodecode.lib.markup_renderer import MarkupRenderer from rhodecode.lib.utils import make_db_config from rhodecode.lib.utils2 import ( safe_str, safe_unicode, remove_prefix, obfuscate_url_pw, get_current_rhodecode_user, safe_int, datetime_to_time, action_logger_generic) from rhodecode.lib.vcs.backends import get_backend +from rhodecode.lib.vcs.exceptions import NodeDoesNotExistError from rhodecode.model import BaseModel from rhodecode.model.db import ( Repository, UserRepoToPerm, UserGroupRepoToPerm, UserRepoGroupToPerm, @@ -933,3 +935,34 @@ class RepoModel(BaseModel): if os.path.isdir(rm_path): shutil.move(rm_path, os.path.join(self.repos_path, _d)) + + +class ReadmeFinder: + """ + Utility which knows how to find a readme for a specific commit. + + The main idea is that this is a configurable algorithm. When creating an + instance you can define parameters, currently only the `default_renderer`. + Based on this configuration the method :meth:`search` behaves slightly + different. + """ + + def __init__(self, default_renderer): + self._default_renderer = default_renderer + + def search(self, commit): + """ + Try to find a readme in the given `commit`. + """ + renderer = MarkupRenderer() + for f in renderer.pick_readme_order(self._default_renderer): + log.debug("Trying README %s", f) + try: + node = commit.get_node(f) + except NodeDoesNotExistError: + continue + + if not node.is_file(): + continue + + return f