Skip to content
Snippets Groups Projects

Fix the mismatch of the folder structure between NRL v1 and v2

Merged Omid Hosseini requested to merge feature-fix_obspy_nrl2 into feature-add_offline_NRL2
1 file
+ 48
2
Compare changes
  • Side-by-side
  • Inline
+ 48
2
@@ -4,7 +4,10 @@
@@ -4,7 +4,10 @@
Lloyd Carothers
Lloyd Carothers
Qt Wizard for selecting responses from the IRIS NRL
Qt Wizard for selecting responses from the IRIS NRL
'''
'''
import inspect
 
import ast
 
import inspect
 
from textwrap import dedent
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2 import QtCore, QtGui, QtWidgets
from obspy.clients.nrl import NRL
from obspy.clients.nrl import NRL
@@ -19,10 +22,15 @@ GAIN = 'gain'
@@ -19,10 +22,15 @@ GAIN = 'gain'
class NRLWizard(QtWidgets.QWizard):
class NRLWizard(QtWidgets.QWizard):
def __init__(self, equipment=SENSOR, root=None):
def __init__(self, equipment=SENSOR, root=None):
super().__init__()
super().__init__()
#Hack for now as NRL(root=None) crashes
# Hack for now as NRL(root=None) crashes
if not root:
if not root:
self.nrl = NRL()
self.nrl = NRL()
else:
else:
 
# This will change the structure of the NRL class to be
 
# compatible with NRL v2.
 
# The NRL v1 will fail if used as a root path
 
fixes = {"sensors": "sensor", "dataloggers": "datalogger"}
 
NRL.__init__ = self.replace_str(fixes)(NRL.__init__)
self.nrl = NRL(root=root)
self.nrl = NRL(root=root)
nrl_root = self.nrl.root
nrl_root = self.nrl.root
@@ -39,6 +47,44 @@ class NRLWizard(QtWidgets.QWizard):
@@ -39,6 +47,44 @@ class NRLWizard(QtWidgets.QWizard):
self.q_and_as = []
self.q_and_as = []
self.addPage(NRLPage(self, self.top))
self.addPage(NRLPage(self, self.top))
 
def replace_str(self, correction: dict = None):
 
"""
 
This function will change the string literal inside a function. This
 
will be used to correct hard-coded string literal inside the __init__
 
function of the NRL class.
 
:param correction: A dictionary that contains target and replacement
 
strings
 
:type correction: dict
 
:return: The function object
 
"""
 
def wrapper(func):
 
tree = ast.parse(dedent(inspect.getsource(func)))
 
for key, val in correction.items():
 
tree = self.__fix_str(key, val, tree)
 
ast.fix_missing_locations(tree)
 
scope = {}
 
exec(compile(tree, inspect.getfile(func), "exec"), func.__globals__
 
, scope)
 
return scope[func.__name__]
 
return wrapper
 
 
def __fix_str(self, target, replacement, tree):
 
"""
 
Replace the target string inside the AST tree with the replacement
 
string.
 
:param target: String to be replaced inside the AST tree
 
:type target: String
 
:param replacement: New replaced string inside the AST tree
 
:type replacement: String
 
:param tree: The AST tree
 
:type tree: AST
 
:return: The modified AST tree
 
"""
 
for node in ast.walk(tree):
 
if isinstance(node, ast.Str) and node.s == target:
 
node.s = replacement
 
return tree
 
def fresh_page(self):
def fresh_page(self):
# remove all pages ahead
# remove all pages ahead
current_id = self.currentId()
current_id = self.currentId()
Loading