/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
6977.2.4 by Jelmer Vernooij
Implement get_file.
22
from io import BytesIO
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from . import (
6006.3.1 by Martin Pool
Start adding ContentFilterTree
25
    tree,
26
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from .filters import (
6006.3.1 by Martin Pool
Start adding ContentFilterTree
28
    ContentFilterContext,
29
    filtered_output_bytes,
30
    )
31
32
33
class ContentFilterTree(tree.Tree):
34
    """A virtual tree that applies content filters to an underlying tree.
35
    
36
    Not every operation is supported yet.
37
    """
38
39
    def __init__(self, backing_tree, filter_stack_callback):
40
        """Construct a new filtered tree view.
41
42
        :param filter_stack_callback: A callable taking a path that returns
43
            the filter stack that should be used for that path.
44
        :param backing_tree: An underlying tree to wrap.
45
        """
46
        self.backing_tree = backing_tree
47
        self.filter_stack_callback = filter_stack_callback
48
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
49
    def get_file_text(self, path, file_id=None):
50
        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.
51
        filters = self.filter_stack_callback(path)
6885.3.1 by Jelmer Vernooij
Simplify ContentFilterContext.
52
        context = ContentFilterContext(path, self)
6006.3.1 by Martin Pool
Start adding ContentFilterTree
53
        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.
54
        content = b''.join(contents)
6006.3.1 by Martin Pool
Start adding ContentFilterTree
55
        return content
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
56
6977.2.4 by Jelmer Vernooij
Implement get_file.
57
    def get_file(self, path, file_id=None):
58
        return BytesIO(self.get_file_text(path, file_id))
59
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
60
    def has_filename(self, filename):
61
        return self.backing_tree.has_filename
62
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
63
    def is_executable(self, path, file_id=None):
64
        return self.backing_tree.is_executable(path, file_id)
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
65
6929.6.1 by Jelmer Vernooij
Remove yield_parents argument to Tree.iter_entries_by_dir.
66
    def iter_entries_by_dir(self, specific_files=None):
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
67
        # NB: This simply returns the parent tree's entries; the length may be
68
        # wrong but it can't easily be calculated without filtering the whole
69
        # text.  Currently all callers cope with this; perhaps they should be
70
        # updated to a narrower interface that only provides things guaranteed
71
        # cheaply available across all trees. -- mbp 20110705
72
        return self.backing_tree.iter_entries_by_dir(
6929.6.1 by Jelmer Vernooij
Remove yield_parents argument to Tree.iter_entries_by_dir.
73
            specific_files=specific_files)
6006.3.4 by Martin Pool
Support exporting tarballs from ContentFilterTree
74
75
    def lock_read(self):
76
        return self.backing_tree.lock_read()
77
78
    def unlock(self):
79
        return self.backing_tree.unlock()