diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f94baf..a7ac5fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 3.0 (2023-04-15) + +- Dropped support for Python 3.7. +- Updated computer matching to prefer exact matches over partials. + # 2.0 (2022-08-16) - Dropped support for Python 3.6. diff --git a/mine/models/computer.py b/mine/models/computer.py index aa8c7a8..2263d9b 100644 --- a/mine/models/computer.py +++ b/mine/models/computer.py @@ -57,12 +57,15 @@ def find(self, name): return computer return None - def match(self, partial): + def match(self, partial: str): """Find a computer with a similar name.""" log.debug("Finding computer similar to '%s'...", partial) + matches = [] for computer in self: if partial.lower() in computer.name.lower(): - return computer + matches.append(computer) + if matches: + return min(matches, key=lambda computer: len(computer.name)) return None def get_current(self): diff --git a/mine/tests/test_models_computer.py b/mine/tests/test_models_computer.py index 469723d..29bf7c7 100644 --- a/mine/tests/test_models_computer.py +++ b/mine/tests/test_models_computer.py @@ -63,7 +63,12 @@ class TestComputers: """Unit tests for lists of computers.""" computers = Computers( - [Computer("abc", "abc.local", 1), Computer("def", "def.local", 2)] + [ + Computer("abc", "abc.local", 1), + Computer("def", "def.local", 2), + Computer("My iMac", "imac.local", 3), + Computer("My Mac", "mac.local", 4), + ] ) def test_get(self): @@ -81,6 +86,11 @@ def test_match(self): computer = self.computers.match("AB") assert "abc" == computer.name + def test_match_partial(self): + """Verify an exact match is preferred over partial.""" + computer = self.computers.match("mac") + assert "My Mac" == computer.name + def test_generate_name(self): """Verify a computer name is generated correctly.""" computers = Computers() diff --git a/pyproject.toml b/pyproject.toml index a9b5e98..5a75081 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "mine" -version = "2.1b1" +version = "3.0" description = "Share application state across computers using Dropbox." license = "MIT"