From 492f5b96f2700dbdb7221abab0e2a13ebcecdb25 Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 14 Jan 2020 13:55:30 +0100 Subject: [PATCH] [refactor] moved functions requiring QT import from util.utils to util.gui --- pylot/core/loc/nll.py | 3 +- pylot/core/util/gui.py | 70 ++++++++++++++++++++++++++++++++++++++++ pylot/core/util/utils.py | 68 +------------------------------------- 3 files changed, 73 insertions(+), 68 deletions(-) create mode 100644 pylot/core/util/gui.py diff --git a/pylot/core/loc/nll.py b/pylot/core/loc/nll.py index 679186d1..4a9f8b27 100644 --- a/pylot/core/loc/nll.py +++ b/pylot/core/loc/nll.py @@ -7,7 +7,8 @@ import subprocess from obspy import read_events from pylot.core.io.phases import writephases -from pylot.core.util.utils import getPatternLine, runProgram, which +from pylot.core.util.utils import getPatternLine, runProgram +from pylot.core.util.gui import which from pylot.core.util.version import get_git_version as _getVersionString __version__ = _getVersionString() diff --git a/pylot/core/util/gui.py b/pylot/core/util/gui.py new file mode 100644 index 00000000..5aa90ebd --- /dev/null +++ b/pylot/core/util/gui.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os +from PySide import QtCore + + +def pick_linestyle_pg(picktype, key): + """ + Get Qt line style by picktype and pick parameter (earliest/latest possible pick, symmetric picking error or + most probable pick) + :param picktype: 'manual' or 'automatic' + :type picktype: str + :param key: which pick parameter should be plotted, 'mpp', 'epp', 'lpp' or 'spe' + :type key: str + :return: Qt line style parameters + :rtype: + """ + linestyles_manu = {'mpp': (QtCore.Qt.SolidLine, 2.), + 'epp': (QtCore.Qt.DashLine, 1.), + 'lpp': (QtCore.Qt.DashLine, 1.), + 'spe': (QtCore.Qt.DashLine, 1.)} + linestyles_auto = {'mpp': (QtCore.Qt.DotLine, 2.), + 'epp': (QtCore.Qt.DashDotLine, 1.), + 'lpp': (QtCore.Qt.DashDotLine, 1.), + 'spe': (QtCore.Qt.DashDotLine, 1.)} + linestyles = {'manual': linestyles_manu, + 'auto': linestyles_auto} + return linestyles[picktype][key] + + +def which(program, parameter): + """ + takes a program name and returns the full path to the executable or None + modified after: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python + :param program: name of the desired external program + :type program: str + :return: full path of the executable file + :rtype: str + """ + try: + from PySide.QtCore import QSettings + settings = QSettings() + for key in settings.allKeys(): + if 'binPath' in key: + os.environ['PATH'] += ':{0}'.format(settings.value(key)) + nllocpath = ":" + parameter.get('nllocbin') + os.environ['PATH'] += nllocpath + except Exception as e: + print(e.message) + + def is_exe(fpath): + return os.path.exists(fpath) and os.access(fpath, os.X_OK) + + def ext_candidates(fpath): + yield fpath + for ext in os.environ.get("PATHEXT", "").split(os.pathsep): + yield fpath + ext + + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + exe_file = os.path.join(path, program) + for candidate in ext_candidates(exe_file): + if is_exe(candidate): + return candidate + + return None \ No newline at end of file diff --git a/pylot/core/util/utils.py b/pylot/core/util/utils.py index 0499e31e..96685a74 100644 --- a/pylot/core/util/utils.py +++ b/pylot/core/util/utils.py @@ -9,13 +9,13 @@ import pyqtgraph as pg import re import subprocess import warnings -from PySide import QtCore from obspy import UTCDateTime, read from obspy.core import AttribDict from obspy.signal.rotate import rotate2zne from scipy.interpolate import splrep, splev from pylot.core.io.inputs import PylotParameter, FilterOptions +from pylot.core.util.gui import pick_linestyle_pg from pylot.core.util.obspyDMT_interface import check_obspydmt_eventfolder from pylot.styles import style_settings @@ -727,30 +727,6 @@ def pick_linestyle_plt(picktype, key): return linestyles[picktype][key] -def pick_linestyle_pg(picktype, key): - """ - Get Qt line style by picktype and pick parameter (earliest/latest possible pick, symmetric picking error or - most probable pick) - :param picktype: 'manual' or 'automatic' - :type picktype: str - :param key: which pick parameter should be plotted, 'mpp', 'epp', 'lpp' or 'spe' - :type key: str - :return: Qt line style parameters - :rtype: - """ - linestyles_manu = {'mpp': (QtCore.Qt.SolidLine, 2.), - 'epp': (QtCore.Qt.DashLine, 1.), - 'lpp': (QtCore.Qt.DashLine, 1.), - 'spe': (QtCore.Qt.DashLine, 1.)} - linestyles_auto = {'mpp': (QtCore.Qt.DotLine, 2.), - 'epp': (QtCore.Qt.DashDotLine, 1.), - 'lpp': (QtCore.Qt.DashDotLine, 1.), - 'spe': (QtCore.Qt.DashDotLine, 1.)} - linestyles = {'manual': linestyles_manu, - 'auto': linestyles_auto} - return linestyles[picktype][key] - - def modify_rgba(rgba, modifier, intensity): """ Modify rgba color by adding the given intensity to the modifier color @@ -1093,48 +1069,6 @@ def runProgram(cmd, parameter=None): subprocess.check_output('{} | tee /dev/stderr'.format(cmd), shell=True) -def which(program, parameter): - """ - takes a program name and returns the full path to the executable or None - modified after: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python - :param program: name of the desired external program - :type program: str - :return: full path of the executable file - :rtype: str - """ - try: - from PySide.QtCore import QSettings - settings = QSettings() - for key in settings.allKeys(): - if 'binPath' in key: - os.environ['PATH'] += ':{0}'.format(settings.value(key)) - nllocpath = ":" + parameter.get('nllocbin') - os.environ['PATH'] += nllocpath - except Exception as e: - print(e.message) - - def is_exe(fpath): - return os.path.exists(fpath) and os.access(fpath, os.X_OK) - - def ext_candidates(fpath): - yield fpath - for ext in os.environ.get("PATHEXT", "").split(os.pathsep): - yield fpath + ext - - fpath, fname = os.path.split(program) - if fpath: - if is_exe(program): - return program - else: - for path in os.environ["PATH"].split(os.pathsep): - exe_file = os.path.join(path, program) - for candidate in ext_candidates(exe_file): - if is_exe(candidate): - return candidate - - return None - - def loopIdentifyPhase(phase): """ Loop through phase string and try to recognize its type (P or S wave).