Skip to content
Draft
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
46d68a6
feat: Create a new validator with valid values getted from a url path
Sarrabah Apr 3, 2025
67145f3
test: Test a real case
Sarrabah Apr 3, 2025
1d6c038
refactor: Linting
Sarrabah Apr 3, 2025
ac68603
docs: Generate documentation to the testing validator
Sarrabah Apr 3, 2025
1659be0
refactor: Reduce the test file size
Sarrabah Apr 3, 2025
95c805d
refactor: Delete the local csv file
Sarrabah Apr 3, 2025
c425167
test: test get valid values function
Sarrabah Apr 3, 2025
0d2d290
refactor: Renaming
Sarrabah Apr 3, 2025
f86ae8e
refactor: Linting
Sarrabah Apr 3, 2025
f901028
refactor: Add comment
Sarrabah Apr 3, 2025
ff7e693
feat: Apply changes to canton
Sarrabah Apr 7, 2025
765288f
fix: Treat csv files
Sarrabah Apr 7, 2025
1e69356
test: Add more tests
Sarrabah Apr 7, 2025
ab96f18
refactor: Reorganize data files for testing
Sarrabah Apr 7, 2025
23ab4b9
test: Ignore this one
Sarrabah Apr 7, 2025
8960f68
test: Remove comment
Sarrabah Apr 7, 2025
645ce55
A void commit
Sarrabah Apr 7, 2025
601878d
test: Reorganization
Sarrabah Apr 7, 2025
fbffcc7
refactor: Linting
Sarrabah Apr 7, 2025
6f97b69
fix: Skip the csv verification
Sarrabah Apr 7, 2025
38f0410
Reorder imports
Sarrabah Apr 7, 2025
b84db05
refactor: Linting
Sarrabah Apr 7, 2025
a107581
fix: Add sleeping time
Sarrabah Apr 7, 2025
c28df74
fix: Return to the first canton code logic
Sarrabah Apr 7, 2025
cdf3d4e
refactor: Remove unused import
Sarrabah Apr 7, 2025
d7c467b
test: Use a simple incorrect url
Sarrabah Apr 7, 2025
aa50bb4
feat: Readapt canton
Sarrabah Apr 7, 2025
502d01f
fix: Correct the import
Sarrabah Apr 7, 2025
49a6525
refactor: Ignore type
Sarrabah Apr 7, 2025
4c917a4
fix: Add the forgotten import
Sarrabah Apr 7, 2025
c530b36
test: Remove the test code
Sarrabah Apr 7, 2025
3547351
refactor: Remove exceptions
Sarrabah Apr 7, 2025
42b443f
refactor: Linting
Sarrabah Apr 7, 2025
ef120be
feat: Ignore Canton changes
Sarrabah Apr 8, 2025
997b15d
refactor: Renaming function
Sarrabah Apr 9, 2025
9536e5d
test: Test a local csv file
Sarrabah Apr 9, 2025
cb51cc6
refactor: Implement private functions to be more redable
Sarrabah Apr 9, 2025
996f343
refactor: Use meaningful names
Sarrabah Apr 9, 2025
e899501
feat: Error handling when the csv file is not well formatted
Sarrabah Apr 10, 2025
45c056a
test: Test a non existing column
Sarrabah Apr 10, 2025
e7f5354
test: Test a missing file
Sarrabah Apr 10, 2025
27dc7f1
feat: Use guard clauses and start without error handling
Sarrabah Apr 14, 2025
5f5143a
test: Testing and errro handling when the csv file is not well formatted
Sarrabah Apr 14, 2025
c5f374e
test: Test a missing column
Sarrabah Apr 14, 2025
0cb318d
test: Test a non existing file
Sarrabah Apr 14, 2025
8f8ece0
test: Testing file and ftp schema
Sarrabah Apr 14, 2025
3f94dea
refactor: Add more errors handling
Sarrabah Apr 14, 2025
51e1514
docs: Add a docstring
Sarrabah Apr 14, 2025
dc3de41
test: WIP to use dependency inversion
Sarrabah Apr 14, 2025
d24e7f8
refactor: Remove a comment
Sarrabah Apr 15, 2025
2a8677b
refactor: Renaming and typing variables
Sarrabah Apr 15, 2025
c10900e
refactor: Raise exceptions without printing a message
Sarrabah Apr 15, 2025
6bb9399
feat: Implement dependency inversion WIP
Sarrabah Apr 16, 2025
bc3ee55
fix: Use Union term
Sarrabah Apr 16, 2025
9c6903d
test: Test a not well formatted csv file
Sarrabah Apr 16, 2025
d2c5082
docs: Update docstring
Sarrabah Apr 17, 2025
2cfe6bb
refactor: Renaming
Sarrabah Apr 17, 2025
755b638
refactor: Remove a non important error handling
Sarrabah Apr 17, 2025
ae61328
refactor: Refactoring conditions
Sarrabah Apr 22, 2025
7db2ca1
refactor: Using the same type
Sarrabah Apr 22, 2025
2fe5c16
test: Implement the dependency inversion
Sarrabah Apr 23, 2025
dc8a951
test: Reorganize tests and renaming
Sarrabah Apr 23, 2025
04abe80
refactor: Move get values function to set_format file
Sarrabah Apr 24, 2025
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
1 change: 1 addition & 0 deletions docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
| Region | Nom de région | Vérifie les régions françaises valides pour un Code Officiel Géographique donné. Depuis 2017, la liste des régions françaises n'a pas changé. |
| Siren | SIREN | Check french SIREN number validity, but does not check if SIREN number exists. |
| Siret | SIRET | Check french SIRET number validity, but does not check if SIRET number exists. |
| Validator | Nom de validateur | Vérification si la valeur donnée est bien valide selon des valeurs valides récupérées |

