Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate YAML schema and add as release asset #298

Open
staticaland opened this issue Nov 12, 2024 · 0 comments
Open

Generate YAML schema and add as release asset #298

staticaland opened this issue Nov 12, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@staticaland
Copy link
Contributor

staticaland commented Nov 12, 2024

Consider having YAML schema as release asset and decouple it from the CLI commands.

Either that, or put them in a directory in this repo, and refer to the GitHub "raw" URLs.

Quick script

Run with uv run hello.py:

# /// script
# dependencies = [
#   "pyyaml==6.0.1",
# ]
# ///

import yaml
from typing import Dict, Any, List
import json

def variable_to_schema_property(variable: Dict[str, Any]) -> Dict[str, Any]:
    property_schema = {
        'title': variable['name']
    }
    
    var_type = variable.get('type', 'string')
    if var_type == 'bool':
        property_schema['type'] = 'boolean'
    elif var_type == 'int':
        property_schema['type'] = 'integer'
    elif var_type == 'float':
        property_schema['type'] = 'number'
    else:
        property_schema['type'] = 'string'
    
    if 'description' in variable:
        property_schema['description'] = variable['description']
    
    if 'options' in variable:
        property_schema['enum'] = variable['options']
    
    if 'default' in variable:
        property_schema['default'] = variable['default']
    
    return property_schema

def generate_config_schema(boilerplate_path: str) -> Dict[str, Any]:
    with open(boilerplate_path, 'r') as f:
        boilerplate = yaml.safe_load(f)
    
    schema = {
        '$schema': 'http://json-schema.org/draft-07/schema#',
        'type': 'object',
        'properties': {},
        'required': []
    }
    
    if 'variables' in boilerplate:
        for variable in boilerplate['variables']:
            var_name = variable['name']
            schema['properties'][var_name] = variable_to_schema_property(variable)
            if 'default' not in variable:
                schema['required'].append(var_name)
    
    if 'dependencies' in boilerplate:
        for dep in boilerplate['dependencies']:
            if not dep.get('dont-inherit-variables', False) and 'variables' in dep:
                for variable in dep['variables']:
                    var_name = variable['name']
                    schema['properties'][var_name] = variable_to_schema_property(variable)
                    if 'default' not in variable:
                        schema['required'].append(var_name)

    return schema

def write_schema(schema: Dict[str, Any], output_path: str):
    with open(output_path, 'w') as f:
        json.dump(schema, f, indent=2)

if __name__ == '__main__':
    import sys
    if len(sys.argv) != 3:
        print("Usage: python config_schema_generator.py <boilerplate.yml> <output.json>")
        sys.exit(1)
    
    schema = generate_config_schema(sys.argv[1])
    write_schema(schema, sys.argv[2])
    print(f"Schema generated successfully at {sys.argv[2]}")
@staticaland staticaland added the enhancement New feature or request label Nov 18, 2024
@falense falense changed the title Consider having YAML schema as release asset Release YAML schema as asset Nov 18, 2024
@falense falense changed the title Release YAML schema as asset Generate YAML schema and add as release asset Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant