# HG changeset patch # User Marcin Kuzminski # Date 2017-01-25 11:45:05 # Node ID 71f19ea6da4a7a7777d03c68988916a9d80ffc54 # Parent c9cd8e9d0ffbce800554fe720e7125443a402168 vcs: added possibility to pre-load attributes for FileNodes. diff --git a/rhodecode/lib/vcs/backends/git/commit.py b/rhodecode/lib/vcs/backends/git/commit.py --- a/rhodecode/lib/vcs/backends/git/commit.py +++ b/rhodecode/lib/vcs/backends/git/commit.py @@ -393,7 +393,7 @@ class GitCommit(base.BaseCommit): nodes.sort() return nodes - def get_node(self, path): + def get_node(self, path, pre_load=None): if isinstance(path, unicode): path = path.encode('utf-8') path = self._fix_path(path) @@ -415,7 +415,7 @@ class GitCommit(base.BaseCommit): else: node = DirNode(path, commit=self) elif type_ == 'blob': - node = FileNode(path, commit=self) + node = FileNode(path, commit=self, pre_load=pre_load) else: raise self.no_node_at_path(path) diff --git a/rhodecode/lib/vcs/backends/hg/commit.py b/rhodecode/lib/vcs/backends/hg/commit.py --- a/rhodecode/lib/vcs/backends/hg/commit.py +++ b/rhodecode/lib/vcs/backends/hg/commit.py @@ -289,7 +289,7 @@ class MercurialCommit(base.BaseCommit): return nodes - def get_node(self, path): + def get_node(self, path, pre_load=None): """ Returns `Node` object from the given `path`. If there is no node at the given `path`, `NodeDoesNotExistError` would be raised. @@ -298,7 +298,7 @@ class MercurialCommit(base.BaseCommit): if path not in self.nodes: if path in self._file_paths: - node = FileNode(path, commit=self) + node = FileNode(path, commit=self, pre_load=pre_load) elif path in self._dir_paths: if path == '': node = RootNode(commit=self) diff --git a/rhodecode/lib/vcs/backends/svn/commit.py b/rhodecode/lib/vcs/backends/svn/commit.py --- a/rhodecode/lib/vcs/backends/svn/commit.py +++ b/rhodecode/lib/vcs/backends/svn/commit.py @@ -140,7 +140,7 @@ class SubversionCommit(base.BaseCommit): lambda: self.repository.get_commit(commit_id=commit_id), content) - def get_node(self, path): + def get_node(self, path, pre_load=None): path = self._fix_path(path) if path not in self.nodes: @@ -152,7 +152,7 @@ class SubversionCommit(base.BaseCommit): if node_type == 'dir': node = nodes.DirNode(path, commit=self) elif node_type == 'file': - node = nodes.FileNode(path, commit=self) + node = nodes.FileNode(path, commit=self, pre_load=pre_load) else: raise NodeDoesNotExistError(self.no_node_at_path(path)) diff --git a/rhodecode/lib/vcs/nodes.py b/rhodecode/lib/vcs/nodes.py --- a/rhodecode/lib/vcs/nodes.py +++ b/rhodecode/lib/vcs/nodes.py @@ -290,8 +290,9 @@ class FileNode(Node): :attribute: commit: if given, first time content is accessed, callback :attribute: mode: stat mode for a node. Default is `FILEMODE_DEFAULT`. """ + _filter_pre_load = [] - def __init__(self, path, content=None, commit=None, mode=None): + def __init__(self, path, content=None, commit=None, mode=None, pre_load=None): """ Only one of ``content`` and ``commit`` may be given. Passing both would raise ``NodeError`` exception. @@ -308,6 +309,22 @@ class FileNode(Node): self._content = content self._mode = mode or FILEMODE_DEFAULT + self._set_bulk_properties(pre_load) + + def _set_bulk_properties(self, pre_load): + if not pre_load: + return + pre_load = [entry for entry in pre_load + if entry not in self._filter_pre_load] + if not pre_load: + return + + for attr_name in pre_load: + result = getattr(self, attr_name) + if callable(result): + result = result() + self.__dict__[attr_name] = result + @LazyProperty def mode(self): """