Last active
August 29, 2015 14:05
-
-
Save MatthieuDartiailh/0a9ab2d41296917d0d5c to your computer and use it in GitHub Desktop.
Measure updater for new version of HQCMeas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
""" | |
import os | |
from shutil import copyfile | |
from configobj import ConfigObj | |
# This dictionary specifies tasks whose name has simply changed but are | |
# otherwise untouched and can be simply renamed. | |
UPDATED_TASK_NAMES = {'PrintTask': 'LogTask', | |
'RFSourceSetFrequencyTask': 'SetRFFrequencyTask', | |
'RFSourceSetPowerTask': 'SetRFPowerTask', | |
'RFSourceSetOnOffTask': 'SetRFOnOffTask'} | |
# The following functions are used to specify more complex transformation than | |
# a simple renaming. They will be passed the Section object for which the task | |
# class macth an entry of the CUSTOM_TASK_UPDATERS dict. | |
def update_loop_task(section): | |
section['interface'] = {} | |
section['task_class'] = 'LoopTask' | |
i_section = section['interface'] | |
i_section['interface_class'] = 'LinspaceLoopInterface' | |
i_section['start'] = section['task_start'] | |
del section['task_start'] | |
i_section['stop'] = section['task_stop'] | |
del section['task_stop'] | |
i_section['step'] = section['task_step'] | |
del section['task_step'] | |
def update_pna_set_freq_task(section): | |
section['interface'] = {} | |
i_section = section['interface'] | |
i_section['interface_class'] = 'PNASetRFFrequencyInterface' | |
i_section['channel'] = section['channel'] | |
del section['channel'] | |
section['task_class'] = 'SetRFFrequencyTask' | |
section['frequency_unti'] = 'Hz' | |
def update_pna_set_power_task(section): | |
section['interface'] = {} | |
i_section = section['interface'] | |
i_section['interface_class'] = 'PNASetRFPowerInterface' | |
i_section['channel'] = section['channel'] | |
del section['channel'] | |
section['task_class'] = 'SetRFPowerTask' | |
CUSTOM_TASK_UPDATERS = {'LoopTask': update_loop_task, | |
'SimpleLoopTask': update_loop_task, | |
'PNASetFreqTask': update_pna_set_freq_task, | |
'PNASetPowerTask': update_pna_set_power_task} | |
def update_subsections(root): | |
for s_name in root.sections: | |
section = root[s_name] | |
if 'task_class' in section: | |
t_class = section['task_class'] | |
if t_class in UPDATED_TASK_NAMES: | |
section['task_class'] = UPDATED_TASK_NAMES[t_class] | |
elif t_class in CUSTOM_TASK_UPDATERS: | |
CUSTOM_TASK_UPDATERS[t_class](section) | |
update_subsections(section) | |
def update_ini_files(file_paths, copy=True): | |
if copy: | |
for path in file_paths: | |
if not os.path.isfile(path[:-4]+'_old.ini'): | |
copyfile(path, path[:-4]+'_old.ini') | |
for path in file_paths: | |
config = root = ConfigObj(path) | |
if 'root_task' in config: | |
root = config['root_task'] | |
update_subsections(root) | |
config.write() | |
if __name__ == '__main__': | |
PATHS = [] | |
if PATHS: | |
update_ini_files(PATHS) | |
else: | |
from enaml.qt.qt_application import QtApplication | |
app = QtApplication() | |
import enaml | |
with enaml.imports(): | |
from measure_updater_view import Main | |
Main().show() | |
app.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
""" | |
import os | |
from enaml.widgets.api import (Window, Container, Label, PushButton, | |
FileDialogEx, CheckBox, MultilineField) | |
from enaml.core.api import Looper | |
from enaml.layout.api import hbox, vbox | |
enamldef Main(Window): main: | |
attr paths : list = [] | |
Container: | |
constraints = [vbox(files, hbox(sel, cop, con), log)] | |
Container: files: | |
Looper: | |
iterable << paths[:] | |
Label: | |
text = loop_item.rsplit(os.sep, 1)[-1] | |
PushButton: sel: | |
text = 'Select files' | |
clicked :: | |
main.paths = FileDialogEx.get_open_file_names(self, | |
name_filters=['*.ini']) | |
CheckBox: cop: | |
text = 'Keep copies' | |
checked = True | |
PushButton: con: | |
text = 'Convert' | |
clicked :: | |
from measure_updater import update_ini_files | |
update_ini_files(paths, cop.checked) | |
log.text += 'Files converted\n' | |
MultilineField: log: | |
read_only = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment