[1306] | 1 | #------------------------------------------------------------------------------- |
---|
| 2 | # Name: ParameterEdit5 |
---|
| 3 | # Purpose: |
---|
| 4 | # |
---|
| 5 | # Author: Kelvin |
---|
| 6 | # |
---|
| 7 | # Created: 4/12/2013 |
---|
| 8 | #------------------------------------------------------------------------------- |
---|
| 9 | |
---|
| 10 | # Do not use this one. Too many random edits I forgot what I did. |
---|
| 11 | # Note to self: Make this program more modular and readable by splitting it into distinct parts and use perhaps use functions to implement them. |
---|
| 12 | # Note to self: Python can also be made into multi-file programs, so that it looks neater. Use 'import' to achieve that. |
---|
| 13 | # TO DO: Make entry windows for users to type their inputs in. |
---|
| 14 | # TO DO: Make the GUI window be able to parse user input for file names to read and write to. |
---|
| 15 | |
---|
| 16 | # Define the file names involved. |
---|
| 17 | defaultParamFileName = '/u/hsu004/IntroToDuchamp/duchampDefaultParameters' |
---|
| 18 | oldParamFileName = '/u/hsu004/IntroToDuchamp/duchampHIPASS.in' |
---|
| 19 | newParamFileName = '/u/hsu004/Kelvin/duchampHIPASSedited.in' |
---|
| 20 | userSpecifiedParamFileName = newParamFileName |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | # Define a simple function to determine the user's intent. |
---|
| 24 | # Note that this may be very inefficient. |
---|
| 25 | YES = 1 |
---|
| 26 | NO = 0 |
---|
| 27 | INVALID = -1 |
---|
| 28 | def toEdit(response): |
---|
| 29 | if (response == 'y') | (response == 'Y') | (response == 'yes'): |
---|
| 30 | return(YES) |
---|
| 31 | elif (response == 'n') | (response == 'N') | (response == 'no'): |
---|
| 32 | return(NO) |
---|
| 33 | else: |
---|
| 34 | return(INVALID) |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | # Define a function to read parameter files with the specified format. |
---|
| 38 | def readParamFile(paramFileName): |
---|
| 39 | |
---|
| 40 | # Initialise an empty Dictionary. |
---|
| 41 | parameters = {} |
---|
| 42 | |
---|
| 43 | # Open the given parameter file. |
---|
| 44 | paramFile = open(paramFileName) |
---|
| 45 | |
---|
| 46 | # Load files for lines of parameter data in the specified format and |
---|
| 47 | # store them into the dictionary. |
---|
| 48 | for line in paramFile: |
---|
| 49 | if line[0] != '#' and len(line) > 1: |
---|
| 50 | linebits = line.split() |
---|
| 51 | parameters[linebits[0]] = linebits[1] |
---|
| 52 | |
---|
| 53 | # Go back to the start of the file. |
---|
| 54 | paramFile.seek(0, 0) |
---|
| 55 | |
---|
| 56 | paramFile.close() |
---|
| 57 | |
---|
| 58 | return parameters |
---|
| 59 | |
---|
| 60 | # Define a function to create a parameter file with the specified format. |
---|
| 61 | def writeParamFile(newParamFileName, parameters): |
---|
| 62 | |
---|
| 63 | # Create a new file. |
---|
| 64 | newParamFile = open(newParamFileName, 'w') |
---|
| 65 | |
---|
| 66 | # Write the editted set of parameters into the new file. |
---|
| 67 | for par in parameters: |
---|
| 68 | newParamFile.write('%s %s\n'%(par, parameters[par])) |
---|
| 69 | |
---|
| 70 | newParamFile.close(); |
---|
| 71 | |
---|
| 72 | # Define a function to print a list of parameters listed in a dictionary. |
---|
| 73 | def listParameters(parameters): |
---|
| 74 | |
---|
| 75 | for par in parameters: |
---|
| 76 | print(par, parameters[par]) |
---|
| 77 | |
---|
| 78 | print('\n') |
---|
| 79 | |
---|
| 80 | # This is the function that defines the shell interface |
---|
| 81 | # which prompts the user with options to edit the parameters in a given file. |
---|
| 82 | def editParameter(): |
---|
| 83 | |
---|
| 84 | # Firstly, determine the default list of parameters |
---|
| 85 | defaultParameters = readParamFile(defaultParamFileName) |
---|
| 86 | |
---|
| 87 | # Display the default set of parameters. |
---|
| 88 | print('Original set of Parameters: \n') |
---|
| 89 | listParameters(defaultParameters) |
---|
| 90 | |
---|
| 91 | parameters = readParamFile(oldParamFileName) |
---|
| 92 | |
---|
| 93 | # Display the original set of parameters. |
---|
| 94 | print('Original set of Parameters: \n') |
---|
| 95 | listParameters(parameters) |
---|
| 96 | |
---|
| 97 | # Prompt the user about whether they want to edit any parameters. |
---|
| 98 | keepEditting = raw_input('\nWould you like to edit any of the parameters? (Type "y" for yes): ') |
---|
| 99 | |
---|
| 100 | # This handles the case when no parameters are to be editted. |
---|
| 101 | if toEdit(keepEditting) == NO: |
---|
| 102 | print('\nNo parameters will be editted.') |
---|
| 103 | |
---|
| 104 | if toEdit(keepEditting) == INVALID: |
---|
| 105 | keepEditting = raw_input('\nInvalid Input. Did you mistype something? Did you want to edit any of the parameters? (Type "y" for yes): ') |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | # This handles the case when the parameters are to be editted. |
---|
| 109 | while toEdit(keepEditting) == YES: |
---|
| 110 | |
---|
| 111 | # Prompt the user for the parameter name. |
---|
| 112 | parameter2edit = raw_input('\nWhich parameter would you like to edit? \n\tParameter: ') |
---|
| 113 | |
---|
| 114 | # If the parameter exists, edit the parameter value. |
---|
| 115 | # Note that the value is always stored as a string, even if the value is a 'number'. |
---|
| 116 | if parameter2edit in parameters: |
---|
| 117 | |
---|
| 118 | oldValue = parameters[parameter2edit] |
---|
| 119 | newValue = raw_input('\nPlease enter a new value or setting for "' + parameter2edit + '": ') |
---|
| 120 | parameters[parameter2edit] = newValue |
---|
| 121 | print('\n"' + parameter2edit + '" has been updated from ' + str(oldValue) + ' to ' + str(newValue) + '!') |
---|
| 122 | |
---|
| 123 | # Otherwise, if no such parameters exist, tell the user. |
---|
| 124 | else: |
---|
| 125 | print('\nThere is no such parameter as "' + parameter2edit + '" in your file "' + oldParamFileName + '"') |
---|
| 126 | |
---|
| 127 | # Prompt the user again if editting is to be continued. |
---|
| 128 | keepEditting = raw_input('\nWould you like to edit any other parameters? (Type "y" for yes): ') |
---|
| 129 | |
---|
| 130 | # Sometimes people mistype... |
---|
| 131 | if toEdit(keepEditting) == INVALID: |
---|
| 132 | keepEditting = raw_input('\nInvalid Input. Did you mistype something? Would you like to edit any other parameters? (Type "y" for yes): ') |
---|
| 133 | |
---|
| 134 | # Let the user know that the editting process has ended. |
---|
| 135 | if toEdit(keepEditting) == NO: |
---|
| 136 | print('\nFinished Editting.') |
---|
| 137 | elif toEdit(keepEditting) == INVALID: |
---|
| 138 | print('\nInvalid Input. No parameters will be editted.') |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | # Display the editted set of parameters. |
---|
| 142 | print('\nEditted set of Parameters: \n'); |
---|
| 143 | listParameters(parameters) |
---|
| 144 | |
---|
| 145 | writeParamFile(newParamFileName, parameters) |
---|
| 146 | |
---|
| 147 | ##def doCheck(master): |
---|
| 148 | ## interface = checkParamInterface(master) |
---|
| 149 | ## |
---|
| 150 | |
---|
| 151 | # This is the function that checks the parameter value of a given parameter. |
---|
| 152 | def checkParameter(): |
---|
| 153 | |
---|
| 154 | # Firstly, determine the default list of parameters |
---|
| 155 | defaultParameters = readParamFile(defaultParamFileName) |
---|
| 156 | |
---|
| 157 | # Then, determine the user specified list of parameters |
---|
| 158 | userSpecifiedParameters = readParamFile(userSpecifiedParamFileName) |
---|
| 159 | |
---|
| 160 | parameter2check = raw_input('\nWhich parameter would you like to check? \n\tParameter: ') |
---|
| 161 | |
---|
| 162 | if parameter2check in userSpecifiedParameters: |
---|
| 163 | print(parameter2check + ': ' + userSpecifiedParameters[parameter2check]) |
---|
| 164 | elif parameter2check in defaultParameters: |
---|
| 165 | print(parameter2check + ': ' + defaultParameters[parameter2check]) |
---|
| 166 | else: |
---|
| 167 | print('\n"' + parameter2check + '" does not exist.') |
---|
| 168 | |
---|
| 169 | #------------------------------------------------------------------------------- |
---|
| 170 | # GUI - Tkinter section |
---|
| 171 | from Tkinter import * |
---|
| 172 | |
---|
| 173 | class mainInterface: |
---|
| 174 | |
---|
| 175 | def __init__(self, master): |
---|
| 176 | |
---|
| 177 | self.master = master |
---|
| 178 | |
---|
| 179 | self.frame = Frame(master) |
---|
| 180 | self.frame.pack() |
---|
| 181 | |
---|
| 182 | self.buttonEDIT = Button(self.frame, text = "Edit", command = editParameter) |
---|
| 183 | self.buttonEDIT.pack() |
---|
| 184 | |
---|
| 185 | self.buttonCHECK = Button(self.frame, text = "Check", command = checkParameter) |
---|
| 186 | self.buttonCHECK.pack() |
---|
| 187 | |
---|
| 188 | self.entry1 = Entry(self.frame, width = 25) |
---|
| 189 | self.entry1.pack() |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | self.createButton = Button(self.frame, text = "Create Button!", command = self.newButton2) |
---|
| 194 | self.createButton.pack(side = LEFT) |
---|
| 195 | |
---|
| 196 | self.printEntry1 = Button(self.frame, text = "Print what's typed!", command = self.printTyped) |
---|
| 197 | self.printEntry1.pack() |
---|
| 198 | |
---|
| 199 | self.buttonQUIT = Button(self.frame, text = "QUIT", fg = "red", command = self.frame.quit) |
---|
| 200 | self.buttonQUIT.pack() |
---|
| 201 | |
---|
| 202 | def newButton(self): |
---|
| 203 | self.buttonNEW = Button(self.frame, text = "New Button", command = self.newButton) |
---|
| 204 | self.buttonNEW.pack() |
---|
| 205 | print("New Button Created!") |
---|
| 206 | |
---|
| 207 | def newButton2(self): |
---|
| 208 | newTK = Tk() |
---|
| 209 | self.newframe = Frame(newTK) |
---|
| 210 | self.buttonNEW2 = Button(self.newframe, text = "New Button", command = self.newButton) |
---|
| 211 | self.buttonNEW2.pack() |
---|
| 212 | print("New Button Created 2!") |
---|
| 213 | newTK.mainloop() |
---|
| 214 | |
---|
| 215 | def printTyped(self): |
---|
| 216 | self.printedEntry1 = Label(self.frame, text = str(self.entry1.get())) |
---|
| 217 | self.printedEntry1.pack() |
---|
| 218 | print(self.entry1.get()) |
---|
| 219 | |
---|
| 220 | ##class checkParamInterface: |
---|
| 221 | ## |
---|
| 222 | ## def __init__(self,master): |
---|
| 223 | ## |
---|
| 224 | ## self.frame = Frame(master) |
---|
| 225 | ## self.frame.pack() |
---|
| 226 | ## |
---|
| 227 | ## self.entry1 = Entry(self.frame, width=20) |
---|
| 228 | ## self.entry1.pack() |
---|
| 229 | ## self.buttonCHECK = Button(self.frame, text = "Check param", command = checkParameter) |
---|
| 230 | ## self.buttonCHECK.pack() |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | ## def check(self): |
---|
| 234 | ## checkTop = Toplevel() |
---|
| 235 | ## checkTop.title("Check Parameter") |
---|
| 236 | ## |
---|
| 237 | ## checkTop.checkPrompt = Message(checkTop, text = 'Which parameter would you like to check? \nParameter: ') |
---|
| 238 | ## checkTop.checkPrompt.pack() |
---|
| 239 | |
---|
| 240 | ## checkTop.checkEntry = Entry(checkTop.frame, width = 25) |
---|
| 241 | ## checkTop.checkEntry.pack() |
---|
| 242 | |
---|
| 243 | ## checkTop.buttonQUIT = Button(checkTop.frame, text = "QUIT", fg = "red", command = self.frame.quit) |
---|
| 244 | ## checkTop.buttonQUIT.pack() |
---|
| 245 | |
---|
| 246 | #------------------------------------------------------------------------------- |
---|
| 247 | # This is the function that checks the parameter value of a given parameter through GUI. |
---|
| 248 | def checkParameterGUI(interface): |
---|
| 249 | |
---|
| 250 | # Firstly, determine the default list of parameters |
---|
| 251 | defaultParameters = readParamFile(defaultParamFileName) |
---|
| 252 | |
---|
| 253 | # Then, determine the user specified list of parameters |
---|
| 254 | userSpecifiedParameters = readParamFile(userSpecifiedParamFileName) |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | parameter2check = raw_input('\nWhich parameter would you like to check? \n\tParameter: ') |
---|
| 258 | |
---|
| 259 | if parameter2check in userSpecifiedParameters: |
---|
| 260 | print(parameter2check + ': ' + userSpecifiedParameters[parameter2check]) |
---|
| 261 | elif parameter2check in defaultParameters: |
---|
| 262 | print(parameter2check + ': ' + defaultParameters[parameter2check]) |
---|
| 263 | else: |
---|
| 264 | print('\n"' + parameter2check + '" does not exist.') |
---|
| 265 | |
---|
| 266 | #------------------------------------------------------------------------------- |
---|
| 267 | |
---|
| 268 | # Main |
---|
| 269 | def main(): |
---|
| 270 | root = Tk() |
---|
| 271 | |
---|
| 272 | interface = mainInterface(root) |
---|
| 273 | |
---|
| 274 | root.mainloop() |
---|
| 275 | root.destroy() |
---|
| 276 | |
---|
| 277 | #------------------------------------------------------------------------------- |
---|
| 278 | |
---|
| 279 | ##def main(): |
---|
| 280 | ## editParameter() |
---|
| 281 | ## checkParameter() |
---|
| 282 | |
---|
| 283 | if __name__ == '__main__': |
---|
| 284 | main() |
---|