Skip to content

Commit

Permalink
let selector without "exact" omit text source
Browse files Browse the repository at this point in the history
  • Loading branch information
mscarey committed Jun 6, 2019
1 parent 5309567 commit 8d2f161
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 51 deletions.
3 changes: 1 addition & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions authorityspoke/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def wrapper(
if factor_record is None:
return None, mentioned

new_factor = func(cls, factor_record, mentioned=[], regime=regime)

mentioned = mentioned or []

new_factor = func(cls, factor_record, mentioned=mentioned, regime=regime)

if not new_factor.name and (
not hasattr(new_factor, "generic") or not new_factor.generic
):
Expand Down
5 changes: 3 additions & 2 deletions authorityspoke/enactments.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,11 @@ def __post_init__(self):
object.__setattr__(
self, "code", self.regime.get_code(self.selector.path)
)
elif self.selector.code:
object.__setattr__(self, "code", self.selector.code)
else:
raise ValueError("'code' and 'regime' cannot both be None")
if not self.selector.exact:
self.selector.set_exact_from_source(self.code)
object.__delattr__(self, "regime")

@property
def effective_date(self):
Expand Down
2 changes: 2 additions & 0 deletions authorityspoke/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ def collection_from_dict(
lists ``mentioned_entities`` followed by a
series of :class:`Rule`\s
:param mentioned:
:param regime:
:returns:
Expand Down
79 changes: 39 additions & 40 deletions authorityspoke/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,58 +46,57 @@ class TextQuoteSelector:
source: Optional[Union["Regime", "Code"]] = None

def __post_init__(self):
def exact_from_ends(text: str) -> str:
"""
Locates and returns an exact quotation from a text passage given the
beginning and end of the passage.
:param text:
the passage where an exact quotation needs to be located.
:returns:
the exact quotation from the text passage
"""

if self.prefix:
left_end: int = text.find(self.prefix)
if left_end == -1:
raise ValueError(
f"'prefix' value '{self.prefix}' not found in '{text}'"
)
left_end += len(self.prefix)
else:
left_end = 0
if self.suffix:
right_end: Optional[int] = text.find(self.suffix)
else:
right_end = None
if right_end == -1:
raise ValueError(
f"'suffix' value '{self.suffix}' not found in '{text}'"
)
return text[left_end:right_end]

if self.path and not self.path.startswith("/"):
object.__setattr__(self, "path", "/" + self.path)
if self.path and self.path.endswith("/"):
object.__setattr__(self, "path", self.path.rstrip("/"))

if not self.exact:
selection = self.code.select_text(self)
object.__setattr__(self, "exact", exact_from_ends(selection))
if self.source:
self.set_exact_from_source(self.source)

@property
def code(self) -> "Code":
object.__delattr__(self, "source")

def set_exact_from_source(self, source: Union["Regime", "Code"]) -> None:
if source.__class__.__name__ == "Regime":
code = source.get_code(self.path)
elif self.source.__class__.__name__ == "Code":
code = source
else:
return None

selection = code.select_text(self)
object.__setattr__(self, "exact", self.exact_from_ends(selection))

def exact_from_ends(self, text: str) -> str:
"""
Locates and returns an exact quotation from a text passage given the
beginning and end of the passage.
:param text:
the passage where an exact quotation needs to be located.
:returns:
A :class:`.Code` object associated with this selector
the exact quotation from the text passage
"""
if self.source.__class__.__name__ == "Regime":
return self.source.get_code(self.path)
elif self.source.__class__.__name__ == "Code":
return self.source

if self.prefix:
left_end: int = text.find(self.prefix)
if left_end == -1:
raise ValueError(
f"'prefix' value '{self.prefix}' not found in '{text}'"
)
left_end += len(self.prefix)
else:
return None
left_end = 0
if self.suffix:
right_end: Optional[int] = text.find(self.suffix)
else:
right_end = None
if right_end == -1:
raise ValueError(f"'suffix' value '{self.suffix}' not found in '{text}'")
return text[left_end:right_end]

@property
def json(self):
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,12 @@ def make_enactment(make_regime) -> Dict[str, Enactment]:
),
source=make_regime
),
regime=make_regime
),
"fourth_a": Enactment(
selector=TextQuoteSelector(
path="/us/const/amendment-IV"),
path="/us/const/amendment-IV",
),
regime=make_regime
),
"due_process_5": Enactment(
Expand Down
10 changes: 6 additions & 4 deletions tests/test_enactments.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ def test_implication_by_factor_fails(self, make_enactment, watt_factor):
assert not dp5 < f1

def test_constitution_effective_date(self, make_regime):
ex_post_facto_provision = Enactment.from_dict(
{"path": "/us/const/article-I/9/3"}, regime=make_regime
ex_post_facto_provision = Enactment(
selector=TextQuoteSelector(path="/us/const/article-I/9/3"),
regime=make_regime,
)
assert ex_post_facto_provision.effective_date == datetime.date(1788, 9, 13)

Expand All @@ -151,8 +152,9 @@ def test_12th_A_effective_date(self, make_regime):
This tests different parsing code because the date is
in the format "dated the 25th of September, 1804"
"""
amendment_12 = Enactment.from_dict(
{"path": "/us/const/amendment-XII"}, regime=make_regime
amendment_12 = Enactment(
selector=TextQuoteSelector(path="/us/const/amendment-XII"),
regime=make_regime,
)
assert amendment_12.effective_date == datetime.date(1804, 9, 25)

Expand Down
1 change: 1 addition & 0 deletions tests/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def test_fail_no_exact_or_source(self, make_code):
copyright_exceptions = TextQuoteSelector(
path="/us/usc/t17/s102/b", prefix="sound recordings"
)
copyright_enactment = Enactment(selector=copyright_exceptions)

def test_failed_suffix(self, make_code):
usc17 = make_code["usc17"]
Expand Down

0 comments on commit 8d2f161

Please sign in to comment.