diff --git a/swmmio/utils/text.py b/swmmio/utils/text.py index a04913b..22cd16e 100644 --- a/swmmio/utils/text.py +++ b/swmmio/utils/text.py @@ -216,23 +216,25 @@ def get_inp_sections_details(inp_path, include_brackets=False): found_sects = OrderedDict() with open(inp_path) as f: - for line in f: - sect_not_found = True - for sect_id, data in INP_OBJECTS.items(): - # find the start of an INP section - search_tag = format_inp_section_header(sect_id) - if search_tag.lower() in line.lower(): - if include_brackets: - sect_id = '[{}]'.format(sect_id.upper()) - found_sects[sect_id.upper()] = data - sect_not_found = False - break - if sect_not_found: - if '[' and ']' in line: - h = line.strip() - if not include_brackets: - h = h.replace('[', '').replace(']', '') - found_sects[h] = OrderedDict(columns=['blob']) + txt = f.read() + section_dict = { + key: txt.find("[{}]".format(key)) for key in INP_OBJECTS.keys() + if txt.find("[{}]".format(key)) >= 0 + } + section_dict = sorted(section_dict, key=section_dict.get) + bracketed_words = re.findall(r"\[([A-Za-z0-9_]+)\]", txt) + + for sect in bracketed_words: + if sect not in section_dict: + if not include_brackets: + h = sect.replace('[', '').replace(']', '') + found_sects[h] = OrderedDict(columns=['blob']) + else: + if include_brackets: + sect_id = '[{}]'.format(sect.upper()) + else: + sect_id = sect.upper() + found_sects[sect_id] = INP_OBJECTS[sect] # make necessary adjustments to columns that change based on options ops_cols = INP_OBJECTS['OPTIONS']['columns']