Skip to content

Commit

Permalink
Merge pull request #849 from strukturag/fix-continentmap
Browse files Browse the repository at this point in the history
Fetch country information for continentmap from correct location.
  • Loading branch information
fancycode authored Oct 28, 2024
2 parents 7eeef6e + 9af5ef6 commit fea4136
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion continentmap.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package signaling

// This file has been automatically generated, do not modify.
// Source: https://github.com/datasets/country-codes/raw/master/data/country-codes.csv
// Source: https://raw.githubusercontent.com/datasets/country-codes/refs/heads/main/data/country-codes.csv

var (
ContinentMap = map[string][]string{
Expand Down
32 changes: 27 additions & 5 deletions scripts/get_continent_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import subprocess
import sys

URL = 'https://github.com/datasets/country-codes/raw/master/data/country-codes.csv'
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):
Expand All @@ -49,6 +49,26 @@ def opentextfile(filename, mode):
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',
Expand All @@ -59,9 +79,9 @@ def generate_map(filename):
reader = csv.DictReader(StringIO(tostr(data)), delimiter=',')
continents = {}
for entry in reader:
country = entry['ISO3166-1-Alpha-2']
continent = entry['Continent']
if not country and not continent:
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)
Expand All @@ -77,7 +97,9 @@ def generate_map(filename):
for country, continents in sorted(continents.items()):
value = []
for continent in continents:
value.append('"%s"' % (continent))
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')
Expand Down

0 comments on commit fea4136

Please sign in to comment.