Builds and File Generation

There are a large number of built and generated files in this module. These are built largely using the python code in the pcdswidgets.builder submodule. Developers usually don’t need to dig into the details: the important thing is to call make and everything will be good.

This page is a primer on the details you don’t usually need to know and the general motivation behind these tools.

What are we Building?

From source .ui files edited in designer, we are building python files that define widgets that can be imported and used at runtime.

Each file in the pcdswidgets/ui filetree is used as a source for the build.

The following files are built or edited when we make:

  • pcdswidgets/generated/**/$widget_name_form.py (if ui changed)

    • These are direct code representations of the .ui file contents

    • This uses pyuic5 under the hood.

  • pcdswidgets/generated/**/$widget_name_base.py (if ui changed)

    • This adds some macro-handling features based on the pydm macros found in the .ui file.

    • It also adds some nice type-hinting of widgets defined in the .ui file.

  • pcdswidgets/**/$widget_name.py (if missing)

    • This is the actual widget object that will be run.

    • Developers are encouraged to edit these.

  • pcdswidgets/**/__init__.py (if missing)

    • This ensures that edited submodules are treated as real Python modules.

  • pyproject.toml (if available widgets changed)

    • This makes sure each widget is included in designer.

  • pcdswidgets/generated/path_defs.py (if pyproject.toml changed)

    • This makes sure each widget is included in pcdswidgets-show.

Why are we Building?

  • A lot of these steps could be done at runtime at some performance cost. Building them now saves that performance cost.

  • The widget type hints are very useful for adding logic to screens.

  • Editing pyproject.toml by hand is fraught and error-prone.

Why are we Comitting These? What about .gitignore?

This library is intended to be installed into a python environment. There is no built-in support for extra actions to be taken during the installation of a python package. You can add a build step to the distribution but the syntax is janky and annoying.

Much simpler is to commit all the generated files and let them always be included in the distribution. This also should be an easy way for us to realize when we’ve made a mistake in editing the build code.

API Docs

Create the uic, base, and main .py files from the input .ui files.

pcdswidgets.builder.build.build_base_widget(designer_ui: str, output_dir: str = '')[source]

Create a .py file with a suitable base widget for inclusion in designer.

The files are named systematically with patterns like: some_name.ui -> some_name_base.py

The base widget will have the following properties: - Imports from the uic output for its widget layouts - Has appropriately-named properties for any expected macros in the pydm source file - Has type hints to help the IDE keep track of which child widgets are available

See ui_base_widget.j2, which is the jinja template for these output files.

pcdswidgets.builder.build.build_main_widget(designer_ui: str, output_dir: str = '')[source]

Create a .py file that will be included in designer as-is.

The files are named systematically with patterns like: some_name.ui -> some_name.py

See ui_main_widget.j2, which is the jinja template for these output files.

pcdswidgets.builder.build.build_uic(designer_ui: str, output_dir: str = '')[source]

Use the standard uic parser to create a .py file with a .ui file’s widget layouts.

The files are named systematically with patterns like: some_name.ui -> some_name_form.py

class pcdswidgets.builder.designer_options.DesignerOptions(group: str, is_container: bool, icon: IconOptions | str | None = None)[source]

Options for designer.

Parameters:
  • group (str) – Widgets with the same group will be grouped together in designer.

  • is_container (bool) – Set this to True if this widget can contain other widgets (via drag and drop), or False otherwise.

  • icon (IconOptions, str, or None, optional) – The icon to use to represent this widget in the qt sidebar. If an IconOptions enum, we’ll use PyDM’s iconfont to pick the matching icon. If a str, we’ll load the image file from the icons folder that matches this name. If None (the default), we’ll use the default icon, or we’ll look for a user-defined get_designer_icon staticmethod that returns a QIcon.

Helper for using designer to layout widgets.

class pcdswidgets.builder.designer_widget.DesignerWidget(*args, **kwargs)[source]

Helper class for converting pydm displays for embedding to standalone widgets.

after_set_macro(macro_name: str, value: str)[source]

Hook for additional actions to take when a macro is set.

This exists so you can extend this behavior without overriding set_macro, which could otherwise make it easy to break the widget.

So, you override after_set_macro instead and define what you’d like to change after specific values are updated.

Note that this runs after all built-in effects of set_macro.

classmethod get_designer_icon() → QIcon | None[source]

Return the correct QIcon for PyDM’s designer hooks.

If we return None, PyDM will use the default icon.

get_macro(macro_name: str) → str[source]

Returns the current value of a macro that is applied to our subwidgets.

retranslateUi(*args, **kwargs)[source]

Required function for setupUi to work in __init__

set_macro(macro_name: str, value: str)[source]

Updates a macro to a new value and propagates to all subwidget properties appropriately.

update_relative_paths()[source]

Special handling for PyDMRelatedDisplay and PyDMEmbeddedDisplay.

Relative filepaths are nearly non-functional when used in pcdswidgets, because they are relative to the filepath of the ui file (Display) they are placed in, not relative to the package data here.

To opt in to a path relative to the package data here, the user should express it in the following form:

pcdswidgets/tests/builder/test.ui

