Skip to content

Commit

Permalink
Modularized client for state management (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
RulerOfCakes committed Jun 21, 2024
1 parent 7ad2963 commit e107d89
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion micropip/_commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async def install(
index_urls = package_index.INDEX_URLS[:]

transaction = Transaction(
ctx=ctx,
ctx=ctx, # type: ignore[arg-type]
ctx_extras=[],
keep_going=keep_going,
deps=deps,
Expand Down
72 changes: 72 additions & 0 deletions micropip/package_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from typing import ( # noqa: UP035 List import is necessary due to the `list` method
Any,
List,
)

from micropip import package_index


class PackageManager:
"""
PackageManager provides an extensible interface for customizing micropip's behavior.
Each Manager instance holds its own local state that is
independent of other instances.
TODO: Implement all of the following global commands to utilize local state.
"""

def __init__(self) -> None:
self.index_urls = package_index.DEFAULT_INDEX_URLS

# TODO: initialize the compatibility layer
self.repodata_packages: dict[str, dict[str, Any]] = {}
self.repodata_info: dict[str, str] = {}

pass

def install(self):
raise NotImplementedError()

def list(self):
raise NotImplementedError()

def freeze(self):
raise NotImplementedError()

def add_mock_package(self):
raise NotImplementedError()

def list_mock_packages(self):
raise NotImplementedError()

def remove_mock_package(self):
raise NotImplementedError()

def uninstall(self):
raise NotImplementedError()

def set_index_urls(self, urls: List[str] | str): # noqa: UP006
"""
Set the index URLs to use when looking up packages.
- The index URL should support the
`JSON API <https://warehouse.pypa.io/api-reference/json/>`__ .
- The index URL may contain the placeholder {package_name} which will be
replaced with the package name when looking up a package. If it does not
contain the placeholder, the package name will be appended to the URL.
- If a list of URLs is provided, micropip will try each URL in order until
it finds a package. If no package is found, an error will be raised.
Parameters
----------
urls
A list of URLs or a single URL to use as the package index.
"""

if isinstance(urls, str):
urls = [urls]

self.index_urls = urls[:]
29 changes: 29 additions & 0 deletions tests/test_package_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import micropip.package_index as package_index
from micropip.package_manager import PackageManager


def test_package_manager() -> PackageManager:
package_manager = PackageManager()
assert package_manager.index_urls == package_index.DEFAULT_INDEX_URLS

return package_manager


def test_set_index_urls():
manager = test_package_manager()

default_index_urls = package_index.DEFAULT_INDEX_URLS
assert manager.index_urls == default_index_urls

valid_url1 = "https://pkg-index.com/{package_name}/json/"
valid_url2 = "https://another-pkg-index.com/{package_name}"
valid_url3 = "https://another-pkg-index.com/simple/"
try:
manager.set_index_urls(valid_url1)
assert manager.index_urls == [valid_url1]

manager.set_index_urls([valid_url1, valid_url2, valid_url3])
assert manager.index_urls == [valid_url1, valid_url2, valid_url3]
finally:
manager.set_index_urls(default_index_urls)
assert manager.index_urls == default_index_urls

0 comments on commit e107d89

Please sign in to comment.