How to Write a Site Specific Formatter

This guide explains how you can create a pvi formatter to generate screens for your own use cases.

Overview

The formatters role is to take a device.yaml file and turn this into a screen file that can be used by the display software. Inside of the device.yaml file is a list of components that specify its name, a widget type and any additional properties that can be assigned to that widget (such as a pv name). During formatting, the device.yaml file is deserialised into component objects, which are later translated into widgets:

@as_discriminated_union
class Component(Labelled):
    """These make up a Device"""
@dataclass
class SignalR(Component):
    """Scalar value backed by a single PV"""

    pv: Annotated[str, desc("PV to be used for get and monitor")]
    widget: Annotated[
        Optional[ReadWidget],
        desc("Widget to use for display, None means don't display"),
    ] = None

To make a screen from this, we need a template file. This contains a blank representation of each supported widget for each of the supported file formats (bob, edl etc…). Below is an example of a ‘text entry’ widget for a .bob file:

    <actions>
      <action type="write_pv">
        <pv_name>$(pv_name)</pv_name>
        <value>value</value>
        <description>WritePV</description>
      </action>
    </actions>
    <text>SignalX</text>
    <x>18</x>
    <y>118</y>
    <width>120</width>
    <height>40</height>
    <tooltip>$(actions)</tooltip>
  </widget>
  <widget type="textupdate" version="2.0.0">
    <name>TextUpdate</name>
    <pv_name>TextRead</pv_name>

By extracting and altering the template widgets with the information provided by the components, we can create a screen file.

Create a formatter subclass

To start, we will need to create our own formatter class. These inherit from an abstract ‘Formatter’ class that is defined in base.py. Inside, we need to define one mandatory ‘format’ function, which will be used to create our screen file:

@as_discriminated_union
@dataclass
class Formatter:
    def format(self, device: Device, prefix: str, path: Path):
        raise NotImplementedError(self)

The format function takes in a device: a list of components obtained from our deserialised device.yaml file, A prefix: the pv prefix of the device, and a path: the output destination for the generated screen file.

With a formatter defined, we now can start to populate this by defining the screen dependencies.

Define the Screen Layout Properties

Each screen requires a number of layout properties that allow you to customise the size and placement of widgets. These are stored within a ‘ScrenLayout’ dataclass that can be imported from utils.py. Within the dataclass are the following configurable parameters:

@dataclass
class ScreenLayout:
    spacing: Annotated[int, desc("Spacing between widgets")]
    title_height: Annotated[int, desc("Height of screen title bar")]
    max_height: Annotated[int, desc("Max height of the screen")]
    group_label_height: Annotated[int, desc("Height of the group title label")]
    label_width: Annotated[int, desc("Width of the labels describing widgets")]
    widget_width: Annotated[int, desc("Width of the widgets")]
    widget_height: Annotated[int, desc("Height of the widgets (Labels use this too)")]
    group_widget_indent: Annotated[
        int, desc("Indentation of widgets within groups. Defaults to 0")
    ] = 0
    group_width_offset: Annotated[
        int, desc("Additional border width when using group objects. Defaults to 0")
    ] = 0

When defining these in our formatter, we have the option of deciding which properties should be configurable inside of the formatter.yaml. Properties defined as member variables of the formatter class (and then referenced by the layout properties in the screen format function) will be available to adjust inside of the formatter.yaml. Anything else, should be considered as defaults for the formatter:

        screen_layout = ScreenLayout(
            spacing=self.spacing,
            title_height=self.title_height,
            max_height=self.max_height,
            group_label_height=26,
            label_width=self.label_width,
            widget_width=self.widget_width,
            widget_height=self.widget_height,
            group_widget_indent=18,
            group_width_offset=26,
        )

In the example above, everything has been made adjustable from the formatter.yaml except the properties relating to groups. This is becuase they are more dependant on the file format used rather than the users personal preference.

