From 3819dad615b95760ca06b853c84f27dadf97793e Mon Sep 17 00:00:00 2001 From: gmweaver Date: Wed, 5 Jun 2024 13:09:04 -0700 Subject: [PATCH] build universal --- .github/workflows/wheels.yml | 1 + install_libpostal.sh | 11 ----- libpypostal/parser.py | 18 ++++--- tests/__init__.py | 0 tests/test_parser.py | 93 ++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_parser.py diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 6e4e568..2fe3060 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -25,6 +25,7 @@ jobs: run: python -m cibuildwheel --output-dir wheelhouse env: CIBW_SKIP: cp36-* cp37-* *musllinux* + CIBW_ARCHS_MACOS: "x86_64 universal2" CIBW_BEFORE_ALL_LINUX: yum install -y autoconf automake curl libtool sudo CIBW_BEFORE_ALL_MACOS: brew install autoconf automake curl libtool pkg-config diff --git a/install_libpostal.sh b/install_libpostal.sh index 6ccd842..49f433c 100755 --- a/install_libpostal.sh +++ b/install_libpostal.sh @@ -1,7 +1,6 @@ #!/bin/bash OS=$(uname -s) -ARCH=$(uname -m) case $OS in Linux) @@ -22,16 +21,6 @@ case $OS in ;; esac -if [ "$OS" = "Linux" ]; then - sudo apt-get install clang curl autoconf automake libtool pkg-config -elif [ "$OS" = "Darwin" ]; then - if [ "$ARCH" == "arm64" ]; then - arch -arm64 brew install curl autoconf automake libtool pkg-config - else - brew install curl autoconf automake libtool pkg-config - fi -fi - cd $1 git clone https://github.com/openvenues/libpostal cd libpostal diff --git a/libpypostal/parser.py b/libpypostal/parser.py index 21e54fe..2596a43 100644 --- a/libpypostal/parser.py +++ b/libpypostal/parser.py @@ -2,8 +2,6 @@ from enum import Enum from typing import Dict, List, Optional, Tuple -from libpypostal import _parser # type: ignore # pylint: disable=no-name-in-module - class LibpostalAddressComponent(str, Enum): """Libpostal address component.""" @@ -30,6 +28,16 @@ class LibpostalAddressComponent(str, Enum): WORLD_REGION = "world_region" +def _parse_address( + address: str, language: str, country_code: str +) -> List[Tuple[str, str]]: + from libpypostal import _parser # type: ignore # pylint: disable=no-name-in-module,import-outside-toplevel + + return _parser.parse_address( # pylint: disable=c-extension-no-member + address, language=language, country=country_code + ) + + def parse_address( address: str, language: Optional[str] = None, country_code: Optional[str] = None ) -> Dict[str, List[str]]: @@ -46,10 +54,8 @@ def parse_address( possibility of multiple matches. Address components not found in the input are set to empty lists. """ - address_component_tuples: List[ - Tuple[str, str] - ] = _parser.parse_address( # pylint: disable=c-extension-no-member - address, language=language, country=country_code + address_component_tuples = _parse_address( + address, language=language, country_code=country_code ) parsed_address_components: Dict[str, List[str]] = {} diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..f7e3806 --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,93 @@ +# pylint: disable=missing-module-docstring,missing-function-docstring, +from unittest.mock import MagicMock, patch + +from libpypostal import parser + + +@patch("libpypostal._parse_address") +def test_parse_address_single_match_each_component( + mock__parser_parse_address: MagicMock, +) -> None: + test_address = "123 Bayes Way, Beverly Hills, CA 90210 US" + mock__parser_parse_address.return_value = [ + ("123", "house_number"), + ("bayes way", "road"), + ("beverly hills", "city"), + ("ca", "state"), + ("90210", "postcode"), + ("us", "country"), + ] + expected = { + "house_number": ["123"], + "road": ["bayes way"], + "city": ["beverly hills"], + "state": ["ca"], + "postcode": ["90210"], + "country": ["us"], + "category": [], + "city_district": [], + "country_region": [], + "entrance": [], + "house": [], + "island": [], + "level": [], + "near": [], + "po_box": [], + "staircase": [], + "state_district": [], + "suburb": [], + "unit": [], + "world_region": [], + } + + actual = parser.parse_address(test_address) + + assert actual == expected + mock__parser_parse_address.assert_called_once_with( + test_address, language=None, country=None + ) + + +@patch("libpypostal._parse_address") +def test_parse_address_multiple_matches_for_component( + mock__parser_parse_address: MagicMock, +) -> None: + test_address = "123 Bayes Way, Beverly Hills, CA 90210 California US" + mock__parser_parse_address.return_value = [ + ("123", "house_number"), + ("bayes way", "road"), + ("beverly hills", "city"), + ("ca", "state"), + ("california", "state"), + ("90210", "postcode"), + ("us", "country"), + ] + expected = { + "house_number": ["123"], + "road": ["bayes way"], + "city": ["beverly hills"], + "state": ["ca", "california"], + "postcode": ["90210"], + "country": ["us"], + "category": [], + "city_district": [], + "country_region": [], + "entrance": [], + "house": [], + "island": [], + "level": [], + "near": [], + "po_box": [], + "staircase": [], + "state_district": [], + "suburb": [], + "unit": [], + "world_region": [], + } + + actual = parser.parse_address(test_address) + + assert actual == expected + mock__parser_parse_address.assert_called_once_with( + test_address, language=None, country=None + )