Skip to content

Commit

Permalink
VersionManifest is now lazily resolved to avoid network errors. The C…
Browse files Browse the repository at this point in the history
…LI error message for socket errors is improved.
  • Loading branch information
mindstorm38 committed Sep 19, 2021
1 parent d8c0638 commit 2e54efd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
28 changes: 18 additions & 10 deletions portablemc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,27 +679,35 @@ def default_runner(args: List[str], cwd: str) -> None:

class VersionManifest:

def __init__(self, data: dict):
self.data = data
def __init__(self):
self.data: Optional[dict] = None

@classmethod
def load_from_url(cls):
""" Load the version manifest from the official URL. Can raise `JsonRequestError` if failed. """
return cls(json_simple_request("https://launchermeta.mojang.com/mc/game/version_manifest.json"))
def _ensure_data(self) -> dict:
if self.data is None:
self.data = json_simple_request("https://launchermeta.mojang.com/mc/game/version_manifest.json")
return self.data

# @classmethod
# def load_from_url(cls):
# """ Load the version manifest from the official URL. Can raise `JsonRequestError` if failed. """
# return cls(json_simple_request("https://launchermeta.mojang.com/mc/game/version_manifest.json"))

def filter_latest(self, version: str) -> Tuple[str, bool]:
latest = self.data["latest"].get(version)
return (version, False) if latest is None else (latest, True)
if version in ("release", "snapshot"):
latest = self._ensure_data()["latest"].get(version)
if latest is not None:
return latest, True
return version, False

def get_version(self, version: str) -> Optional[dict]:
version, _alias = self.filter_latest(version)
for version_data in self.data["versions"]:
for version_data in self._ensure_data()["versions"]:
if version_data["id"] == version:
return version_data
return None

def all_versions(self) -> list:
return self.data["versions"]
return self._ensure_data()["versions"]


class AuthSession:
Expand Down
4 changes: 2 additions & 2 deletions portablemc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def new_context(ns: Namespace) -> CliContext:


def load_version_manifest(_ctx: CliContext) -> VersionManifest:
return VersionManifest.load_from_url()
return VersionManifest()


def new_auth_database(ctx: CliContext) -> AuthDatabase:
Expand Down Expand Up @@ -1015,7 +1015,7 @@ def print_task(status: Optional[str], msg_key: str, msg_args: Optional[dict] = N
# Json Request
f"json_request.error.{JsonRequestError.INVALID_RESPONSE_NOT_JSON}": "Invalid JSON response from {method} {url}, status: {status}, data: {data}",
# Misc errors
f"error.socket": "Socket error: {reason}",
f"error.socket": "This operation requires an operational network, but a socket error happened: {reason}",
# Command search
"search.type": "Type",
"search.name": "Identifier",
Expand Down

0 comments on commit 2e54efd

Please sign in to comment.