From 6b21e9919e9858df489ecd9f92416fb2f3dc5b6b Mon Sep 17 00:00:00 2001 From: Sebastien Jourdain Date: Tue, 31 Dec 2024 17:35:44 -0700 Subject: [PATCH] fix(tools): using google syntax in widget generator --- trame/tools/widgets/generator.py | 44 +++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/trame/tools/widgets/generator.py b/trame/tools/widgets/generator.py index 256c29a9..b1a182a5 100644 --- a/trame/tools/widgets/generator.py +++ b/trame/tools/widgets/generator.py @@ -55,7 +55,7 @@ async def create_base_structure(ref_path, config, output): ) # Register trame/modules/{webdir} trame_plugins[f"trame/modules/{module}.py"] = ( - f"from {name}.module.{module} import *\n" + f"from {name}.module.{module} import * # noqa F403\n" ) create_module_init(module_root_init, module_conf_init) @@ -99,7 +99,7 @@ async def create_base_structure(ref_path, config, output): properties = class_info.get("properties", []) events = class_info.get("events", []) file.write(f"\nclass {class_name}(HtmlElement):") - to_py_help(file, class_info) + to_py_help(file, class_info, class_name) if class_info.get("methods"): file.write("\n _next_id = 0") file.write( @@ -174,7 +174,7 @@ async def create_base_structure(ref_path, config, output): # Register trame/modules/{webdir} trame_plugins[f"trame/widgets/{module}.py"] = ( - f"from {name}.widgets.{module} import *\n" + f"from {name}.widgets.{module} import * # noqa F403\n" "\n" f"def initialize(server):\n" f" from {name}.module import {module}\n" @@ -313,7 +313,25 @@ def to_py_attr(file, item, indent=4): file.write(f'\n{" "*indent}"{entry}",') -def to_py_help(file, class_info): +def multi_line_help(help, indent): + help = help.strip() + if len(help) > 80: + tokens = help.split(" ") + description_lines = [] + while len(tokens): + line = [] + while len(" ".join(line)) < 60 and len(tokens): + line.append(tokens.pop(0)) + description_lines.append(" ".join(line)) + line = [] + if len(line): + description_lines.append(" ".join(line)) + + help = f"\n{' '*indent}".join(description_lines) + return help + + +def to_py_help(file, class_info, class_name): indent = 4 file.write(f'\n{" "*indent}"""') main_help = class_info.get("help", "") @@ -322,22 +340,30 @@ def to_py_help(file, class_info): if len(main_help): file.write(f'\n{" "* indent}{main_help}') + else: + file.write( + f'\n{" "* indent}Component {class_name} with the following properties\n' + ) + + if len(properties) + len(events) > 0: + file.write(f'\n{" "* indent}Args:') if len(properties): - file.write(f'\n{" "* indent}Properties\n') for prop in properties: name = prop.get("name") help = prop.get("help", "").replace("\n", " ") if isinstance(name, (list, tuple)): name = name[0] - file.write(f'\n{" "* indent}:param {name}: {help}') + file.write(f'\n{" "* (indent + 2)}{name}:\n{" "* (indent + 4)}') + file.write(multi_line_help(help, indent + 4)) if len(events): - file.write(f'\n\n{" "* indent}Events\n') for prop in events: name = prop.get("name") help = prop.get("help", "").replace("\n", " ") if isinstance(name, (list, tuple)): name = name[0] - file.write(f'\n{" "* indent}:param {name}: {help}') - file.write(f'\n{" "*indent}"""') + file.write(f'\n{" "* (indent + 2)}{name} (event):\n{" "* (indent + 4)}') + file.write(multi_line_help(help, indent + 4)) + + file.write(f'\n\n{" "*indent}"""')