~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Jelmer Vernooij
  • Date: 2011-03-10 11:59:16 UTC
  • mto: (5712.4.2 bzrdir-weave)
  • mto: This revision was merged to the branch mainline in revision 5716.
  • Revision ID: jelmer@samba.org-20110310115916-xy2r15t3mpcqxgu0
Deprecate BzrProber.{un,}register_format in favour of format registry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
import re
19
 
 
20
18
from bzrlib.lazy_import import lazy_import
21
19
lazy_import(globals(), """
22
20
import bisect
23
21
import datetime
24
 
""")
25
22
 
26
23
from bzrlib import (
27
24
    branch as _mod_branch,
28
 
    errors,
29
25
    osutils,
30
 
    registry,
31
26
    revision,
32
27
    symbol_versioning,
 
28
    workingtree,
 
29
    )
 
30
""")
 
31
 
 
32
from bzrlib import (
 
33
    errors,
 
34
    lazy_regex,
 
35
    registry,
33
36
    trace,
34
 
    workingtree,
35
37
    )
36
38
 
37
39
 
115
117
        return RevisionInfo(branch, revno, revision_id)
116
118
 
117
119
 
118
 
_revno_regex = None
119
 
 
120
 
 
121
120
class RevisionSpec(object):
122
121
    """A parsed revision specification."""
123
122
 
168
167
                         spectype.__name__, spec)
169
168
            return spectype(spec, _internal=True)
170
169
        else:
171
 
            for spectype in SPEC_TYPES:
172
 
                if spec.startswith(spectype.prefix):
173
 
                    trace.mutter('Returning RevisionSpec %s for %s',
174
 
                                 spectype.__name__, spec)
175
 
                    return spectype(spec, _internal=True)
176
170
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
177
171
            # wait for _match_on to be called.
178
172
            return RevisionSpec_dwim(spec, _internal=True)
304
298
    # each revspec we try.
305
299
    wants_revision_history = False
306
300
 
 
301
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
302
 
 
303
    # The revspecs to try
 
304
    _possible_revspecs = []
 
305
 
307
306
    def _try_spectype(self, rstype, branch):
308
307
        rs = rstype(self.spec, _internal=True)
309
308
        # Hit in_history to find out if it exists, or we need to try the
314
313
        """Run the lookup and see what we can get."""
315
314
 
316
315
        # First, see if it's a revno
317
 
        global _revno_regex
318
 
        if _revno_regex is None:
319
 
            _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
320
 
        if _revno_regex.match(self.spec) is not None:
 
316
        if self._revno_regex.match(self.spec) is not None:
321
317
            try:
322
318
                return self._try_spectype(RevisionSpec_revno, branch)
323
319
            except RevisionSpec_revno.dwim_catchable_exceptions:
324
320
                pass
325
321
 
326
322
        # Next see what has been registered
 
323
        for objgetter in self._possible_revspecs:
 
324
            rs_class = objgetter.get_obj()
 
325
            try:
 
326
                return self._try_spectype(rs_class, branch)
 
327
            except rs_class.dwim_catchable_exceptions:
 
328
                pass
 
329
 
 
330
        # Try the old (deprecated) dwim list:
327
331
        for rs_class in dwim_revspecs:
328
332
            try:
329
333
                return self._try_spectype(rs_class, branch)
335
339
        # really relevant.
336
340
        raise errors.InvalidRevisionSpec(self.spec, branch)
337
341
 
 
342
    @classmethod
 
343
    def append_possible_revspec(cls, revspec):
 
344
        """Append a possible DWIM revspec.
 
345
 
 
346
        :param revspec: Revision spec to try.
 
347
        """
 
348
        cls._possible_revspecs.append(registry._ObjectGetter(revspec))
 
349
 
 
350
    @classmethod
 
351
    def append_possible_lazy_revspec(cls, module_name, member_name):
 
352
        """Append a possible lazily loaded DWIM revspec.
 
353
 
 
354
        :param module_name: Name of the module with the revspec
 
355
        :param member_name: Name of the revspec within the module
 
356
        """
 
357
        cls._possible_revspecs.append(
 
358
            registry._LazyObjectGetter(module_name, member_name))
 
359
 
338
360
 
339
361
class RevisionSpec_revno(RevisionSpec):
340
362
    """Selects a revision using a number."""
663
685
                                   August 14th, 2006 at 5:10pm.
664
686
    """
665
687
    prefix = 'date:'
666
 
    _date_re = re.compile(
 
688
    _date_regex = lazy_regex.lazy_compile(
667
689
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
668
690
            r'(,|T)?\s*'
669
691
            r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
687
709
        elif self.spec.lower() == 'tomorrow':
688
710
            dt = today + datetime.timedelta(days=1)
689
711
        else:
690
 
            m = self._date_re.match(self.spec)
 
712
            m = self._date_regex.match(self.spec)
691
713
            if not m or (not m.group('date') and not m.group('time')):
692
714
                raise errors.InvalidRevisionSpec(self.user_spec,
693
715
                                                 branch, 'invalid date')
971
993
# The order in which we want to DWIM a revision spec without any prefix.
972
994
# revno is always tried first and isn't listed here, this is used by
973
995
# RevisionSpec_dwim._match_on
974
 
dwim_revspecs = [
975
 
    RevisionSpec_tag, # Let's try for a tag
976
 
    RevisionSpec_revid, # Maybe it's a revid?
977
 
    RevisionSpec_date, # Perhaps a date?
978
 
    RevisionSpec_branch, # OK, last try, maybe it's a branch
979
 
    ]
 
996
dwim_revspecs = symbol_versioning.deprecated_list(
 
997
    symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
980
998
 
 
999
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
 
1000
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
 
1001
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
 
1002
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
981
1003
 
982
1004
revspec_registry = registry.Registry()
983
1005
def _register_revspec(revspec):
994
1016
_register_revspec(RevisionSpec_submit)
995
1017
_register_revspec(RevisionSpec_annotate)
996
1018
_register_revspec(RevisionSpec_mainline)
997
 
 
998
 
# classes in this list should have a "prefix" attribute, against which
999
 
# string specs are matched
1000
 
SPEC_TYPES = symbol_versioning.deprecated_list(
1001
 
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])