Source code for superdjango.contrib.i18n.countries.factories

# Imports

from configparser import ConfigParser
import os
from ..currencies import Currency
from ..languages import Language
from .descriptors import Addressing, Codes, Country, Geography, Wiki

# Exports

__all__ = (
    "country_factory",
)

# Functions


[docs]def country_factory(code, lang="en_US"): """Load a country descriptor. :param code: The two-character ISO-3166 code of the country. :type code: str :param lang: The language to use when loading country data. :type lang: str :rtype: Country | None :raise: ValueError :raises: ``ValueError`` if the corresponding INI file does not have a ``[country]`` section. This should never happen, but is possible if malformed data is inadvertently introduced. """ # Define the path. path = os.path.join(os.path.dirname(__file__), "data", lang, code + ".ini") if not os.path.exists(path): return None # Load the ini. It must have a country section or it is considered malformed. ini = ConfigParser() ini.read(path) if not ini.has_section("country"): raise ValueError("INI file does not have a country section: %s" % path) # Prepare to store kwargs for the descriptor instance. kwargs = dict() # Load country keys. for key, value in ini.items("country"): kwargs[key] = value # Load address specification. if ini.has_section("addressing"): _kwargs = dict() for key, value in ini.items("addressing"): _kwargs[key] = value if ini.has_section("fields"): _kwargs['fields'] = dict() for key, value in ini.items("fields"): _kwargs['fields'][key] = value if ini.has_section("regions"): _kwargs['regions'] = list() for key, value in ini.items("regions"): _kwargs['regions'].append((key.upper(), value)) if ini.has_section("states"): _kwargs['states'] = list() for key, value in ini.items("states"): _kwargs['states'].append((key.upper(), value)) addressing = Addressing(code, **_kwargs) kwargs['addressing'] = addressing # Load additional codes. if ini.has_section("codes"): _kwargs = dict() for key, value in ini.items("codes"): _kwargs[key] = value kwargs['codes'] = Codes(**_kwargs) # Load currency specification. if ini.has_section("currency"): _kwargs = dict() for key, value in ini.items("currency"): _kwargs[key] = value kwargs['currency'] = Currency(**_kwargs) # Load geographic info. if ini.has_section("geography"): region_name = ini.get("geography", "region_name") region_code = ini.get("geography", "region_code") region = Geography(region_code, region_name) region_name = ini.get("geography", "intermediate_region_name") region_code = ini.get("geography", "intermediate_region_code") if region_name and region_code: region.intermediate = Geography(region_code, region_name) region_name = ini.get("geography", "sub_region_name") region_code = ini.get("geography", "sub_region_code") if region_name and region_code: region.sub = Geography(region_code, region_name) kwargs['geography'] = region # Load languages. if ini.has_section("languages"): languages = list() for key, value in ini.items("languages"): languages.append(Language(key, country=code, label=value)) kwargs['languages'] = languages # Load Wiki info. if ini.has_section("wiki"): _kwargs = dict() for key, value in ini.items("wiki"): _kwargs[key] = value kwargs['wiki'] = Wiki(**_kwargs) # Initialize the instance. return Country(code, **kwargs)