For clarity, the example below shows how the formatter.yaml can be used to set the layout properties. Note that these are optional as each property is defined with a default value:

# yaml-language-server: $schema=../../../schemas/pvi.formatter.schema.json
type: DLSFormatter
# Remove screen property to use default value
spacing: 4
title_height: 26
max_height: 900
label_width: 120
widget_width: 120
widget_height: 20

Assign a Template File

As previously stated, a template file provides the formatter with a base model of all of the supported widgets that it can then overwrite with component data. Currently, pvi supports templates for edl, adl and bob files, which can be referenced from the _format directory with the filename ‘dls’ + the file formats suffix (eg. dls.bob).

Inside of the format function, we need to provide a reference to the template file that can then be used to identify what each widget should look like:

template = BobTemplate(str(Path(__file__).parent / "dls.bob"))

Divide the Template into Widgets

With a template defined, we now need to assign each part of it to a supported widget. This is achieved using the ScreenWidgets dataclass (from utils.py). With this, we can assign each of the widget classes to a snippet of the template using the WidgetFactory.from_template method:

        widget_formatter_factory = WidgetFormatterFactory(
            header_formatter_cls=LabelWidgetFormatter.from_template(
                template,
                search="Heading",
                property_map=dict(text="text"),
            ),
            label_formatter_cls=LabelWidgetFormatter.from_template(
                template,
                search="Label",
                property_map=dict(text="text"),
            ),
            led_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="LED",
                sized=Bounds.square,
                property_map=dict(pv_name="pv"),
            ),
            progress_bar_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="ProgressBar",
                property_map=dict(pv_name="pv"),
            ),
            text_read_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="TextUpdate",
                property_map=dict(pv_name="pv"),
            ),
            check_box_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="ChoiceButton",
                property_map=dict(pv_name="pv"),
            ),
            combo_box_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="ComboBox",
                property_map=dict(pv_name="pv"),
            ),
            text_write_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="TextEntry",
                property_map=dict(pv_name="pv"),
            ),
            table_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="Table",
                property_map=dict(pv_name="pv"),
            ),
            action_formatter_cls=ActionWidgetFormatter.from_template(
                template,
                search="ActionButton",
                property_map=dict(text="label", pv_name="pv", value="value"),
            ),
            sub_screen_formatter_cls=SubScreenWidgetFormatter.from_template(
                template,
                search="SubScreen",
                property_map=dict(file="file_name"),
            ),
        )

This function uses a unique search term to locate and extract a widget from the template. As such, the search term MUST be unique to avoid extracing multiple or irrelevant widgets from the template.

Define screen and group widget functions

Two widgets that are not handled by ScreenWidgets are the screen title and group object. This is because the style of these widgets differ greatly for each file type. For instance, with edl and adl files, groups are represented by a rectangle and title placed behind a collection of widgets. Conversely, bob files handle groups using its dedicated group object, which places widgets as children under the group object. Becuase of this, we need to define two functions: one for the additional screen widgets (such as the title), and one to represent the group widgets.

We then need to define two functions that can be used to create multiple instances of these widgets. In this example, we provide two arguments: The ‘bounds’, to set the widgets size and position, and the ‘title’ to populate the label with.

        screen_title_cls = LabelWidgetFormatter.from_template(
            template,
            search="Title",
            property_map=dict(text="text"),
        )
        group_title_cls = LabelWidgetFormatter.from_template(
            template,
            search="Group",
            property_map=dict(name="text"),
        )

        def create_group_object_formatter(
            bounds: Bounds, title: str
        ) -> List[WidgetFormatter[str]]:
            return [
                group_title_cls(
                    Bounds(bounds.x, bounds.y, bounds.w, bounds.h), f"{title}"
                )
            ]

        def create_screen_title_formatter(
            bounds: Bounds, title: str
        ) -> List[WidgetFormatter[str]]:
            return [
                screen_title_cls(
                    Bounds(0, 0, bounds.w, screen_layout.title_height), title
                )
            ]

