Skip to content

Commit

Permalink
feat: added search by version to index
Browse files Browse the repository at this point in the history
  • Loading branch information
RonTamG committed Dec 31, 2023
1 parent d604131 commit 518b4b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import re
from operator import itemgetter
from typing import Dict

from src.package import Package
from src.version import Version

LATEST_INDEX = 0


class Index:
def __init__(self, index_file: str) -> None:
Expand All @@ -17,8 +20,20 @@ def __init__(self, index_file: str) -> None:
def __len__(self) -> int:
return len(self.packages)

def search(self, name) -> Package:
return list(self.packages[name].values())[0]
def search(self, name) -> Package | None:
result = None

if (
match := re.match(r"(\S+)(?: \((<<|<=|=|>=|>>) (\S+)\))?", name)
) is not None:
package, operation, version = match.groups()
match operation:
case None:
result = list(self.packages[package].values())[LATEST_INDEX]
case "=":
result = self.packages[package][Version(version)]

return result

def add_package(self, package: Package):
if package.name in self.packages:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def test_search_by_name_should_return_latest_version():
assert index.search("python3").version == Version("3.12.4")


def test_should_search_by_name_and_version():
index_file = valid_index_file_with_multiple_version_of_package() # noqa: F821

index = Index(index_file)

assert index.search("python3 (= 3.11.4-5+b1)").version == Version("3.11.4-5+b1")


def valid_index_file():
return """Package: python3
Source: python3-defaults (3.11.4-5)
Expand Down

0 comments on commit 518b4b3

Please sign in to comment.