Skip to content

Commit

Permalink
feat: types de milieux
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Feb 17, 2024
1 parent cb6f86a commit 6f62dda
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 14 additions & 1 deletion alexi/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
from typing import Optional

from .analyse import PALIERS, Document
from .analyse import PALIERS, Document, MILIEU, MTYPE

LOGGER = logging.getLogger("link")

Expand All @@ -23,6 +23,7 @@
re.IGNORECASE,
)
REG_RE = re.compile(r"règlement[^\d]+(?P<reg>[\d\.A-Z-]+)", re.IGNORECASE)
MILIEU_RE = re.compile(rf"{MILIEU}\s+(?P<mtype>{MTYPE})", re.IGNORECASE | re.VERBOSE)
PALIER_IDX = {palier: idx for idx, palier in enumerate(PALIERS)}


Expand Down Expand Up @@ -61,10 +62,22 @@ def __call__(
self, text: str, srcpath: str = "", doc: Optional[Document] = None
) -> str:
url = self.resolve_external(text)
if url:
return url
url = self.resolve_zonage(text, srcpath)
if url:
return url
return self.resolve_internal(text, srcpath, doc)

def resolve_zonage(self, text: str, srcpath: str) -> Optional[str]:
m = MILIEU_RE.search(text)
if m is None:
return None
milieu = self.metadata["zonage"]["milieu"].get(m.group("mtype"))
if milieu is None:
return None
return os.path.relpath(f"../{milieu['url']}/index.html", srcpath)

def resolve_absolute_internal(
self, docpath: str, secpath: str, srcpath: str
) -> Optional[str]:
Expand Down
8 changes: 5 additions & 3 deletions test/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,22 @@ def test_laws(test_input, expected):
"Règlement de zonage",
"../index.html#20231213-Codification-administrative-Rgl-1314-2021-Z",
),
# NOTE: sous-section not expected to work yet
(
"section 3 du chapitre 5 du Règlement de zonage 1314-2021-Z",
"../20231213-Codification-administrative-Rgl-1314-2021-Z/Chapitre/5/Section/3/index.html",
),
# TODO: links to milieux, usages, etc
(
"type de milieu T5.2",
"../20231213-Codification-administrative-Rgl-1314-2021-Z/Chapitre/7/Section/6/SousSection/_89/index.html",
),
# FIXME: Also test invalid links somehow!
]


@pytest.mark.parametrize("test_input,expected", BYLAWS)
def test_bylaws(test_input, expected):
r = Resolver(METADATA)
assert r.resolve_internal(test_input, ".") == expected
assert r(test_input, ".") == expected


with open(DATADIR / "lotissement.json", "rt") as infh:
Expand Down

0 comments on commit 6f62dda

Please sign in to comment.