Construct a Screen Object

Provided that you have defined the LayoutProperties, template, ScreenWidgets and the screen title and group object functions, we are now ready to define a screen object.

        formatter_factory = ScreenFormatterFactory(
            screen_formatter_cls=GroupFormatter.from_template(
                template,
                search=GroupType.SCREEN,
                sized=with_title(screen_layout.spacing, screen_layout.title_height),
                widget_formatter_hook=create_screen_title_formatter,
            ),
            group_formatter_cls=GroupFormatter.from_template(
                template,
                search=GroupType.GROUP,
                sized=with_title(
                    screen_layout.spacing, screen_layout.group_label_height
                ),
                widget_formatter_hook=create_group_object_formatter,
            ),
            widget_formatter_factory=widget_formatter_factory,
            prefix=prefix,
            layout=screen_layout,
            base_file_name=path.stem,
        )

Note that screen_cls and group_cls are defined separately here as GroupFactories. This is because they take in the make_widgets function, which has the possibility of returning multiple widgets. (In edl files for example, we return a rectangle and label widget to represent a group.)

The screen object itself contains two key functions: The ‘screen’ function takes a deserialised device.yaml file and converts each of its components into widgets. It then calculates the size and position of these widgets to generate a uniform screen layout. On the output of this, we can call a (screen.)format function that populates these widgets with the extracted properties from the device.yaml, and converts them into the chosen file format:

        title = f"{device.label} - {prefix}"

        screen_formatter, sub_screens = formatter_factory.create_screen_formatter(
            device.children, title
        )

        write_bob(screen_formatter, path)
        for sub_screen_name, sub_screen_formatter in sub_screens:
            sub_screen_path = Path(path.parent / f"{sub_screen_name}{path.suffix}")
            write_bob(sub_screen_formatter, sub_screen_path)


def write_bob(screen_formatter: GroupFormatter, path: Path):

Generate the Screen file

After calling format on the screen object, you will be left with a list of strings that represent each widget in your chosen file format. The final step is to create a screen file by unpacking the list and writing each widget to the file:

    # The root:'Display' is always the first element in texts
    texts = screen_formatter.format()
    ET = etree.fromstring(etree.tostring(texts[0]))
    for element in texts[:0:-1]:
        ET.insert(ET.index(ET.find("grid_step_y")) + 1, element)
    ET = ET.getroottree()
    ET.write(str(path), pretty_print=True)

And thats it. With this you can now create your own custom formatters. Below you can find a complete example formatter, supporting both edl and bob file formats for DLS:

