Utility Functions¶
Utility functions for typhos
-
class
typhos.utils.
DeviceConnectionMonitorThread
(device, include_lazy=False, **kwargs)[source]¶ Monitor connection status in a background thread
- Parameters
device (ophyd.Device) – The device to grab signals from
include_lazy (bool, optional) – Include lazy signals as well
-
connection_update
¶ Connection update signal with signature:
(signal, connected, metadata_dict)
- Type
QtCore.Signal
-
class
typhos.utils.
GrabKindItem
(attr, component, signal)¶ -
attr
¶ Alias for field number 0
-
component
¶ Alias for field number 1
-
signal
¶ Alias for field number 2
-
-
class
typhos.utils.
ObjectConnectionMonitorThread
(objects=None, **kwargs)[source]¶ Monitor connection status in a background thread
-
connection_update
¶ Connection update signal with signature:
(signal, connected, metadata_dict)
- Type
QtCore.Signal
-
-
class
typhos.utils.
ThreadPoolWorker
(func, *args, **kwargs)[source]¶ Worker thread helper
- Parameters
func (callable) – The function to call during
run()
*args – Arguments for the function call
**kwargs – Keyword rarguments for the function call
-
class
typhos.utils.
TyphosBase
(*args, **kwargs)[source]¶ Base widget for all Typhos widgets that interface with devices
-
class
typhos.utils.
TyphosLoading
(timeout_message, *, parent=None, **kwargs)[source]¶ A QLabel with an animation for loading status.
-
LOADING_TIMEOUT_MS
¶ The timeout value in milliseconds for when to stop the animation and replace it with a default timeout message.
- Type
-
-
typhos.utils.
channel_from_signal
(signal, read=True)[source]¶ Create a PyDM address from arbitrary signal type
-
typhos.utils.
clean_attr
(attr)[source]¶ Create a nicer, human readable alias from a Python attribute name
-
typhos.utils.
clean_name
(device, strip_parent=True)[source]¶ Create a human readable name for a device
- Parameters
device (ophyd.Device) –
strip_parent (bool or Device) – Remove the parent name of the device from name. If strip_parent is True, the name of the direct parent of the device is stripped. If a device is provided the name of that device is used. This allows specification for removal at any point of the device schema
-
typhos.utils.
code_from_device
(device)[source]¶ Generate code required to load
device
in another process
-
typhos.utils.
code_from_device_repr
(device)[source]¶ Return code to create a device from its
repr
information.- Parameters
device (ophyd.Device) –
-
typhos.utils.
connection_status_monitor
(*signals, callback)[source]¶ [Context manager] Monitor connection status from a number of signals
Filters out any other metadata updates, only calling once connected/disconnected
- Parameters
*signals (ophyd.OphydObj) – Signals to monitor
callback (callable) – Callback to run, with same signature as that of
ophyd.OphydObj.subscribe()
.obj
andconnected
are guaranteed kwargs.
-
typhos.utils.
dump_grid_layout
(layout, rows=None, cols=None, *, cell_width=60)[source]¶ Dump the layout of a
QtWidgets.QGridLayout
tofile
.
-
typhos.utils.
find_file_in_paths
(filename, *, paths=None)[source]¶ Search for filename
filename
in the list of pathspaths
- Parameters
filename (str or pathlib.Path) – The filename
paths (list or iterable, optional) – List of paths to search. Defaults to DISPLAY_PATHS.
- Yields
All filenames that match in the given paths
-
typhos.utils.
find_parent_with_class
(widget, cls=<class 'PyQt5.QtWidgets.QWidget'>)[source]¶ Finds the first parent of a widget that is an instance of
klass
- Parameters
widget (QWidget) – The widget from which to start the search
cls (type, optional) – The class which the parent must be an instance of
-
typhos.utils.
find_templates_for_class
(cls, view_type, paths, *, extensions=None, include_mro=True)[source]¶ Given a class cls and a view type (such as ‘detailed’), search paths for potential templates to show.
- Parameters
cls (class) – Search for templates with this class name
view_type ({'detailed', 'engineering', 'embedded'}) – The view type
paths (iterable) – Iterable of paths to be expanded, de-duplicated, and searched
extensions (str or list, optional) – The template filename extension (default is
'.ui'
or'.py'
)include_mro (bool, optional) – Include superclasses - those in the MRO - of
cls
as well
- Yields
path (pathlib.Path) – A matching path, ordered from most-to-least specific.
-
typhos.utils.
get_all_signals_from_device
(device, include_lazy=False, filter_by=None)[source]¶ Get all signals in a given device
- Parameters
device (ophyd.Device) – ophyd Device to monitor
include_lazy (bool, optional) – Include lazy signals as well
filter_by (callable, optional) – Filter signals, with signature
callable(ophyd.Device.ComponentWalk)
-
typhos.utils.
get_component
(obj)[source]¶ Get the component that made the given object.
- Parameters
obj (ophyd.OphydItem) – The ophyd item for which to get the component.
- Returns
component – The component, if available.
- Return type
ophyd.Component
-
typhos.utils.
get_device_from_fake_class
(cls)[source]¶ Return the non-fake class, given a fake class
That is:
fake_cls = ophyd.sim.make_fake_device(cls) get_device_from_fake_class(fake_cls) # -> cls
- Parameters
cls (type) – The fake class
-
typhos.utils.
get_variety_metadata
(cpt)[source]¶ Get “variety” metadata from a component or signal.
- Parameters
cpt (ophyd.Component or ophyd.OphydItem) – The component / ophyd item to get the metadata for.
- Returns
metadata – The metadata, if set. Otherwise an empty dictionary. This metadata is guaranteed to be valid according to the known schemas.
- Return type
-
typhos.utils.
is_fake_device_class
(cls)[source]¶ Is
cls
a fake device fromophyd.sim.make_fake_device()
?
-
typhos.utils.
is_signal_ro
(signal)[source]¶ Return whether the signal is read-only, based on its class.
In the future this may be easier to do through improvements to introspection in the ophyd library. Until that day we need to check classes
-
typhos.utils.
is_standard_template
(template)[source]¶ Is the template a core one provided with typhos?
- Parameters
template (str or pathlib.Path) –
-
typhos.utils.
link_signal_to_widget
(signal, widget)[source]¶ Registers the signal with PyDM, and sets the widget channel.
- Parameters
signal (ophyd.OphydObj) – The signal to use.
widget (QtWidgets.QWidget) – The widget with which to connect the signal.
-
typhos.utils.
linked_attribute
(property_attr, widget_attr, hide_unavailable=False)[source]¶ Decorator which connects a device signal with a widget.
Retrieves the signal from the device, registers it with PyDM, and sets the widget channel.
- Parameters
property_attr (str) – This is one level of indirection, allowing for the component attribute to be configurable by way of designable properties. In short, this looks like:
getattr(self.device, getattr(self, property_attr))
The component attribute name may include multiple levels (e.g.,'cpt1.cpt2.low_limit'
).widget_attr (str) – The attribute name of the widget, referenced from
self
. The component attribute name may include multiple levels (e.g.,'ui.low_limit'
).hide_unavailable (bool) – Whether or not to hide widgets for which the device signal is not available
-
typhos.utils.
load_suite
(path, cfg=None)[source]¶ ” Load a file saved via Typhos
- Parameters
- Returns
suite
- Return type
-
typhos.utils.
no_device_lazy_load
()[source]¶ Context manager which disables the ophyd.device.Device lazy_wait_for_connection behavior and later restore its value.
-
typhos.utils.
patch_connect_slots
()[source]¶ Patches QtCore.QMetaObject.connectSlotsByName to catch SystemErrors.
-
typhos.utils.
pyqt_class_from_enum
(enum)[source]¶ Create an inheritable base class from a Python Enum, which can also be used for Q_ENUMS.
-
typhos.utils.
raise_window
(widget)[source]¶ Bring a widget’s window into focus and on top of the window stack.
If the window is minimized, unminimize it.
Different window managers respond differently to the various methods called here, the chosen sequence was intended for good behavior on as many systems as possible.
-
typhos.utils.
reload_widget_stylesheet
(widget, cascade=False)[source]¶ Reload the stylesheet of the provided widget
-
typhos.utils.
remove_duplicate_items
(list_)[source]¶ Return a de-duplicated list/tuple of items in list_, retaining order
-
typhos.utils.
save_suite
(suite, file_or_buffer)[source]¶ Create a file capable of relaunching the TyphosSuite
- Parameters
suite (TyphosSuite) –
file_or_buffer (str or file-like) – Either a path to the file or a handle that supports
write
-
typhos.utils.
subscription_context
(*objects, callback, event_type=None, run=True)[source]¶ [Context manager] Subscribe to a specific event from all objects
Unsubscribes all signals before exiting
- Parameters
-
typhos.utils.
subscription_context_device
(device, callback, event_type=None, run=True, *, include_lazy=False, filter_by=None)[source]¶ [Context manager] Subscribe to
event_type
from signals indevice
Unsubscribes all signals before exiting
- Parameters
device (ophyd.Device) – ophyd Device to monitor
callback (callable) – Callback to run, with same signature as that of
ophyd.OphydObj.subscribe()
event_type (str, optional) – The event type to subscribe to
run (bool, optional) – Run the previously cached subscription immediately
include_lazy (bool, optional) – Include lazy signals as well
filter_by (callable, optional) – Filter signals, with signature
callable(ophyd.Device.ComponentWalk)
Cache Utilities¶
Path caching¶
-
class
typhos.cache.
_GlobalDisplayPathCache
[source]¶ A cache for all configured display paths.
- All paths from utils.DISPLAY_PATHS will be included:
Environment variable
PYDM_DISPLAYS_PATH
.Typhos package built-in paths.
-
add_path
(path)[source]¶ Add a path to be searched during
glob
.- Parameters
path (pathlib.Path or str) – The path to add.
Ophyd Object Description Caching¶
-
class
typhos.cache.
_GlobalDescribeCache
[source]¶ Cache of ophyd object descriptions.
obj.describe()
is called in a thread from the global QThreadPool, and new results are marked by the Signalnew_description
.To access a description, call
get()
. If available, it will be returned immediately. Otherwise, wait for thenew_description
Signal.-
connect_thread
¶ The thread which monitors connection status.
- Type
ObjectConnectionMonitorThread
-
Ophyd Object to Widget Type Cache¶
-
class
typhos.cache.
_GlobalWidgetTypeCache
[source]¶ Cache of ophyd object Typhos widget types.
obj.describe()
is called using_GlobalDescribeCache
and are therefore threaded and run in the background. New results are marked by the Signalwidgets_determined
.To access a set of widget types, call
get()
. If available, it will be returned immediately. Otherwise, wait for thewidgets_determined
Signal.-
describe_cache
¶ The describe cache, used for determining widget types.
- Type
-
cache
¶ The cache holding widget type information. Keyed on
obj
, the values areSignalWidgetInfo
tuples.- Type
-
get
(obj)[source]¶ To access widget types, call this method. If available, it will be returned immediately. Otherwise, upon connection and successful
describe()
call, thewidgets_determined
Signal will be emitted.- Parameters
obj (
ophyd.OphydObj
) – The object to get the widget types.- Returns
desc – If available in the cache, the information will be returned.
- Return type
SignalWidgetInfo
or None
-