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

Added parse function for gui components in the future. #14

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions installation_instruction/installation_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,46 @@ def validate_and_render(self, input: dict) -> tuple[str, bool]:
instruction = helpers._replace_whitespace_in_string(instruction)

return (instruction, False)

def parse_schema(self) -> dict:
"""
Parses schema into a dict.

This is only important for merging enum, anyOf and oneOf into one type.

:return: Schema as dict.
:rtype: dict
"""
result = {}

for key, value in self.schema.get('properties', {}).items():

result[key] = {
"title": value.get("title", key),
"description": value.get("description", ""),
"type": value.get("type", "enum"),
"default": value.get("default", None),
}
if "enum" in value:
result[key]["enum"] = [
{
"title": e,
"value": e,
"description": "",
} for e in value["enum"]
]
elif type := "anyOf" if "anyOf" in value else "oneOf" if "oneOf" in value else None:
result[key]["enum"] = [
{
"title": c.get("title", c.get("const", "")),
"value": c.get("const", ""),
"description": c.get("description", ""),
} for c in value[type]
]
else:
result[key]["type"] = "string"

return result


def __init__(self, config: str) -> None:
Expand Down
1 change: 1 addition & 0 deletions tests/data/flags_options_example.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ properties:
const: cu118
- title: CUDA 12.1
const: cu121
description: CUDA 12.1 is the latest version of CUDA.
default: cu118

verbose:
Expand Down
45 changes: 45 additions & 0 deletions tests/test_installation_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,48 @@ def test_validate_and_render(user_input_tests):
install.validate_and_render(user_input_tests.get("invalid_data"))



def test_parse_schema(test_data_flags_options):
from .conftest import example_schemas
install = InstallationInstruction.from_file(example_schemas.get("pytorch"))
install.schema = test_data_flags_options
schema = install.parse_schema()
print(schema)

assert schema["packager"] == {
"title": "Packager",
"description": "The package manager of your choosing.",
"default": "pip",
"type": "enum",
"enum": [
{
"title": "pip",
"value": "pip",
"description": ""
},
{
"title": "conda",
"value": "conda",
"description": ""
}
]
}

assert schema["compute_platform"] == {
"title": "Compute Platform",
"description": "Should your gpu or your cpu handle the task?",
"enum": [
{
"title": "CUDA 11.8",
"value": "cu118",
"description": ""
},
{
"title": "CUDA 12.1",
"value": "cu121",
"description": "CUDA 12.1 is the latest version of CUDA."
}
],
"default": "cu118",
"type": "enum"
}