~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/filters/__init__.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
 
18
17
"""Working tree content filtering support.
19
18
 
20
19
A filter consists of a read converter, write converter pair.
21
20
The content in the working tree is called the convenience format
22
 
while the content actually stored in called the canonical format.
 
21
while the content actually stored is called the canonical format.
23
22
The read converter produces canonical content from convenience
24
23
content while the writer goes the other way.
25
24
 
39
38
Note that context is currently only supported for write converters.
40
39
"""
41
40
 
 
41
from __future__ import absolute_import
 
42
 
42
43
from cStringIO import StringIO
43
44
from bzrlib.lazy_import import lazy_import
44
45
lazy_import(globals(), """
49
50
    registry,
50
51
    )
51
52
""")
 
53
from bzrlib.symbol_versioning import (
 
54
    deprecated_function,
 
55
    deprecated_in,
 
56
    )
52
57
 
53
58
 
54
59
class ContentFilter(object):
85
90
        # Cached values
86
91
        self._revision_id = None
87
92
        self._revision = None
88
 
        self._config = None
89
93
 
90
94
    def relpath(self):
91
95
        """Relative path of file to tree-root."""
126
130
                self._revision = repo.get_revision(rev_id)
127
131
        return self._revision
128
132
 
129
 
    def config(self):
130
 
        """The Config object to search for configuration settings."""
131
 
        if self._config is None:
132
 
            branch = getattr(self._tree, 'branch', None)
133
 
            if branch is not None:
134
 
                self._config = branch.get_config()
135
 
            else:
136
 
                self._config = config.GlobalConfig()
137
 
        return self._config
138
 
 
139
133
 
140
134
def filtered_input_file(f, filters):
141
135
    """Get an input file that converts external to internal content.
186
180
 
187
181
 
188
182
# The registry of filter stacks indexed by name.
189
 
# See register_filter_stack_map for details on the registered values.
190
 
_filter_stacks_registry = registry.Registry()
 
183
filter_stacks_registry = registry.Registry()
191
184
 
192
185
 
193
186
# Cache of preferences -> stack
195
188
_stack_cache = {}
196
189
 
197
190
 
 
191
# XXX: This function doesn't have any tests. JRV 2012-03-29
 
192
@deprecated_function(deprecated_in((2, 6, 0)))
198
193
def register_filter_stack_map(name, stack_map_lookup):
199
194
    """Register the filter stacks to use for various preference values.
200
195
 
204
199
      the result is the matching stack of filters to use,
205
200
      or None if none.
206
201
    """
207
 
    if name in _filter_stacks_registry:
208
 
        raise errors.BzrError(
209
 
            "filter stack for %s already installed" % name)
210
 
    _filter_stacks_registry.register(name, stack_map_lookup)
211
 
 
212
 
 
 
202
    filter_stacks_registry.register(name, stack_map_lookup)
 
203
 
 
204
 
 
205
# XXX: This function doesn't have any tests. JRV 2012-03-29
 
206
@deprecated_function(deprecated_in((2, 6, 0)))
213
207
def lazy_register_filter_stack_map(name, module_name, member_name):
214
208
    """Lazily register the filter stacks to use for various preference values.
215
209
 
218
212
    :param member_name: The name of the stack_map_lookup callable
219
213
      in the module.
220
214
    """
221
 
    if name in _filter_stacks_registry:
222
 
        raise errors.BzrError(
223
 
            "filter stack for %s already installed" % name)
224
 
    _filter_stacks_registry.register_lazy(name, module_name, member_name)
 
215
    filter_stacks_registry.register_lazy(name, module_name, member_name)
225
216
 
226
217
 
227
218
def _get_registered_names():
228
219
    """Get the list of names with filters registered."""
229
220
    # Note: We may want to intelligently order these later.
230
221
    # If so, the register_ fn will need to support an optional priority.
231
 
    return _filter_stacks_registry.keys()
 
222
    return filter_stacks_registry.keys()
232
223
 
233
224
 
234
225
def _get_filter_stack_for(preferences):
249
240
        if v is None:
250
241
            continue
251
242
        try:
252
 
            stack_map_lookup = _filter_stacks_registry.get(k)
 
243
            stack_map_lookup = filter_stacks_registry.get(k)
253
244
        except KeyError:
254
245
            # Some preferences may not have associated filters
255
246
            continue
272
263
    :param value: the value to set the registry to or None for an empty one.
273
264
    :return: the existing value before it reset.
274
265
    """
275
 
    global _filter_stacks_registry
276
 
    original = _filter_stacks_registry
 
266
    global filter_stacks_registry
 
267
    original = filter_stacks_registry
277
268
    if value is None:
278
 
        _filter_stacks_registry = registry.Registry()
 
269
        filter_stacks_registry = registry.Registry()
279
270
    else:
280
 
        _filter_stacks_registry = value
 
271
        filter_stacks_registry = value
281
272
    _stack_cache.clear()
282
273
    return original
283
274
 
284
275
 
285
 
# Register the standard filters
286
 
from bzrlib.filters import eol
287
 
eol.register_eol_content_filter()
 
276
filter_stacks_registry.register_lazy('eol', 'bzrlib.filters.eol', 'eol_lookup')