mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-03-05 00:12:49 +01:00
105 lines
3.3 KiB
Python
105 lines
3.3 KiB
Python
# pylint: disable=missing-module-docstring
|
|
import inspect
|
|
import typing
|
|
import black
|
|
|
|
|
|
class ClientGenerator:
|
|
def __init__(self):
|
|
self.header = """# This file was automatically generated by generate_cli.py\n
|
|
from bec_widgets.cli.client_utils import rpc_call, RPCBase, BECFigureClientMixin
|
|
from typing import Literal, Optional, overload"""
|
|
|
|
self.content = ""
|
|
|
|
def generate_client(self, published_classes: list):
|
|
"""
|
|
Generate the client for the published classes.
|
|
|
|
Args:
|
|
published_classes(list): The list of published classes (e.g. [BECWaveform1D, BECFigure]).
|
|
"""
|
|
for cls in published_classes:
|
|
self.content += "\n\n"
|
|
self.generate_content_for_class(cls)
|
|
|
|
def generate_content_for_class(self, cls):
|
|
"""
|
|
Generate the content for the class.
|
|
Args:
|
|
cls: The class for which to generate the content.
|
|
"""
|
|
|
|
class_name = cls.__name__
|
|
module = cls.__module__
|
|
|
|
# Generate the header
|
|
# self.header += f"""
|
|
# from {module} import {class_name}"""
|
|
|
|
# Generate the content
|
|
if cls.__name__ == "BECFigure":
|
|
self.content += f"""
|
|
class {class_name}(RPCBase, BECFigureClientMixin):"""
|
|
else:
|
|
self.content += f"""
|
|
class {class_name}(RPCBase):"""
|
|
for method in cls.USER_ACCESS:
|
|
obj = getattr(cls, method)
|
|
if isinstance(obj, property):
|
|
self.content += """
|
|
@property
|
|
@rpc_call"""
|
|
sig = str(inspect.signature(obj.fget))
|
|
doc = inspect.getdoc(obj.fget)
|
|
else:
|
|
sig = str(inspect.signature(obj))
|
|
doc = inspect.getdoc(obj)
|
|
overloads = typing.get_overloads(obj)
|
|
for overload in overloads:
|
|
sig_overload = str(inspect.signature(overload))
|
|
self.content += f"""
|
|
@overload
|
|
def {method}{str(sig_overload)}: ...
|
|
"""
|
|
|
|
self.content += """
|
|
@rpc_call"""
|
|
self.content += f"""
|
|
def {method}{str(sig)}:
|
|
\"\"\"
|
|
{doc}
|
|
\"\"\""""
|
|
|
|
def write(self, file_name: str):
|
|
"""
|
|
Write the content to a file, automatically formatted with black.
|
|
|
|
Args:
|
|
file_name(str): The name of the file to write to.
|
|
"""
|
|
# Combine header and content, then format with black
|
|
full_content = self.header + "\n" + self.content
|
|
try:
|
|
formatted_content = black.format_str(full_content, mode=black.FileMode(line_length=100))
|
|
except black.NothingChanged:
|
|
formatted_content = full_content
|
|
|
|
with open(file_name, "w", encoding="utf-8") as file:
|
|
file.write(formatted_content)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import os
|
|
|
|
# Assuming ClientGenerator is defined in this script or imported correctly
|
|
from bec_widgets.widgets.figure import BECFigure
|
|
from bec_widgets.widgets.plots import BECPlotBase, BECWaveform1D, BECCurve
|
|
|
|
current_path = os.path.dirname(__file__)
|
|
client_path = os.path.join(current_path, "client.py")
|
|
clss = [BECPlotBase, BECWaveform1D, BECFigure, BECCurve]
|
|
generator = ClientGenerator()
|
|
generator.generate_client(clss)
|
|
generator.write(client_path)
|