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 |
|
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 |
filters = self.filter_stack_callback(path) |
|
50 |
if path is None: |
|
51 |
path = self.backing_tree.id2path(file_id) |
|
52 |
context = ContentFilterContext(path, self, None) |
|
53 |
contents = filtered_output_bytes(chunks, filters, context) |
|
54 |
content = ''.join(contents) |
|
55 |
return content |
|
6006.3.4
by Martin Pool
Support exporting tarballs from ContentFilterTree |
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() |