#!/usr/bin/env python3 import argparse import importlib import struct from sipyco import common_args from artiq import __version__ as artiq_version from artiq.master.databases import DeviceDB def get_argparser(): parser = argparse.ArgumentParser(description="ARTIQ RTIO channel name map encoder tool") parser.add_argument("--version", action="version", version="ARTIQ v{}".format(artiq_version), help="print the ARTIQ version number") common_args.verbosity_args(parser) parser.add_argument("--device-db", default="device_db.py", help="device database file (default: '%(default)s')") parser.add_argument("file", metavar="FILE", default=None, help="write the result into the specified file, or read from it to show the map (see `--show`)") parser.add_argument("--show", default=False, action="store_true", help="show the channel mapping from the specified file, instead of writing to it") return parser def get_rtio_channels(desc): if desc["type"] == "local": module = importlib.import_module(desc["module"]) device_class = getattr(module, desc["class"]) return getattr(device_class, "get_rtio_channels", lambda **kwargs: [])(**desc.get("arguments", {})) return [] def get_channel_map(device_db): reversed_map = {} for dev_name, device in device_db.items(): try: channels = get_rtio_channels(device) except Exception as e: raise Exception(f"failed to process the device `{dev_name}`") from e for chan, suffix in channels: assert chan not in reversed_map reversed_map[chan] = dev_name + (" " + suffix if suffix is not None else "") return reversed_map def serialize_device_map(channel_map, outfile): outfile.write(struct.pack(" {device}") else: ddb = DeviceDB(args.device_db) chan_map = get_channel_map(ddb.get_device_db()) with open(args.file, "wb") as outfile: serialize_device_map(chan_map, outfile) if __name__ == "__main__": main()