#------------------------------------------------------------------------------- # Name: DuchampParameterGUI_fileIO # Purpose: # # Author: Kelvin # # Created: 4/12/2013 #------------------------------------------------------------------------------- import os # Determine the current directory. directory = os.getcwd() # Define the default file path. defaultParamFileName = '/InputComplete' defaultParamFilePath = directory + defaultParamFileName # This reads parameter files with the specified format. def readParamFile(paramFilePath): # Create a dictionary that maps parameter names to its value and a list to keep track of its order. parameters = {} orderedParameters = [] try: # Open the given parameter file. paramFile = open(paramFilePath) # Load files for lines of parameter data in the specified format and # store them into the dictionary. for line in paramFile: if line[0] != '#' and len(line.split()) > 1: linebits = line.split() parameters[linebits[0]] = linebits[1] orderedParameters = orderedParameters + [linebits[0]] paramFile.close() # Note that if the file could not be read, then the dictionary will stay empty. # The GUI window can make use of this fact. except IOError: print("Cannot open file") return(parameters, orderedParameters) # This reads the default file for all relevant information of parameter classes, description, type, and format. def readParamByClass(paramFilePath): # Create a diciontary that maps parameter names to its class and a list to keep track of class orders. classOfParameter = {} classNames = [] # Create dictionaries that maps parameter names to its description, value type, and value format. description = {} paramType = {} paramFormat = {} try: # Open the given parameter file. paramFile = open(paramFilePath) # Load files for lines of parameter data in the specified format and # store them into the dictionaries. for line in paramFile: if line[0:3] == '###' and len(line.split('###')) > 1: className = line.strip('###').strip() classNames = classNames + [className] elif line[0:2] == '#*' and len(line.split()) > 1 and '--' in line: param = line.split()[1].strip() paramType[param] = line.split('[')[1].split(']')[0].strip() paramFormat[param] = line.split('{')[1].split('}')[0].strip() description[param] = '--'.join(line.split('--')[1:len(line.split('--'))]).strip() elif line[0] != '#' and len(line.split()) > 1: linebits = line.split() classOfParameter[linebits[0]] = className paramFile.close() # Note that if the file could not be read, then the dictionary will stay empty. # The GUI window can make use of this fact. except IOError: print("Cannot open file") return(classOfParameter, classNames, description, paramType, paramFormat) # Define a function to create a parameter file with the specified format. # It will return 0 if the file already exists and will not overwrite the file just yet (unless write = 1) # 1 if the file is successfully created (or overwriten) # -1 if the file was not successfully created (or overwriten) due to some error def writeParamFile(newParamFilePath, parameters, parameterOrder, write): # If we want to make sure if it is okay to write, do the following. if write != 1: # First, try opening the file for reading. # If the file exists for reading, then close it and then return a 0, # which the GUI window use to warn the user that a file with that name already exists. try: newParamFile = open(newParamFilePath) newParamFile.close() return(0) # If we have trouble reading the file, then the file doesn't exist and we're safe to write to it. except IOError: write = 1 # If we are sure we can write to the file, then do it. if write == 1: try: newParamFile = open(newParamFilePath, 'w') # Write the editted set of parameters into the new file. for par in parameterOrder: newParamFile.write('%s %s\n'%(par, parameters[par])) newParamFile.close() return(1) except IOError: return(-1)