Add dependency download and CLI functionality

- Implement dependency download and extraction for Zig packages
- Create new CLI commands for downloading and converting ZON files
- Add support for downloading dependencies from ZON files
- Update project dependencies to include httpx and tqdm
- Add WTFPL license file
- Enhance README with more detailed usage instructions and project motivation
This commit is contained in:
2025-03-07 17:59:32 +08:00
parent fc48162fcb
commit 765c98502c
9 changed files with 562 additions and 93 deletions

View File

@ -1,60 +1,79 @@
"""
Command-line interface for the ZON parser.
Command-line interface for zon2json.
"""
import json
import sys
from pathlib import Path
import click
from loguru import logger
from zig_fetch_py.parser import parse_zon_file
from zig_fetch_py.parser import zon_to_json
def setup_logger(verbose: bool = False):
"""
Set up the logger.
Args:
verbose: Whether to enable verbose logging
"""
logger.remove()
log_level = "DEBUG" if verbose else "INFO"
logger.add(sys.stderr, level=log_level)
@click.command()
@click.argument("file", type=click.Path(exists=True, readable=True))
@click.argument("zon_file", type=click.Path(exists=True, readable=True, path_type=Path))
@click.option(
"-o",
"--output",
type=click.Path(writable=True),
help="Output JSON file path (default: stdout)",
type=click.Path(writable=True, path_type=Path),
help="Output file (default: stdout)",
)
@click.option(
"-i", "--indent", type=int, default=2, help="Indentation for the JSON output"
)
@click.option(
"--empty-tuple-as-dict",
is_flag=True,
help="Parse empty tuples as empty dictionaries",
)
@click.option("-p", "--pretty", is_flag=True, help="Pretty print JSON output")
@click.option("-v", "--verbose", is_flag=True, help="Enable verbose logging")
def main(file, output, pretty, verbose):
"""Parse ZON files and convert to JSON.
This tool parses Zig Object Notation (ZON) files and converts them to JSON format.
def main(zon_file, output, indent, empty_tuple_as_dict, verbose):
"""
# Configure logging
log_level = "DEBUG" if verbose else "INFO"
logger.remove() # Remove default handler
logger.add(sys.stderr, level=log_level)
Convert a ZON file to JSON.
logger.info(f"Processing file: {file}")
ZON_FILE: Path to the ZON file to convert
"""
# Set up logging
setup_logger(verbose)
try:
result = parse_zon_file(file)
# Read the ZON file
with open(zon_file, "r") as f:
zon_content = f.read()
indent = 4 if pretty else None
json_str = json.dumps(result, indent=indent)
# Convert to JSON
json_content = zon_to_json(
zon_content, indent=indent, empty_tuple_as_dict=empty_tuple_as_dict
)
# Output the JSON
if output:
logger.info(f"Writing output to: {output}")
with open(output, "w") as f:
f.write(json_str)
f.write(json_content)
logger.info(f"JSON written to {output}")
else:
logger.debug("Writing output to stdout")
click.echo(json_str)
click.echo(json_content)
except FileNotFoundError:
logger.error(f"File not found: {zon_file}")
sys.exit(1)
except Exception as e:
logger.error(f"Error: {e}")
sys.exit(1)
# This is only executed when the module is run directly
if __name__ == "__main__":
# When imported as a module, click will handle the function call
# When run directly, we need to call it explicitly
main()