Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle typed babel library #1049

Merged
merged 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ ENV DEBIAN_FRONTEND noninteractive
# build-essential
RUN apt-get -qq -y update \
&& apt-get -qq -y install locales ca-certificates tzdata curl \
python3-pip libpq-dev python3-icu python3-psycopg2 python3-cryptography \
python3-pip libpq-dev python3-icu python3-psycopg2 \
python3-venv python3-cryptography \
&& apt-get -qq -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Expand All @@ -23,6 +24,8 @@ RUN localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG="en_US.UTF-8" \
TZ="UTC"

RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
RUN pip3 install -q --no-cache-dir -U pip setuptools six psycopg2-binary
COPY . /opt/followthemoney
RUN pip3 install -q --no-cache-dir -e /opt/followthemoney
Expand Down
4 changes: 2 additions & 2 deletions followthemoney/types/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from itertools import product
from babel.core import Locale # type: ignore
from babel.core import Locale
from banal import ensure_list
from normality import stringify
from typing import Any, Dict, Optional, Sequence, Callable, TYPE_CHECKING, TypedDict
Expand Down Expand Up @@ -217,7 +217,7 @@ def __init__(self) -> None:
self._names: Dict[Locale, EnumValues] = {}
self.codes = set(self.names.keys())

def _locale_names(self, locale: str) -> EnumValues:
def _locale_names(self, locale: Locale) -> EnumValues:
return {}

@property
Expand Down
2 changes: 1 addition & 1 deletion followthemoney/types/country.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import countrynames
from typing import Optional, TYPE_CHECKING
from babel.core import Locale # type: ignore
from babel.core import Locale

from followthemoney.rdf import URIRef, Identifier
from followthemoney.types.common import EnumType, EnumValues
Expand Down
2 changes: 1 addition & 1 deletion followthemoney/types/gender.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Optional, TYPE_CHECKING
from babel.core import Locale # type: ignore
from babel.core import Locale

from followthemoney.types.common import EnumType, EnumValues
from followthemoney.rdf import URIRef, Identifier
Expand Down
2 changes: 1 addition & 1 deletion followthemoney/types/language.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Optional, TYPE_CHECKING
from babel.core import Locale # type: ignore
from babel.core import Locale
from languagecodes import iso_639_alpha3

from followthemoney.types.common import EnumType, EnumValues
Expand Down
2 changes: 1 addition & 1 deletion followthemoney/types/topic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from babel.core import Locale # type: ignore
from babel.core import Locale

from followthemoney.types.common import EnumType, EnumValues
from followthemoney.rdf import URIRef, Identifier
Expand Down
12 changes: 7 additions & 5 deletions followthemoney/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import logging
from hashlib import sha1
from babel import Locale # type: ignore
from babel import Locale
from gettext import translation

from threading import local
Expand Down Expand Up @@ -31,7 +31,7 @@

def gettext(*args: Optional[str], **kwargs: Dict[str, str]) -> str:
if not hasattr(state, "translation"):
set_model_locale(DEFAULT_LOCALE)
set_model_locale(Locale.parse(DEFAULT_LOCALE))
return cast(str, state.translation.gettext(*args, **kwargs))


Expand All @@ -42,14 +42,14 @@ def defer(text: str) -> str:
def set_model_locale(locale: Locale) -> None:
state.locale = locale
state.translation = translation(
"followthemoney", i18n_path, [locale], fallback=True
"followthemoney", i18n_path, [str(locale)], fallback=True
)


def get_locale() -> Locale:
if not hasattr(state, "locale"):
return Locale(DEFAULT_LOCALE)
return Locale(state.locale)
return Locale.parse(DEFAULT_LOCALE)
return cast(Locale, state.locale)


def get_env_list(name: str, default: List[str] = []) -> List[str]:
Expand Down Expand Up @@ -138,6 +138,8 @@ def merge_context(left: Dict[K, V], right: Dict[K, V]) -> Dict[K, List[V]]:
combined = {}
keys = [*left.keys(), *right.keys()]
for key in set(keys):
if key in ("caption",):
continue
lval: List[V] = [i for i in ensure_list(left.get(key)) if i is not None]
rval: List[V] = [i for i in ensure_list(right.get(key)) if i is not None]
combined[key] = unique_list([*lval, *rval])
Expand Down