Skip to content

Commit

Permalink
Make DS9RegionParserError part of the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
cdeil committed Dec 10, 2016
1 parent 81d6439 commit b57b992
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 10 additions & 5 deletions regions/io/read_ds9.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'ds9_string_to_region_list',
'ds9_region_list_to_objects',
'DS9RegionParserWarning',
'DS9RegionParserError',
]


Expand All @@ -27,11 +28,13 @@ class DS9RegionParserWarning(AstropyUserWarning):
warnings
"""


class DS9RegionParserError(ValueError):
"""
A generic error class for DS9 region parsing
"""


def read_ds9(filename, errors='strict'):
"""Read a ds9 region file in as a list of region objects.
Expand Down Expand Up @@ -220,7 +223,7 @@ def ds9_region_list_to_objects(region_list):
" is not recognized."
.format(str(coord_list), region_type),
DS9RegionParserWarning
)
)
continue
reg.visual = {key: meta[key] for key in meta.keys() if key in viz_keywords}
reg.meta = {key: meta[key] for key in meta.keys() if key not in viz_keywords}
Expand Down Expand Up @@ -345,7 +348,7 @@ def line_parser(line, coordsys=None, errors='strict'):
include : bool
Whether the region is included (False -> excluded)
"""
if errors not in ('strict','ignore','warn'):
if errors not in ('strict', 'ignore', 'warn'):
raise ValueError("``errors`` must be one of strict, ignore, or warn")

if '# Region file format' in line and line[0] == '#':
Expand Down Expand Up @@ -415,7 +418,8 @@ def line_parser(line, coordsys=None, errors='strict'):
parsed = type_parser(coords_etc, language_spec[region_type],
coordsys)
if region_type == 'polygon':
# have to special-case polygon in the phys coord case b/c can't typecheck when iterating as in sky coord case
# have to special-case polygon in the phys coord case
# b/c can't typecheck when iterating as in sky coord case
coord = PixCoord(parsed[0::2], parsed[1::2])
parsed_return = [coord]
else:
Expand All @@ -434,7 +438,7 @@ def line_parser(line, coordsys=None, errors='strict'):
# # Region file format: DS9 version 4.1
# That behavior is unfortunate, but there's not a great workaround
# except to let the user set `errors='ignore'`
if errors in ('warn','strict'):
if errors in ('warn', 'strict'):
message = ("Region type '{0}' was identified, but it is not one of "
"the known region types.".format(region_type))
if errors == 'strict':
Expand Down Expand Up @@ -504,10 +508,11 @@ def meta_parser(meta_str):
meta_token_split = [x for x in meta_token.split(meta_str.strip()) if x]
equals_inds = [i for i, x in enumerate(meta_token_split) if x is '=']
result = {meta_token_split[ii - 1]:
" ".join(meta_token_split[ii + 1:jj - 1 if jj is not None else None])
" ".join(meta_token_split[ii + 1:jj - 1 if jj is not None else None])
for ii, jj in zip(equals_inds, equals_inds[1:] + [None])}

return result


def global_parser(line):
return "global", meta_parser(line)
2 changes: 2 additions & 0 deletions regions/io/tests/test_ds9_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def test_ds9_io(tmpdir):
assert_allclose(reg.center.dec.deg, 43)
assert_allclose(reg.radius.value, 3)


def test_missing_region_warns():
ds9_str = '# Region file format: DS9 astropy/regions\nfk5\ncircle(42.0000,43.0000,3.0000)\nnotaregiontype(blah)'

Expand All @@ -144,6 +145,7 @@ def test_missing_region_warns():
assert len(ASWarn) == 1
assert "Region type 'notaregiontype'" in str(ASWarn[0].message)


def test_global_parser():
""" Check that the global_parser does what's expected """
# have to force "str" here because unicode_literals makes this string a
Expand Down

0 comments on commit b57b992

Please sign in to comment.