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

Now support dict data before # comment in line. Added must_exist=False option...

Now support dict data before # comment in line. Added must_exist=False option to get_key_values() and get_key_value().
parent 21d9d872
Branches
Tags v0.5.10
No related merge requests found
...@@ -52,13 +52,17 @@ ...@@ -52,13 +52,17 @@
the '=' character can not be used in keys. The '=' can be used in values, 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. 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 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 '#' or multiple lines.
and will get stripped.
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 # This is a comment section
# a key starts on a new line and extends until the '=' # a key starts on a new line and extends until the '='
# a key and its values are separated by '=' # 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 key =string
key = string key = string
...@@ -176,24 +180,25 @@ class CommonDictFile: ...@@ -176,24 +180,25 @@ class CommonDictFile:
key = '' key = ''
value = '' value = ''
for line in fp: for line in fp:
if line.find(self.CDF_COMMENT)==-1: # Strip lines with comment ln = line.split(self.CDF_COMMENT, 1) # Strip comment from line by if necessary splitting it at the first CDF_COMMENT
section_begin= line.find('[') # Search for [section] header in this line ln = ln[0] # Continue with the beginning of the line before the first CDF_COMMENT
section_end = line.find(']') section_begin= ln.find('[') # Search for [section] header in this line
section_end = ln.find(']')
if section_begin>=0 and section_end>section_begin: if section_begin>=0 and section_end>section_begin:
section_header = line[section_begin+1:section_end].strip() # new section header section_header = ln[section_begin+1:section_end].strip() # new section header
section_headers.append(section_header) section_headers.append(section_header)
include_section = True # default include this new section include_section = True # default include this new section
if self.fileSections!=None: if self.fileSections!=None:
if section_header not in self.fileSections: if section_header not in self.fileSections:
include_section = False # skip this section include_section = False # skip this section
else: else:
key_end = line.find(self.CDF_SEPARATOR) # Search for key in this line key_end = ln.find(self.CDF_SEPARATOR) # Search for key in this line
if key_end>=0: if key_end>=0:
key = line[0:key_end].strip() # new key key = ln[0:key_end].strip() # new key
value = line[key_end+1:].strip() # new value value = ln[key_end+1:].strip() # new value
else: else:
value += ' ' # replace newline by space to separate values value += ' ' # replace newline by space to separate values
value += line.strip() # append value value += ln.strip() # append value
if include_section==True and key!='': if include_section==True and key!='':
file_dict[key] = value.strip() # Update dict with key and values 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 file_dict['section_headers'] = section_headers # Add the section headers as a key-value pair to the dict
...@@ -312,22 +317,27 @@ class CommonDictFile: ...@@ -312,22 +317,27 @@ class CommonDictFile:
"""Get file path to the dictionary file location including the dictionary file name.""" """Get file path to the dictionary file location including the dictionary file name."""
return self.filePathNames[self.dicts.index(the_dict)] return self.filePathNames[self.dicts.index(the_dict)]
def get_key_values(self, key, dicts=None): 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.""" """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 if dicts==None: dicts=self.dicts
key_values = [] key_values = []
for fd in cm.listify(dicts): for fd in cm.listify(dicts):
if key in fd: if key in fd:
key_values.append(fd[key]) key_values.append(fd[key])
elif must_exist:
sys.exit('Error : Key %s does not exist in the dictionary:\n%s.' % (key, fd))
else: else:
key_values.append(None) key_values.append(None)
return cm.unlistify(key_values) return cm.unlistify(key_values)
def get_key_value(self, key, dict=None): 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.""" """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 dict==None: dict=self.dicts[0] if key in the_dict:
if key in dict: key_value = the_dict[key]
key_value = dict[key] elif must_exist:
sys.exit('Error : Key %s does not exist in the dictionary:\n%s.' % (key, fd))
else: else:
key_value = None key_value = None
return key_value return key_value
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment