Пример #1
0
def write_complete_data(in_memory=False):
    """
    This function is responsible of generating the needed py files from the
    CLDR files (JSON format) and supplementary language data (YAML format).

    Use it with in_memory=True to avoid writing real files and getting a
    dictionary containing the file names and their content (used when testing).
    """
    in_memory_result = {}

    encoding_comment = "# -*- coding: utf-8 -*-\n"
    if not in_memory:
        if not os.path.isdir(translation_data_directory):
            os.mkdir(translation_data_directory)
        if os.path.isdir(date_translation_directory):
            shutil.rmtree(date_translation_directory)
        os.mkdir(date_translation_directory)

    with open(supplementary_directory + 'base_data.yaml') as f:
        base_data = RoundTripLoader(f).get_data()

    for language in all_languages:
        date_translation_data = _get_complete_date_translation_data(language)
        date_translation_data = combine_dicts(date_translation_data, base_data)
        _modify_data(date_translation_data)
        translation_data = json.dumps(date_translation_data, indent=4, separators=(',', ': '),
                                      ensure_ascii=False)
        out_text = (encoding_comment + 'info = ' + translation_data + '\n').encode('utf-8')
        _write_file(date_translation_directory + language + '.py', out_text, 'wb', in_memory, in_memory_result)

    if not in_memory and os.path.isdir(numeral_translation_directory):
        shutil.rmtree(numeral_translation_directory)

    if not in_memory:
        os.mkdir(numeral_translation_directory)

    for language in cldr_numeral_languages:
        with open(cldr_numeral_directory + language + '.json') as f:
            numeral_translation_data = json.load(f, object_pairs_hook=OrderedDict)
        numeral_data = json.dumps(numeral_translation_data, indent=4, separators=(',', ': '),
                                  ensure_ascii=False)
        out_text = (encoding_comment + 'info = ' + numeral_data + '\n').encode('utf-8')
        _write_file(numeral_translation_directory + language + '.py', out_text, 'wb', in_memory, in_memory_result)

    init_text = '\n'.join(
            ["from dateparser.data import date_translation_data, numeral_translation_data",
             "from .languages_info import language_order, language_locale_dict"]
             )

    _write_file(translation_data_directory + '__init__.py', encoding_comment + init_text, 'w', False, in_memory_result)
    _write_file(date_translation_directory + '__init__.py', encoding_comment, 'w', False, in_memory_result)
    _write_file(numeral_translation_directory + '__init__.py', encoding_comment, 'w', False, in_memory_result)

    return in_memory_result
Пример #2
0
def main():
    encoding_comment = "# -*- coding: utf-8 -*-\n"
    if not os.path.isdir(translation_data_directory):
        os.mkdir(translation_data_directory)
    if os.path.isdir(date_translation_directory):
        shutil.rmtree(date_translation_directory)
    os.mkdir(date_translation_directory)
    with open(supplementary_directory + 'base_data.yaml') as f:
        base_data = RoundTripLoader(f).get_data()

    for language in all_languages:
        date_translation_data = _get_complete_date_translation_data(language)
        date_translation_data = combine_dicts(date_translation_data, base_data)
        _modify_data(date_translation_data)
        translation_data = json.dumps(date_translation_data,
                                      indent=4,
                                      separators=(',', ': '),
                                      ensure_ascii=False)
        out_text = (encoding_comment + 'info = ' +
                    translation_data).encode('utf-8')
        with open(date_translation_directory + language + '.py', 'wb') as out:
            out.write(out_text)

    if os.path.isdir(numeral_translation_directory):
        shutil.rmtree(numeral_translation_directory)
    os.mkdir(numeral_translation_directory)
    for language in cldr_numeral_languages:
        with open(cldr_numeral_directory + language + '.json') as f:
            numeral_translation_data = json.load(f,
                                                 object_pairs_hook=OrderedDict)
        numeral_data = json.dumps(numeral_translation_data,
                                  indent=4,
                                  separators=(',', ': '),
                                  ensure_ascii=False)
        out_text = (encoding_comment + 'info = ' +
                    numeral_data).encode('utf-8')
        with open(numeral_translation_directory + language + '.py',
                  'wb') as out:
            out.write(out_text)

    init_text = '\n'.join([
        "from dateparser.data import date_translation_data, numeral_translation_data",
        "from .languages_info import language_order, language_locale_dict"
    ])
    with open(translation_data_directory + '__init__.py', 'w') as out:
        out.write(encoding_comment + init_text)

    with open(date_translation_directory + '__init__.py', 'w') as out:
        out.write(encoding_comment)

    with open(numeral_translation_directory + '__init__.py', 'w') as out:
        out.write(encoding_comment)
def _get_complete_date_translation_data(language):
    cldr_data = {}
    supplementary_data = {}
    if language in cldr_languages:
        with open(cldr_date_directory + language + '.json') as f:
            cldr_data = json.load(f, object_pairs_hook=OrderedDict)
    if language in supplementary_languages:
        with open(supplementary_date_directory + language + '.yaml') as g:
            supplementary_data = OrderedDict(RoundTripLoader(g).get_data())
    complete_data = combine_dicts(cldr_data, supplementary_data)
    if 'name' not in complete_data:
        complete_data['name'] = language
    return complete_data