Skip to content

Commit

Permalink
handle typed babel library (#1049)
Browse files Browse the repository at this point in the history
* handle typed babel library

* expect state.locale to be correctly typed

* use .parse

* mama mia

* install python in venv
  • Loading branch information
pudo authored Mar 21, 2023
1 parent a634ae7 commit a2125ab
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 12 deletions.
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

0 comments on commit a2125ab

Please sign in to comment.