From 14b77728d07d959102779be5dbfacad645a9b946 Mon Sep 17 00:00:00 2001 From: Jan Garrevoet Date: Thu, 8 Jul 2021 13:10:01 +0200 Subject: [PATCH 1/9] add support for running without an X-session --- src/sardana/spock/ipython_01_00/genutils.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/sardana/spock/ipython_01_00/genutils.py b/src/sardana/spock/ipython_01_00/genutils.py index 6de631c8e..c11e2e787 100644 --- a/src/sardana/spock/ipython_01_00/genutils.py +++ b/src/sardana/spock/ipython_01_00/genutils.py @@ -116,10 +116,20 @@ def get_gui_mode(): try: import taurus.external.qt.Qt - return 'qt' + ret_val = 'qt' except ImportError: return None + # Check for running without an X-session + # No display environment + if os.environ.get("DISPLAY") is None: + ret_val = None + + # In docker without X-session auth + elif not os.path.exists("/tmp/.X11-unix"): + ret_val = None + + return ret_val def get_pylab_mode(): return get_app().pylab From 99965f00e2a29a04f83356a0fc909c7d5d22bcbf Mon Sep 17 00:00:00 2001 From: Jan Garrevoet Date: Thu, 8 Jul 2021 20:38:21 +0200 Subject: [PATCH 2/9] make Xsession checking only on linux --- src/sardana/spock/ipython_01_00/genutils.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/sardana/spock/ipython_01_00/genutils.py b/src/sardana/spock/ipython_01_00/genutils.py index c11e2e787..2383c3a1a 100644 --- a/src/sardana/spock/ipython_01_00/genutils.py +++ b/src/sardana/spock/ipython_01_00/genutils.py @@ -120,14 +120,15 @@ def get_gui_mode(): except ImportError: return None - # Check for running without an X-session - # No display environment - if os.environ.get("DISPLAY") is None: - ret_val = None - - # In docker without X-session auth - elif not os.path.exists("/tmp/.X11-unix"): - ret_val = None + # Check for running without an X-session on linux + if sys.platform.startswith("linux"): + # No display environment + if os.environ.get("DISPLAY") is None: + ret_val = None + + # In docker without X-session auth + elif not os.path.exists("/tmp/.X11-unix"): + ret_val = None return ret_val From d5d370d83bb304158e55d611b72cb46d0fed1e3a Mon Sep 17 00:00:00 2001 From: Jan Garrevoet Date: Thu, 8 Jul 2021 22:11:28 +0200 Subject: [PATCH 3/9] return proper feedback when expconf and other UIs are started without an X-session --- src/sardana/spock/ipython_01_00/genutils.py | 11 ++--- src/sardana/spock/magic.py | 23 +++++++++ src/sardana/util/graphics.py | 52 +++++++++++++++++++++ 3 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 src/sardana/util/graphics.py diff --git a/src/sardana/spock/ipython_01_00/genutils.py b/src/sardana/spock/ipython_01_00/genutils.py index 2383c3a1a..01962f766 100644 --- a/src/sardana/spock/ipython_01_00/genutils.py +++ b/src/sardana/spock/ipython_01_00/genutils.py @@ -76,6 +76,7 @@ from IPython.utils.path import get_ipython_dir +from sardana.util.graphics import xsession_available import taurus #from taurus.core import Release as TCRelease @@ -121,14 +122,8 @@ def get_gui_mode(): return None # Check for running without an X-session on linux - if sys.platform.startswith("linux"): - # No display environment - if os.environ.get("DISPLAY") is None: - ret_val = None - - # In docker without X-session auth - elif not os.path.exists("/tmp/.X11-unix"): - ret_val = None + if not xsession_available(): + ret_val = None return ret_val diff --git a/src/sardana/spock/magic.py b/src/sardana/spock/magic.py index f5578052e..a6ffcf74c 100644 --- a/src/sardana/spock/magic.py +++ b/src/sardana/spock/magic.py @@ -31,12 +31,14 @@ 'post_mortem', 'macrodata', 'edmac', 'spock_late_startup_hook', 'spock_pre_prompt_hook'] +from sardana.util.graphics import xsession_available from sardana.util.whichpython import which_python_executable from .genutils import MSG_DONE, MSG_FAILED from .genutils import get_ipapi from .genutils import page, get_door, get_macro_server, ask_yes_no, arg_split + def expconf(self, parameter_s=''): """Launches a GUI for configuring the environment variables for the experiments (scans)""" @@ -49,6 +51,13 @@ def expconf(self, parameter_s=''): "https://sardana-controls.org/users/standard_macro_catalog.html#experiment-configuration-macros)") return + try: + if not xsession_available(): + raise RuntimeError + except RuntimeError: + print("No X-session available. ExpConf cannot work without it.") + return + try: from sardana.taurus.qt.qtgui.extra_sardana import ExpDescriptionEditor except: @@ -97,6 +106,13 @@ def showscan(self, parameter_s=''): print("Qt binding is not available. Showscan cannot work without it.") return + try: + if not xsession_available(): + raise RuntimeError + except RuntimeError: + print("No X-session available. Showscan cannot work without it.") + return + params = parameter_s.split() door = get_door() scan_nb = None @@ -118,6 +134,13 @@ def spsplot(self, parameter_s=''): print("Qt binding is not available. SPSplot cannot work without it.") return + try: + if not xsession_available(): + raise RuntimeError + except RuntimeError: + print("No X-session available. SPSplot cannot work without it.") + return + get_door().plot() diff --git a/src/sardana/util/graphics.py b/src/sardana/util/graphics.py new file mode 100644 index 000000000..2fa86302f --- /dev/null +++ b/src/sardana/util/graphics.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +############################################################################## +## +# This file is part of Sardana +## +# http://www.sardana-controls.org/ +## +# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain +## +# Sardana is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +## +# Sardana is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +## +# You should have received a copy of the GNU Lesser General Public License +# along with Sardana. If not, see . +## +############################################################################## + +"""This package provides graphics related helper functions""" + +import os +import sys + + +def xsession_available(): + """ + Checks if an X-session is available. + + Returns: + bool: True when an X-session is available. False when not. + """ + + ret_val = True + + if sys.platform.startswith("linux"): + # No display environment + if os.environ.get("DISPLAY") is None: + ret_val = False + + # In docker without X-session auth + elif not os.path.exists("/tmp/.X11-unix"): + ret_val = False + + return ret_val From a7669e7d52bd390a7720e50de9bfda0dd4423fb6 Mon Sep 17 00:00:00 2001 From: Jan Garrevoet Date: Thu, 8 Jul 2021 22:15:23 +0200 Subject: [PATCH 4/9] switch to rest docstring --- src/sardana/util/graphics.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sardana/util/graphics.py b/src/sardana/util/graphics.py index 2fa86302f..6f2fed245 100644 --- a/src/sardana/util/graphics.py +++ b/src/sardana/util/graphics.py @@ -34,8 +34,7 @@ def xsession_available(): """ Checks if an X-session is available. - Returns: - bool: True when an X-session is available. False when not. + :returns: True when an X-session is available. False when not. """ ret_val = True From d97a8c4d47addadbf1d6c37c9bc412b68d22f65d Mon Sep 17 00:00:00 2001 From: Jan Garrevoet Date: Fri, 9 Jul 2021 14:17:52 +0200 Subject: [PATCH 5/9] refactor error message; docs --- src/sardana/spock/ipython_01_00/genutils.py | 4 +-- src/sardana/spock/magic.py | 23 +++++++++----- src/sardana/util/graphics.py | 35 ++++++++++++++++----- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/sardana/spock/ipython_01_00/genutils.py b/src/sardana/spock/ipython_01_00/genutils.py index 01962f766..489c88786 100644 --- a/src/sardana/spock/ipython_01_00/genutils.py +++ b/src/sardana/spock/ipython_01_00/genutils.py @@ -76,7 +76,7 @@ from IPython.utils.path import get_ipython_dir -from sardana.util.graphics import xsession_available +from sardana.util.graphics import display_available import taurus #from taurus.core import Release as TCRelease @@ -122,7 +122,7 @@ def get_gui_mode(): return None # Check for running without an X-session on linux - if not xsession_available(): + if not display_available(): ret_val = None return ret_val diff --git a/src/sardana/spock/magic.py b/src/sardana/spock/magic.py index a6ffcf74c..b8a27c7de 100644 --- a/src/sardana/spock/magic.py +++ b/src/sardana/spock/magic.py @@ -31,7 +31,7 @@ 'post_mortem', 'macrodata', 'edmac', 'spock_late_startup_hook', 'spock_pre_prompt_hook'] -from sardana.util.graphics import xsession_available +from sardana.util.graphics import display_available from sardana.util.whichpython import which_python_executable from .genutils import MSG_DONE, MSG_FAILED from .genutils import get_ipapi @@ -52,10 +52,13 @@ def expconf(self, parameter_s=''): return try: - if not xsession_available(): + if not display_available(): raise RuntimeError except RuntimeError: - print("No X-session available. ExpConf cannot work without it.") + print( + "Running without graphical user interface support." + " ExpConf cannot work without it." + ) return try: @@ -107,10 +110,13 @@ def showscan(self, parameter_s=''): return try: - if not xsession_available(): + if not display_available(): raise RuntimeError except RuntimeError: - print("No X-session available. Showscan cannot work without it.") + print( + "Running without graphical user interface support." + " Showscan cannot work without it." + ) return params = parameter_s.split() @@ -135,10 +141,13 @@ def spsplot(self, parameter_s=''): return try: - if not xsession_available(): + if not display_available(): raise RuntimeError except RuntimeError: - print("No X-session available. SPSplot cannot work without it.") + print( + "Running without graphical user interface support." + " SPSplot cannot work without it." + ) return get_door().plot() diff --git a/src/sardana/util/graphics.py b/src/sardana/util/graphics.py index 6f2fed245..da9d9a455 100644 --- a/src/sardana/util/graphics.py +++ b/src/sardana/util/graphics.py @@ -30,22 +30,43 @@ import sys +def display_available(): + """ + Checks if a graphical display is available. + + :returns: True when a display is available. False when not. + :rtype: bool + + .. note :: + This is only used for linux since it can run without graphical + sessions. + For Windows this always returns True since it can not run without. + """ + + ret_val = True + + if sys.platform.startswith("linux"): + ret_val = xsession_available() + + return ret_val + + def xsession_available(): """ Checks if an X-session is available. :returns: True when an X-session is available. False when not. + :rtype: bool """ ret_val = True - if sys.platform.startswith("linux"): - # No display environment - if os.environ.get("DISPLAY") is None: - ret_val = False + # No display environment + if os.environ.get("DISPLAY") is None: + ret_val = False - # In docker without X-session auth - elif not os.path.exists("/tmp/.X11-unix"): - ret_val = False + # In docker without X-session auth + elif not os.path.exists("/tmp/.X11-unix"): + ret_val = False return ret_val From 5abb6cfba9d0b0eefdbce2b7d8ea44c988e65af6 Mon Sep 17 00:00:00 2001 From: zreszela Date: Tue, 13 Jul 2021 16:12:45 +0200 Subject: [PATCH 6/9] print warning when no display available during macro plotting --- src/sardana/spock/spockms.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/sardana/spock/spockms.py b/src/sardana/spock/spockms.py index fe2f7aa65..9d9ef575d 100644 --- a/src/sardana/spock/spockms.py +++ b/src/sardana/spock/spockms.py @@ -38,6 +38,7 @@ from sardana.sardanautils import is_pure_str, is_non_str_seq from sardana.spock import genutils from sardana.util.parser import ParamParser +from sardana.util.graphics import display_available from sardana import sardanacustomsettings CHANGE_EVTS = TaurusEventType.Change, TaurusEventType.Periodic @@ -519,6 +520,11 @@ def processRecordData(self, data): except ImportError: print("Qt binding is not available. Macro plotting cannot work without it.") return + if not display_available(): + print("Running without graphical user interface support." + " Macro plotting cannot work without it." + ) + return func_name = self.MathFrontend + "." + func_name args = data['args'] kwargs = data['kwargs'] From 190e21d507c26235a63e61e691877eaea117f067 Mon Sep 17 00:00:00 2001 From: zreszela Date: Tue, 13 Jul 2021 21:08:21 +0200 Subject: [PATCH 7/9] Avoid raising and catching RuntimeError --- src/sardana/spock/magic.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/sardana/spock/magic.py b/src/sardana/spock/magic.py index b8a27c7de..570047da1 100644 --- a/src/sardana/spock/magic.py +++ b/src/sardana/spock/magic.py @@ -51,10 +51,7 @@ def expconf(self, parameter_s=''): "https://sardana-controls.org/users/standard_macro_catalog.html#experiment-configuration-macros)") return - try: - if not display_available(): - raise RuntimeError - except RuntimeError: + if not display_available(): print( "Running without graphical user interface support." " ExpConf cannot work without it." @@ -109,10 +106,7 @@ def showscan(self, parameter_s=''): print("Qt binding is not available. Showscan cannot work without it.") return - try: - if not display_available(): - raise RuntimeError - except RuntimeError: + if not display_available(): print( "Running without graphical user interface support." " Showscan cannot work without it." @@ -140,10 +134,7 @@ def spsplot(self, parameter_s=''): print("Qt binding is not available. SPSplot cannot work without it.") return - try: - if not display_available(): - raise RuntimeError - except RuntimeError: + if not display_available(): print( "Running without graphical user interface support." " SPSplot cannot work without it." From b4ae1bc5deb7695f995923921eedbb827bd1713c Mon Sep 17 00:00:00 2001 From: zreszela Date: Tue, 13 Jul 2021 21:10:58 +0200 Subject: [PATCH 8/9] Add hint on expconf macros --- src/sardana/spock/magic.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/sardana/spock/magic.py b/src/sardana/spock/magic.py index 570047da1..b92724485 100644 --- a/src/sardana/spock/magic.py +++ b/src/sardana/spock/magic.py @@ -53,8 +53,10 @@ def expconf(self, parameter_s=''): if not display_available(): print( - "Running without graphical user interface support." - " ExpConf cannot work without it." + "Running without graphical user interface support. " + "ExpConf cannot work without it. " + "(hint: maybe you want to use experiment configuration macros? " + "https://sardana-controls.org/users/standard_macro_catalog.html#experiment-configuration-macros)" ) return From a66ad34a4805dd5c9d182b9b8fdecb94c9866e33 Mon Sep 17 00:00:00 2001 From: zreszela Date: Tue, 13 Jul 2021 21:15:43 +0200 Subject: [PATCH 9/9] check if display available for Qt input handler --- src/sardana/spock/ipython_01_00/genutils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sardana/spock/ipython_01_00/genutils.py b/src/sardana/spock/ipython_01_00/genutils.py index 489c88786..79a948a04 100644 --- a/src/sardana/spock/ipython_01_00/genutils.py +++ b/src/sardana/spock/ipython_01_00/genutils.py @@ -1305,6 +1305,11 @@ def prepare_input_handler(): except ImportError: raise Exception("Cannot use Spock Qt input handler!") + if not display_available(): + raise Exception( + "Running without graphical user interface support." + " Cannot use Spock Qt input handler!" + ) def prepare_cmdline(argv=None): if argv is None: