Created
October 14, 2025 03:23
-
-
Save Siliconrob/038229d7c0882d04a13da8fde8a9cba8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import json | |
| from dataclasses import dataclass, field | |
| from typing import Dict, Optional, List, Self | |
| @dataclass | |
| class PackageInfo: | |
| version: Optional[str] = None | |
| resolved: Optional[str] = None | |
| integrity: Optional[str] = None | |
| dev: Optional[bool] = None | |
| dependencies: Dict[str, str] = field(default_factory=dict) | |
| engines: Dict[str, str] = field(default_factory=dict) | |
| cpu: List[str] = field(default_factory=list) | |
| os: List[str] = field(default_factory=list) | |
| optional: Optional[bool] = None | |
| @dataclass | |
| class PackageLockFile: | |
| name: str | |
| version: str | |
| lockfileVersion: int | |
| requires: bool | |
| packages: Dict[str, PackageInfo] = field(default_factory=dict) | |
| @staticmethod | |
| def from_json(file_contents: str) -> Self: | |
| json_data = json.loads(file_contents) | |
| packages = {} | |
| for key, value in json_data.get("packages", {}).items(): | |
| packages[key] = PackageInfo( | |
| version=value.get("version"), | |
| resolved=value.get("resolved"), | |
| integrity=value.get("integrity"), | |
| dev=value.get("dev"), | |
| dependencies=value.get("dependencies", {}), | |
| engines=value.get("engines", {}), | |
| cpu=value.get("cpu", []), | |
| os=value.get("os", []), | |
| optional=value.get("optional"), | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment