File: gen.py

package info (click to toggle)
python-countrynames 1.16.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,320 kB
  • sloc: python: 705; makefile: 12; sh: 5
file content (28 lines) | stat: -rw-r--r-- 929 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import yaml
import babel
import pycountry
from collections import defaultdict

data = defaultdict(list)
data_file = os.path.join(os.path.dirname(__file__), "countrynames", "data.yaml")
with open(data_file, "r") as fh:
    for code, names in yaml.load(fh).items():
        data[code].extend(names)

for country in pycountry.countries:
    cc = country.alpha2
    data[cc].append(country.name)
    data[cc].append(country.alpha3)
    if hasattr(country, "official_name"):
        data[cc].append(country.official_name)
    for loc in babel.localedata.locale_identifiers():
        name = babel.Locale(loc).territories.get(cc)
        if name is not None:
            data[cc].append(name)

    data[cc] = list(set(data[cc]))

data_file = os.path.join(os.path.dirname(__file__), "countrynames", "data.yaml")
with open(data_file, "w") as fh:
    yaml.safe_dump(dict(data), fh, default_flow_style=False, allow_unicode=True)