forked from Fediversity/Fediversity
Revert "POC: render pydantic schema as module in Python"
This reverts commit b9c12db2ed613dd2157060aaba7004fa3b530f14.
This commit is contained in:
parent
8fbb3b6614
commit
4aeec9249a
2 changed files with 0 additions and 97 deletions
|
@ -1,53 +0,0 @@
|
||||||
import json
|
|
||||||
import textwrap
|
|
||||||
|
|
||||||
# TODO(@fricklerhandwerk): mix this in as a method on the Pydantic Schema itself
|
|
||||||
def generate_module(schema, service_name):
|
|
||||||
properties = schema.get("properties", {})
|
|
||||||
required = schema.get("required", [])
|
|
||||||
|
|
||||||
options = []
|
|
||||||
for name, prop in properties.items():
|
|
||||||
nix_type = {
|
|
||||||
"string": "types.str",
|
|
||||||
"integer": "types.int",
|
|
||||||
"boolean": "types.bool"
|
|
||||||
}[prop["type"]]
|
|
||||||
|
|
||||||
desc = prop.get("description")
|
|
||||||
|
|
||||||
default = None
|
|
||||||
if name not in required and "default" in prop:
|
|
||||||
to_nix_value = {
|
|
||||||
"string": lambda v: f'"{v}"',
|
|
||||||
"boolean": lambda v: str(v).lower(),
|
|
||||||
"integer": lambda v: str(v),
|
|
||||||
"number": lambda v: str(v)
|
|
||||||
}
|
|
||||||
default = to_nix_value[prop["type"]](prop["default"])
|
|
||||||
|
|
||||||
option = textwrap.dedent(f"""
|
|
||||||
{name} = mkOption {{{f'''
|
|
||||||
description = "{desc}";''' if desc else ''}
|
|
||||||
type = {nix_type};{f'''
|
|
||||||
default = {default};''' if default is not None else ''}
|
|
||||||
}};
|
|
||||||
""")
|
|
||||||
|
|
||||||
options.append(option.strip())
|
|
||||||
|
|
||||||
module = textwrap.dedent(f"""
|
|
||||||
{{ lib, ... }}:
|
|
||||||
|
|
||||||
let
|
|
||||||
cfg = config.services.{service_name};
|
|
||||||
inherit (lib) mkOption types;
|
|
||||||
in
|
|
||||||
{{
|
|
||||||
options.services.{service_name} = {{
|
|
||||||
{textwrap.indent("\n\n".join(options), ' ').strip()}
|
|
||||||
}};
|
|
||||||
}}
|
|
||||||
""")
|
|
||||||
|
|
||||||
return module
|
|
|
@ -1,44 +0,0 @@
|
||||||
from django.test import TestCase
|
|
||||||
from pydantic import BaseModel, Field
|
|
||||||
import textwrap
|
|
||||||
|
|
||||||
from panel.configuration import to_module
|
|
||||||
|
|
||||||
|
|
||||||
class ConvertToModule(TestCase):
|
|
||||||
def test_minimal_module(self):
|
|
||||||
class Example(BaseModel):
|
|
||||||
name: str = Field(..., description="Service name")
|
|
||||||
port: int = Field(8080)
|
|
||||||
enable: bool = Field(True, description="Enable service")
|
|
||||||
|
|
||||||
expected = textwrap.dedent("""
|
|
||||||
{ lib, ... }:
|
|
||||||
|
|
||||||
let
|
|
||||||
cfg = config.services.myservice;
|
|
||||||
inherit (lib) mkOption types;
|
|
||||||
in
|
|
||||||
{
|
|
||||||
options.services.myservice = {
|
|
||||||
name = mkOption {
|
|
||||||
description = "Service name";
|
|
||||||
type = types.str;
|
|
||||||
};
|
|
||||||
|
|
||||||
port = mkOption {
|
|
||||||
type = types.int;
|
|
||||||
default = 8080;
|
|
||||||
};
|
|
||||||
|
|
||||||
enable = mkOption {
|
|
||||||
description = "Enable service";
|
|
||||||
type = types.bool;
|
|
||||||
default = true;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
""")
|
|
||||||
|
|
||||||
actual = to_module.generate_module(Example.schema(), "myservice")
|
|
||||||
self.assertEqual(actual, expected)
|
|
Loading…
Add table
Reference in a new issue