File size: 2,961 Bytes
66ddc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""
Apply step-attribute patch to OpenEnv web_interface.py so number inputs
use step=0.01 (not default 1), allowing float values like lr_scale=0.02 and momentum_coef=0.9.
Idempotent: safe to run multiple times.
"""
import sys
from pathlib import Path


def main() -> None:
    if len(sys.argv) < 2:
        # Discover path from current Python's site-packages
        import openenv.core.env_server.web_interface as m

        path = Path(m.__file__).resolve()
    else:
        path = Path(sys.argv[1]).resolve()

    if not path.exists():
        print(f"File not found: {path}", file=sys.stderr)
        sys.exit(1)

    text = path.read_text()

    # Already patched?
    if "_get_step_for_number_field" in text and '"step": _get_step_for_number_field(field_info)' in text:
        print("Already patched:", path)
        return

    # 1) Insert helper before _extract_action_fields
    old1 = 'def _extract_action_fields(action_cls: Type[Action]) -> List[Dict[str, Any]]:'
    new1 = '''def _get_step_for_number_field(field_info: Dict[str, Any]) -> float | None:
    """Step for number inputs; avoids HTML5 default of 1 which restricts float ranges."""
    if field_info.get("type") == "integer":
        return 1
    if field_info.get("type") == "number":
        return field_info.get("multipleOf") if field_info.get("multipleOf") is not None else 0.01
    return None


def _extract_action_fields(action_cls: Type[Action]) -> List[Dict[str, Any]]:'''
    if new1 not in text and old1 in text:
        text = text.replace(old1, new1, 1)

    # 2) Add "step" to action_fields dict
    old2 = '                "help_text": _generate_help_text(field_name, field_info),\n            }\n        )'
    new2 = '                "help_text": _generate_help_text(field_name, field_info),\n                "step": _get_step_for_number_field(field_info),\n            }\n        )'
    if new2 not in text and old2 in text:
        text = text.replace(old2, new2, 1)

    # 3) Add step_value and step attribute in _generate_single_field
    old3a = "    max_value = field.get(\"max_value\")\n    default_value = field.get(\"default_value\")"
    new3a = "    max_value = field.get(\"max_value\")\n    step_value = field.get(\"step\")\n    default_value = field.get(\"default_value\")"
    if new3a not in text and old3a in text:
        text = text.replace(old3a, new3a, 1)

    old3b = "    if default_value is not None:\n        input_attrs.append(f'value=\"{default_value}\"')\n\n    attrs_str = \" \".join(input_attrs)"
    new3b = "    if default_value is not None:\n        input_attrs.append(f'value=\"{default_value}\"')\n    if step_value is not None:\n        input_attrs.append(f'step=\"{step_value}\"')\n\n    attrs_str = \" \".join(input_attrs)"
    if new3b not in text and old3b in text:
        text = text.replace(old3b, new3b, 1)

    path.write_text(text)
    print("Patched:", path)


if __name__ == "__main__":
    main()