[If you want to add a new French Format, click here!](../CONTRIBUTING.md#implementing-a-new-french-format)
2 changes: 2 additions & 0 deletions src/frformat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
from .formats.region import Region as Region
from .formats.siren import Siren as Siren
from .formats.siret import Siret as Siret
from .formats.validator import Validator as Validator
from .options import Options as Options
from .versions import Millesime as Millesime

all_formats = [
Validator,
Canton,
CodeCommuneInsee,
CodeFantoir,
Expand Down
13 changes: 10 additions & 3 deletions src/frformat/formats/canton.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from frformat import set_format
from frformat.formats.canton_frozenset import CANTON_COG_2023, CANTON_COG_2024
from frformat.get_values_script import get_valid_values
from frformat.set_format import INSEE_SOURCE
from frformat.versioned_set import VersionedSet
from frformat.versions import Millesime
Expand All @@ -9,7 +9,14 @@
source = INSEE_SOURCE

canton_versioned_data = VersionedSet[Millesime]()
canton_versioned_data.add_version(Millesime.M2023, CANTON_COG_2023)
canton_versioned_data.add_version(Millesime.M2024, CANTON_COG_2024)
canton_values_2023 = get_valid_values(
"https://www.insee.fr/fr/statistiques/fichier/6800675/v_canton_2023.csv", "LIBELLE"
)
canton_versioned_data.add_version(Millesime.M2023, canton_values_2023)

canton_values_2024 = get_valid_values(
"https://www.insee.fr/fr/statistiques/fichier/7766585/v_canton_2024.csv", "LIBELLE"
)
canton_versioned_data.add_version(Millesime.M2024, canton_values_2024)

Canton = set_format.new("Canton", name, description, source, canton_versioned_data)
20 changes: 20 additions & 0 deletions src/frformat/formats/validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from frformat import set_format
from frformat.get_values_script import get_valid_values
from frformat.versioned_set import VersionedSet
from frformat.versions import Millesime

name = "Nom de validateur"
description = "Vérification si la valeur donnée est bien valide selon des valeurs valides récupérées"
source = "source1"

validator_versioned_data = VersionedSet[Millesime]()

valid_values_2023 = get_valid_values(
"https://www.insee.fr/fr/statistiques/fichier/8377162/v_commune_2025.csv", "COM"
)
validator_versioned_data.add_version(Millesime.M2023, valid_values_2023)


Validator = set_format.new(
"Validator", name, description, source, validator_versioned_data
)
43 changes: 43 additions & 0 deletions src/frformat/get_values_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import csv
import io
import os
import urllib.parse
import urllib.request


def get_valid_values(path: str, column: str) -> frozenset[str]:
Comment thread
Sarrabah marked this conversation as resolved.
Outdated

parsed_url = urllib.parse.urlparse(path)
is_url = parsed_url.scheme in ("http", "https")
Comment thread
Sarrabah marked this conversation as resolved.
Outdated

valid_values = []

if is_url:
try:
response = urllib.request.urlopen(path)
csvfile = io.StringIO(response.read().decode("utf-8"))
except Exception as e:
raise ValueError(f"Failed to fetch CSV from URL: {e} .")

elif os.path.isfile(path):
Comment thread
Sarrabah marked this conversation as resolved.
Outdated
splitted_path = path.split(".")
if splitted_path[len(splitted_path) - 1] == "csv":
Comment thread
Sarrabah marked this conversation as resolved.
Outdated
try:
csvfile = open(path, newline="", encoding="utf-8")

except Exception as e:
raise ValueError(f"Failed to open local CSV file: {e} .")
else:
raise ValueError("The given path must be referenced to a csv file.")
else:
raise ValueError(f"Invalid path: {path}.It must be a URL or existing csv file.")

with csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
Comment thread
Sarrabah marked this conversation as resolved.
Outdated
if column in row:
valid_values.append(row[column])
else:
raise ValueError(f"CSV file is missing the {column} column.")

return frozenset(valid_values)
Binary file added src/tests/test_files_data/text_file.odt
Binary file not shown.
3 changes: 3 additions & 0 deletions src/tests/test_files_data/values.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"Username","Identifier","First name","Last name"
"booker12","9012","Rachel","Booker"
"grey07","2070","Laura","Grey"
57 changes: 57 additions & 0 deletions src/tests/test_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest

from frformat import Millesime, Validator
from frformat.get_values_script import get_valid_values


def test_validator():
isvalid = Validator(Millesime.M2023).is_valid("01001")
assert isvalid is True


def test_get_valid_values_with_local_file():
valid_values = get_valid_values(
"src/tests/test_files_data/values.csv", "First name"
)
assert valid_values == frozenset({"Rachel", "Laura"})

with pytest.raises(
ValueError, match="The given path must be referenced to a csv file."
):
valid_values = get_valid_values(
"src/tests/test_files_data/text_file.odt", "coucou"
)

with pytest.raises(ValueError, match="CSV file is missing the Link column."):
valid_values = get_valid_values("src/tests/test_files_data/values.csv", "Link")


# mocking ?
""" def test_get_valid_values_with_url():
valid_values = get_valid_values(
"https://cdn.wsform.com/wp-content/uploads/2021/04/month.csv",
"Name",
)
assert valid_values == frozenset(
{
"January",
"Feburary",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
)

with pytest.raises(ValueError):
valid_values = get_valid_values(
"https://coucou.csv/",
"Name",
)
"""
Loading