~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/globbing.py

  • Committer: Martin Pool
  • Date: 2007-04-04 01:22:11 UTC
  • mfrom: (2393.1.1 bzr.docs)
  • mto: This revision was merged to the branch mainline in revision 2397.
  • Revision ID: mbp@sourcefrog.net-20070404012211-sq269me6bai9m6xk
merge trunk and doc fix from elliot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2006 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tools for converting globs to regular expressions.
18
18
 
52
52
 
53
53
        The pattern must not contain capturing groups.
54
54
        The replacement might be either a string template in which \& will be
55
 
        replaced with the match, or a function that will get the matching text
56
 
        as argument. It does not get match object, because capturing is
 
55
        replaced with the match, or a function that will get the matching text  
 
56
        as argument. It does not get match object, because capturing is 
57
57
        forbidden anyway.
58
58
        """
59
59
        self._pat = None
160
160
 
161
161
    Patterns are translated to regular expressions to expidite matching.
162
162
 
163
 
    The regular expressions for multiple patterns are aggregated into
164
 
    a super-regex containing groups of up to 99 patterns.
 
163
    The regular expressions for multiple patterns are aggregated into 
 
164
    a super-regex containing groups of up to 99 patterns.  
165
165
    The 99 limitation is due to the grouping limit of the Python re module.
166
166
    The resulting super-regex and associated patterns are stored as a list of
167
167
    (regex,[patterns]) in _regex_patterns.
168
 
 
 
168
    
169
169
    For performance reasons the patterns are categorised as extension patterns
170
170
    (those that match against a file extension), basename patterns
171
171
    (those that match against the basename of the filename),
172
172
    and fullpath patterns (those that match against the full path).
173
 
    The translations used for extensions and basenames are relatively simpler
 
173
    The translations used for extensions and basenames are relatively simpler 
174
174
    and therefore faster to perform than the fullpath patterns.
175
175
 
176
 
    Also, the extension patterns are more likely to find a match and
 
176
    Also, the extension patterns are more likely to find a match and 
177
177
    so are matched first, then the basename patterns, then the fullpath
178
178
    patterns.
179
179
    """
192
192
                base_patterns.append(pat)
193
193
        self._add_patterns(ext_patterns,_sub_extension,
194
194
            prefix=r'(?:.*/)?(?!.*/)(?:.*\.)')
195
 
        self._add_patterns(base_patterns,_sub_basename,
 
195
        self._add_patterns(base_patterns,_sub_basename, 
196
196
            prefix=r'(?:.*/)?(?!.*/)')
197
 
        self._add_patterns(path_patterns,_sub_fullpath)
 
197
        self._add_patterns(path_patterns,_sub_fullpath) 
198
198
 
199
199
    def _add_patterns(self, patterns, translator, prefix=''):
200
200
        while patterns:
201
201
            grouped_rules = ['(%s)' % translator(pat) for pat in patterns[:99]]
202
202
            joined_rule = '%s(?:%s)$' % (prefix, '|'.join(grouped_rules))
203
 
            self._regex_patterns.append((re.compile(joined_rule, re.UNICODE),
 
203
            self._regex_patterns.append((re.compile(joined_rule, re.UNICODE), 
204
204
                patterns[:99]))
205
205
            patterns = patterns[99:]
206
206
 
207
207
    def match(self, filename):
208
208
        """Searches for a pattern that matches the given filename.
209
 
 
 
209
        
210
210
        :return A matching pattern or None if there is no matching pattern.
211
211
        """
212
212
        for regex, patterns in self._regex_patterns:
214
214
            if match:
215
215
                return patterns[match.lastindex -1]
216
216
        return None
217
 
 
218
 
 
219
 
class _OrderedGlobster(Globster):
220
 
    """A Globster that keeps pattern order."""
221
 
 
222
 
    def __init__(self, patterns):
223
 
        """Constructor.
224
 
 
225
 
        :param patterns: sequence of glob patterns
226
 
        """
227
 
        # Note: This could be smarter by running like sequences together
228
 
        self._regex_patterns = []
229
 
        for pat in patterns:
230
 
            pat = normalize_pattern(pat)
231
 
            if pat.startswith(u'RE:') or u'/' in pat:
232
 
                self._add_patterns([pat], _sub_fullpath)
233
 
            elif pat.startswith(u'*.'):
234
 
                self._add_patterns([pat], _sub_extension,
235
 
                    prefix=r'(?:.*/)?(?!.*/)(?:.*\.)')
236
 
            else:
237
 
                self._add_patterns([pat], _sub_basename,
238
 
                    prefix=r'(?:.*/)?(?!.*/)')
239
 
 
 
217
        
240
218
 
241
219
def normalize_pattern(pattern):
242
220
    """Converts backslashes in path patterns to forward slashes.
243
 
 
 
221
    
244
222
    Doesn't normalize regular expressions - they may contain escapes.
245
223
    """
246
224
    if not pattern.startswith('RE:'):