11"""Generate lightpanda/_methods.py from the binary's tools/list schemas.
22
3- Emits one concrete, annotated, documented method per browser tool (snake_case,
4- with the original tool name aliased) , all forwarding to ``Session.call``.
5- Being real code, the methods are visible to IDEs, type checkers, and pdoc
6- alike. Run from bindings/python with a binary available:
3+ Emits one concrete, annotated, documented method per browser tool, with the
4+ tool name and its parameters in snake_case , all forwarding to ``Session.call``
5+ (which maps them back to the schema's names). Being real code, the methods are
6+ visible to IDEs, type checkers, and pdoc alike. Run with a binary available:
77
88 uv run --no-project python scripts/generate_methods.py
99
2727 """Generated by scripts/generate_methods.py — do not edit.
2828
2929One method per lightpanda browser tool, signatures derived from the tool
30- JSON schemas. camelCase aliases mirror the underlying tool names.
31- ``AsyncSessionMethods`` is the same surface with ``async def`` signatures.
30+ JSON schemas, with tool and parameter names in snake_case (the schemas'
31+ camelCase names still work through ``Session.call``). ``AsyncSessionMethods``
32+ is the same surface with ``async def`` signatures.
3233"""
3334
3435from __future__ import annotations
@@ -53,23 +54,27 @@ def method_source(name: str, spec: dict, is_async: bool = False) -> str:
5354 schema = spec ["schema" ]
5455 properties = schema .get ("properties" , {})
5556 required = set (schema .get ("required" , []))
56- for prop in properties :
57- if keyword .iskeyword (prop ) or not prop .isidentifier ():
57+ args = {prop : _snake (prop ) for prop in properties }
58+ for prop , arg in args .items ():
59+ if keyword .iskeyword (arg ) or not arg .isidentifier ():
5860 raise SystemExit (f"tool { name } : property { prop !r} is not a valid Python parameter" )
61+ if len (set (args .values ())) != len (args ):
62+ raise SystemExit (f"tool { name } : properties collide once in snake_case: { sorted (args )} " )
5963
6064 params = ["self" ]
6165 if properties :
6266 params .append ("*" )
6367 forwards = []
6468 for prop in sorted (properties , key = lambda p : p not in required ):
69+ arg = args [prop ]
6570 py_type = PY_TYPES .get (properties [prop ].get ("type" , "" ), "Any" )
6671 if name == "extract" and prop == "schema" :
6772 py_type = "str | dict | list"
6873 if prop in required :
69- params .append (f"{ prop } : { py_type } " )
74+ params .append (f"{ arg } : { py_type } " )
7075 else :
71- params .append (f"{ prop } : { py_type } | None = None" )
72- forwards .append (f"{ prop } ={ prop } " )
76+ params .append (f"{ arg } : { py_type } | None = None" )
77+ forwards .append (f"{ arg } ={ arg } " )
7378
7479 snake = _snake (name )
7580 call_args = ", " .join ([f'"{ name } "' ] + forwards )
@@ -78,9 +83,6 @@ def method_source(name: str, spec: dict, is_async: bool = False) -> str:
7883 lines = [f" { prefix } { snake } ({ ', ' .join (params )} ) -> Any:" ]
7984 lines .append (docstring (spec ["description" ]))
8085 lines .append (f" return { await_ } self.call({ call_args } )" )
81- if snake != name :
82- lines .append ("" )
83- lines .append (f" { name } = { snake } " )
8486 return "\n " .join (lines )
8587
8688
0 commit comments