~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/filters/__init__.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

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