Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add breadcrumbs #86

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/catleg/catleg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
app.add_typer(lf, name="lf", help="Commands for querying the raw Legifrance API")


def _article(aid_or_url: str):
def _article(aid_or_url: str, breadcrumbs: bool):
article_id = article_id_or_url(aid_or_url)
if article_id is None:
raise ValueError(f"Sorry, I do not know how to process {aid_or_url}")

skel = asyncio.run(askel(article_id))
skel = asyncio.run(askel(article_id, breadcrumbs=breadcrumbs))
return skel


Expand All @@ -35,14 +35,20 @@ def article(
help="An article ID or Legifrance URL, for instance 'LEGIARTI000033971416' "
"or 'https://www.legifrance.gouv.fr/codes/article_lc/LEGIARTI000033971416'."
),
]
],
nb: Annotated[
bool,
typer.Option(
help="If specified, do not print breadcrumbs (table of contents headers)"
),
] = False,
):
"""
Output an article.
By default, outputs markdown-formatted text.
"""

print(_article(aid_or_url))
print(_article(aid_or_url, not nb))


@app.command()
Expand Down
25 changes: 24 additions & 1 deletion src/catleg/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,43 @@ async def markdown_skeleton(textid: str, sectionid: str) -> str:
return "\n\n".join(parts)


async def article_skeleton(articleid: str) -> str:
async def article_skeleton(articleid: str, breadcrumbs: bool = True) -> str:
"""
Return an article skeleton (markdown-formatted law article).

Parameters
----------
articleid: str
Legifrance article identifier
breadcrumbs: bool
if True, emits breadcrumbs (table of contents headers) before
outputting the article itself

Returns
-------
str
Markdown-formatted article
"""
back = get_backend("legifrance")
# This uses the Legifrance API directly, not the backend abstraction
raw_article_json = await back.query_article_legi(articleid)
return _article_skeleton(raw_article_json=raw_article_json, breadcrumbs=breadcrumbs)


# separate network calls and processing to ease unit testing
def _article_skeleton(raw_article_json, breadcrumbs: bool = True):
article_json = raw_article_json["article"]
article = _article_from_legifrance_reply(raw_article_json)
if article is None:
raise RuntimeError(
"Could not extract article from json reply %s", raw_article_json
)

parts = []
if breadcrumbs:
for i, toc_entry in enumerate(article_json["context"]["titresTM"], start=1):
parts.append(f"{'#' * i} {toc_entry['titre']}")

# level: code (1) + length of section hierarchy + article (1)
level = 1 + len(article_json["context"]["titresTM"]) + 1
parts.append(f"{'#' * level} Article {article_json['num']} | {article.id}")
Expand Down
13 changes: 12 additions & 1 deletion tests/test_skeleton.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from io import StringIO

import pytest

from catleg.parse_catala_markdown import parse_catala_file
from catleg.query import _article_from_legifrance_reply
from catleg.skeleton import _formatted_atricle
from catleg.skeleton import _article_skeleton, _formatted_atricle

from .test_legifrance_queries import _json_from_test_file

Expand Down Expand Up @@ -85,3 +87,12 @@ def test_no_article_renumbering_in_catala_file_parsing():
assert "1. Le bénéfice ou revenu imposable est constitué" in article.text
assert "2. Le revenu global net annuel" in article.text
assert "3. Le bénéfice ou revenu net de chacune des catégories" in article.text


@pytest.mark.parametrize("breadcrumbs", [False, True])
def test_article_skeleton(breadcrumbs: bool):
article_json = _json_from_test_file("LEGIARTI000044983201.json")
askel = _article_skeleton(article_json)
assert "excédent du produit brut" in askel
if breadcrumbs:
assert "## Première Partie : Impôts d'État" in askel