mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-29 19:10:42 +01:00
Ensemble: Add Export/Import method with json files.
This commit is contained in:
1 parent
88345c58a7
commit
63592b91a9
7 files changed
+156
-5
No files matched your search
@@ -253,12 +253,21 @@ class Ensemble(SQLSubModel):
|
||||
self._target_data = target_data
|
||||
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 {
|
||||
"name": self.name,
|
||||
"data_type": self.data_type,
|
||||
"target_data": self.target_data,
|
||||
"distribution": self.distribution,
|
||||
"target_data": target_data,
|
||||
"distribution": distribution,
|
||||
"params": self.params,
|
||||
}
|
||||
|
||||
|
||||
@@ -51,3 +51,12 @@ class EnsembleList(PamhyrModelList):
|
||||
self._lst.insert(index, r)
|
||||
self._status.modified()
|
||||
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
|
||||
@@ -114,6 +114,33 @@ class AddCommand(QUndoCommand):
|
||||
else:
|
||||
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):
|
||||
def __init__(self, ensembles, rows):
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
import json
|
||||
|
||||
from tools import trace, timer, logger_exception
|
||||
|
||||
@@ -47,7 +48,7 @@ from View.Ensembles.Table import (
|
||||
EnsembleTableModel, ComboBoxDelegate
|
||||
)
|
||||
from View.Ensembles.Translate import EnsemblesTranslate
|
||||
from View.Ensembles.UndoCommand import SetAllCommand
|
||||
from View.Ensembles.UndoCommand import SetAllCommand, ImportCommand
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
@@ -140,6 +141,12 @@ class EnsemblesWindow(PamhyrWindow):
|
||||
self.find(QAction, "action_add").triggered.connect(self.add)
|
||||
self.find(QAction, "action_del").triggered.connect(self.delete)
|
||||
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.selectionModel()\
|
||||
@@ -204,6 +211,75 @@ class EnsemblesWindow(PamhyrWindow):
|
||||
logger_exception(e)
|
||||
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):
|
||||
_pamhyr_ui = "Ensemble1ParamsDialog"
|
||||
|
||||
@@ -107,7 +107,7 @@ class WindowToolKit(object):
|
||||
|
||||
dialog = QFileDialog(self, options=options)
|
||||
|
||||
if select_file == "Existing_file":
|
||||
if select_file == "ExistingFile":
|
||||
mode = QFileDialog.FileMode.ExistingFile
|
||||
elif select_file == "Directory":
|
||||
mode = QFileDialog.FileMode.Directory
|
||||
|
||||
@@ -72,6 +72,9 @@ class CommonWordTranslate(PamhyrTranslate):
|
||||
self._dict["file_csv"] = _translate(
|
||||
"CommonWord", "CSV file (*.csv)"
|
||||
)
|
||||
self._dict["file_json"] = _translate(
|
||||
"CommonWord", "JSON file (*.json)"
|
||||
)
|
||||
|
||||
|
||||
class UnitTranslate(CommonWordTranslate):
|
||||
|
||||
@@ -33,6 +33,9 @@
|
||||
<addaction name="action_add"/>
|
||||
<addaction name="action_del"/>
|
||||
<addaction name="action_edit"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="action_export"/>
|
||||
<addaction name="action_import"/>
|
||||
</widget>
|
||||
<action name="action_add">
|
||||
<property name="icon">
|
||||
@@ -79,6 +82,30 @@
|
||||
<enum>QAction::NoRole</enum>
|
||||
</property>
|
||||
</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>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
||||
Reference in new issue
Block a user