Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
336bdad
PathBoxWidget: set workdir when setting path
jnsebgosselin May 11, 2023
861403d
SardesPlugin: added a __post_init__ abstract method to base class
jnsebgosselin May 11, 2023
5549c66
SardesPlugin: added a __post_init__ abstract method to base class
jnsebgosselin May 23, 2023
06ab692
Merge branch 'master' into add_import_hg_survey
jnsebgosselin May 23, 2023
f67b4b7
Merge branch 'add_import_hg_survey' of https://github.com/geo-stack/s…
jnsebgosselin May 29, 2023
040487b
Merge branch 'master' into add_import_hg_survey
jnsebgosselin May 29, 2023
adf1c9b
Create HGSurveyImportDialog
jnsebgosselin May 29, 2023
cc24602
Complete missing docstrings
jnsebgosselin May 29, 2023
70096ea
Add get_default_contents_margins qthelpers function
jnsebgosselin May 29, 2023
3d6e8db
SardesTableModelsManager: add get_table_model and complete typehint
jnsebgosselin May 29, 2023
eab56e3
Add HGSurveyImportManager
jnsebgosselin May 29, 2023
e6cd4e4
HGSurveyImportDialog: add start/stop importing methods
jnsebgosselin May 29, 2023
354a90a
HGSurveyImportDialog: add sig_continue_import
jnsebgosselin May 29, 2023
2fdc454
Minor correction of a desc in the conceptual model
jnsebgosselin May 30, 2023
27c18bf
Add read_hgsurvey_data base func
jnsebgosselin May 30, 2023
b2f5309
Add format_hg_survey_imported_data base func
jnsebgosselin May 30, 2023
42d9ac0
Install the HGSurveyImportManager in the Hydrogeochemistry plugin
jnsebgosselin May 30, 2023
9f8ee01
PathBoxWidget: emit signal when path change
jnsebgosselin May 30, 2023
996e261
Minor workaround to avoid GUI glitch
jnsebgosselin May 30, 2023
a46b6ec
Prevent the dialog from closing when import is in progress
jnsebgosselin May 30, 2023
7b67be1
Add ImportHGSurveysError class
jnsebgosselin Jun 6, 2023
ab59092
Raise errors in format_hg_survey_imported_data
jnsebgosselin Jun 6, 2023
1ed73cd
Add a dialog to show import errors
jnsebgosselin Jun 6, 2023
750a575
Add format_purge_imported_data base function
jnsebgosselin Jun 6, 2023
dd440c8
Add format_params_data_imported_data base function
jnsebgosselin Jun 6, 2023
d55d555
Codestyle changes
jnsebgosselin Jun 6, 2023
fa2dac0
Correct a small code error
jnsebgosselin Jun 6, 2023
14dd1a7
DatabaseConnectionWorker: add _add_hg_survey_data method
jnsebgosselin Jun 6, 2023
6ea2840
Add new 'import_geochemistry' icon
jnsebgosselin Jun 6, 2023
4e27fe7
HGSurveyImportManager: Implement logic in _import_hg_surveys
jnsebgosselin Jun 6, 2023
c239cad
HGSurveyImportManager: add _handle_import_hg_surveys_results method
jnsebgosselin Jun 6, 2023
8f85b80
Improve import error message
jnsebgosselin Jun 8, 2023
3db73e5
Improve layout
jnsebgosselin Jun 8, 2023
4e76b42
Get rid of conftest file in tables plugin tests
jnsebgosselin Jun 9, 2023
4157ecf
read_hgsurvey_data: make sure values are str
jnsebgosselin Jun 9, 2023
bedeeff
read_hgsurvey_data: break at first invalid purge line
jnsebgosselin Jun 9, 2023
060f3db
Add test_read_hgsurvey_data
jnsebgosselin Jun 9, 2023
3b13e90
Add test_format_hgsurvey_imported_data
jnsebgosselin Jun 13, 2023
aff4e38
format_purge_imported_data: test prev_seq_end < next_seq_start
jnsebgosselin Jun 13, 2023
f44a41d
Fix errors in format_purge_imported_data
jnsebgosselin Jun 13, 2023
b183b3d
Fir purge_sequence_no in format_purge_imported_data
jnsebgosselin Jun 13, 2023
397a05a
Get and check pump_type_id in format_purge_imported_data
jnsebgosselin Jun 13, 2023
ad3a998
Add test_format_purge_imported_data
jnsebgosselin Jun 13, 2023
a120ca6
Rework error codes
jnsebgosselin Jun 13, 2023
b489cb4
Check that param_name is not empty in format_params_data_imported_data
jnsebgosselin Jun 13, 2023
fd82391
rework regex in hg_params test data
jnsebgosselin Jun 14, 2023
4376666
Fix some errors in format_params_data_imported_data
jnsebgosselin Jun 14, 2023
3ee6eea
Add test_format_params_data_imported_data
jnsebgosselin Jun 14, 2023
6903eb6
Complete missing docstrings
jnsebgosselin Jun 14, 2023
fa4341f
format_params_data_imported_data: fix return indentation
jnsebgosselin Jun 14, 2023
5f91e8a
Extend test_format_params_data_imported_data for 2 params
jnsebgosselin Jun 14, 2023
93c92e9
Add a TODO comment
jnsebgosselin Jun 14, 2023
8967b9b
DatabaseConnectionWorker: complete imported_survey_data
jnsebgosselin Jun 15, 2023
915bcc4
DatabaseConnectionManager: implement add_hg_survey_data
jnsebgosselin Jun 15, 2023
031dbca
SardesTableModelsManager: fix get_table_model
jnsebgosselin Jun 15, 2023
97aa5da
Rename and add new test file
jnsebgosselin Jun 15, 2023
fe94e6d
Update test_hgsurveys.py
jnsebgosselin Jun 15, 2023
40e2a89
Send message when import is successful
jnsebgosselin Jun 15, 2023
fed2d45
Add sucess dialog in HGSurveyImportManager
jnsebgosselin Jun 15, 2023
9e79512
Change where success message is formatted
jnsebgosselin Jun 15, 2023
27fd897
Remove a bad print
jnsebgosselin Jun 15, 2023
74d5d90
Rework success message
jnsebgosselin Jun 15, 2023
c194866
Force show the import dialog
jnsebgosselin Jun 15, 2023
1182d92
Remove success dialog and use progress widget instead
jnsebgosselin Jun 15, 2023
4ff74b8
Add tests for the HG plugin
jnsebgosselin Jun 15, 2023
6560009
show fail icon when import fails
jnsebgosselin Jun 16, 2023
1889c03
Extend test_import_hg_survey_error
jnsebgosselin Jun 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sardes/api/database_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def _transaction_wrapper(self, func, mode, *args, **kwargs):
self.begin_transaction()
try:
results = func(self, *args, **kwargs)
# TODO: I am not sure we should auto commit transaction for every
# read. What if we want to do a read in-between two write within
# the same transaction?
if mode == 'read' or kwargs.get('auto_commit', True):
self.commit_transaction()
finally:
Expand Down
3 changes: 2 additions & 1 deletion sardes/api/database_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ def __delitem__(self, key):
name='hg_param_name',
dtype='str',
desc=("The name of the hydrogeochemical parameter."
"Used when importing results from a lab report."),
"Used when exporting results to a water "
"quality report."),
),
Column(
name='hg_param_regex',
Expand Down
6 changes: 6 additions & 0 deletions sardes/api/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,11 @@ def __init__(self, parent=None):
# to be displayed in Sardes main window for this plugin.
self.dockwindow = None

# Plugin setup.
self.setup_plugin()
self.pane_widget = self.create_pane_widget()
self.mainwindow_toolbars = self.create_mainwindow_toolbars()
self.__post_init__()

def main_option_actions(self):
return []
Expand Down Expand Up @@ -499,6 +501,10 @@ def get_plugin_icon(cls):
"""
return get_icon('master')

def __post_init__(self):
"""A method that is called at the end of SardesPlugin constructor."""
pass

def setup_plugin(self):
pass

Expand Down
3 changes: 3 additions & 0 deletions sardes/config/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
'import_clipboard': [
('mdi.clipboard-plus-outline',),
{'color': ICON_COLOR, 'scale_factor': 1.2}],
'import_geochemistry': [
('mdi.flask-plus-outline',),
{'color': ICON_COLOR, 'scale_factor': 1.2}],
'information': [
('mdi.information-outline',),
{'color': ICON_COLOR, 'scale_factor': 1.3}],
Expand Down
7 changes: 7 additions & 0 deletions sardes/database/accessors/accessor_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
from sardes.config.locale import _


class ImportHGSurveysError(Exception):
def __init__(self, message: str, code: int):
super().__init__(message)
self.message = message
self.code = code


class SardesVersionError(Exception):
"""
Exception that should be raised when the version of Sardes is outdated
Expand Down
8 changes: 4 additions & 4 deletions sardes/database/accessors/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ def pump_types():
@pytest.fixture
def hg_params():
df = pd.DataFrame(
data=[['PARAM#1', 'Name param #1', 'PARAM#1 Regex', '111-0-11'],
['PARAM#2', 'Name param #2', 'PARAM#2 Regex', '22-0-23'],
['PARAM#3', 'Name param #3', 'PARAM#3 Regex', '34-5-12'],
['PARAM#4', 'Name param #4', 'PARAM#4 Regex', '73-2-9']],
data=[['PARAM#1', 'Name param #1', 'PARAM(.*)?1', '111-0-11'],
['PARAM#2', 'Name param #2', 'PARAM(.*)?2', '22-0-23'],
['PARAM#3', 'Name param #3', 'PARAM(.*)?3', '34-5-12'],
['PARAM#4', 'Name param #4', 'PARAM(.*)?4', '73-2-9']],
index=[1, 2, 3, 4],
columns=['hg_param_code', 'hg_param_name', 'hg_param_regex',
'cas_registry_number']
Expand Down
105 changes: 103 additions & 2 deletions sardes/database/database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from sardes.config.ospath import get_documents_logo_filename
from sardes.config.main import CONF
from sardes.database.accessors.accessor_helpers import create_empty_readings
from sardes.database.accessors.accessor_errors import ImportHGSurveysError
from sardes.tools.hydrographs import HydrographCanvas
from sardes.tools.save2excel import _save_reading_data_to_xlsx
from sardes.tools.waterquality import _save_hg_data_to_xlsx
Expand Down Expand Up @@ -456,7 +457,7 @@ def _get_sonde_installation_info(self, sonde_serial_no, date_time):

return sonde_install,

# ---- Publish Network Data
# ---- Complex operations
def _publish_to_kml(self, kml_filename, iri_data=None, iri_logs=None,
iri_graphs=None, iri_quality=None):
"""
Expand Down Expand Up @@ -709,6 +710,93 @@ def _publish_to_kml(self, kml_filename, iri_data=None, iri_logs=None,
kml.save(kml_filename)
return True,

def _add_hg_survey_data(self, imported_survey_data: dict(dict)):
"""
Add HG survey data imported from a XLSX file.
"""
from sardes.plugins.hydrogeochemistry.hgsurveys import (
format_hg_survey_imported_data,
format_purge_imported_data,
format_params_data_imported_data
)

hg_surveys_data, = self._get('hg_surveys')
stations_data, = self._get('observation_wells_data')
hg_sampling_methods_data, = self._get('hg_sampling_methods')
pump_types_data, = self._get('pump_types')
hg_params_data, = self._get('hg_params')
measurement_units_data, = self._get('measurement_units')

new_hg_surveys = []
new_purges = []
new_hg_param_vals = []

for sheet_name, sheet_data in imported_survey_data.items():
try:
new_hg_surveys.append(
format_hg_survey_imported_data(
sheet_name,
sheet_data['hg_surveys_data'],
hg_surveys_data,
stations_data,
hg_sampling_methods_data
))
new_purges.extend(
format_purge_imported_data(
sheet_name,
sheet_data['purges_data'],
pump_types_data
))
new_hg_param_vals.extend(
format_params_data_imported_data(
sheet_name,
sheet_data['hg_param_values_data'],
hg_params_data,
measurement_units_data
))
except ImportHGSurveysError as e:
return e,

# Clear the cache.
for name in ['hg_surveys', 'purges', 'hg_param_values']:
if name in self._cache:
del self._cache[name]

# We first add the new HG surveys to the database.
indexes = self.db_accessor.add(
'hg_surveys', new_hg_surveys, auto_commit=False)

# Substitute the right HG survey id for the purges and HG param values.
sheet_names = list(imported_survey_data.keys())
indexes_map = {
sheet_name: hg_survey_id for
sheet_name, hg_survey_id in
zip(sheet_names, indexes)
}

for new_purge in new_purges:
new_purge['hg_survey_id'] = indexes_map[
new_purge['hg_survey_id']]
for new_hg_param_val in new_hg_param_vals:
new_hg_param_val['hg_survey_id'] = indexes_map[
new_hg_param_val['hg_survey_id']]

# Add the new purges and HG parameter values to the database.
self.db_accessor.add(
'purges', new_purges, auto_commit=False)
self.db_accessor.add(
'hg_param_values', new_hg_param_vals, auto_commit=True)

if len(imported_survey_data) == 1:
message = _(
"1 new survey has been imported into the database."
)
else:
message = _(
"{} new surveys have been imported into the database."
).format(len(imported_survey_data))
return message,


class DatabaseConnectionManager(TaskManagerBase):
sig_database_connected = Signal(object, object)
Expand Down Expand Up @@ -1026,7 +1114,7 @@ def _handle_run_tasks_finished(self):
list(self._tseries_data_changed))
self._tseries_data_changed = set()

# ---- Publish Network Data
# ---- Other
def publish_to_kml(self, filename, iri_data=None, iri_logs=None,
iri_graphs=None, iri_quality=None, callback=None,
postpone_exec=False):
Expand All @@ -1038,6 +1126,19 @@ def publish_to_kml(self, filename, iri_data=None, iri_logs=None,
if not postpone_exec:
self.run_tasks()

def add_hg_survey_data(self, imported_survey_data: dict(dict),
callback: Callable = None,
postpone_exec: bool = False):
"""
Add HG survey data imported from a XLSX file.
"""
self._data_changed.add('hg_surveys')
self._data_changed.add('purges')
self._data_changed.add('hg_param_values')
self.add_task('add_hg_survey_data', callback, imported_survey_data)
if not postpone_exec:
self.run_tasks()


if __name__ == '__main__':
from sardes.database.accessors import DatabaseAccessorSardesLite
Expand Down
19 changes: 19 additions & 0 deletions sardes/plugins/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,24 @@
# -----------------------------------------------------------------------------


# ---- Standard imports
import os
import os.path as osp
os.environ['SARDES_PYTEST'] = 'True'

# ---- Third party imports
import pytest

# ---- Local imports
from sardes.database.accessors import DatabaseAccessorSardesLite
from sardes.database.accessors.tests.conftest import *


@pytest.fixture
def dbaccessor(tmp_path, database_filler):
dbaccessor = DatabaseAccessorSardesLite(
osp.join(tmp_path, 'sqlite_database_test.db'))
dbaccessor.init_database()
database_filler(dbaccessor)

return dbaccessor
Loading