|
| 1 | +""" |
| 2 | +This module contains helper methods to retrieve a list of all devices in a directory. |
| 3 | +It does so by recursively traversing the directory and checking if the file contains a |
| 4 | +class that inherits from the Device class. |
| 5 | +""" |
| 6 | + |
| 7 | +import importlib.util |
| 8 | +import os |
| 9 | + |
| 10 | +from ophyd import Device |
| 11 | + |
| 12 | + |
| 13 | +def get_devices(repo_name: str, ignore: str = "") -> dict: |
| 14 | + """ |
| 15 | + Get all devices in a directory. |
| 16 | +
|
| 17 | + Args: |
| 18 | + directory (str): The directory to search for devices. |
| 19 | + ignore (str): A comma-separated list of module names to ignore. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + list: A list of all devices in the directory. |
| 23 | + """ |
| 24 | + devices = {} |
| 25 | + |
| 26 | + anchor_module = importlib.import_module(f"{repo_name}.devices") |
| 27 | + directory = os.path.dirname(anchor_module.__file__) |
| 28 | + |
| 29 | + for root, _, files in os.walk(directory): |
| 30 | + for file in files: |
| 31 | + if not file.endswith(".py") or file.startswith("__"): |
| 32 | + continue |
| 33 | + |
| 34 | + path = os.path.join(root, file) |
| 35 | + subs = os.path.dirname(os.path.relpath(path, directory)).split("/") |
| 36 | + if len(subs) == 1 and not subs[0]: |
| 37 | + module_name = file.split(".")[0] |
| 38 | + else: |
| 39 | + module_name = ".".join(subs + [file.split(".")[0]]) |
| 40 | + |
| 41 | + if module_name in ignore.split(","): |
| 42 | + continue |
| 43 | + module = importlib.import_module(f"{repo_name}.devices.{module_name}") |
| 44 | + |
| 45 | + for name in dir(module): |
| 46 | + obj = getattr(module, name) |
| 47 | + if not hasattr(obj, "__module__") or obj.__module__ != module.__name__: |
| 48 | + continue |
| 49 | + if isinstance(obj, type) and issubclass(obj, Device) and obj is not Device: |
| 50 | + devices[obj.__name__] = obj |
| 51 | + |
| 52 | + return dict(sorted(devices.items(), key=lambda x: x[0].lower())) |
| 53 | + |
| 54 | + |
| 55 | +def write_device_list(devices: dict, file_name: str, repo_name: str, append=False): |
| 56 | + """ |
| 57 | + Write the list of devices to a file. |
| 58 | +
|
| 59 | + Args: |
| 60 | + devices (list): A list of devices. |
| 61 | + file_out (str): The file to write the devices to. |
| 62 | + repo_name (str): The repository name for linking to the source code. |
| 63 | + append (bool): Whether to append to the file or overwrite it. |
| 64 | + """ |
| 65 | + if not append or not os.path.exists(file_name): |
| 66 | + with open(file_name, "w", encoding="utf-8") as output_file: |
| 67 | + output_file.write("// This file was autogenerated. Do not edit it manually.\n") |
| 68 | + output_file.write("## Device List\n") |
| 69 | + |
| 70 | + with open(file_name, "a", encoding="utf-8") as output_file: |
| 71 | + output_file.write(f"### {repo_name} \n") |
| 72 | + output_file.write("| Device | Documentation | Module |\n") |
| 73 | + output_file.write("| :----- | :------------- | :------ |\n") |
| 74 | + for dev, cls in devices.items(): |
| 75 | + doc = cls.__doc__ |
| 76 | + doc = "" if doc is None else doc.replace("\n", "<br>") |
| 77 | + output_file.write( |
| 78 | + f"| {dev} | {doc} | [{cls.__module__}](https://github.com/bec-project/{repo_name}/tree/main/{cls.__module__.replace('.', '/')}.py) |\n" |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + import argparse |
| 84 | + |
| 85 | + parser = argparse.ArgumentParser(description="Retrieve a list of devices in a directory.") |
| 86 | + parser.add_argument("repo", type=str, help="The repository to link to.") |
| 87 | + parser.add_argument("output", type=str, help="The file to write the devices to.") |
| 88 | + parser.add_argument( |
| 89 | + "--append", action="store_true", help="Append to the file instead of overwriting it." |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "--ignore", type=str, help="Ignore the specified modules (comma-separated).", default="" |
| 93 | + ) |
| 94 | + |
| 95 | + args = parser.parse_args() |
| 96 | + devs = get_devices(args.repo, ignore=args.ignore) |
| 97 | + write_device_list(devs, args.output, args.repo, append=args.append) |
0 commit comments