locales.py 1.82 KB
Newer Older
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
#!/usr/bin/python

#  This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function, unicode_literals

import re

OPEN_LOCALES = "locales = ["
CLOSE_LOCALES = "]"

def trim_to_locale(str):
    match = re.search('\s*"([a-z]+-?[A-Z]*)",\s*', str)
    if not match:
        raise Exception("Failed parsing locale found in l10n.toml: " + str)
    return match.group(1)


# This file is a dumb parser that converts values from '/l10n.toml' to be easily consumed from
# Python.
#
# 'l10n.toml' has a very simple structure, and it is reasonable to believe that this (very basic)
# algorithm will continue to work as it is changed.
#
# Alternatives to custom parsing that were considered:
# - Using standard library module --- none exists to parse TOML
# - Importing a TOML parsing module --- introduces an additional build step, complexity, and
#   security risk
# - Vendoring a TOML module --- large amount of code given the use case. Introduces a security
#   risk
def get_release_locales():
    with open(r"l10n.toml") as f:
        file = f.read().splitlines()

    locales_opened = False
    locales_closed = False

    found_locales = []

    for line in file:
        if line == OPEN_LOCALES:
            locales_opened = True
        elif line == CLOSE_LOCALES:
            locales_closed = True
            break
        elif locales_opened:
            found_locales.append(trim_to_locale(line))

    if locales_opened == False:
        raise Exception("Could not find `locales` open in l10n.toml")
    if locales_closed == False:
        raise Exception("Could not find `locales` close in l10n.toml")

    return found_locales


RELEASE_LOCALES = get_release_locales()