summaryrefslogtreecommitdiffhomepage
path: root/gui/scripts
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2019-05-21 12:24:52 +0200
committerAndrej Mihajlov <and@mullvad.net>2019-05-21 12:43:02 +0200
commit9568ada8a91846f879540e1ef36a4ca354fc4649 (patch)
tree365112e9b2147a54cb080522021a24cae733e8e8 /gui/scripts
parent09d9f56e50f19ba4afec6396183032b8707cb358 (diff)
downloadmullvadvpn-9568ada8a91846f879540e1ef36a4ca354fc4649.tar.xz
mullvadvpn-9568ada8a91846f879540e1ef36a4ca354fc4649.zip
Build the index for the geo dataset to speed up the translations lookup
Diffstat (limited to 'gui/scripts')
-rw-r--r--gui/scripts/extract-geo-data.py58
1 files changed, 41 insertions, 17 deletions
diff --git a/gui/scripts/extract-geo-data.py b/gui/scripts/extract-geo-data.py
index 273392e3ad..fa1a094fe6 100644
--- a/gui/scripts/extract-geo-data.py
+++ b/gui/scripts/extract-geo-data.py
@@ -431,11 +431,8 @@ class PlaceTranslator(object):
def __init__(self):
super(PlaceTranslator, self).__init__()
- shape_path = get_shape_path("ne_10m_populated_places")
- self.source = fiona.open(shape_path, "r")
- def __del__(self):
- self.source.close()
+ self.dataset = self.__build_index()
def translate(self, locale, english_city_name):
"""
@@ -450,25 +447,52 @@ class PlaceTranslator(object):
preferred_locales = (get_locale_language(locale), convert_locale_ident(locale))
match_prop_keys = list("name_" + x for x in preferred_locales)
- for feat in self.source:
- props = lower_dict_keys(feat["properties"])
+ props = self.dataset.get(english_city_name)
- # namepar works for "Wien"
- # use nameascii to match "Sao Paolo"
- if props.get("name") == english_city_name or \
- props.get("namepar") == english_city_name or \
- props.get("nameascii") == english_city_name:
- for key in match_prop_keys:
- value = props.get(key)
+ if props is not None:
+ for key in match_prop_keys:
+ value = props.get(key)
- if value is not None:
- return value
+ if value is not None:
+ return value
- print c.orange(u"Missing translation for {} ({}). Probe keys: {}".format(
- english_city_name, locale, match_prop_keys).encode('utf-8'))
+ print c.orange(u"Missing translation for {} ({}). Probe keys: {}".format(
+ english_city_name, locale, match_prop_keys).encode('utf-8'))
return None
+ def __build_index(self):
+ """
+ Private helper to build the index for the geo dataset, that can be used to speed up the
+ translations lookup.
+ """
+ shape_path = get_shape_path("ne_10m_populated_places")
+ dataset = dict()
+
+ # build a hash map of the entire datasource in memory
+ with fiona.open(shape_path, "r") as source:
+ for feat in source:
+ props = lower_dict_keys(feat["properties"])
+
+ name = props.get("name")
+
+ # namepar works for "Wien"
+ namepar = props.get("namepar")
+
+ # use nameascii to match "Sao Paolo"
+ nameascii = props.get("nameascii")
+
+ if name is not None:
+ dataset[name] = props
+
+ if namepar is not None:
+ dataset[namepar] = props
+
+ if nameascii is not None:
+ dataset[nameascii] = props
+
+ return dataset
+
def get_shape_path(dataset_name):
return path.join(SCRIPT_DIR, dataset_name, dataset_name + ".shp")