# pylint: disable=missing-module-docstring from __future__ import annotations import inspect import sys import black if sys.version_info >= (3, 11): from typing import get_overloads else: print( "Python version is less than 3.11, using dummy function for get_overloads. " "If you want to use the real function 'typing.get_overloads()', please use Python 3.11 or later." ) def get_overloads(obj): # Dummy function for Python versions before 3.11 return [] 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, BECGuiClientMixin 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__ == "BECDockArea": self.content += f""" class {class_name}(RPCBase, BECGuiClientMixin):""" 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 = 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__": # pragma: no cover import os from bec_widgets.utils import BECConnector from bec_widgets.widgets import BECDock, BECDockArea, BECFigure, SpiralProgressBar from bec_widgets.widgets.figure.plots.image.image import BECImageShow from bec_widgets.widgets.figure.plots.image.image_item import BECImageItem from bec_widgets.widgets.figure.plots.motor_map.motor_map import BECMotorMap from bec_widgets.widgets.figure.plots.plot_base import BECPlotBase from bec_widgets.widgets.figure.plots.waveform.waveform import BECWaveform from bec_widgets.widgets.figure.plots.waveform.waveform_curve import BECCurve from bec_widgets.widgets.spiral_progress_bar.ring import Ring from bec_widgets.widgets.website.website import WebsiteWidget current_path = os.path.dirname(__file__) client_path = os.path.join(current_path, "client.py") clss = [ BECPlotBase, BECWaveform, BECFigure, BECCurve, BECImageShow, BECConnector, BECImageItem, BECMotorMap, BECDock, BECDockArea, SpiralProgressBar, Ring, WebsiteWidget, ] generator = ClientGenerator() generator.generate_client(clss) generator.write(client_path)