Go to the documentation of this file.
3 Module providing the ParReader class for reading parameter files for the
4 multilevel_processor.py program.
11 __author__ =
'melliott'
13 SECTION_HEADER_TEXT =
'section'
14 VALID_KEYS = [
'deletefiles',
'overwrite',
'use_existing',
'use_ancillary',
'odir',
'ifile']
40 Adds an entry to the par file part of the passed in section dictionary.
42 if the_key
in the_dict:
43 the_dict[the_key].append(val_to_add)
45 the_dict[the_key] = [val_to_add]
49 Adds an entry to a section dictionary.
51 if sect_key !=
'main':
52 if the_key
not in the_dict:
53 the_dict[the_key] = val_to_add
55 err_msg =
'Duplicate entry found for {0} in {1}'.format(the_key, filename)
59 if the_key
not in the_dict:
60 the_dict[the_key] = val_to_add
62 err_msg =
'Duplicate entry found for {0} in {1}'.format(the_key, filename)
65 err_msg =
'Invalid entry {0} found in {1}'.format(the_key, filename)
71 Returns the section name from a line of text.
72 The line is expected to be of the form '# section SECTION_NAME' (without the quotes).
75 sect_key = re.sub(
r'^\s*\[\s*(.*?)\s*\]\s*$',
'\\1', line)
82 Returns True if a line is the header for a new section; returns False otherwise.
84 section_pattern =
r'\s*\[\s*\S+.*\]\s*'
85 compiled_pattern = re.compile(section_pattern, re.IGNORECASE)
86 if re.search(compiled_pattern, line.strip()):
93 Returns True if opt is one of the valid options for Main section.
96 if key_str
in VALID_KEYS:
102 Returns True if an entire line is a comment; returns False otherwise.
104 if line.lstrip()[0:1] ==
'#':
111 A class to perform reading of a seadas_processor.py parameter file.
113 def __init__(self, fname, acceptable_single_keys, section_converter=None):
115 Initializes the ParReader object: Confirms that the passed in file
116 exists, is a regular file, and is readable. If those are all true,
117 sets the object filename value to the passed in value.
119 if os.path.exists(fname):
120 if os.path.isfile(fname):
121 if os.access(fname, os.R_OK):
127 err_msg =
"Error! Unable to read {0}.".format(fname)
130 err_msg =
"Error! {0} is not a regular file.".format(fname)
133 err_msg =
"Error! File {0} could not be found.".format(fname)
136 def _start_new_section(self, hdr_line):
138 Initializes a new section of the dictionary to be returned.
145 err_msg =
'Error! Section name "{0}" is not recognized.'.\
148 return sect_dict, sect_key
152 Parses a parameter file, returning the contents in a dictionary of
153 dictionaries. The "outer" dictionary contains each section. The "inner"
154 dictionaries contain the parameter/value pairs.
158 with open(self.
filename,
'rt')
as par_file:
159 for line
in par_file.readlines():
166 if line.find(
'=') != -1:
167 key, val = line.split(
'=', 2)
170 val.strip().strip(
'"').strip(
"'"))
174 val.strip().strip(
'"').strip(
"'"))
192 sect_dict[key] =
'True'
194 if line.startswith(
'--'):
197 err_msg =
'Found entry "{0}" with no value in {1}'.format(line, self.
filename)
200 err_msg =
'Error in {0}, no section header found!'.\
def is_whole_line_comment(line)
list(APPEND LIBS ${PGSTK_LIBRARIES}) add_executable(atteph_info_modis atteph_info_modis.c) target_link_libraries(atteph_info_modis $
def is_section_header(line)
def add_sect_entry(filename, sect_key, the_dict, the_key, val_to_add)
def __init__(self, fname, acceptable_single_keys, section_converter=None)
def _start_new_section(self, hdr_line)
def is_key_valid(key_str)
def add_par_entry(the_dict, the_key, val_to_add)