Esempio n. 1
0
def pull_xtb_without_transifex(grd_file_path, brave_source_root):
    xtb_files = get_xtb_files(grd_file_path)
    chromium_grd_file_path = get_chromium_grd_src_with_fallback(grd_file_path,
                                                                brave_source_root)
    chromium_xtb_files = get_xtb_files(grd_file_path)
    if len(xtb_files) != len(chromium_xtb_files):
        assert false, 'XTB files and Chromium XTB file length mismatch.'

    grd_base_path = os.path.dirname(grd_file_path)
    chromium_grd_base_path = os.path.dirname(chromium_grd_file_path)

    # Update XTB FPs so it uses the branded source string
    grd_strings = get_grd_strings(grd_file_path)
    chromium_grd_strings = get_grd_strings(chromium_grd_file_path)
    assert(len(grd_strings) == len(chromium_grd_strings))

    fp_map = {chromium_grd_strings[idx][2]: grd_strings[idx][2] for
              (idx, grd_string) in enumerate(grd_strings)}

    xtb_file_paths = [os.path.join(
        grd_base_path, path) for (lang, path) in xtb_files]
    chromium_xtb_file_paths = [
        os.path.join(chromium_grd_base_path, path) for
        (lang, path) in chromium_xtb_files]
    for idx, xtb_file in enumerate(xtb_file_paths):
        chromium_xtb_file = chromium_xtb_file_paths[idx]
        if not os.path.exists(chromium_xtb_file):
            print 'Warning: Skipping because Chromium path does not exist: ', chromium_xtb_file
            continue
        xml_tree = lxml.etree.parse(chromium_xtb_file)

        for node in xml_tree.xpath('//translation'):
            pre_text_node = textify(node)
            pre_meaning = node.get('meaning') if 'meaning' in node.attrib else ''
            pre_desc = node.get('desc') if 'desc' in node.attrib else ''

            generate_braveified_node(node, False, True)

            meaning = node.get('meaning') if 'meaning' in node.attrib else ''
            desc = node.get('desc') if 'desc' in node.attrib else ''

            # A node's fingerprint changes if the desc or the meaning change
            if (pre_text_node != textify(node) or
                    pre_meaning != meaning or
                    pre_desc == desc):
                old_fp = node.attrib['id']
                # It's possible for an xtb string to not be in our GRD
                # This happens for exmaple with Chrome OS strings which
                #  we don't process files for
                # print 'old fp: ', old_fp
                if old_fp in fp_map:
                    node.attrib['id'] = fp_map.get(old_fp)

        transformed_content = ('<?xml version="1.0" ?>\n' +
                               lxml.etree.tostring(xml_tree, pretty_print=True,
                                                   xml_declaration=False,
                                                   encoding='UTF-8').strip())
        with open(xtb_file, mode='w') as f:
            f.write(transformed_content)
Esempio n. 2
0
def pull_xtb_without_transifex(grd_file_path, brave_source_root):
    xtb_files = get_xtb_files(grd_file_path)
    chromium_grd_file_path = get_chromium_grd_src_with_fallback(
        grd_file_path, brave_source_root)
    chromium_xtb_files = get_xtb_files(grd_file_path)
    if len(xtb_files) != len(chromium_xtb_files):
        assert false, 'XTB files and Chromium XTB file length mismatch.'

    grd_base_path = os.path.dirname(grd_file_path)
    chromium_grd_base_path = os.path.dirname(chromium_grd_file_path)

    # Update XTB FPs so it uses the branded source string
    grd_strings = get_grd_strings(grd_file_path, validate_tags=False)
    chromium_grd_strings = get_grd_strings(chromium_grd_file_path,
                                           validate_tags=False)
    assert (len(grd_strings) == len(chromium_grd_strings))

    fp_map = {
        chromium_grd_strings[idx][2]: grd_strings[idx][2]
        for (idx, grd_string) in enumerate(grd_strings)
    }

    xtb_file_paths = [
        os.path.join(grd_base_path, path) for (lang, path) in xtb_files
    ]
    chromium_xtb_file_paths = [
        os.path.join(chromium_grd_base_path, path)
        for (lang, path) in chromium_xtb_files
    ]
    for idx, xtb_file in enumerate(xtb_file_paths):
        chromium_xtb_file = chromium_xtb_file_paths[idx]
        if not os.path.exists(chromium_xtb_file):
            print 'Warning: Skipping because Chromium path does not exist: ', chromium_xtb_file
            continue
        xml_tree = lxml.etree.parse(chromium_xtb_file)

        for node in xml_tree.xpath('//translation'):
            generate_braveified_node(node, False, True)
            # Use our fp, when exists.
            old_fp = node.attrib['id']
            # It's possible for an xtb string to not be in our GRD.
            # This happens, for exmaple, with Chrome OS strings which
            # we don't process files for.
            if old_fp in fp_map:
                new_fp = fp_map.get(old_fp)
                if new_fp != old_fp:
                    node.attrib['id'] = new_fp
                    # print('fp: {0} -> {1}').format(old_fp, new_fp)

        fix_links_with_target_blank_in_xtb_tree(xml_tree)
        transformed_content = ('<?xml version="1.0" ?>\n' +
                               lxml.etree.tostring(xml_tree,
                                                   pretty_print=True,
                                                   xml_declaration=False,
                                                   encoding='UTF-8').strip())
        with open(xtb_file, mode='w') as f:
            f.write(transformed_content)