~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/filter_tree.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Content-filtered view of any tree.
 
18
"""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
from bzrlib import (
 
23
    tree,
 
24
    )
 
25
from bzrlib.filters import (
 
26
    ContentFilterContext,
 
27
    filtered_output_bytes,
 
28
    )
 
29
 
 
30
 
 
31
class ContentFilterTree(tree.Tree):
 
32
    """A virtual tree that applies content filters to an underlying tree.
 
33
    
 
34
    Not every operation is supported yet.
 
35
    """
 
36
 
 
37
    def __init__(self, backing_tree, filter_stack_callback):
 
38
        """Construct a new filtered tree view.
 
39
 
 
40
        :param filter_stack_callback: A callable taking a path that returns
 
41
            the filter stack that should be used for that path.
 
42
        :param backing_tree: An underlying tree to wrap.
 
43
        """
 
44
        self.backing_tree = backing_tree
 
45
        self.filter_stack_callback = filter_stack_callback
 
46
 
 
47
    def get_file_text(self, file_id, path=None):
 
48
        chunks = self.backing_tree.get_file_lines(file_id, path)
 
49
        if path is None:
 
50
            path = self.backing_tree.id2path(file_id)
 
51
        filters = self.filter_stack_callback(path)
 
52
        context = ContentFilterContext(path, self, None)
 
53
        contents = filtered_output_bytes(chunks, filters, context)
 
54
        content = ''.join(contents)
 
55
        return content
 
56
 
 
57
    def has_filename(self, filename):
 
58
        return self.backing_tree.has_filename
 
59
 
 
60
    def is_executable(self, file_id, path=None):
 
61
        return self.backing_tree.is_executable(file_id, path)
 
62
 
 
63
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=None):
 
64
        # NB: This simply returns the parent tree's entries; the length may be
 
65
        # wrong but it can't easily be calculated without filtering the whole
 
66
        # text.  Currently all callers cope with this; perhaps they should be
 
67
        # updated to a narrower interface that only provides things guaranteed
 
68
        # cheaply available across all trees. -- mbp 20110705
 
69
        return self.backing_tree.iter_entries_by_dir(
 
70
            specific_file_ids=specific_file_ids,
 
71
            yield_parents=yield_parents)
 
72
 
 
73
    def lock_read(self):
 
74
        return self.backing_tree.lock_read()
 
75
 
 
76
    def unlock(self):
 
77
        return self.backing_tree.unlock()