@dataclass
class DLSFormatter(Formatter):
    spacing: Annotated[int, desc("Spacing between widgets")] = 5
    title_height: Annotated[int, desc("Height of screen title bar")] = 25
    max_height: Annotated[int, desc("Max height of the screen")] = 900
    label_width: Annotated[int, desc("Width of the widget description labels")] = 115
    widget_width: Annotated[int, desc("Width of the widgets")] = 120
    widget_height: Annotated[int, desc("Height of the widgets")] = 20

    def format(self, device: Device, prefix: str, path: Path):
        if path.suffix == ".edl":
            f = self.format_edl
        elif path.suffix == ".bob":
            f = self.format_bob
        else:
            raise ValueError("Can only write .edl or .bob files")
        f(device, prefix, path)

    def format_edl(self, device: Device, prefix: str, path: Path):
        template = EdlTemplate((Path(__file__).parent / "dls.edl").read_text())
        screen_layout = ScreenLayout(
            spacing=self.spacing,
            title_height=self.title_height,
            max_height=self.max_height,
            group_label_height=10,
            label_width=self.label_width,
            widget_width=self.widget_width,
            widget_height=self.widget_height,
            group_widget_indent=5,
            group_width_offset=0,
        )
        widget_formatter_factory = WidgetFormatterFactory(
            header_formatter_cls=LabelWidgetFormatter.from_template(
                template,
                search='"Heading"',
                property_map=dict(value="text"),
            ),
            label_formatter_cls=LabelWidgetFormatter.from_template(
                template,
                search='"Label"',
                property_map=dict(value="text"),
            ),
            led_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search='"LED"',
                sized=Bounds.square,
                property_map=dict(controlPv="pv"),
            ),
            progress_bar_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search='"ProgressBar"',
                property_map=dict(indicatorPv="pv"),
            ),
            text_read_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search='"TextRead"',
                property_map=dict(controlPv="pv"),
            ),
            check_box_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search='"ComboBox"',
                property_map=dict(controlPv="pv"),
            ),
            combo_box_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search='"ComboBox"',
                property_map=dict(controlPv="pv"),
            ),
            text_write_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search='"TextWrite"',
                property_map=dict(controlPv="pv"),
            ),
            # Cannot handle dynamic tables so insert a label with the PV name
            table_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search='"Label"',
                property_map=dict(value="pv"),
            ),
            action_formatter_cls=ActionWidgetFormatter.from_template(
                template,
                search='"SignalX"',
                property_map=dict(onLabel="label", offLabel="label", controlPv="pv"),
            ),
            sub_screen_formatter_cls=SubScreenWidgetFormatter.from_template(
                template,
                search='"SubScreenFile"',
                property_map=dict(displayFileName="file_name"),
            ),
        )
        screen_title_cls = LabelWidgetFormatter.from_template(
            template,
            search='"Title"',
            property_map=dict(value="text"),
        )
        group_title_cls = LabelWidgetFormatter.from_template(
            template,
            search='"  Group  "',
            property_map=dict(value="text"),
        )
        group_box_cls = WidgetFormatter.from_template(
            template, search="fillColor index 5"
        )

        def create_group_box_formatter(
            bounds: Bounds, title: str
        ) -> List[WidgetFormatter[str]]:
            x, y, w, h = bounds.x, bounds.y, bounds.w, bounds.h
            return [
                group_box_cls(
                    Bounds(
                        x,
                        y + screen_layout.spacing,
                        w,
                        h - screen_layout.spacing,
                    )
                ),
                group_title_cls(
                    Bounds(x, y, w, screen_layout.group_label_height),
                    f"  {title}  ",
                ),
            ]

        def create_screen_title_formatter(
            bounds: Bounds, title: str
        ) -> List[WidgetFormatter[str]]:
            return [
                screen_title_cls(
                    Bounds(0, 0, bounds.w, screen_layout.title_height), title
                )
            ]

        formatter_factory = ScreenFormatterFactory(
            screen_formatter_cls=GroupFormatter.from_template(
                template,
                search=GroupType.SCREEN,
                sized=with_title(screen_layout.spacing, screen_layout.title_height),
                widget_formatter_hook=create_screen_title_formatter,
            ),
            group_formatter_cls=GroupFormatter.from_template(
                template,
                search=GroupType.GROUP,
                sized=with_title(
                    screen_layout.spacing, screen_layout.group_label_height
                ),
                widget_formatter_hook=create_group_box_formatter,
            ),
            widget_formatter_factory=widget_formatter_factory,
            prefix=prefix,
            layout=screen_layout,
            base_file_name=path.stem,
        )
        title = f"{device.label} - {prefix}"

        screen_formatter, sub_screens = formatter_factory.create_screen_formatter(
            device.children, title
        )

        path.write_text("".join(screen_formatter.format()))
        for sub_screen_name, sub_screen_formatter in sub_screens:
            sub_screen_path = Path(path.parent / f"{sub_screen_name}{path.suffix}")
            sub_screen_path.write_text("".join(sub_screen_formatter.format()))

    def format_bob(self, device: Device, prefix: str, path: Path):
        template = BobTemplate(str(Path(__file__).parent / "dls.bob"))
        # LP DOCS REF: Define the layout properties
        screen_layout = ScreenLayout(
            spacing=self.spacing,
            title_height=self.title_height,
            max_height=self.max_height,
            group_label_height=26,
            label_width=self.label_width,
            widget_width=self.widget_width,
            widget_height=self.widget_height,
            group_widget_indent=18,
            group_width_offset=26,
        )
        # SW DOCS REF: Extract widget types from template file
        widget_formatter_factory = WidgetFormatterFactory(
            header_formatter_cls=LabelWidgetFormatter.from_template(
                template,
                search="Heading",
                property_map=dict(text="text"),
            ),
            label_formatter_cls=LabelWidgetFormatter.from_template(
                template,
                search="Label",
                property_map=dict(text="text"),
            ),
            led_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="LED",
                sized=Bounds.square,
                property_map=dict(pv_name="pv"),
            ),
            progress_bar_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="ProgressBar",
                property_map=dict(pv_name="pv"),
            ),
            text_read_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="TextUpdate",
                property_map=dict(pv_name="pv"),
            ),
            check_box_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="ChoiceButton",
                property_map=dict(pv_name="pv"),
            ),
            combo_box_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="ComboBox",
                property_map=dict(pv_name="pv"),
            ),
            text_write_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="TextEntry",
                property_map=dict(pv_name="pv"),
            ),
            table_formatter_cls=PVWidgetFormatter.from_template(
                template,
                search="Table",
                property_map=dict(pv_name="pv"),
            ),
            action_formatter_cls=ActionWidgetFormatter.from_template(
                template,
                search="ActionButton",
                property_map=dict(text="label", pv_name="pv", value="value"),
            ),
            sub_screen_formatter_cls=SubScreenWidgetFormatter.from_template(
                template,
                search="SubScreen",
                property_map=dict(file="file_name"),
            ),
        )
        # MAKE_WIDGETS DOCS REF: Define screen and group widgets
        screen_title_cls = LabelWidgetFormatter.from_template(
            template,
            search="Title",
            property_map=dict(text="text"),
        )
        group_title_cls = LabelWidgetFormatter.from_template(
            template,
            search="Group",
            property_map=dict(name="text"),
        )

        def create_group_object_formatter(
            bounds: Bounds, title: str
        ) -> List[WidgetFormatter[str]]:
            return [
                group_title_cls(
                    Bounds(bounds.x, bounds.y, bounds.w, bounds.h), f"{title}"
                )
            ]

        def create_screen_title_formatter(
            bounds: Bounds, title: str
        ) -> List[WidgetFormatter[str]]:
            return [
                screen_title_cls(
                    Bounds(0, 0, bounds.w, screen_layout.title_height), title
                )
            ]

        # SCREEN_INI DOCS REF: Construct a screen object
        formatter_factory = ScreenFormatterFactory(
            screen_formatter_cls=GroupFormatter.from_template(
                template,
                search=GroupType.SCREEN,
                sized=with_title(screen_layout.spacing, screen_layout.title_height),
                widget_formatter_hook=create_screen_title_formatter,
            ),
            group_formatter_cls=GroupFormatter.from_template(
                template,
                search=GroupType.GROUP,
                sized=with_title(
                    screen_layout.spacing, screen_layout.group_label_height
                ),
                widget_formatter_hook=create_group_object_formatter,
            ),
            widget_formatter_factory=widget_formatter_factory,
            prefix=prefix,
            layout=screen_layout,
            base_file_name=path.stem,
        )
        # SCREEN_FORMAT DOCS REF: Format the screen
        title = f"{device.label} - {prefix}"

        screen_formatter, sub_screens = formatter_factory.create_screen_formatter(
            device.children, title
        )

        write_bob(screen_formatter, path)
        for sub_screen_name, sub_screen_formatter in sub_screens:
            sub_screen_path = Path(path.parent / f"{sub_screen_name}{path.suffix}")
            write_bob(sub_screen_formatter, sub_screen_path)