-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjsonIO.py
166 lines (130 loc) · 5.38 KB
/
jsonIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#importing and exporting settings to json files
import easygui as eg
from os import path
import json
import pandas as pd
##################### Checking User Input ##############
def check_user_entry(keys, entries, valid_strings):
if(entries == None):
return False
entry_dict = dict(zip(keys, entries))
for key in entry_dict:
if not is_entry_valid(key, entry_dict[key], valid_strings):
return False
return True
def is_entry_valid(key, value, valid_strings):
try:
float(value)
return True
except ValueError:
try:
if value in valid_strings[key]:
return True
except (AttributeError, KeyError):
return False
return False
return False
def convert_to_float(settings):
#if possible, convert items to floats
for key in settings:
try:
settings[key] = float(settings[key])
except ValueError:
pass
return settings
def convert_keys_to_int(settings):
settings = {int(k):v for k,v in settings.items()}
return settings
def force_extension(filename, extension):
#Checking the file type
file_root, file_extension = path.splitext(filename)
if(file_extension != extension):
file_extension = extension
file_name = file_root + file_extension
return file_name
def get_extension(filename):
name, ext = path.splitext(filename)
return ext
####################### Getting Input from User #######################
def update_settings(settings, new_value_list):
#assuming an ordered response list
#a bit hacky but it should work
index = 0
for key in settings.keys():
settings.update({key: new_value_list[index]})
index += 1
return settings
def export_cycle_settings(settings, cycle_name = "", file_name = None):
#add extra space to get formatting correct
if (cycle_name != ""):
cycle_name += " "
write_mode = "a"
if file_name == None:
write_mode = "w"
#get the file to export to
file_name = eg.filesavebox(msg = "Choose a File to export {}settings to".format(cycle_name),
title = "Settings", filetypes = ['*.json', 'JSON files'])
if file_name == None:
return None
#force file name extension
file_name = force_extension(file_name, '.json')
#export the file
with open(file_name, write_mode) as write_file:
json.dump(settings, write_file, indent = 4)
return file_name
def import_multi_step_from_csv():
file_name = eg.fileopenbox(msg = "Choose a File to import step settings from",
title = "Settings", filetypes = ['*.csv', 'CSV files'])
if file_name == None:
return None
df = pd.read_csv(file_name)
settings_list = df.to_dict(orient = 'records')
return settings_list
def import_cycle_settings(cycle_name = "", queue = None, ch_num = None):
#add extra space to get formatting correct
if (cycle_name != ""):
cycle_name += " "
#get the file to import from
file_name = eg.fileopenbox(msg = "Choose a File to import {}settings from".format(cycle_name),
title = "Settings", filetypes = [['*.json', 'JSON files'],['*.csv', 'CSV files']])
if file_name == None:
return None
settings = None
#import the file
if(file_name != None):
#determine the file type - JSON or CSV
extension = get_extension(file_name)
if extension == '.json':
with open(file_name, "r") as read_file:
settings = json.load(read_file)
elif extension == '.csv':
df = pd.read_csv(file_name)
settings = df.to_dict(orient = 'records')[0]
if queue != None:
settings = {'ch_num': ch_num, 'cdc_input_dict': settings}
queue.put_nowait(settings)
else:
return settings
def get_cycle_settings(settings, valid_strings = None, cycle_name = ""):
if(cycle_name != ""):
cycle_name += " "
response = eg.buttonbox(msg = "Would you like to import settings for {}cycle or create new settings?".format(cycle_name),
title = "Settings for {}cycle".format(cycle_name), choices = ("New Settings", "Import Settings"))
if response == None:
return None
elif response == "New Settings":
valid_entries = False
while not valid_entries:
response_list = eg.multenterbox(msg = "Enter Info for {}cycle".format(cycle_name), title = response,
fields = list(settings.keys()), values = list(settings.values()))
if response_list == None:
return None
valid_entries = check_user_entry(list(settings.keys()), response_list, valid_strings)
#update dict entries with the response - can't use the dict.update since we only have a list here.
settings = update_settings(settings, response_list)
if (eg.ynbox(msg = "Would you like to save these settings for future use?", title = "Save Settings")):
export_cycle_settings(settings, cycle_name)
elif response == "Import Settings":
settings = import_cycle_settings(cycle_name)
settings = convert_to_float(settings)
return settings