Commit c7e0b4b3 authored by Michael Rudolf's avatar Michael Rudolf
Browse files

Working Project Config

 - Opening folders automatically creates a reloadable project config
parent 3babe1c1
......@@ -3,7 +3,7 @@
# @Author: M. Rudolf, M. Warsitzka
# @Date: 2019-02-20 12:00:00
# @Last Modified by: M. Rudolf
# @Last Modified time: 2020-05-06 14:46:03
# @Last Modified time: 2020-05-16 15:35:52
"""
RST_pick_GUI.py
......@@ -57,7 +57,7 @@ class RST_pick_GUI(tk.Tk):
def initalize(self):
# Read or create config and set entries from config
self.refresh_entries_from_cfg()
self.refresh_entries_from_cfg(init=True)
# == WIDGETS and INTERFACE ==
self.grid()
......@@ -162,7 +162,7 @@ class RST_pick_GUI(tk.Tk):
from ctypes import windll, pointer, wintypes
try:
windll.shcore.SetProcessDpiAwareness(1)
except Exception as e:
except Exception as _:
logging.info('High-DPI scaling failed')
pass # this will fail on Win Server and maybe early Windows
DPI100pc = 96 # DPI 96 is 100% scaling
......@@ -187,22 +187,22 @@ class RST_pick_GUI(tk.Tk):
else:
return None, None, 1 # What to do for other OSs?
def TkGeometryScale(s, cvtfunc):
def make_dpi_aware(self):
"""Adapted from the user 'dingles at stack-overflow"""
# format "WxH+X+Y"
patt = r"(?P<W>\d+)x(?P<H>\d+)\+(?P<X>\d+)\+(?P<Y>\d+)"
R = re.compile(patt).search(s)
G = str(cvtfunc(R.group("W"))) + "x"
G += str(cvtfunc(R.group("H"))) + "+"
G += str(cvtfunc(R.group("X"))) + "+"
G += str(cvtfunc(R.group("Y")))
return G
def make_dpi_aware(self):
"""Adapted from the user 'dingles' at stack-overflow.com"""
def TkGeometryScale(s, cvtfunc):
# format "WxH+X+Y"
patt = r"(?P<W>\d+)x(?P<H>\d+)\+(?P<X>\d+)\+(?P<Y>\d+)"
R = re.compile(patt).search(s)
G = str(cvtfunc(R.group("W"))) + "x"
G += str(cvtfunc(R.group("H"))) + "+"
G += str(cvtfunc(R.group("X"))) + "+"
G += str(cvtfunc(R.group("Y")))
return G
self.DPI_X, self.DPI_Y, self.DPI_scaling = self.get_HWND_dpi()
self.TkScale = lambda v: int(float(v) * self.DPI_scaling)
self.TkGeometryScale = lambda s: self.TkGeometryScale(s, self.TkScale)
self.TkGeometryScale = lambda s: TkGeometryScale(s, self.TkScale)
def resize_all_elements(self):
"""
......@@ -228,7 +228,10 @@ class RST_pick_GUI(tk.Tk):
self.entry_output.delete(0, 'end')
self.entry_output.insert(0, f_path + '/')
self.entry_projname.delete(0, 'end')
self.entry_projname.insert(0, f_path.split('/')[-1])
pname = f_path.split('/')[-1]
self.entry_projname.insert(0, pname)
self.path_cfg.set(f_path + '/' + pname + '.cfg')
self.refresh_entries_from_cfg(path=f_path + '/' + pname + '.cfg')
elif event.widget._name == '!button2': # Output path
self.entry_output.delete(0, 'end')
self.entry_output.insert(0, f_path + '/')
......@@ -237,10 +240,20 @@ class RST_pick_GUI(tk.Tk):
def start_processing(self, event):
"""Initiates Processing of data"""
# Create Var dictionary:
# Create Var dictionary and save to config:
Vars = dict()
for item in self.params:
Vars[item] = float(self.params[item].get())
self.cfg['parameters'][item] = self.params[item].get()
self.cfg ['options']['plot_ts'] = str(self.plot_ts.get())
self.cfg ['options']['save_ts'] = str(self.save_ts.get())
self.cfg ['options']['rst'] = str(self.rst.get())
self.cfg ['options']['rev_pick'] = str(self.rev_pick.get())
self.cfg ['options']['is_VST'] = str(self.is_VST.get())
self.cfg ['options']['quit_onprc'] = str(self.quit_onprc.get())
with open(self.path_cfg.get(), 'w') as f:
self.cfg.write(f)
# Other Variables
path_in = self.path_in.get()
path_out = self.path_out.get()
......@@ -373,21 +386,34 @@ class RST_pick_GUI(tk.Tk):
text=self.cfg['units'][item[0]])
opt_dlg.label_unit.grid(column=2, row=i + 1, sticky='w')
def refresh_entries_from_cfg(self):
def refresh_entries_from_cfg(self, path=None, init=False):
"""Refresh entries from config file"""
# Try to see whether there is already a config
try:
self.cfg.read(self.path_cfg.get())
self.projectname.set(self.cfg['paths']['projectname'])
except Exception as e:
self.create_default_config()
print('created new config')
self.cfg.read(self.path_cfg.get())
if init:
try:
self.cfg.read(self.path_cfg.get())
print('read default config')
except:
self.create_default_config()
self.cfg.read(self.path_cfg.get())
print('created default config')
if path:
try:
c = self.cfg.read(self.path_cfg.get())
if not(c):
self.create_default_config(path=path)
self.cfg.read(self.path_cfg.get())
print('created project config')
print('read project config')
except:
self.create_default_config(path=path)
self.cfg.read(self.path_cfg.get())
print('created project config')
# Set contents of fields and variables according to config
self.projectname.set(self.cfg['paths']['projectname'])
self.path_in.set(self.cfg['paths']['path_in'])
self.path_out.set(self.cfg['paths']['path_out'])
self.path_cfg.set(self.cfg['paths']['path_cfg'])
self.plot_ts.set(self.cfg.getint('options', 'plot_ts'))
self.save_ts.set(self.cfg.getint('options', 'save_ts'))
......@@ -401,22 +427,33 @@ class RST_pick_GUI(tk.Tk):
self.params[item[0]] = tk.StringVar()
self.params[item[0]].set(item[1])
def create_default_config(self):
def create_default_config(self, path=None):
"""Creates default config in the given path"""
config = configparser.ConfigParser()
config.optionxform = str
if not(path):
path = 'default.ini'
projectname = 'example'
path_in = 'input/'
path_out = 'output/'
else:
projectname = self.projectname.get()
path_in = self.path_in.get()
path_out = self.path_out.get()
config['paths'] = {
'projectname': 'example',
'path_in': 'input/',
'path_out': 'output/'
'projectname': projectname,
'path_in': path_in,
'path_out': path_out,
'path_cfg': path
}
config['options'] = {
'plot_ts': 1, # if 1: data plotted, if 0: not
'save_ts': 1, # if 1: data stored in .txt files, if 0: not
'rst': 1, # if 1: automatic picking
'rev_pick': 0, # if 1: the user can review the picked peaks
'rev_pick': 1, # if 1: the user can review the picked peaks
'is_VST': 0, # if 1: the file is a VST-Test file
'quit_onprc': 0, # if 1: automatically quits after processing
}
......@@ -430,7 +467,7 @@ class RST_pick_GUI(tk.Tk):
'velnoise': 1e-2,
'stressnoise': 0.025,
'cellweight': 2185.3, # weight of shear cell
'smoothwindow': 51 # Smoothing Factor for plots
'smoothwindow': 5 # Smoothing Factor for plots
}
config['units'] = {
......@@ -444,9 +481,10 @@ class RST_pick_GUI(tk.Tk):
'cellweight': 'g',
'smoothwindow': 'samples'
}
self.path_cfg.set('default.ini')
self.path_cfg.set(config['paths']['path_cfg'])
with open(self.path_cfg.get(), 'w') as f:
config.write(f)
self.cfg = config
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment