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 |
||
20 |
||
21 |
from bzrlib import ( |
|
22 |
tree, |
|
23 |
)
|
|
24 |
from bzrlib.filters import ( |
|
25 |
ContentFilter, |
|
26 |
ContentFilterContext, |
|
27 |
filtered_input_file, |
|
28 |
filtered_output_bytes, |
|
29 |
_get_filter_stack_for, |
|
30 |
_get_registered_names, |
|
31 |
internal_size_sha_file_byname, |
|
32 |
register_filter_stack_map, |
|
33 |
)
|
|
34 |
||
35 |
||
36 |
class ContentFilterTree(tree.Tree): |
|
37 |
"""A virtual tree that applies content filters to an underlying tree.
|
|
38 |
|
|
39 |
Not every operation is supported yet.
|
|
40 |
"""
|
|
41 |
||
42 |
def __init__(self, backing_tree, filter_stack_callback): |
|
43 |
"""Construct a new filtered tree view.
|
|
44 |
||
45 |
:param filter_stack_callback: A callable taking a path that returns
|
|
46 |
the filter stack that should be used for that path.
|
|
47 |
:param backing_tree: An underlying tree to wrap.
|
|
48 |
"""
|
|
49 |
self.backing_tree = backing_tree |
|
50 |
self.filter_stack_callback = filter_stack_callback |
|
51 |
||
52 |
def get_file_text(self, file_id, path=None): |
|
53 |
chunks = self.backing_tree.get_file_lines(file_id, path) |
|
54 |
filters = self.filter_stack_callback(path) |
|
55 |
if path is None: |
|
56 |
path = self.backing_tree.id2path(file_id) |
|
57 |
context = ContentFilterContext(path, self, None) |
|
58 |
contents = filtered_output_bytes(chunks, filters, context) |
|
59 |
content = ''.join(contents) |
|
60 |
return content |
|
6006.3.4
by Martin Pool
Support exporting tarballs from ContentFilterTree |
61 |
|
62 |
def has_filename(self, filename): |
|
63 |
return self.backing_tree.has_filename |
|
64 |
||
65 |
def is_executable(self, file_id, path=None): |
|
66 |
return self.backing_tree.is_executable(file_id, path) |
|
67 |
||
68 |
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=None): |
|
69 |
# NB: This simply returns the parent tree's entries; the length may be
|
|
70 |
# wrong but it can't easily be calculated without filtering the whole
|
|
71 |
# text. Currently all callers cope with this; perhaps they should be
|
|
72 |
# updated to a narrower interface that only provides things guaranteed
|
|
73 |
# cheaply available across all trees. -- mbp 20110705
|
|
74 |
return self.backing_tree.iter_entries_by_dir( |
|
75 |
specific_file_ids=specific_file_ids, |
|
76 |
yield_parents=yield_parents) |
|
77 |
||
78 |
def lock_read(self): |
|
79 |
return self.backing_tree.lock_read() |
|
80 |
||
81 |
def unlock(self): |
|
82 |
return self.backing_tree.unlock() |