diff --git a/tools/oneclick/base/common_dict_file.py b/tools/oneclick/base/common_dict_file.py index 415c5a6f912829851dd928478faadfaaa08b268d..a624aeba4d42780469276198ac32b35e366373bf 100644 --- a/tools/oneclick/base/common_dict_file.py +++ b/tools/oneclick/base/common_dict_file.py @@ -195,7 +195,7 @@ class CommonDictFile: def insert_key_in_dict_file_before_another_key(self, filePathName, key, value, beforeKey): """Write a new key = value pair in the filePathName file just before another existing beforeKey.""" - # Read dict file into string and insert new key = value pair at insertLineNr + # Read dict file into string and insert new key = value pair before beforeKey dict_string = '' with open(filePathName, 'r') as fp: for line in fp: @@ -207,7 +207,39 @@ class CommonDictFile: with open(filePathName, 'w') as fp: fp.write(dict_string) - def change_key_in_dict_file(self, filePathName, key, value): + def remove_key_from_dict_file(self, filePathName, key): + """Remove a key and value pair from the dictfile.""" + # Read dict file into string and skip the lines of the key = value pair that must be removed + dict_string = '' + with open(filePathName, 'r') as fp: + remove_line = False + for line in fp: + if remove_line==True: + if line.find(self.CDF_SEPARATOR)>=0: + remove_line = False # found next key, which indicates the end of remove key-value pair + if line.find(key)==0: + remove_line = True # found key that has to be removed + if not remove_line: + dict_string += line + # Write string to dict file + with open(filePathName, 'w') as fp: + fp.write(dict_string) + + def rename_key_in_dict_file(self, filePathName, old_key, new_key): + """Write new key name for old_key in the filePathName file.""" + # Read dict file into string + with open(filePathName, 'r') as fp: + dict_string = fp.read() + # Rename old_key in dict string + old_key_start = dict_string.find(old_key) # find old_key + if old_key_start>0: + separator_start = dict_string.find(self.CDF_SEPARATOR, old_key_start) # find separator + dict_string = dict_string[0:old_key_start] + new_key + ' ' + dict_string[separator_start:] # replace old key by new key + # Write string to dict file + with open(filePathName, 'w') as fp: + fp.write(dict_string) + + def change_key_value_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: