def parse_grid(grid_data):
try:<EOL><INDENT>grid_parts = NEWLINE_RE.split(grid_data)<EOL>if len(grid_parts) < <NUM_LIT:2>:<EOL><INDENT>raise ZincParseException('<STR_LIT>',<EOL>grid_data, <NUM_LIT:1>, <NUM_LIT:1>)<EOL><DEDENT>grid_meta_str = grid_parts.pop(<NUM_LIT:0>)<EOL>col_meta_str = grid_parts.pop(<NUM_LIT:0>)<EOL>ver_match = VERSION_RE.match(grid_meta_str)<EOL>if ver_match is None:<EOL><INDENT>raise ZincParseException(<EOL>'<STR_LIT>' % grid_meta_str,<EOL>grid_data, <NUM_LIT:1>, <NUM_LIT:1>)<EOL><DEDENT>version = Version(ver_match.group(<NUM_LIT:1>))<EOL>try:<EOL><INDENT>grid_meta = hs_gridMeta[version].parseString(grid_meta_str, parseAll=True)[<NUM_LIT:0>]<EOL><DEDENT>except pp.ParseException as pe:<EOL><INDENT>raise ZincParseException(<EOL>'<STR_LIT>' % pe,<EOL>grid_data, <NUM_LIT:1>, pe.col)<EOL><DEDENT>except: <EOL><INDENT>LOG.debug('<STR_LIT>', grid_meta_str)<EOL>raise<EOL><DEDENT>try:<EOL><INDENT>col_meta = hs_cols[version].parseString(col_meta_str, parseAll=True)[<NUM_LIT:0>]<EOL><DEDENT>except pp.ParseException as pe:<EOL><INDENT>raise ZincParseException(<EOL>'<STR_LIT>'% reformat_exception(pe, <NUM_LIT:2>),<EOL>grid_data, <NUM_LIT:2>, pe.col)<EOL><DEDENT>except: <EOL><INDENT>LOG.debug('<STR_LIT>', col_meta_str)<EOL>raise<EOL><DEDENT>row_grammar = hs_row[version]<EOL>def _parse_row(row_num_and_data):<EOL><INDENT>(row_num, row) = row_num_and_data<EOL>line_num = row_num + <NUM_LIT:3><EOL>try:<EOL><INDENT>return dict(zip(col_meta.keys(),<EOL>row_grammar.parseString(row, parseAll=True)[<NUM_LIT:0>].asList()))<EOL><DEDENT>except pp.ParseException as pe:<EOL><INDENT>raise ZincParseException(<EOL>'<STR_LIT>'% reformat_exception(pe, line_num),<EOL>grid_data, line_num, pe.col)<EOL><DEDENT>except: <EOL><INDENT>LOG.debug('<STR_LIT>', row)<EOL>raise<EOL><DEDENT><DEDENT>g = Grid(version=grid_meta.pop('<STR_LIT>'),<EOL>metadata=grid_meta,<EOL>columns=list(col_meta.items()))<EOL>g.extend(map(_parse_row, filter(lambda gp : bool(gp[<NUM_LIT:1>]), enumerate(grid_parts))))<EOL>return g<EOL><DEDENT>except:<EOL><INDENT>LOG.debug('<STR_LIT>', grid_data)<EOL>raise<EOL><DEDENT>
Parse the incoming grid.