Example #1
0
 def test_error_for_unusable_selector(self, section_11_subdivided):
     schema = EnactmentSchema()
     section = schema.load(section_11_subdivided)
     selection = TextPositionSet(TextPositionSelector(0, 10),
                                 TextPositionSelector(1000, 1010))
     with pytest.raises(ValueError):
         section.children[3].select(selection)
Example #2
0
 def test_select_nested_text_with_positions(self, section_11_subdivided):
     phrases = TextPositionSet(
         TextPositionSelector(0, 51),
         TextPositionSelector(61, 73),
         TextPositionSelector(112, 127),
     )
     schema = EnactmentSchema()
     section = schema.load(section_11_subdivided)
     section.select(phrases)
     text_sequence = section.text_sequence()
     assert str(text_sequence) == (
         "The Department of Beards may issue licenses to "
         "such…hairdressers…as they see fit…")
Example #3
0
    def test_get_passage(self, section_11_subdivided):
        """
        Use selector to get passage from Enactment without changing which part is selected.

        Checks that `.selected_text()` is the same before and after `.get_passage()`.
        """
        schema = EnactmentSchema()
        section = schema.load(section_11_subdivided)
        section.select(TextPositionSelector(61, 73))
        assert section.selected_text() == "…hairdressers…"
        passage = section.get_passage(TextPositionSelector(112, 127))
        assert passage == "…as they see fit…"
        assert section.selected_text() == "…hairdressers…"
Example #4
0
 def test_get_positions_from_quotes(self, section_11_subdivided):
     schema = EnactmentSchema()
     section = schema.load(section_11_subdivided)
     quotes = [
         TextQuoteSelector(
             exact="The Department of Beards may issue licenses to such"),
         TextQuoteSelector(exact="hairdressers",
                           suffix=", or other male grooming"),
         TextQuoteSelector(exact="as they see fit"),
     ]
     positions = section.convert_quotes_to_position(quotes)
     assert positions == TextPositionSet(
         TextPositionSelector(0, 51),
         TextPositionSelector(61, 73),
         TextPositionSelector(112, 127),
     )
Example #5
0
 def test_select_method_clears_previous_selection(self, test_client,
                                                  section_8):
     old_version = test_client.read_from_json(section_8["children"][1])
     old_selector = TextPositionSet(TextPositionSelector(start=0, end=65), )
     old_version.select(old_selector)
     assert old_version.selected_text() == (
         "Any such person issued a notice to remedy under subsection 1 must…"
     )
Example #6
0
 def select_all(self) -> None:
     """Select all text of Enactment."""
     if self.content:
         self._selection = TextPositionSet(
             TextPositionSelector(0, len(self.content)))
     else:
         self._selection = TextPositionSet()
     for child in self._children:
         child.select_all()
Example #7
0
 def limit_selection(
     self,
     start: Optional[Union[int, str]] = None,
     end: Optional[Union[int, str]] = None,
 ) -> None:
     """Limit selection to the range defined by start and end points."""
     if (start is not None) or (end is not None):
         selector = TextPositionSelector.from_text(text=self.text,
                                                   start=start,
                                                   end=end)
         new_selection = self.tree_selection() & selector
         self.select_from_text_positions(new_selection)
Example #8
0
 def convert_selection_to_set(
     self,
     selection: Union[bool, str, TextPositionSelector, TextPositionSet,
                      TextQuoteSelector, Sequence[TextQuoteSelector], ],
 ) -> TextPositionSet:
     """Create a TextPositionSet from a different selection method."""
     if selection is True:
         return TextPositionSet(
             TextPositionSelector(0,
                                  len(self.content) + 1,
                                  include_end=False))
     factory = TextPositionSetFactory(self.text)
     return factory.from_selection(selection)
Example #9
0
    def select_from_text_positions_without_nesting(
        self, selections: Union[List[TextPositionSelector], RangeSet]
    ) -> TextPositionSet:
        r"""
        Move selectors from `selection` to `self._selection` and return any that can't be used.

        Replaces any preexisting _selection attribute on this Enactment object.

        :param selection:
            A TextPositionSet of TextPositionSelectors to apply to this Enactment.

        :returns:
            Any unused selections (beyond the end of the node content)
        """
        self._selection: List[TextPositionSelector] = []

        if isinstance(selections, RangeSet):
            selections = selections.ranges()
        selections = deque(selections)

        while selections and selections[0].start < len(self.content):
            current = selections.popleft()
            if current.end < len(self.content):
                self._selection.append(current)
            else:
                self._selection.append(
                    TextPositionSelector(start=current.start,
                                         end=len(self.content)))
                if current.end > self.padded_length:
                    selections.appendleft(
                        TextPositionSelector(start=self.padded_length,
                                             end=current.end))
        selection_as_set = TextPositionSet(self._selection)
        self._selection = selection_as_set.add_margin(text=self.content,
                                                      margin_width=4)
        return TextPositionSet(selections)
Example #10
0
    def test_get_recursive_selection(self, section_8, test_client):
        version = test_client.read_from_json(section_8["children"][1])
        selector = TextPositionSet(TextPositionSelector(start=0, end=65), )
        version.select(selector)
        version.children[4].select(
            "obtain a beardcoin from the Department of Beards")
        selector_set = version.tree_selection()
        ranges = selector_set.ranges()
        assert ranges[0].start == 0
        assert ranges[0].end == 65

        assert ranges[1].start == 218
        assert ranges[1].end == 266

        as_quotes = selector_set.as_quotes(version.text)
        assert as_quotes[
            1].exact == "obtain a beardcoin from the Department of Beards"
Example #11
0
    def test_add_selection_from_child_node(self, section_8, test_client):
        parent_version = test_client.read_from_json(section_8["children"][1])
        parent_selector = TextPositionSet(
            TextPositionSelector(start=0, end=65), )
        parent_version.select(parent_selector)
        parent_version.children[4].select(
            "obtain a beardcoin from the Department of Beards")
        assert parent_version.selected_text() == (
            "Any such person issued a notice to remedy under subsection 1 must…"
            "obtain a beardcoin from the Department of Beards…")

        child_version = test_client.read_from_json(
            section_8["children"][1]["children"][3])
        child_version.select()

        combined = parent_version + child_version

        assert combined.selected_text() == (
            "Any such person issued a notice to remedy under subsection 1 must…"
            "remove the beard with a laser, or "
            "obtain a beardcoin from the Department of Beards…")
Example #12
0
 def test_select_near_end_of_section(self, test_client):
     amendment = test_client.read(query="/us/const/amendment/XIV")
     selector = TextPositionSelector(1920, 1980)
     amendment.select(selector)
     assert "The validity of the public debt" in amendment.selected_text()
Example #13
0
 def test_select_with_position_selector(self, section_11_together):
     schema = EnactmentSchema()
     section = schema.load(section_11_together)
     section.select(TextPositionSelector(start=29, end=43))
     assert section.selected_text() == "…issue licenses…"