generated from Hazel/python-project
fix: fallback country
This commit is contained in:
parent
78d60c9908
commit
62fd09b8de
@ -17,3 +17,13 @@ def cli():
|
|||||||
empty_country = EmptyCountry(country="zwx")
|
empty_country = EmptyCountry(country="zwx")
|
||||||
print(type(empty_country))
|
print(type(empty_country))
|
||||||
print(empty_country)
|
print(empty_country)
|
||||||
|
|
||||||
|
print()
|
||||||
|
normal_country = Country("UK")
|
||||||
|
print(type(normal_country))
|
||||||
|
print(normal_country)
|
||||||
|
|
||||||
|
print()
|
||||||
|
fallback_country = Country("zwx")
|
||||||
|
print(type(fallback_country))
|
||||||
|
print(fallback_country)
|
@ -27,12 +27,12 @@ class Country:
|
|||||||
self,
|
self,
|
||||||
country: Optional[str] = None,
|
country: Optional[str] = None,
|
||||||
pycountry_object: Optional[pycountry.db.Country] = None,
|
pycountry_object: Optional[pycountry.db.Country] = None,
|
||||||
force_disable_fallback: bool = False
|
disable_fallback: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
if pycountry_object is None:
|
if pycountry_object is None:
|
||||||
# search for the country string instead if the pycountry_object isn't given
|
# search for the country string instead if the pycountry_object isn't given
|
||||||
# this also implements the optional fallback
|
# this also implements the optional fallback
|
||||||
pycountry_object = self._search_pycountry_object(country=country, force_disable_fallback=force_disable_fallback)
|
pycountry_object = self._search_pycountry_object(country=country, disable_fallback=disable_fallback)
|
||||||
|
|
||||||
if pycountry_object is None:
|
if pycountry_object is None:
|
||||||
raise EmptyCountryException(f"the country {country} was not found and config.fallback_country isn't set")
|
raise EmptyCountryException(f"the country {country} was not found and config.fallback_country isn't set")
|
||||||
@ -41,16 +41,10 @@ class Country:
|
|||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _search_pycountry_object(cls, country: Optional[str], is_fallback: bool = False, force_disable_fallback: bool = False) -> Optional[pycountry.db.Country]:
|
def _search_pycountry_object(cls, country: Optional[str], disable_fallback: bool = False) -> Optional[pycountry.db.Country]:
|
||||||
# fallback to configured country if necessary
|
|
||||||
if country is None and not force_disable_fallback:
|
|
||||||
if force_disable_fallback or config.fallback_country is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
country = config.fallback_country
|
|
||||||
|
|
||||||
pycountry_object = None
|
pycountry_object = None
|
||||||
|
|
||||||
|
if country is not None:
|
||||||
# the reason I don't immediately return the result is because then there would be a chance
|
# the reason I don't immediately return the result is because then there would be a chance
|
||||||
# I would return None even though a country could be found through fuzzy search
|
# I would return None even though a country could be found through fuzzy search
|
||||||
country = country.strip()
|
country = country.strip()
|
||||||
@ -71,6 +65,12 @@ class Country:
|
|||||||
except LookupError:
|
except LookupError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if pycountry_object is not None:
|
||||||
|
return pycountry_object
|
||||||
|
|
||||||
|
if config.fallback_country is not None and not disable_fallback:
|
||||||
|
return cls._search_pycountry_object(country=config.fallback_country, disable_fallback=True)
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def search(cls, country: Optional[str]) -> Optional[Country]:
|
def search(cls, country: Optional[str]) -> Optional[Country]:
|
||||||
@ -122,7 +122,7 @@ class EmptyCountry(Country):
|
|||||||
"""
|
"""
|
||||||
def __new__(cls, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None, **kwargs):
|
def __new__(cls, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None, **kwargs):
|
||||||
try:
|
try:
|
||||||
return Country(country=country, pycountry_object=pycountry_object, force_disable_fallback=False)
|
return Country(country=country, pycountry_object=pycountry_object, disable_fallback=True)
|
||||||
except EmptyCountryException:
|
except EmptyCountryException:
|
||||||
return super().__new__(cls)
|
return super().__new__(cls)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user