-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularized client for state management (#114)
- Loading branch information
1 parent
7ad2963
commit e107d89
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |