Skip to content

Commit 6d2554b

Browse files
committed
factor out table parsers into tsv and votable
1 parent 568f252 commit 6d2554b

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

astroquery/vizier/core.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -601,51 +601,18 @@ def _parse_result(self, response, get_catalog_names=False, verbose=False,
601601
table_list : `astroquery.utils.TableList` or str
602602
If there are errors in the parsing, then returns the raw results as a string.
603603
"""
604-
if not verbose:
605-
commons.suppress_vo_warnings()
606-
try:
607-
tf = six.BytesIO(response.content)
608-
609-
if invalid == 'mask':
610-
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
611-
elif invalid == 'warn':
612-
try:
613-
vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
614-
except Exception as ex:
615-
warnings.warn("VOTABLE parsing raised exception: {0}".format(ex))
616-
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
617-
elif invalid == 'raise':
618-
vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
619-
else:
620-
raise ValueError("Invalid keyword 'invalid'. Must be raise, mask, or warn")
621-
622-
if get_catalog_names:
623-
return dict([(R.name, R) for R in vo_tree.resources])
624-
else:
625-
table_dict = OrderedDict()
626-
for t in vo_tree.iter_tables():
627-
if len(t.array) > 0:
628-
if t.ref is not None:
629-
name = vo_tree.get_table_by_id(t.ref).name
630-
else:
631-
name = t.name
632-
if name not in table_dict.keys():
633-
table_dict[name] = []
634-
table_dict[name] += [t.to_table()]
635-
for name in table_dict.keys():
636-
if len(table_dict[name]) > 1:
637-
table_dict[name] = tbl.vstack(table_dict[name])
638-
else:
639-
table_dict[name] = table_dict[name][0]
640-
return commons.TableList(table_dict)
641-
642-
except Exception as ex:
643-
self.response = response
644-
self.table_parse_error = ex
645-
raise TableParseError("Failed to parse VIZIER result! The raw response can be found "
646-
"in self.response, and the error in self.table_parse_error."
647-
" The attempted parsed result is in self.parsed_result.\n"
648-
"Exception: " + str(self.table_parse_error))
604+
if response.content[:5] == '<?xml':
605+
try:
606+
return parse_vizier_votable(response.content, verbose=verbose)
607+
except Exception as ex:
608+
self.response = response
609+
self.table_parse_error = ex
610+
raise TableParseError("Failed to parse VIZIER result! The raw response can be found "
611+
"in self.response, and the error in self.table_parse_error."
612+
" The attempted parsed result is in self.parsed_result.\n"
613+
"Exception: " + str(self.table_parse_error))
614+
elif response.content[:5] == '#\n# ':
615+
return parse_vizier_tsvfile(data, verbose=verbose)
649616

650617
@property
651618
def valid_keywords(self):
@@ -659,7 +626,7 @@ def valid_keywords(self):
659626

660627
return self._valid_keyword_dict
661628

662-
def parse_vizier_tsvfile(data):
629+
def parse_vizier_tsvfile(data, verbose=False):
663630
"""
664631
Parse a Vizier-generated list of tsv data tables into a list of astropy
665632
Tables.
@@ -679,6 +646,45 @@ def parse_vizier_tsvfile(data):
679646
a,b in split_limits]
680647
return tables
681648

649+
def parse_vizier_votable(data, verbose=False):
650+
if not verbose:
651+
commons.suppress_vo_warnings()
652+
653+
tf = BytesIO(data)
654+
655+
if invalid == 'mask':
656+
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
657+
elif invalid == 'warn':
658+
try:
659+
vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
660+
except Exception as ex:
661+
warnings.warn("VOTABLE parsing raised exception: {0}".format(ex))
662+
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
663+
elif invalid == 'raise':
664+
vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
665+
else:
666+
raise ValueError("Invalid keyword 'invalid'. Must be raise, mask, or warn")
667+
668+
if get_catalog_names:
669+
return dict([(R.name, R) for R in vo_tree.resources])
670+
else:
671+
table_dict = OrderedDict()
672+
for t in vo_tree.iter_tables():
673+
if len(t.array) > 0:
674+
if t.ref is not None:
675+
name = vo_tree.get_table_by_id(t.ref).name
676+
else:
677+
name = t.name
678+
if name not in table_dict.keys():
679+
table_dict[name] = []
680+
table_dict[name] += [t.to_table()]
681+
for name in table_dict.keys():
682+
if len(table_dict[name]) > 1:
683+
table_dict[name] = tbl.vstack(table_dict[name])
684+
else:
685+
table_dict[name] = table_dict[name][0]
686+
return commons.TableList(table_dict)
687+
682688

683689
def _parse_angle(angle):
684690
"""

0 commit comments

Comments
 (0)