Skip to content

Commit e107d89

Browse files
authored
Modularized client for state management (#114)
1 parent 7ad2963 commit e107d89

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

micropip/_commands/install.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async def install(
130130
index_urls = package_index.INDEX_URLS[:]
131131

132132
transaction = Transaction(
133-
ctx=ctx,
133+
ctx=ctx, # type: ignore[arg-type]
134134
ctx_extras=[],
135135
keep_going=keep_going,
136136
deps=deps,

micropip/package_manager.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from typing import ( # noqa: UP035 List import is necessary due to the `list` method
2+
Any,
3+
List,
4+
)
5+
6+
from micropip import package_index
7+
8+
9+
class PackageManager:
10+
"""
11+
PackageManager provides an extensible interface for customizing micropip's behavior.
12+
13+
Each Manager instance holds its own local state that is
14+
independent of other instances.
15+
16+
TODO: Implement all of the following global commands to utilize local state.
17+
"""
18+
19+
def __init__(self) -> None:
20+
self.index_urls = package_index.DEFAULT_INDEX_URLS
21+
22+
# TODO: initialize the compatibility layer
23+
self.repodata_packages: dict[str, dict[str, Any]] = {}
24+
self.repodata_info: dict[str, str] = {}
25+
26+
pass
27+
28+
def install(self):
29+
raise NotImplementedError()
30+
31+
def list(self):
32+
raise NotImplementedError()
33+
34+
def freeze(self):
35+
raise NotImplementedError()
36+
37+
def add_mock_package(self):
38+
raise NotImplementedError()
39+
40+
def list_mock_packages(self):
41+
raise NotImplementedError()
42+
43+
def remove_mock_package(self):
44+
raise NotImplementedError()
45+
46+
def uninstall(self):
47+
raise NotImplementedError()
48+
49+
def set_index_urls(self, urls: List[str] | str): # noqa: UP006
50+
"""
51+
Set the index URLs to use when looking up packages.
52+
53+
- The index URL should support the
54+
`JSON API <https://warehouse.pypa.io/api-reference/json/>`__ .
55+
56+
- The index URL may contain the placeholder {package_name} which will be
57+
replaced with the package name when looking up a package. If it does not
58+
contain the placeholder, the package name will be appended to the URL.
59+
60+
- If a list of URLs is provided, micropip will try each URL in order until
61+
it finds a package. If no package is found, an error will be raised.
62+
63+
Parameters
64+
----------
65+
urls
66+
A list of URLs or a single URL to use as the package index.
67+
"""
68+
69+
if isinstance(urls, str):
70+
urls = [urls]
71+
72+
self.index_urls = urls[:]

tests/test_package_manager.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import micropip.package_index as package_index
2+
from micropip.package_manager import PackageManager
3+
4+
5+
def test_package_manager() -> PackageManager:
6+
package_manager = PackageManager()
7+
assert package_manager.index_urls == package_index.DEFAULT_INDEX_URLS
8+
9+
return package_manager
10+
11+
12+
def test_set_index_urls():
13+
manager = test_package_manager()
14+
15+
default_index_urls = package_index.DEFAULT_INDEX_URLS
16+
assert manager.index_urls == default_index_urls
17+
18+
valid_url1 = "https://pkg-index.com/{package_name}/json/"
19+
valid_url2 = "https://another-pkg-index.com/{package_name}"
20+
valid_url3 = "https://another-pkg-index.com/simple/"
21+
try:
22+
manager.set_index_urls(valid_url1)
23+
assert manager.index_urls == [valid_url1]
24+
25+
manager.set_index_urls([valid_url1, valid_url2, valid_url3])
26+
assert manager.index_urls == [valid_url1, valid_url2, valid_url3]
27+
finally:
28+
manager.set_index_urls(default_index_urls)
29+
assert manager.index_urls == default_index_urls

0 commit comments

Comments
 (0)