Skip to content

Commit

Permalink
Refactor legacy area file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Nov 19, 2023
1 parent 28c7a2d commit a960a79
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions pyresample/area_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import os
import pathlib
import warnings
from typing import Any, Union
from typing import Any, Iterable, Union

import numpy as np
import yaml
Expand Down Expand Up @@ -285,35 +285,23 @@ def _parse_legacy_area_file(area_file_name, *regions):
"""Parse area information from a legacy area file."""
area_file = _read_legacy_area_file_lines(area_file_name)
area_list = list(regions)
if not area_list:
select_all_areas = True
area_defs = []
else:
select_all_areas = False
area_defs = [None for i in area_list]
select_all_areas = bool(not area_list)
area_defs = [] if select_all_areas else [None for area_id in area_list]

# Extract area from file
in_area = False
for line in area_file:
if not in_area:
if 'REGION' in line and not line.strip().startswith('#'):
area_id = line.replace('REGION:', ''). \
replace('{', '').strip()
if area_id in area_list or select_all_areas:
in_area = True
area_content = ''
elif '};' in line:
in_area = False
try:
if select_all_areas:
area_defs.append(_create_area(area_id, area_content))
else:
area_defs[area_list.index(area_id)] = _create_area(area_id,
area_content)
except KeyError:
raise ValueError('Invalid area definition: %s, %s' % (area_id, area_content))
if "REGION" not in line or line.strip().startswith("#"):
continue

area_id = line.replace('REGION:', '').replace('{', '').strip()
if area_id not in area_list and not select_all_areas:
continue

area_def = _parse_one_legacy_area_lines(area_file, area_id)
if select_all_areas:
area_defs.append(area_def)
else:
area_content += line
area_defs[area_list.index(area_id)] = area_def

# Check if all specified areas were found
if not select_all_areas:
Expand All @@ -324,6 +312,18 @@ def _parse_legacy_area_file(area_file_name, *regions):
return area_defs


def _parse_one_legacy_area_lines(area_file: Iterable[str], area_id: str):
area_content = ""
for line in area_file:
if '};' in line:
try:
return _create_area(area_id, area_content)
except KeyError:
raise ValueError('Invalid area definition: %s, %s' % (area_id, area_content))

Check warning on line 322 in pyresample/area_config.py

View check run for this annotation

Codecov / codecov/patch

pyresample/area_config.py#L321-L322

Added lines #L321 - L322 were not covered by tests
else:
area_content += line


def _create_area(area_id, area_content):
"""Parse area configuration."""
from configobj import ConfigObj
Expand Down

0 comments on commit a960a79

Please sign in to comment.