diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-10-14 15:15:14 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-10-14 15:15:14 +0200 |
| commit | 4f01d3f097b6bf3c6489bbc7b133ef3055fb3980 (patch) | |
| tree | bd8ee625619efc0330cf2a40af27ddd3e56b7d9a /gui | |
| parent | e27b511c7246d6cc8f0207f7e10c31ff903e6e04 (diff) | |
| download | mullvadvpn-4f01d3f097b6bf3c6489bbc7b133ef3055fb3980.tar.xz mullvadvpn-4f01d3f097b6bf3c6489bbc7b133ef3055fb3980.zip | |
Refactor code
Diffstat (limited to 'gui')
| -rw-r--r-- | gui/scripts/extract-geo-data.py | 68 | ||||
| -rw-r--r-- | gui/scripts/integrate-into-app.py | 26 |
2 files changed, 62 insertions, 32 deletions
diff --git a/gui/scripts/extract-geo-data.py b/gui/scripts/extract-geo-data.py index b45f1c154b..611e6acffd 100644 --- a/gui/scripts/extract-geo-data.py +++ b/gui/scripts/extract-geo-data.py @@ -16,17 +16,38 @@ from shapely.geometry import shape, mapping import fiona SCRIPT_DIR = path.dirname(path.realpath(__file__)) + +# The directory with the existing localizations content LOCALE_DIR = path.normpath(path.join(SCRIPT_DIR, "../locales")) + +# The output directory for the generated content OUT_DIR = path.join(SCRIPT_DIR, "out") + +# the directory with the generated localizations content LOCALE_OUT_DIR = path.join(OUT_DIR, "locales") +# Relay locations gettext catalogue template filename (.pot) +RELAY_LOCATIONS_POT_FILENAME = "relay-locations.pot" + +# Relay locations gettext catalogue filename (.po) +RELAY_LOCATIONS_PO_FILENAME = "relay-locations.po" + +# Countries gettext catalogue filename (.po) +COUNTRIES_PO_FILENAME = "countries.po" + +# Cities gettext catalogue filename (.po) +CITIES_PO_FILENAME = "cities.po" + +# The minimum population cap used to narrow down the cities dataset POPULATION_MAX_FILTER = 50000 +# Custom locale mapping between the identifiers in the app and Natural Earth datasets LOCALE_MAPPING = { # "zh" in Natural Earth Data referes to simplified chinese "zh-CN": "zh" } + def extract_cities(): input_path = get_shape_path("ne_50m_populated_places") output_path = path.join(OUT_DIR, "cities.json") @@ -155,12 +176,12 @@ def extract_countries_po(): with fiona.open(input_path) as source: po = POFile(encoding='utf-8', check_for_duplicates=True) po.metadata = {"Content-Type": "text/plain; charset=utf-8"} - output_path = path.join(locale_out_dir, "countries.po") + output_path = path.join(locale_out_dir, COUNTRIES_PO_FILENAME) if not path.exists(locale_out_dir): os.makedirs(locale_out_dir) - print "Generating {}/countries.po".format(locale) + print "Generating {}".format(output_path) for feat in source: props = lower_dict_keys(feat["properties"]) @@ -175,7 +196,7 @@ def extract_countries_po(): translated_name = props.get(name_key) elif props.get(name_fallback) is not None: translated_name = props.get(name_fallback) - print c.orange(u" Missing translation for {}".format(translated_name)) + print c.orange(u"Missing translation for {}".format(translated_name)) else: raise ValueError( "Cannot find the translation for {}. Probe keys: {}" @@ -228,14 +249,14 @@ def extract_cities_po(): if os.path.isdir(locale_dir): po = POFile(encoding='utf-8', check_for_duplicates=True) po.metadata = {"Content-Type": "text/plain; charset=utf-8"} - output_path = path.join(locale_out_dir, "cities.po") + output_path = path.join(locale_out_dir, CITIES_PO_FILENAME) hits = 0 misses = 0 if not path.exists(locale_out_dir): os.makedirs(locale_out_dir) - print "Generating {}/cities.po".format(locale) + print "Generating {}".format(output_path) with fiona.open(input_path) as source: for feat in source: @@ -250,7 +271,7 @@ def extract_cities_po(): hits += 1 elif props.get(name_fallback) is not None: translated_name = props.get(name_fallback) - print c.orange(u" Missing translation for {}".format(translated_name)) + print c.orange(u"Missing translation for {}".format(translated_name)) misses += 1 else: raise ValueError( @@ -297,15 +318,15 @@ def extract_relay_translations(): raise Exception("Missing the result field.") extract_relay_locations_pot(countries) - translate_relay_locations_pot(countries) + translate_relay_locations(countries) def extract_relay_locations_pot(countries): pot = POFile(encoding='utf-8', check_for_duplicates=True) pot.metadata = {"Content-Type": "text/plain; charset=utf-8"} - output_path = path.join(LOCALE_OUT_DIR, "relay-locations.pot") + output_path = path.join(LOCALE_OUT_DIR, RELAY_LOCATIONS_POT_FILENAME) - print "Generating relay-locations.pot" + print "Generating {}".format(output_path) for country in countries: country_name = country.get("name") @@ -334,7 +355,7 @@ def extract_relay_locations_pot(countries): except ValueError as err: print c.orange(u"Cannot add an entry: {}".format(err)) - print u" {} ({})".format(city_name, city.get("code")).encode('utf-8') + print u"{} ({})".format(city_name, city.get("code")).encode('utf-8') pot.save(output_path) @@ -366,7 +387,15 @@ def print_stats_table(title, data): print "" -def translate_relay_locations_pot(countries): +def translate_relay_locations(countries): + """ + A helper function to generate the relay-locations.po with automatic translations for each + corresponding locale. + + The `countries` argument is an array that's contained within the "countries" key of the + relay location list. + """ + country_translator = CountryTranslator() city_translator = CityTranslator() stats = [] @@ -374,18 +403,25 @@ def translate_relay_locations_pot(countries): for locale in os.listdir(LOCALE_DIR): locale_dir = path.join(LOCALE_DIR, locale) if path.isdir(locale_dir): - print "Generating {}/relay-locations.po".format(locale) - (hits, misses) = translate_relay_locations(country_translator, city_translator, countries, locale) + print "Generating {}".format(path.join(locale, RELAY_LOCATIONS_PO_FILENAME)) + (hits, misses) = translate_single_relay_locations(country_translator, city_translator, countries, locale) stats.append((locale, hits, misses)) print_stats_table("Relay location translations", stats) -def translate_relay_locations(country_translator, city_translator, countries, locale): +def translate_single_relay_locations(country_translator, city_translator, countries, locale): + """ + A helper function to generate the relay-locations.po for the given locale. + + The `countries` argument is an array value that's contained within the "countries" key of the + relay location list. + """ + po = POFile(encoding='utf-8', check_for_duplicates=True) po.metadata = {"Content-Type": "text/plain; charset=utf-8"} locale_out_dir = path.join(LOCALE_OUT_DIR, locale) - output_path = path.join(locale_out_dir, "relay-locations.po") + output_path = path.join(locale_out_dir, RELAY_LOCATIONS_PO_FILENAME) hits = 0 misses = 0 @@ -452,7 +488,7 @@ def translate_relay_locations(country_translator, city_translator, countries, lo translated_name = "" misses += 1 - log_message = u" {} ({}) -> \"{}\"".format( + log_message = u"{} ({}) -> \"{}\"".format( city_name, city_code, translated_name).encode('utf-8') if found_translation: print c.green(log_message) diff --git a/gui/scripts/integrate-into-app.py b/gui/scripts/integrate-into-app.py index 81c6426cc0..ce94da537c 100644 --- a/gui/scripts/integrate-into-app.py +++ b/gui/scripts/integrate-into-app.py @@ -10,19 +10,19 @@ import colorful as c SCRIPT_DIR = path.dirname(path.realpath(__file__)) -# the name of the relay locations gettext catalogue template file +# The name of the relay locations gettext catalogue template file RELAY_LOCATIONS_POT_FILENAME = "relay-locations.pot" -# the directory with the generated content +# The directory with the generated content GENERATED_CONTENT_OUTPUT_PATH = path.join(SCRIPT_DIR, "out") -# the directory with the generated localizations content +# The directory with the generated localizations content GENERATED_TRANSLATIONS_PATH = path.join(GENERATED_CONTENT_OUTPUT_PATH, "locales") -# the directory with the app's geo assets +# The directory with the app's geo assets APP_GEO_ASSETS_PATH = path.realpath(path.join(SCRIPT_DIR, "../assets/geo")) -# the directory with the existing app localizations +# The directory with the existing app localizations APP_TRANSLATIONS_PATH = path.realpath(path.join(SCRIPT_DIR, "../locales")) # Geo assets for copying from generated content folder into the app folder @@ -41,7 +41,7 @@ TRANSLATIONS_TO_COPY = [ "countries.po" ] -# the filenames of gettext catalogues that should be merged using msgcat +# The filenames of gettext catalogues that should be merged using msgcat TRANSLATIONS_TO_MERGE = [ "relay-locations.po" ] @@ -52,7 +52,7 @@ def copy_geo_assets(): src = path.join(GENERATED_CONTENT_OUTPUT_PATH, f) dst = path.join(APP_GEO_ASSETS_PATH, f) - print u"Copying {} to {}".format(*remove_common_prefix(src, dst)) + print u"Copying {} to {}".format(src, dst) shutil.copyfile(src, dst) @@ -79,7 +79,7 @@ def merge_single_locale_folder(src, dst): dst_po = path.join(dst, f) if f in TRANSLATIONS_TO_COPY: - print u"Copying {} to {}".format(*remove_common_prefix(src_po, dst_po)) + print u"Copying {} to {}".format(src_po, dst_po) shutil.copyfile(src_po, dst_po) elif f in TRANSLATIONS_TO_MERGE: # merge ../locales/*/file.po with ./out/locales/*/file.po @@ -109,21 +109,15 @@ def merge_gettext_catalogues(existing_catalogue_file, generated_catalogue_file): (exit_code, errors) = run_program("msgcat", *args) if exit_code == 0: - print c.green(u"Merged {} into {}." - .format(*remove_common_prefix(generated_catalogue_file, existing_catalogue_file))) + print c.green(u"Merged {} into {}.".format(generated_catalogue_file, existing_catalogue_file)) else: print c.red(u"msgcat exited with {}: {}".format(exit_code, errors.decode('utf-8').strip())) else: print c.orange(u"The existing catalogue does not exist. Copying {} to {}" - .format(*remove_common_prefix(generated_catalogue_file, existing_catalogue_file))) + .format(generated_catalogue_file, existing_catalogue_file)) shutil.copyfile(generated_catalogue_file, existing_catalogue_file) -def remove_common_prefix(*args): - prefix_len = len(path.commonprefix(args)) - return map(lambda str: str[prefix_len:], args) - - def run_program(*args): p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) |
