Skip to content
Snippets Groups Projects
Commit c64b8814 authored by Kien Le's avatar Kien Le
Browse files

Make loadData uses factory pattern instead of instantiating data object directly

parent dba1cedd
No related branches found
No related tags found
1 merge request!30Make loadData uses factory pattern instead of instantiating data object directly
...@@ -12,7 +12,7 @@ from obspy.core import read as read_ms ...@@ -12,7 +12,7 @@ from obspy.core import read as read_ms
from obspy.io.reftek.core import Reftek130Exception from obspy.io.reftek.core import Reftek130Exception
from sohstationviewer.model.mseed.mseed import MSeed from sohstationviewer.model.mseed.mseed import MSeed
from sohstationviewer.model.reftek.reftek import RT130 from sohstationviewer.model.data_type_model import DataTypeModel
from sohstationviewer.database.extractData import signatureChannels from sohstationviewer.database.extractData import signatureChannels
from sohstationviewer.controller.util import validateFile, displayTrackingInfo from sohstationviewer.controller.util import validateFile, displayTrackingInfo
...@@ -34,21 +34,14 @@ def loadData(dataType, tracking_box, listOfDir, reqWFChans=[], reqSOHChans=[], ...@@ -34,21 +34,14 @@ def loadData(dataType, tracking_box, listOfDir, reqWFChans=[], reqSOHChans=[],
dataObject = None dataObject = None
for d in listOfDir: for d in listOfDir:
if dataObject is None: if dataObject is None:
if dataType == 'RT130': try:
dataObject = RT130( dataObject = DataTypeModel.create_data_object(
tracking_box, d, dataType, tracking_box, d,
reqWFChans=reqWFChans, reqSOHChans=reqSOHChans, reqWFChans=reqWFChans, reqSOHChans=reqSOHChans,
readStart=readStart, readEnd=readEnd) readStart=readStart, readEnd=readEnd)
else: except Exception as e:
try: msg = f"Dir {d} can't be read due to error: {str(e)}"
dataObject = MSeed( displayTrackingInfo(tracking_box, msg, "Warning")
tracking_box, d, reqWFChans=reqWFChans,
reqSOHChans=reqSOHChans,
readStart=readStart, readEnd=readEnd)
except Exception as e:
msg = f"Dir {d} can't be read due to error: {str(e)}"
displayTrackingInfo(tracking_box, msg, "Warning")
pass
# if dataObject.hasData(): # if dataObject.hasData():
# continue # continue
# If no data can be read from the first dir, throw exception # If no data can be read from the first dir, throw exception
......
...@@ -216,3 +216,21 @@ class DataTypeModel(): ...@@ -216,3 +216,21 @@ class DataTypeModel():
displayTrackingInfo(self.trackingBox, text, type) displayTrackingInfo(self.trackingBox, text, type)
if type != 'info': if type != 'info':
self.processingLog.append((text, type)) self.processingLog.append((text, type))
@classmethod
def create_data_object(cls, data_type, tracking_box, folder,
readChanOnly=False, reqWFChans=[], reqSOHChans=[],
readStart=0, readEnd=constants.HIGHEST_INT):
if data_type == 'RT130':
from sohstationviewer.model.reftek.reftek import RT130
dataObject = RT130(
tracking_box, folder, readChanOnly=readChanOnly,
reqWFChans=reqWFChans, reqSOHChans=reqSOHChans,
readStart=readStart, readEnd=readEnd)
else:
from sohstationviewer.model.mseed.mseed import MSeed
dataObject = MSeed(
tracking_box, folder, readChanOnly=readChanOnly,
reqWFChans=reqWFChans, reqSOHChans=reqSOHChans,
readStart=readStart, readEnd=readEnd)
return dataObject
...@@ -64,14 +64,14 @@ class TestLoadDataAndReadChannels(TestCase): ...@@ -64,14 +64,14 @@ class TestLoadDataAndReadChannels(TestCase):
MSeed MSeed
) )
def test_load_data_rt130_no_dir(self): def test_load_data_no_dir(self):
"""Test basic functionality of loadData - no directory was given.""" """Test basic functionality of loadData - no directory was given."""
no_dir_given = [] no_dir_given = []
self.assertIsNone(loadData('RT130', self.widget_stub, no_dir_given)) self.assertIsNone(loadData('RT130', self.widget_stub, no_dir_given))
self.assertIsNone( self.assertIsNone(
loadData(self.mseed_dtype, self.widget_stub, no_dir_given)) loadData(self.mseed_dtype, self.widget_stub, no_dir_given))
def test_load_data_rt130_dir_does_not_exist(self): def test_load_data_dir_does_not_exist(self):
""" """
Test basic functionality of loadData - the given directory does not Test basic functionality of loadData - the given directory does not
exist. exist.
...@@ -79,35 +79,35 @@ class TestLoadDataAndReadChannels(TestCase): ...@@ -79,35 +79,35 @@ class TestLoadDataAndReadChannels(TestCase):
empty_name_dir = [''] empty_name_dir = ['']
non_existent_dir = ['dir_that_does_not_exist'] non_existent_dir = ['dir_that_does_not_exist']
with self.assertRaises(Exception): self.assertIsNone(
loadData('RT130', self.widget_stub, empty_name_dir) loadData('RT130', self.widget_stub, empty_name_dir))
with self.assertRaises(Exception): self.assertIsNone(
loadData('RT130', self.widget_stub, non_existent_dir) loadData('RT130', self.widget_stub, non_existent_dir))
self.assertIsNone( self.assertIsNone(
loadData(self.mseed_dtype, self.widget_stub, empty_name_dir)) loadData(self.mseed_dtype, self.widget_stub, empty_name_dir))
self.assertIsNone( self.assertIsNone(
loadData(self.mseed_dtype, self.widget_stub, non_existent_dir)) loadData(self.mseed_dtype, self.widget_stub, non_existent_dir))
def test_load_data_rt130_empty_dir(self): def test_load_data_empty_dir(self):
""" """
Test basic functionality of loadData - the given directory is empty. Test basic functionality of loadData - the given directory is empty.
""" """
with TemporaryDirectory() as empty_dir: with TemporaryDirectory() as empty_dir:
with self.assertRaises(Exception): self.assertIsNone(
loadData('RT130', self.widget_stub, [empty_dir]) loadData('RT130', self.widget_stub, [empty_dir]))
self.assertIsNone( self.assertIsNone(
loadData(self.mseed_dtype, self.widget_stub, [empty_dir])) loadData(self.mseed_dtype, self.widget_stub, [empty_dir]))
def test_load_data_rt130_empty_data_dir(self): def test_load_data_empty_data_dir(self):
""" """
Test basic functionality of loadData - the given directory Test basic functionality of loadData - the given directory
contains a data folder but no data file. contains a data folder but no data file.
""" """
with TemporaryDirectory() as outer_dir: with TemporaryDirectory() as outer_dir:
with TemporaryDirectory(dir=outer_dir) as data_dir: with TemporaryDirectory(dir=outer_dir) as data_dir:
with self.assertRaises(Exception): self.assertIsNone(
loadData('RT130', self.widget_stub, [data_dir]) loadData('RT130', self.widget_stub, [data_dir]))
self.assertIsNone( self.assertIsNone(
loadData(self.mseed_dtype, self.widget_stub, [outer_dir])) loadData(self.mseed_dtype, self.widget_stub, [outer_dir]))
...@@ -116,8 +116,8 @@ class TestLoadDataAndReadChannels(TestCase): ...@@ -116,8 +116,8 @@ class TestLoadDataAndReadChannels(TestCase):
Test basic functionality of loadData - the data type given does not Test basic functionality of loadData - the data type given does not
match the type of the data contained in the given directory. match the type of the data contained in the given directory.
""" """
with self.assertRaises(Exception): self.assertIsNone(
loadData('RT130', self.widget_stub, [q330_dir]) loadData('RT130', self.widget_stub, [q330_dir]))
self.assertIsNone( self.assertIsNone(
loadData(self.mseed_dtype, self.widget_stub, [rt130_dir])) loadData(self.mseed_dtype, self.widget_stub, [rt130_dir]))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment