diff --git a/tools/oneclick/base/common_dict_file.py b/tools/oneclick/base/common_dict_file.py index 7a62d475466c510876f3e4877ecb07055fc0a135..a685077d33bbc902cf4c2cae6e1b273a6efb87a1 100644 --- a/tools/oneclick/base/common_dict_file.py +++ b/tools/oneclick/base/common_dict_file.py @@ -52,13 +52,17 @@ the '=' character can not be used in keys. The '=' can be used in values, because subsequent '=' on the same line are part of the value. Every key must start on a new line. The value string can extend over one - or multiple lines. Comment lines are supported by preceding it with a '#' - and will get stripped. + or multiple lines. + + Comment in line is supported by preceding it with a '#'. The '#' and the + text after it on the line are stripped. The remainder of the line before + the '#' is still interpreted. # This is a comment section # a key starts on a new line and extends until the '=' # a key and its values are separated by '=' + key=string # this is a comment and the key is still interpreted key=string key =string key = string @@ -176,26 +180,27 @@ class CommonDictFile: key = '' value = '' for line in fp: - if line.find(self.CDF_COMMENT)==-1: # Strip lines with comment - section_begin= line.find('[') # Search for [section] header in this line - section_end = line.find(']') - if section_begin>=0 and section_end>section_begin: - section_header = line[section_begin+1:section_end].strip() # new section header - section_headers.append(section_header) - include_section = True # default include this new section - if self.fileSections!=None: - if section_header not in self.fileSections: - include_section = False # skip this section + ln = line.split(self.CDF_COMMENT, 1) # Strip comment from line by if necessary splitting it at the first CDF_COMMENT + ln = ln[0] # Continue with the beginning of the line before the first CDF_COMMENT + section_begin= ln.find('[') # Search for [section] header in this line + section_end = ln.find(']') + if section_begin>=0 and section_end>section_begin: + section_header = ln[section_begin+1:section_end].strip() # new section header + section_headers.append(section_header) + include_section = True # default include this new section + if self.fileSections!=None: + if section_header not in self.fileSections: + include_section = False # skip this section + else: + key_end = ln.find(self.CDF_SEPARATOR) # Search for key in this line + if key_end>=0: + key = ln[0:key_end].strip() # new key + value = ln[key_end+1:].strip() # new value else: - key_end = line.find(self.CDF_SEPARATOR) # Search for key in this line - if key_end>=0: - key = line[0:key_end].strip() # new key - value = line[key_end+1:].strip() # new value - else: - value += ' ' # replace newline by space to separate values - value += line.strip() # append value - if include_section==True and key!='': - file_dict[key] = value.strip() # Update dict with key and values + value += ' ' # replace newline by space to separate values + value += ln.strip() # append value + if include_section==True and key!='': + file_dict[key] = value.strip() # Update dict with key and values file_dict['section_headers'] = section_headers # Add the section headers as a key-value pair to the dict return file_dict @@ -312,22 +317,27 @@ class CommonDictFile: """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): - """Get the value of a key in the dicts or None in case the key does not exist.""" + def get_key_values(self, key, dicts=None, must_exist=False): + """Get the value of a key in the dicts, or None in case the key does not exist, or exit if the key must exist. + If no dicts are specified then default to the self.dicts of the object. + """ if dicts==None: dicts=self.dicts key_values = [] for fd in cm.listify(dicts): if key in fd: key_values.append(fd[key]) + elif must_exist: + sys.exit('Error : Key %s does not exist in the dictionary:\n%s.' % (key, fd)) else: key_values.append(None) return cm.unlistify(key_values) - def get_key_value(self, key, dict=None): - """Get the value of a key from a single dict or None in case the key does not exist.""" - if dict==None: dict=self.dicts[0] - if key in dict: - key_value = dict[key] + def get_key_value(self, key, the_dict): + """Get the value of a key from a single dict, or None in case the key does not exist, or exit if the key must exist.""" + if key in the_dict: + key_value = the_dict[key] + elif must_exist: + sys.exit('Error : Key %s does not exist in the dictionary:\n%s.' % (key, fd)) else: key_value = None return key_value