source: branches/GUIdev/DuchampParameterGUI_fileIO.py @ 1441

Last change on this file since 1441 was 1332, checked in by KelvinHsu, 11 years ago

DuchampParameterGUI.py split into four files (it could be made more modular but so far it is simply just code split into four files, without really using the advantage of multi-file programs...)

File size: 4.5 KB
Line 
1#-------------------------------------------------------------------------------
2# Name:        DuchampParameterGUI_fileIO
3# Purpose:
4#
5# Author:      Kelvin
6#
7# Created:     4/12/2013
8#-------------------------------------------------------------------------------
9
10import os
11
12# Determine the current directory.
13directory = os.getcwd()
14
15# Define the default file path.
16defaultParamFileName = '/InputComplete'
17defaultParamFilePath = directory + defaultParamFileName
18
19
20# This reads parameter files with the specified format.
21def readParamFile(paramFilePath):
22
23    # Create a dictionary that maps parameter names to its value and a list to keep track of its order.
24    parameters = {}
25    orderedParameters = []
26
27    try:
28        # Open the given parameter file.
29        paramFile = open(paramFilePath)
30
31        # Load files for lines of parameter data in the specified format and
32        # store them into the dictionary.
33        for line in paramFile:
34            if line[0] != '#' and len(line.split()) > 1:
35                linebits = line.split()
36                parameters[linebits[0]] = linebits[1]
37                orderedParameters = orderedParameters + [linebits[0]]
38
39        paramFile.close()
40
41    # Note that if the file could not be read, then the dictionary will stay empty.
42    # The GUI window can make use of this fact.
43    except IOError:
44        print("Cannot open file")
45
46    return(parameters, orderedParameters)
47
48
49# This reads the default file for all relevant information of parameter classes, description, type, and format.
50def readParamByClass(paramFilePath):
51
52    # Create a diciontary that maps parameter names to its class and a list to keep track of class orders.
53    classOfParameter = {}
54    classNames = []
55
56    # Create dictionaries that maps parameter names to its description, value type, and value format.
57    description = {}
58    paramType = {}
59    paramFormat = {}
60
61    try:
62        # Open the given parameter file.
63        paramFile = open(paramFilePath)
64
65        # Load files for lines of parameter data in the specified format and
66        # store them into the dictionaries.
67        for line in paramFile:
68
69            if line[0:3] == '###' and len(line.split('###')) > 1:
70                className = line.strip('###').strip()
71                classNames = classNames + [className]
72
73            elif line[0:2] == '#*' and len(line.split()) > 1 and '--' in line:
74                param = line.split()[1].strip()
75                paramType[param] = line.split('[')[1].split(']')[0].strip()
76                paramFormat[param] = line.split('{')[1].split('}')[0].strip()
77                description[param] = '--'.join(line.split('--')[1:len(line.split('--'))]).strip()
78               
79            elif line[0] != '#' and len(line.split()) > 1:
80                linebits = line.split()
81                classOfParameter[linebits[0]] = className
82
83        paramFile.close()
84
85    # Note that if the file could not be read, then the dictionary will stay empty.
86    # The GUI window can make use of this fact.
87    except IOError:
88        print("Cannot open file")
89                 
90    return(classOfParameter, classNames, description, paramType, paramFormat)
91
92
93# Define a function to create a parameter file with the specified format.
94# It will return 0 if the file already exists and will not overwrite the file just yet (unless write = 1)
95#                1 if the file is successfully created (or overwriten)
96#               -1 if the file was not successfully created (or overwriten) due to some error
97def writeParamFile(newParamFilePath, parameters, parameterOrder, write):
98
99    # If we want to make sure if it is okay to write, do the following.
100    if write != 1:
101       
102        # First, try opening the file for reading.
103        # If the file exists for reading, then close it and then return a 0,
104        # which the GUI window use to warn the user that a file with that name already exists.
105        try:
106            newParamFile = open(newParamFilePath)
107            newParamFile.close()
108            return(0)
109
110        # If we have trouble reading the file, then the file doesn't exist and we're safe to write to it.
111        except IOError:
112            write = 1
113
114    # If we are sure we can write to the file, then do it.
115    if write == 1:
116
117        try:
118            newParamFile = open(newParamFilePath, 'w')
119           
120            # Write the editted set of parameters into the new file.
121            for par in parameterOrder:
122                newParamFile.write('%s    %s\n'%(par, parameters[par]))
123
124            newParamFile.close()
125            return(1)
126       
127        except IOError:
128            return(-1)
Note: See TracBrowser for help on using the repository browser.