/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6006.3.1 by Martin Pool
Start adding ContentFilterTree
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
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
20
from __future__ import absolute_import
6006.3.1 by Martin Pool
Start adding ContentFilterTree
21
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
22
from . import (
6006.3.1 by Martin Pool
Start adding ContentFilterTree
23
    tree,
24
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
25
from .filters import (
6006.3.1 by Martin Pool
Start adding ContentFilterTree
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
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
47
    def get_file_text(self, path, file_id=None):
48
        chunks = self.backing_tree.get_file_lines(path, file_id)
6549.2.1 by Vincent Ladeuil
Fix obvious bug, the filter stack needs the path to not be None.
49
        filters = self.filter_stack_callback(path)
6006.3.1 by Martin Pool
Start adding ContentFilterTree
50
        context = ContentFilterContext(path, self, None)
51
        contents = filtered_output_bytes(chunks, filters, context)
52
        content = ''.join(contents)
53
        return content
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
54
55
    def has_filename(self, filename):
56
        return self.backing_tree.has_filename
57
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
58
    def is_executable(self, path, file_id=None):
59
        return self.backing_tree.is_executable(path, file_id)
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
60
61
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=None):
62
        # NB: This simply returns the parent tree's entries; the length may be
63
        # wrong but it can't easily be calculated without filtering the whole
64
        # text.  Currently all callers cope with this; perhaps they should be
65
        # updated to a narrower interface that only provides things guaranteed
66
        # cheaply available across all trees. -- mbp 20110705
67
        return self.backing_tree.iter_entries_by_dir(
68
            specific_file_ids=specific_file_ids,
69
            yield_parents=yield_parents)
70
71
    def lock_read(self):
72
        return self.backing_tree.lock_read()
73
74
    def unlock(self):
75
        return self.backing_tree.unlock()