Skip to content
Snippets Groups Projects
Commit 705e924f authored by Eric Kooistra's avatar Eric Kooistra
Browse files

Added change_key_in_dict_file() to write new value for key in the filePathName file.

parent ea7bc2d4
No related branches found
No related tags found
No related merge requests found
......@@ -148,6 +148,7 @@ class CommonDictFile:
return file_dict
def write_dict_file(self, dicts, filePathNames, keySeparator=None):
"""Write the dictionary information to the filePathName file."""
if keySeparator==None: keySeparator=self.CDF_SEPARATOR
for fpn, the_dict in zip(cm.listify(filePathNames), cm.listify(dicts)):
with open(fpn, 'w') as fp:
......@@ -155,6 +156,7 @@ class CommonDictFile:
fp.write('%s%s%s\n' % (key, keySeparator, the_dict[key]))
def append_key_to_dict_file(self, filePathName, key, values):
"""Write append the key = value pair to the filePathName file."""
with open(filePathName, 'a') as fp:
if len(cm.listify(values))==1:
fp.write('%s = %s' % (key, values))
......@@ -163,10 +165,33 @@ class CommonDictFile:
for v in cm.listify(values):
fp.write('%s\n' % v)
def change_key_in_dict_file(self, filePathName, key, value):
"""Write new value for key in the filePathName file. The original key = value pair must fit on one line."""
# Read dict file into string
with open(filePathName, 'r') as fp:
dict_string = fp.read()
# Modify key value in dict string
key_start = dict_string.find(key) # find key
if key_start>0:
separator = dict_string.find(self.CDF_SEPARATOR, key_start) # find separator
if separator>0:
value_start = separator + 1 # value start index
eol = dict_string.find('\n', value_start)
if eol>0:
value_end = eol # value end index at end of line
else:
value_end = len(dict_string) # value end index at end of file
dict_string = dict_string[0:value_start] + ' ' + value + dict_string[value_end:]
# Write string to dict file
with open(filePathName, 'w') as fp:
fp.write(dict_string)
def get_filePath(self, the_dict):
"""Get file path to the dictionary file location."""
return self.filePaths[self.dicts.index(the_dict)]
def get_filePathName(self, the_dict):
"""Get file path to the dictionary file location including the dictionary file name."""
return self.filePathNames[self.dicts.index(the_dict)]
def get_key_values(self, key, dicts=None):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment