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
3 unresolved threads
1 file
+ 47
2
Compare changes
  • Side-by-side
  • Inline
+ 47
2
@@ -4,7 +4,10 @@
Lloyd Carothers
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 obspy.clients.nrl import NRL
@@ -19,10 +22,15 @@ GAIN = 'gain'
class NRLWizard(QtWidgets.QWizard):
def __init__(self, equipment=SENSOR, root=None):
super().__init__()
#Hack for now as NRL(root=None) crashes
# Hack for now as NRL(root=None) crashes
if not root:
self.nrl = NRL()
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)
self.nrl = NRL(root=root)
nrl_root = self.nrl.root
@@ -39,6 +47,43 @@ class NRLWizard(QtWidgets.QWizard):
self.q_and_as = []
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
: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):
# remove all pages ahead
current_id = self.currentId()
Loading