Ensemble: Add Export/Import method with json files.

This commit is contained in:
parouby committed 2026-09-25 11:46:37 +02:00
1 parent 88345c58a7
commit 63592b91a9
7 files changed
+156 -5

No files matched your search

+12 -3
View File
@@ -253,12 +253,21 @@ class Ensemble(SQLSubModel):
self._target_data = target_data self._target_data = target_data
self._status.modified() self._status.modified()
def get_data_dict(self): def get_data_dict(self, use_ids=False):
target_data = self.target_data
distribution = self.distribution
if use_ids:
if self.target_data:
target_data = self.target_data.pamhyr_id
if self.distribution:
distribution = self.distribution.name
return { return {
"name": self.name, "name": self.name,
"data_type": self.data_type, "data_type": self.data_type,
"target_data": self.target_data, "target_data": target_data,
"distribution": self.distribution, "distribution": distribution,
"params": self.params, "params": self.params,
} }
+9
View File
@@ -51,3 +51,12 @@ class EnsembleList(PamhyrModelList):
self._lst.insert(index, r) self._lst.insert(index, r)
self._status.modified() self._status.modified()
return r return r
def new_with_data(self, index, data):
r = Ensemble(status=self._status)
r.set_data_dict(data)
self._lst.insert(index, r)
self._status.modified()
return r
+27
View File
@@ -114,6 +114,33 @@ class AddCommand(QUndoCommand):
else: else:
self._ensembles.insert(self._index, self._new) self._ensembles.insert(self._index, self._new)
class ImportCommand(QUndoCommand):
def __init__(self, ensembles, data, index):
QUndoCommand.__init__(self)
self._ensembles = ensembles
self._index = index
self._data = data
self._news = None
def undo(self):
for new in self._news:
new.set_as_deleted()
def redo(self):
if self._news is None:
self._news = list(
map(
lambda d: self._ensembles.new_with_data(
self._index, d
),
self._data,
)
)
else:
for new in self._news:
new.set_as_not_deleted()
class DelCommand(QUndoCommand): class DelCommand(QUndoCommand):
def __init__(self, ensembles, rows): def __init__(self, ensembles, rows):
+77 -1
View File
@@ -17,6 +17,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
import json
from tools import trace, timer, logger_exception from tools import trace, timer, logger_exception
@@ -47,7 +48,7 @@ from View.Ensembles.Table import (
EnsembleTableModel, ComboBoxDelegate EnsembleTableModel, ComboBoxDelegate
) )
from View.Ensembles.Translate import EnsemblesTranslate from View.Ensembles.Translate import EnsemblesTranslate
from View.Ensembles.UndoCommand import SetAllCommand from View.Ensembles.UndoCommand import SetAllCommand, ImportCommand
logger = logging.getLogger() logger = logging.getLogger()
@@ -140,6 +141,12 @@ class EnsemblesWindow(PamhyrWindow):
self.find(QAction, "action_add").triggered.connect(self.add) self.find(QAction, "action_add").triggered.connect(self.add)
self.find(QAction, "action_del").triggered.connect(self.delete) self.find(QAction, "action_del").triggered.connect(self.delete)
self.find(QAction, "action_edit").triggered.connect(self.edit) self.find(QAction, "action_edit").triggered.connect(self.edit)
self.find(QAction, "action_export").triggered.connect(
self.export_to_json
)
self.find(QAction, "action_import").triggered.connect(
self.import_from_json
)
table = self.find(QTableView, f"tableView") table = self.find(QTableView, f"tableView")
table.selectionModel()\ table.selectionModel()\
@@ -204,6 +211,75 @@ class EnsemblesWindow(PamhyrWindow):
logger_exception(e) logger_exception(e)
return return
def export_to_json(self):
ens = []
for e in self._ensembles.lst:
ens.append(e.get_data_dict(use_ids=True))
data = {"ensembles" : ens}
def aux(filename):
with open(filename[0], 'w') as f:
f.write(json.dumps(data))
self.file_dialog(
select_file=None,
callback=aux,
default_suffix='.json',
file_filter=[self._trad["file_json"]]
)
def import_from_json(self):
def aux(filename):
with open(filename[0], 'r') as f:
return json.load(f)
data = self.file_dialog(
select_file=None,
callback=aux,
default_suffix='.json',
file_filter=[self._trad["file_json"]]
)
dtarget = dict(
map(
lambda s: (s.pamhyr_id, s),
self._study.river.stricklers.stricklers
)
)
ddistr = dict(
map(
lambda d: (d.name, d),
self._study._ens_distributions.lst
)
)
def update(d, values):
d.update(values)
return d
updated = list(
map(
lambda d: update(
d, {"target_data": dtarget[d["target_data"]]}
),
map(
lambda d: update(
d, {"distribution": ddistr[d["distribution"]]}
),
data["ensembles"]
)
)
)
self._undo_stack.push(
ImportCommand(
self._ensembles, updated, 0
)
)
self._table.update()
class Ensemble1ParamsDialog(PamhyrDialog): class Ensemble1ParamsDialog(PamhyrDialog):
_pamhyr_ui = "Ensemble1ParamsDialog" _pamhyr_ui = "Ensemble1ParamsDialog"
+1 -1
View File
@@ -107,7 +107,7 @@ class WindowToolKit(object):
dialog = QFileDialog(self, options=options) dialog = QFileDialog(self, options=options)
if select_file == "Existing_file": if select_file == "ExistingFile":
mode = QFileDialog.FileMode.ExistingFile mode = QFileDialog.FileMode.ExistingFile
elif select_file == "Directory": elif select_file == "Directory":
mode = QFileDialog.FileMode.Directory mode = QFileDialog.FileMode.Directory
+3
View File
@@ -72,6 +72,9 @@ class CommonWordTranslate(PamhyrTranslate):
self._dict["file_csv"] = _translate( self._dict["file_csv"] = _translate(
"CommonWord", "CSV file (*.csv)" "CommonWord", "CSV file (*.csv)"
) )
self._dict["file_json"] = _translate(
"CommonWord", "JSON file (*.json)"
)
class UnitTranslate(CommonWordTranslate): class UnitTranslate(CommonWordTranslate):
+27
View File
@@ -33,6 +33,9 @@
<addaction name="action_add"/> <addaction name="action_add"/>
<addaction name="action_del"/> <addaction name="action_del"/>
<addaction name="action_edit"/> <addaction name="action_edit"/>
<addaction name="separator"/>
<addaction name="action_export"/>
<addaction name="action_import"/>
</widget> </widget>
<action name="action_add"> <action name="action_add">
<property name="icon"> <property name="icon">
@@ -79,6 +82,30 @@
<enum>QAction::NoRole</enum> <enum>QAction::NoRole</enum>
</property> </property>
</action> </action>
<action name="action_export">
<property name="icon">
<iconset>
<normaloff>ressources/export.png</normaloff>ressources/export.png</iconset>
</property>
<property name="text">
<string>Export</string>
</property>
<property name="toolTip">
<string>Export all ensemble configuration into json file</string>
</property>
</action>
<action name="action_import">
<property name="icon">
<iconset>
<normaloff>ressources/import.png</normaloff>ressources/import.png</iconset>
</property>
<property name="text">
<string>Import</string>
</property>
<property name="toolTip">
<string>Import ensemble configuration from json file</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>