File: get_continent_map.py

package info (click to toggle)
nextcloud-spreed-signaling 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,620 kB
  • sloc: sh: 447; makefile: 187; python: 78
file content (118 lines) | stat: -rwxr-xr-x 3,268 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
#
# Standalone signaling server for the Nextcloud Spreed app.
# Copyright (C) 2019 struktur AG
#
# @author Joachim Bauch <bauch@struktur.de>
#
# @license GNU AGPL version 3 or any later version
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

try:
  # Fallback for Python2
  from cStringIO import StringIO
except ImportError:
  from io import StringIO
import csv
import subprocess
import sys

URL = 'https://raw.githubusercontent.com/datasets/country-codes/refs/heads/main/data/country-codes.csv'

def tostr(s):
  if isinstance(s, bytes) and not isinstance(s, str):
    s = s.decode('utf-8')
  return s

try:
  unicode
except NameError:
  # Python 3 files are returning bytes by default.
  def opentextfile(filename, mode):
    if 'b' in mode:
      mode = mode.replace('b', '')
    return open(filename, mode, encoding='utf-8')
else:
  def opentextfile(filename, mode):
    return open(filename, mode)

def country_fallback(entry):
  country = entry['ISO3166-1-Alpha-3']
  if not country:
    return ''

  if country == 'NAM':
    # Special case for Namibia
    return 'NA'
  else:
    return ''

def continent_fallback(entry):
  country = entry['ISO3166-1-Alpha-2']
  if not country:
    return ''

  region = entry['Region Name']
  assert region == 'Americas', 'Unknown entry: %r' % (entry)
  return 'NA'

def generate_map(filename):
  data = subprocess.check_output([
    'curl',
    '-L',
    URL,
  ])

  reader = csv.DictReader(StringIO(tostr(data)), delimiter=',')
  continents = {}
  for entry in reader:
    country = entry['ISO3166-1-Alpha-2'] or country_fallback(entry)
    continent = entry['Continent'] or continent_fallback(entry)
    if not country or not continent:
      continue

    continents.setdefault(country, []).append(continent)

  out = StringIO()
  out.write('package signaling\n')
  out.write('\n')
  out.write('// This file has been automatically generated, do not modify.\n')
  out.write('// Source: %s\n' % (URL))
  out.write('\n')
  out.write('var (\n')
  out.write('\tContinentMap = map[string][]string{\n')
  for country, continents in sorted(continents.items()):
    value = []
    for continent in continents:
      continent = '"%s"' % (continent)
      if not continent in value:
        value.append(continent)
    out.write('\t\t"%s": {%s},\n' % (country, ', '.join(value)))
  out.write('\t}\n')
  out.write(')\n')
  with opentextfile(filename, 'wb') as fp:
    fp.write(out.getvalue())

def main():
  if len(sys.argv) != 2:
    sys.stderr.write('USAGE: %s <filename>\n' % (sys.argv[0]))
    sys.exit(1)

  filename = sys.argv[1]
  generate_map(filename)

if __name__ == '__main__':
  main()