def algnexpr_spans_do_not_overlap(item, refattr): itemref = item.attributes.get(refattr) reftier = get_referred_tier(item, refattr) if not itemref or not reftier: return spans_by_id = defaultdict(Counter) for span in spans(itemref): if span in alignment_expression_delimiters: continue # schema validator should catch bad algnexprs like '+w3' if isinstance(span, str): tgt_item = reftier.get(span) if tgt_item is None: continue # this should be caught by algnexpr_ids_in_referred_tier val = tgt_item.value() if val is None: continue # does this need to be handled? item_len = len(tgt_item.value()) spans_by_id[span].update(range(0, item_len)) else: item_id, start, end = span spans_by_id[item_id].update(range(start, end)) error_spans = [ item_id for item_id, count in spans_by_id.items() if any(count[c] > 1 for c in count) ] if error_spans: return ( 'Alignment expression spans {{modal}} NOT overlap. Spans ' 'for the following <item> elements overlap: {}' .format(', '.join(error_spans)) )
def algnexpr_spans_in_aligned_item(item, refattr): itemref = item.attributes.get(refattr) reftier = get_referred_tier(item, refattr) if not itemref or not reftier: return error_spans = [] for span in spans(itemref): if span in alignment_expression_delimiters: continue # schema validator should catch bad algnexprs like '+w3' sel_delim, item_id, _range = selection_re.match(span).groups() if _range: _, start, end = span_re.match(_range).groups() start = int(start) end = int(end) else: continue tgt_item = reftier.get(item_id) if tgt_item is None: continue # this should be caught by algnexpr_ids_in_referred_tier item_len = len(tgt_item.value()) if start > item_len or end > item_len: error_spans.append(span) if error_spans: return ( 'Alignment expressions {{modal}} select spans within ' 'the aligned <item> elements. The following spans are not ' 'valid: {}'.format(', '.join(error_spans)) )
def test_spans(self): assert ref.spans('') == [] assert ref.spans('a1') == ['a1'] assert ref.spans('a1[3:5]') == ['a1[3:5]'] assert ref.spans('a1[3:5+6:7]') == ['a1[3:5]', '+', 'a1[6:7]'] assert ref.spans('a1[3:5+6:7]', keep_delimiters=False) == ['a1[3:5]', 'a1[6:7]'] assert ref.spans('a1[3:5+6:7]+a2[1:4]') == ['a1[3:5]', '+', 'a1[6:7]', '+', 'a2[1:4]'] assert ref.spans('a1 a2 a3') == ['a1', ' ', 'a2', ' ', 'a3']
def test_spans(self): self.assertEqual(ref.spans(''), []) self.assertEqual(ref.spans('a1'), ['a1']) self.assertEqual(ref.spans('a1[3:5]'), ['a1[3:5]']) self.assertEqual(ref.spans('a1[3:5+6:7]'), ['a1[3:5]', '+', 'a1[6:7]']) self.assertEqual(ref.spans('a1[3:5+6:7]', keep_delimiters=False), ['a1[3:5]', 'a1[6:7]']) self.assertEqual(ref.spans('a1[3:5+6:7]+a2[1:4]'), ['a1[3:5]', '+', 'a1[6:7]', '+', 'a2[1:4]']) self.assertEqual(ref.spans('a1 a2 a3'), ['a1', ' ', 'a2', ' ', 'a3'] )