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
+ 48
2
Compare changes
  • Side-by-side
  • Inline
+ 48
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)(NRL.__init__)
self.nrl = NRL(root=root)
    • Developer

      I don't know if you have seen this error, but I'll put it here so that we have a record. On this line (line 34), the program fails with an error of

      File contains no section headers.
      
      file: '<???>', line: 1
      
      '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n'
      • Author Maintainer

        Haven't seen this. I will do more testing to see what's going on.

      • Author Maintainer

        Did you use the extracted NRL V2 archive? I haven't implemented the entire functionality in this MR. It needs to download the archive first and extract it and then pass the Path of the folder as root to NRL() method. The default is also online NRL v1 which needs to be fixed.

      • Please register or sign in to reply
Please register or sign in to reply
nrl_root = self.nrl.root
@@ -39,6 +47,44 @@ 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 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):
# remove all pages ahead
current_id = self.currentId()
Loading