Skip to content

Commit

Permalink
Store EFMs as JSON (#522)
Browse files Browse the repository at this point in the history
* JSON EFM prototype

* Improved JSON saving

* Cleanup lel

* Switch to zipped JSONs
  • Loading branch information
Paulocracy authored Aug 16, 2024
1 parent b76d21b commit 4243c2e
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions cnapy/gui_elements/mode_navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
from cnapy.appdata import AppData
from cnapy.flux_vector_container import FluxVectorContainer
from cnapy.utils import QComplReceivLineEdit
import zipfile
import os
from io import BytesIO

import json
from typing import Any


def json_zip_write(
zip_path: str,
zipped_file_name: str,
json_data: Any,
zip_method: int = zipfile.ZIP_LZMA,
) -> None:
data = json.dumps(json_data, indent=4)

json_bytes = BytesIO(data.encode('utf-8'))

with zipfile.ZipFile(zip_path, 'w', zip_method) as zipf:
zipf.writestr(zipped_file_name, json_bytes.getvalue())


class ModeNavigator(QWidget):
"""A navigator widget"""
Expand Down Expand Up @@ -116,11 +137,20 @@ def save_mcs(self):

def save_efm(self):
dialog = QFileDialog(self)
filename: str = dialog.getSaveFileName(
directory=self.appdata.work_directory, filter="*.npz")[0]
filename, selected_filter = dialog.getSaveFileName(
directory=self.appdata.work_directory, filter=("*.npz;;*.json.zip")
)
if not filename or len(filename) == 0:
return
self.appdata.project.modes.save(filename)
if "json" not in selected_filter:
self.appdata.project.modes.save(filename)
else:
modelist = []
for mode in self.appdata.project.modes:
for r, v in mode.items():
mode[r] = v
modelist.append(mode)
json_zip_write(filename, "efms.json", modelist)

def save_sd(self):
dialog = QFileDialog(self)
Expand Down Expand Up @@ -215,6 +245,10 @@ def next(self):
break
self.display_mode()

for i in range (len(self.appdata.project.modes)):
values = self.appdata.project.modes[i]
print(values)

def apply(self):
self.appdata.scen_values_set_multiple(list(self.current_flux_values.keys()),
list(self.current_flux_values.values()))
Expand Down

0 comments on commit 4243c2e

Please sign in to comment.