concert_workspacecreator.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import sys
  2. import logging
  3. import PyTango
  4. class Workspace(object):
  5. """A workspace."""
  6. def __init__(self, device, workspace_id):
  7. self._device = device
  8. self._id = workspace_id
  9. self._path = device.GetWorkspacePath(workspace_id)
  10. logging.getLogger().info("Create {}".format(self))
  11. def __enter__(self):
  12. return self
  13. def __exit__(self, *exc):
  14. self.close()
  15. def __repr__(self):
  16. return '<Workspace:id={} path={}>'.format(self.id, self._path)
  17. @property
  18. def id(self):
  19. """Unique id of this workspace."""
  20. return self._id
  21. @property
  22. def path(self):
  23. """Path associated with the workspace"""
  24. return self._path
  25. def close(self):
  26. """Close this workspace."""
  27. self._device.CloseWorkspace(self.id)
  28. logging.getLogger().info("Closed {}".format(self))
  29. class WorkspaceCreator(object):
  30. """Workspace creation facility."""
  31. def __init__(self, device_name):
  32. """
  33. Args:
  34. device_name (str): TANGO device name
  35. """
  36. try:
  37. self._device = PyTango.DeviceProxy(device_name)
  38. except PyTango.DevFailed as e:
  39. raise IOError("PyTango: {}".format(e[0].desc))
  40. def create(self, name):
  41. """Create a new workspace.
  42. Args:
  43. name (str): Name of the workspace or dataset
  44. Returns:
  45. Workspace: A new workspace object
  46. """
  47. workspace_id = self._device.CreateWorkspace(name)
  48. return Workspace(self._device, workspace_id)