Skip to content

Commit 5b0942a

Browse files
committed
allow saving (not yet proper loading) of plot config
1 parent b26087c commit 5b0942a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

wxmplot/plotconfigframe.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/python
22
#
33
# wxmplott GUI to Configure Line Plots
4-
#
5-
import os
4+
5+
from pathlib import Path
66
from functools import partial
77
import yaml
88
import numpy as np
@@ -13,7 +13,7 @@
1313

1414
from wxutils import get_cwd
1515

16-
from .utils import LabeledTextCtrl, MenuItem, Choice, FloatSpin
16+
from .utils import LabeledTextCtrl, MenuItem, Choice, FloatSpin, fix_filename
1717
from .config import PlotConfig
1818
from .colors import hexcolor, mpl_color, GUI_COLORS
1919

@@ -128,7 +128,13 @@ def __init__(self, parent=None, config=None, trace_color_callback=None):
128128
mbar.Append(fmenu, 'File')
129129
self.SetMenuBar(mbar)
130130

131-
def save_config(self, evt=None, fname='wxmplot.yaml'):
131+
def save_config(self, evt=None, fname='wxmplot'):
132+
conf = self.conf.get_config()
133+
title = conf.get('title', '').strip()
134+
if len(title) < 2:
135+
title = fname
136+
fname = fix_filename(f'{title}.yaml')
137+
132138
file_choices = 'YAML Config File (*.yaml)|*.yaml'
133139
dlg = wx.FileDialog(self, message='Save plot configuration',
134140
defaultDir=get_cwd(),
@@ -137,10 +143,11 @@ def save_config(self, evt=None, fname='wxmplot.yaml'):
137143
style=wx.FD_SAVE|wx.FD_CHANGE_DIR)
138144

139145
if dlg.ShowModal() == wx.ID_OK:
140-
conf = self.conf.get_current_config()
141-
ppath = os.path.abspath(dlg.GetPath())
146+
conf = self.conf.get_config()
147+
text = yaml.dump(conf, default_flow_style=None, indent=13, sort_keys=False)
148+
ppath = Path(dlg.GetPath())
142149
with open(ppath, 'w', encoding='utf-8') as fh:
143-
fh.write("%s\n" % yaml.dump(conf))
150+
fh.write(f"{text}\n")
144151

145152

146153
def load_config(self, evt=None):
@@ -151,7 +158,7 @@ def load_config(self, evt=None):
151158
style=wx.FD_OPEN)
152159

153160
if dlg.ShowModal() == wx.ID_OK:
154-
conf = yaml.safe_load(open(os.path.abspath(dlg.GetPath()), 'r').read())
161+
conf = yaml.safe_load(open(Path(dlg.GetPath()), 'r').read())
155162
self.conf.load_config(conf)
156163

157164
def DrawPanel(self):

0 commit comments

Comments
 (0)