When we see a relative filepath that starts with “pcdswidgets”, we will update it to be the absolute path to that resource to avoid these sorts of issues. This allows us to make widgets that refer to each other or to ui file resources shipped with the module.

For example, if pcdswidgets is installed at /some/path/python/lib/site-packages/pcdswidgets and we pick a relative path pcdswidgets/tests/builder/test.ui then we will replace it with the absolute path /some/path/python/lib/site-packages/pcdswidgets/tests/builder/test.ui

class pcdswidgets.builder.designer_widget.MacroEditExtension(widget: DesignerWidget)[source]

Adds helpful macro editing options in designer on double or right click.

See the BasicSettingsExtension from PyDM

actions() → list[QAction][source]

PyDM checks this to decide which actions to prepent in designer. The first action is mapped to double-click.

class pcdswidgets.builder.designer_widget.MacroValueEditor(widget: DesignerWidget, parent: QWidget | None)[source]

Dialog for MacroEditExtension

See the BasicSettingsEditor from PyDM

pcdswidgets.builder.designer_widget.fix_pcdswidgets_filename(filename: str) → str[source]

If a relative filename starts with “pcdswidgets”, make it absolute, attaching it to the pcdswidgets install root.

Otherwise, return the original filename.

Helper module for creating the [project.entry-points.”pydm.widget”] section in pyproject.toml

python -m pcdswidgets.builder.entrypoint_finder

pcdswidgets.builder.entrypoint_finder.iter_all_widgets() → Iterator[tuple[str, type[QWidget]]][source]

Recursively yield all widgets to export from pcdswidgets.

Yields:

name, widget (str, QWidget)

pcdswidgets.builder.entrypoint_finder.iter_submodules(package: str = 'pcdswidgets') → Iterator[ModuleType][source]

Recursively yield all submodules of a package.

Generate a fresh version of icon_options.py

This helps us figure out what options exist for designer icons as provided by pydm.

pcdswidgets.builder.get_icon_options.generate_icon_options()[source]

Generate icon_options.py, which contains a large enum with icon options.

pcdswidgets.builder.get_icon_options.get_icon_options() → list[str][source]

Returns the names of all the icons present in pydm’s iconfont with valid rendering.

pcdswidgets.builder.get_icon_options.show_icon_options()[source]

Show a simple qt window with a grid of valid rendered icons alongside their names.

This is the full set of usable icons from pydm’s iconfont.

Define the IconOptions enum, which helps us keep track of which string icon names are valid.

This is a file generated from icon_options.j2

class pcdswidgets.builder.icon_options.IconOptions(*values)[source]
pcdswidgets.builder.inits.build_inits(base_dir: Path)[source]

Creates blank __init__.py files wherever they are needed in generated directories.

This makes Python treat these directories as Python modules.

Parameters:

base_dir (Path) – The directory path that contains generated python files in a nested filetree.

pcdswidgets.builder.inits.get_generated_top_dirs() → list[Path][source]

Returns the top-level directories that were originally generated.

pcdswidgets.builder.inits.main()[source]

Backfill generated directories with __init__.py to make them python modules.

Define functions for reading ui files an extracting information from them.

class pcdswidgets.builder.read_ui.InfoForJinja(macro_set: set[str], all_widget_set: set[str], macro_widget_set: set[str], macro_to_widget: dict[str, list[str]], widget_to_macro: dict[str, list[str]], widget_to_pre_templ_strs: dict[str, list[tuple[str, str]]], widget_to_pre_templ_lists: dict[str, list[tuple[str, list[str]]]])[source]

Distilled widget and macro information for easily filling in the jinja template.

class pcdswidgets.builder.read_ui.UiInfo(widget_name_to_class: dict[str, str], widget_macros: dict[str, dict[str, str | list[str]]], form_cls: str)[source]

Information parsed from a .ui file.

pcdswidgets.builder.read_ui.add_prop_to_widget_macros(widget_macros: defaultdict[str, dict[str, str | list[str]]], name: str, prop: Element)[source]

Incorporate a single property into the macros dict if there is a macro in it.

pcdswidgets.builder.read_ui.get_ui_info(designer_ui: str) → UiInfo[source]

Parse a .ui file and collect information about each widget.

pcdswidgets.builder.read_ui.process_widget_macros(ui_info: UiInfo) → InfoForJinja[source]

Convert the raw ui info into a more useful form for filling the jinja template.

Updates the generated/path_defs.py file used by pcdswidgets-show.

The file contains information about the screen and widget options that would otherwise need to be inspected more slowly at runtime. It is generated as a python file instead of e.g. json so it can be imported from bytecode at runtime instead of parsed from text data.

The widgets are sourced directly from pyproject.toml. The screens are sourced by scraping the “screens” folder.

pcdswidgets.builder.screen_paths.get_screen_paths() → dict[str, str][source]

Return a dict mapping from screen name to relative path.

pcdswidgets.builder.screen_paths.main()[source]

Entrypoint for pcdswidgets.builder.screen_paths.

pcdswidgets.builder.screen_paths.write_path_defs(screen_paths: dict[str, str], widget_imports: dict[str, str])[source]

Write out the generated/path_defs.py file given the input mappings.