/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
6977.2.4 by Jelmer Vernooij
Implement get_file.
20
from io import BytesIO
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.
7143.15.2 by Jelmer Vernooij
Run autopep8.
33
6006.3.1 by Martin Pool
Start adding ContentFilterTree
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
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
47
    def get_file_text(self, path):
48
        chunks = self.backing_tree.get_file_lines(path)
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)
6885.3.1 by Jelmer Vernooij
Simplify ContentFilterContext.
50
        context = ContentFilterContext(path, self)
6006.3.1 by Martin Pool
Start adding ContentFilterTree
51
        contents = filtered_output_bytes(chunks, filters, context)
6977.2.1 by Jelmer Vernooij
Require that get_file implementations are contect managers, simplify file handling in transform.
52
        content = b''.join(contents)
6006.3.1 by Martin Pool
Start adding ContentFilterTree
53
        return content
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
54
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
55
    def get_file(self, path):
56
        return BytesIO(self.get_file_text(path))
6977.2.4 by Jelmer Vernooij
Implement get_file.
57
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
58
    def has_filename(self, filename):
59
        return self.backing_tree.has_filename
60
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
61
    def is_executable(self, path):
62
        return self.backing_tree.is_executable(path)
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
63
7404.3.2 by Jelmer Vernooij
Merge rename of flag to recurse_nested.
64
    def iter_entries_by_dir(self, specific_files=None, recurse_nested=False):
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
65
        # NB: This simply returns the parent tree's entries; the length may be
66
        # wrong but it can't easily be calculated without filtering the whole
67
        # text.  Currently all callers cope with this; perhaps they should be
68
        # updated to a narrower interface that only provides things guaranteed
69
        # cheaply available across all trees. -- mbp 20110705
70
        return self.backing_tree.iter_entries_by_dir(
7404.3.2 by Jelmer Vernooij
Merge rename of flag to recurse_nested.
71
            specific_files=specific_files, recurse_nested=recurse_nested)
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
72
73
    def lock_read(self):
74
        return self.backing_tree.lock_read()
75
76
    def unlock(self):
77
        return self.backing_tree.unlock()