Skip to content

Commit

Permalink
Don't crash on conversion error in convert cmd
Browse files Browse the repository at this point in the history
Fixes #200
  • Loading branch information
goodmami committed Apr 1, 2019
1 parent ed63805 commit aa573a4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
50 changes: 49 additions & 1 deletion delphin/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
import io
import json
from functools import partial
import logging

from delphin import itsdb, tsql
from delphin.mrs import xmrs
from delphin.util import safe_int, SExpr
from delphin.exceptions import PyDelphinException


logging.basicConfig()

###############################################################################
### CONVERT ###################################################################

Expand Down Expand Up @@ -91,7 +95,36 @@ def convert(path, source_fmt, target_fmt, select='result:mrs',
kwargs['predicate_modifiers'] = predicate_modifiers
kwargs['properties'] = properties

return dumps(xs, **kwargs)
# this is not a great way to improve robustness when converting
# many representations, but it'll do until v1.0.0. Also, it only
# improves robustness on the output, not the input.
# Note that all the code below is to replace the following:
# return dumps(xs, **kwargs)
head, joiner, tail = _get_output_details(target_fmt)
parts = []
if pretty_print:
joiner = joiner.strip() + '\n'
def _trim(s):
if head and s.startswith(head):
s = s[len(head):].lstrip('\n')
if tail and s.endswith(tail):
s = s[:-len(tail)].rstrip('\n')
return s
for x in xs:
try:
s = dumps([x], **kwargs)
except (PyDelphinException, KeyError, IndexError):
logging.exception('could not convert representation')
else:
s = _trim(s)
parts.append(s)
# set these after so head and tail are used correctly in _trim
if pretty_print:
if head:
head += '\n'
if tail:
tail = '\n' + tail
return head + joiner.join(parts) + tail


def _get_codec(codec, load=True):
Expand Down Expand Up @@ -146,6 +179,21 @@ def _get_codec(codec, load=True):
raise ValueError('invalid target format: ' + codec)


def _get_output_details(codec):
if codec == 'mrx':
return ('<mrs-list', '', '</mrs-list>')

elif codec == 'dmrx':
from delphin.mrs import dmrx
return ('<dmrs-list>', '', '</dmrs-list>')

elif codec in ('mrs-json', 'dmrs-json', 'eds-json'):
return ('[', ',', ']')

else:
return ('', ' ', '')


# simulate json codecs for MRS and DMRS

class _MRS_JSON(object):
Expand Down
2 changes: 1 addition & 1 deletion delphin/interfaces/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def tokens(self, tokenset='internal'):

class FieldMapper(object):
"""
A class for mapping respsonses to [incr tsdb()] fields.
A class for mapping responses to [incr tsdb()] fields.
This class provides two methods for mapping responses to fields:
Expand Down

0 comments on commit aa573a4

Please sign in to comment.