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

Allow user to restore database to default settings

parent fddb3daf
No related branches found
No related tags found
1 merge request!225Allow user to restore database to default settings
......@@ -53,4 +53,17 @@ python3.11:
- passoft
stage: Build Env and Test
script:
- python -m unittest
- python -m unittest
check-backup-db:
tags:
- passoft
stage: Build Env and Test
before_script:
- ''
script:
- |-
if ! cmp sohstationviewer/database/soh.db sohstationviewer/database/backup.db; then
echo "Backup database (sohstationviewer/database/backup.db) is different from working database (sohstationviewer/database/soh.db). Please copy the working database to the backup database."
exit 1
fi
\ No newline at end of file
include HISTORY.rst
include README.rst
include sohstationviewer/database/soh.db
include sohstationviewer/database/backup.db
graft sohstationviewer/documentation
graft sohstationviewer/images
......
......@@ -24,6 +24,7 @@ test:
source_files:
- tests
- sohstationviewer/database/soh.db
- sohstationviewer/database/backup.db
commands:
- python -m unittest
......
import os
from pathlib import Path
from typing import Literal
# The path to the package's root
ROOT_PATH = Path(os.path.abspath(__file__)).parent.parent
ROOT_PATH = Path(__file__).resolve().parent.parent
# The current version of SOHStationViewer
SOFTWARE_VERSION = '2024.2.1.0'
......
......@@ -13,6 +13,7 @@ Third letter (Orientation Code): ZNE123
dbConf = {
'dbpath': 'database/soh.db',
'backup_db_path': 'database/backup.db',
'seisRE': re.compile(f'[{WF_1ST}][{WF_2ND}][{WF_3RD}]'),
# key is last char of chan
'seisLabel': {'1': 'NS', '2': 'EW', 'N': 'NS', 'E': 'EW', 'Z': 'V'},
......
......@@ -181,6 +181,7 @@ def get_val(text: str) -> float:
and remove '<' and '=' characters
"""
text = text.replace('=', '').replace('<', '')
# Retrieve a number (with sign if available) from a piece of text.
re_val = '^\+?\-?[0-9]+\.?[0-9]?' # noqa: W605
return float(re.search(re_val, text).group())
......
File added
from .edit_value_color_dialog import * # noqa: F403, F401
from .line_dot_dialog import * # noqa: F403, F401
from .multi_color_dot_dialog import * # noqa: F403, F401
from .tri_color_lines_dialog import * # noqa: F403, F401
from .up_down_dialog import * # noqa: F403, F401
from .dot_for_time_dialog import * # noqa: F403, F401
from .edit_value_color_dialog import ( # noqa: F401
display_color, EditValueColorDialog
)
from .line_dot_dialog import LineDotDialog # noqa: F401
from .multi_color_dot_dialog import ( # noqa: F401
MultiColorDotLowerEqualDialog, MultiColorDotUpperEqualDialog
)
from .tri_color_lines_dialog import TriColorLinesDialog # noqa: F401
from .up_down_dialog import UpDownDialog # noqa: F401
from .dot_for_time_dialog import DotForTimeDialog # noqa: F401
import pathlib
import shutil
import sqlite3
import traceback
from datetime import datetime, date
from typing import List, Tuple, Union, Dict
......@@ -11,6 +12,7 @@ from PySide6.QtGui import QFont, QPalette, QColor
from PySide6.QtWidgets import QFrame, QListWidgetItem, QMessageBox
from sohstationviewer.conf import constants
from sohstationviewer.conf.dbSettings import dbConf
from sohstationviewer.model.data_loader import DataLoader
from sohstationviewer.model.general_data.general_data import \
GeneralData
......@@ -1271,6 +1273,49 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
self.gps_dialog.show()
self.gps_dialog.raise_()
@QtCore.Slot()
def restore_default_database(self):
"""
Restore the working database to its original state by replacing its
content with the content of the backup database. If something goes
wrong when restoring the database, keep the working database in its
current state and notify the user.
"""
reset_prompt = ('Do you want to reset the database to its default '
'state?')
is_do_reset = QMessageBox.question(self, 'Restore Default Database',
reset_prompt)
if is_do_reset:
# We want a backup of the working database to restore to if
# something goes wrong when restoring from the backup database.
current_main_db_backup = sqlite3.connect(':memory:')
main_db = sqlite3.connect(dbConf['dbpath'])
backup_db = sqlite3.connect(dbConf['backup_db_path'])
main_db.backup(current_main_db_backup)
# Copy the content of the backup database into the working
# database. Using SQLite's built-in backup to make sure that there
# is no problem with the backup file.
backup_db.backup(main_db)
# Do one final check to make sure that the copy happened
# successfully.
# https://stackoverflow.com/questions/37195819/comparing-2-sqlite-databases # noqa
# The method used for comparing the content of two sqlite databases
# comes from the link above.
main_db_dump = list(main_db.iterdump())
backup_db_dump = list(backup_db.iterdump())
if main_db_dump != backup_db_dump:
current_main_db_backup.backup(main_db)
error_msg = ('Something went wrong while resetting the '
'working database. Please try again.')
QMessageBox.warning(self, 'Resetting database failed',
error_msg)
current_main_db_backup.close()
main_db.close()
backup_db.close()
@QtCore.Slot()
def set_plots_color(self, checked: bool, color_mode: ColorMode) -> None:
"""
......
......@@ -217,6 +217,12 @@ class UIMainWindow(object):
"""
self.gps_plotter_action: Union[QAction, None] = None
"""
restore_default_database: menu item to reset the database back to its
original condition
"""
self.restore_default_database_action: Union[QAction, None] = None
# ========================== Option Menu =======================
"""
warn_big_file_sizes: option to check file sizes and give warning if
......@@ -657,6 +663,11 @@ class UIMainWindow(object):
'GPS Plotter', main_window)
menu.addAction(self.gps_plotter_action)
self.restore_default_database_action = QAction(
'Reset Database to Default', main_window
)
menu.addAction(self.restore_default_database_action)
def create_option_menu(self, main_window, menu):
"""
Create Options Menu's Actions which help setting options for plotting.
......@@ -773,6 +784,9 @@ class UIMainWindow(object):
# Commands
self.gps_plotter_action.triggered.connect(main_window.open_gps_plotter)
self.restore_default_database_action.triggered.connect(
main_window.restore_default_database
)
# Options
self.mp_regular_color_action.triggered.connect(
......
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