From 239fa712c8f221c4b5fed797dcf506022fb69f89 Mon Sep 17 00:00:00 2001 From: Chris Mostert <15890652+chrismostert@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:57:03 +0200 Subject: [PATCH] Format reporting unit name to exclude zip code and prefix --- src/csv_write.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/csv_write.py b/src/csv_write.py index 7df02a8..3717bad 100644 --- a/src/csv_write.py +++ b/src/csv_write.py @@ -1,4 +1,5 @@ import csv +import re from typing import Dict, List, Optional from eml import EML, CheckResult @@ -21,6 +22,9 @@ ] PROTOCOL_VERSION = "EP2024" +ZIP_CODE_PATTERN = re.compile(r"\(postcode: \d{4} [A-Z]{2}\)") +STEMBUREAU_PREFIX_PATTERN = re.compile(r"^Stembureau Stembureau") + def _write_header(writer, metadata: EmlMetadata, description: str) -> None: writer.writerow(["Versie controleprotocol", PROTOCOL_VERSION]) @@ -68,6 +72,16 @@ def _format_percentage_deviation(percentage: float) -> str: return f"{sign}{percentage_int}%" +def _format_reporting_unit_name(reporting_unit_name: Optional[str]) -> str: + return ( + STEMBUREAU_PREFIX_PATTERN.sub( + "Stembureau", ZIP_CODE_PATTERN.sub("", reporting_unit_name) + ).strip() + if reporting_unit_name + else "" + ) + + def _id_cols(metadata: EmlMetadata, id: str) -> List[Optional[str]]: return [ metadata.election_id, @@ -75,7 +89,7 @@ def _id_cols(metadata: EmlMetadata, id: str) -> List[Optional[str]]: metadata.authority_id, metadata.authority_name, _format_id(id), - metadata.reporting_unit_names.get(id), + _format_reporting_unit_name(metadata.reporting_unit_names.get(id)), ]