Skip to content

Commit

Permalink
feat: validation des codes géographiques (#5)
Browse files Browse the repository at this point in the history
Cette PR a pour but d'initialiser la validation de codes géographiques.

- Codes fantoir
- Communes, code postaux, code insee
- Cantons
- Départements, numéros de département
- Régions, code région
- Pays, codes pays iso2 et iso3

---------

Co-authored-by: Amélie Rondot <[email protected]>
  • Loading branch information
pierrecamilleri and amelie-rondot authored Apr 18, 2024
1 parent 7c7e6b2 commit fa872be
Show file tree
Hide file tree
Showing 36 changed files with 141,988 additions and 28 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ flake8:
.PHONY: pyright
pyright:
@ poetry run pyright ./src

.PHONY: test
test: ## Runs all tests
@ poetry run pytest
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ pytest = "^7.4.4"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
]
6 changes: 5 additions & 1 deletion pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"venv":".venv", "venvPath":"."}
{
"venv":".venv",
"venvPath":".",
"ignore": ["./src/frformat/code_postal_set.py"]
}
23 changes: 19 additions & 4 deletions src/frformat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# flake8: noqa
from .custom_format import *
from .french_gps_coordinates import FrenchGPSCoordinates as FrenchGPSCoordinates
from .custom_format import * # isort:skip

from .geo.canton import Canton as Canton
from .geo.code_commune_insee import CodeCommuneInsee as CodeCommuneInsee
from .geo.code_fantoir import CodeFantoir as CodeFantoir
from .geo.code_pays import CodePaysISO2 as CodePaysISO2
from .geo.code_pays import CodePaysISO3 as CodePaysISO3
from .geo.code_postal import CodePostal as CodePostal
from .geo.code_region import CodeRegion as CodeRegion
from .geo.commune import Commune as Commune
from .geo.coordonnees_gps_francaises import (
CoordonneesGPSFrancaises as CoordonneesGPSFrancaises,
)
from .geo.departement import Departement as Departement
from .geo.numero_departement import NumeroDepartement as NumeroDepartement
from .geo.pays import Pays as Pays
from .geo.region import Region as Region
from .nomenclature_acte_format import NomenclatureActe as NomenclatureActe
from .siren_format import Siren as Siren
from .siret_format import Siret as Siret
from .siren import Siren as Siren
from .siret import Siret as Siret
16 changes: 16 additions & 0 deletions src/frformat/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import re


def normalize_text(val: str) -> str:
val = val.lower()
val = val.replace("-", " ")
val = val.replace("_", " ")
val = val.replace("'", " ")
val = val.replace(",", " ")
val = val.replace(" ", " ")
val = re.sub(r"[èéêë]", "e", val)
val = re.sub(r"[àáâãäå]", "a", val)
val = re.sub(r"[ìíîï]", "i", val)
val = re.sub(r"[òóôõö]", "o", val)
val = re.sub(r"[ùúûü]", "u", val)
return val
4 changes: 2 additions & 2 deletions src/frformat/custom_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ def format(cls, value: str) -> str:
return cls._format(value)

@classmethod
@abstractmethod
def _format(cls, value: str) -> str:
...
# Specify the default behaviour
return value
39 changes: 39 additions & 0 deletions src/frformat/enum_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Optional, Set

from frformat import CustomFormat
from frformat.common import normalize_text


def new(
name: str,
description: str,
strict_enum: Set[str],
lenient_enum: Optional[Set[str]] = None,
):
if not lenient_enum:
lenient_enum = {normalize_text(e) for e in strict_enum}

class EnumFormat(CustomFormat):
"""Checks if a value is in a given list
May check with or without string normalization with the "strict"
validation.
"""

@classmethod
def name(cls) -> str:
return name

@classmethod
def description(cls) -> str:
return description

@classmethod
def is_valid(cls, value: str, strict: bool = True) -> bool:
if not strict:
norm_value = normalize_text(value)
return norm_value in lenient_enum
else:
return value in strict_enum

return EnumFormat
9 changes: 9 additions & 0 deletions src/frformat/geo/canton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from frformat import enum_format
from frformat.geo.canton_set import CANTON_SET

name = "Nom de canton"
description = (
"Vérifie que le nom de canton est un canton ou pseudo-canton français valide"
)

Canton = enum_format.new(name, description, CANTON_SET)
Loading

0 comments on commit fa872be

Please sign in to comment.