comparison mercurial/error.py @ 45897:8cc9e7f762d6

errors: move similarity_hint() to error module I want to be able to reuse it from `UnknownIdentifier`'s constructor. Moving it results in a new import of `difflib` in the `error` module. There was a comment at the top of `error.py` saying "Do not import anything but pycompat here, please", which was added (except for the "pycompat" bit) in 08cabecfa8a8 (errors: move revlog errors, 2009-01-11). I don't know the reason for the comment. I'm guessing the point was to not make the module depend on other Mercurial modules. If that was it, then importing `difflib` should be fine. Sorry about the churn (I moved this code from the `dispatch` module to the `scmutil` module very recently). Differential Revision: https://phab.mercurial-scm.org/D9345
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 19 Nov 2020 12:20:26 -0800
parents ac362d5a7893
children 1817b66897ad
comparison
equal deleted inserted replaced
45896:bb1b7a5bc96b 45897:8cc9e7f762d6
10 This allows us to catch exceptions at higher levels without forcing 10 This allows us to catch exceptions at higher levels without forcing
11 imports. 11 imports.
12 """ 12 """
13 13
14 from __future__ import absolute_import 14 from __future__ import absolute_import
15
16 import difflib
15 17
16 # Do not import anything but pycompat here, please 18 # Do not import anything but pycompat here, please
17 from . import pycompat 19 from . import pycompat
18 20
19 21
266 __bytes__ = _tobytes 268 __bytes__ = _tobytes
267 269
268 270
269 class PatchError(Exception): 271 class PatchError(Exception):
270 __bytes__ = _tobytes 272 __bytes__ = _tobytes
273
274
275 def getsimilar(symbols, value):
276 sim = lambda x: difflib.SequenceMatcher(None, value, x).ratio()
277 # The cutoff for similarity here is pretty arbitrary. It should
278 # probably be investigated and tweaked.
279 return [s for s in symbols if sim(s) > 0.6]
280
281
282 def similarity_hint(similar):
283 from .i18n import _
284
285 if len(similar) == 1:
286 return _(b"did you mean %s?") % similar[0]
287 elif similar:
288 ss = b", ".join(sorted(similar))
289 return _(b"did you mean one of %s?") % ss
290 else:
291 return None
271 292
272 293
273 class UnknownIdentifier(ParseError): 294 class UnknownIdentifier(ParseError):
274 """Exception raised when a {rev,file}set references an unknown identifier""" 295 """Exception raised when a {rev,file}set references an unknown identifier"""
275 296