This commit is contained in:
2025-03-07 16:42:22 +08:00
parent 3404b6b7e0
commit 807fcb2849
11 changed files with 587 additions and 88 deletions

11
examples/example.zon Normal file
View File

@ -0,0 +1,11 @@
.{
.name = "example",
.version = "1.0.0",
.dependencies = .{
.lib1 = .{
.url = "https://example.com/lib1.tar.gz",
.hash = "abcdef123456",
},
},
.tags = .["tag1", "tag2"],
}

64
examples/simple_example.py Executable file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""
Simple example demonstrating how to use the zig-fetch-py library.
"""
import json
import sys
from pathlib import Path
from loguru import logger
from zig_fetch_py.parser import parse_zon_file, zon_to_json
# Configure logging
logger.remove()
logger.add(sys.stderr, level="INFO")
# Example ZON content
ZON_CONTENT = """.{
.name = "example",
.version = "1.0.0",
.dependencies = .{
.lib1 = .{
.url = "https://example.com/lib1.tar.gz",
.hash = "abcdef123456",
},
},
.tags = .["tag1", "tag2"],
}"""
def main():
# Create a temporary ZON file
example_dir = Path(__file__).parent
zon_file = example_dir / "example.zon"
zon_file.write_text(ZON_CONTENT)
logger.info(f"Created example ZON file: {zon_file}")
# Parse the ZON file
result = parse_zon_file(str(zon_file))
logger.info("Parsed ZON file to Python dictionary:")
logger.info(result)
# Convert to JSON
json_str = zon_to_json(ZON_CONTENT, indent=2)
logger.info("Converted ZON to JSON:")
logger.info(json_str)
# Save JSON to file
json_file = example_dir / "example.json"
with open(json_file, "w") as f:
f.write(json_str)
logger.info(f"Saved JSON to file: {json_file}")
# Access specific values from the parsed data
logger.info(f"Package name: {result['name']}")
logger.info(f"Package version: {result['version']}")
logger.info(f"Dependencies: {list(result['dependencies'].keys())}")
logger.info(f"Tags: {result['tags']}")
if __name__ == "__main__":
main()

29
examples/test.zon Normal file
View File

@ -0,0 +1,29 @@
.{
.name = .zls,
// Must match the `zls_version` in `build.zig`
.version = "0.15.0-dev",
// Must be kept in line with the `minimum_build_zig_version` in `build.zig`.
// Should be a Zig version that is downloadable from https://ziglang.org/download/ or a mirror.
.minimum_zig_version = "0.14.0",
.dependencies = .{
.known_folders = .{
.url = "https://github.com/ziglibs/known-folders/archive/aa24df42183ad415d10bc0a33e6238c437fc0f59.tar.gz",
.hash = "known_folders-0.0.0-Fy-PJtLDAADGDOwYwMkVydMSTp_aN-nfjCZw6qPQ2ECL",
},
.diffz = .{
.url = "https://github.com/ziglibs/diffz/archive/ef45c00d655e5e40faf35afbbde81a1fa5ed7ffb.tar.gz",
.hash = "N-V-__8AABhrAQAQLLLGadghhPsdxTgBk9N9aLVOjXW3ay0V",
},
.@"lsp-codegen" = .{
.url = "https://github.com/zigtools/zig-lsp-codegen/archive/063a98c13a2293d8654086140813bdd1de6501bc.tar.gz",
.hash = "lsp_codegen-0.1.0-CMjjo0ZXCQB-rAhPYrlfzzpU0u0u2MeGvUucZ-_g32eg",
},
.tracy = .{
.url = "https://github.com/wolfpld/tracy/archive/refs/tags/v0.11.1.tar.gz",
.hash = "N-V-__8AAMeOlQEipHjcyu0TCftdAi9AQe7EXUDJOoVe0k-t",
.lazy = true,
},
},
.paths = .{""},
.fingerprint = 0xa66330b97eb969ae, // Changing this has security and trust implications.
}