import sys import logging import PyTango class Workspace(object): """A workspace.""" def __init__(self, device, workspace_id): self._device = device self._id = workspace_id self._path = device.GetWorkspacePath(workspace_id) logging.getLogger().info("Create {}".format(self)) def __enter__(self): return self def __exit__(self, *exc): self.close() def __repr__(self): return ''.format(self.id, self._path) @property def id(self): """Unique id of this workspace.""" return self._id @property def path(self): """Path associated with the workspace""" return self._path def close(self): """Close this workspace.""" self._device.CloseWorkspace(self.id) logging.getLogger().info("Closed {}".format(self)) class WorkspaceCreator(object): """Workspace creation facility.""" def __init__(self, device_name): """ Args: device_name (str): TANGO device name """ try: self._device = PyTango.DeviceProxy(device_name) except PyTango.DevFailed as e: raise IOError("PyTango: {}".format(e[0].desc)) def create(self, name): """Create a new workspace. Args: name (str): Name of the workspace or dataset Returns: Workspace: A new workspace object """ workspace_id = self._device.CreateWorkspace(name) return Workspace(self._device, workspace_id)