File: countries-update-list.py

package info (click to toggle)
onionshare 2.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,628 kB
  • sloc: python: 14,487; javascript: 10,725; sh: 137; makefile: 56; xml: 29
file content (42 lines) | stat: -rwxr-xr-x 1,287 bytes parent folder | download | duplicates (2)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
import os
import tempfile
import subprocess
import json

import onionshare_cli


def main():
    # Clone the country-list repo
    tmp_dir = tempfile.TemporaryDirectory()
    subprocess.run(
        ["git", "clone", "https://github.com/umpirsky/country-list.git"],
        cwd=tmp_dir.name,
    )
    repo_dir = os.path.join(tmp_dir.name, "country-list")

    # Get the list of enabled languages
    common = onionshare_cli.common.Common()
    settings = onionshare_cli.settings.Settings(common)
    available_locales = list(settings.available_locales)

    # Make a dictionary that makes a language's ISO 3166-1 to its name in all enabled languages
    os.makedirs(os.path.join("onionshare", "resources", "countries"), exist_ok=True)
    for locale in available_locales:
        with open(os.path.join(repo_dir, "data", locale, "country.json")) as f:
            countries = json.loads(f.read())

        # Remove countries we don't have images for
        for key in ["JE", "MH", "FM", "MP", "PS", "TV", "UM"]:
            del countries[key]

        with open(
            os.path.join("onionshare", "resources", "countries", f"{locale}.json"),
            "w",
        ) as f:
            f.write(json.dumps(countries))


if __name__ == "__main__":
    main()