Exemple #1
0
    def check_properties(main_property_file, property_files):
        main_lines = read_file(filename=main_property_file)
        main_keys = get_keys_from_lines(lines=main_lines)

        # the main property file gets compared to itself, but that is OK
        for file in property_files:
            filename = get_filename(filepath=file)
            lines = read_file(file)
            keys = get_keys_from_lines(lines=lines)

            keys_missing = get_missing_keys(main_keys, keys)
            keys_obsolete = get_missing_keys(keys, main_keys)
            keys_duplicate = get_duplicates(lines=lines)
            keys_not_translated = get_empty_keys(lines=lines)

            num_keys = len(keys)
            num_keys_missing = len(keys_missing)
            num_keys_not_translated = len(keys_not_translated)
            num_keys_obsolete = len(keys_obsolete)
            num_keys_duplicate = len(keys_duplicate)
            num_keys_translated = num_keys - num_keys_not_translated

            log = logger.error if num_keys_missing != 0 or num_keys_not_translated != 0 or num_keys_obsolete != 0 or num_keys_duplicate != 0 else logger.ok
            log("Status of file '{file}' with {num_keys} Keys".format(file=filename, num_keys=num_keys))
            logger.ok("\t{} translated keys".format(num_keys_translated))

            log = logger.error if num_keys_not_translated != 0 else logger.ok
            log("\t{} not translated keys".format(num_keys_not_translated))
            if extended and num_keys_not_translated != 0:
                logger.neutral(u"\t\t{}".format(", ".join(keys_not_translated)))

            log = logger.error if num_keys_missing != 0 else logger.ok
            log("\t{} missing keys".format(num_keys_missing))
            if extended and num_keys_missing != 0:
                logger.neutral(u"\t\t{}".format(", ".join(keys_missing)))

            log = logger.error if num_keys_obsolete != 0 else logger.ok
            log("\t{} obsolete keys".format(num_keys_obsolete))
            if extended and num_keys_obsolete != 0:
                logger.neutral(u"\t\t{}".format(", ".join(keys_obsolete)))

            log = logger.error if num_keys_duplicate != 0 else logger.ok
            log("\t{} duplicates".format(num_keys_duplicate))
            if extended and num_keys_duplicate != 0:
                logger.neutral(u"\t\t{}".format(", ".join(keys_duplicate)))
Exemple #2
0
    def update_properties(main_property_file, other_property_files):
        main_lines = read_file(filename=main_property_file)
        # saved the stripped lines
        write_file(main_property_file, main_lines)
        main_keys = get_keys_from_lines(lines=main_lines)

        main_duplicates = get_duplicates(lines=main_lines)
        num_main_duplicates = len(main_duplicates)
        if num_main_duplicates != 0:
            logger.error("There are {num_duplicates} duplicates in {file}, please fix them manually".format(num_duplicates=num_main_duplicates, file=get_filename(filepath=main_property_file)))
            if extended:
                logger.neutral(u"\t{}".format(", ".join(main_duplicates)))
            return


        for other_property_file in other_property_files:
            filename = get_filename(filepath=other_property_file)
            lines = read_file(filename=other_property_file)
            keys, not_fixed, fixed = fix_duplicates(lines=lines)

            num_keys = len(keys)
            num_not_fixed = len(not_fixed)
            num_fixed = len(fixed)

            if num_not_fixed != 0:
                logger.error("There are {num_not_fixed_duplicates} ambiguous duplicates in {file}, please fix them manually".format(num_not_fixed_duplicates=num_not_fixed, file=filename))
                if extended:
                    logger.error(u"\t{}".format(u", ".join(not_fixed)))
                continue

            keys_missing = get_missing_keys(main_keys, keys)
            keys_obsolete = get_missing_keys(keys, main_keys)

            num_keys_missing = len(keys_missing)
            num_keys_obsolete = len(keys_obsolete)

            for missing_key in keys_missing:
                keys[missing_key] = ""

            for obsolete_key in keys_obsolete:
                del keys[obsolete_key]

            other_lines_to_write = []
            for line in main_lines:
                key = get_key_from_line(line)
                if key is not None:
                    other_lines_to_write.append(u"{key}={value}\r\n".format(key=key, value=keys[key]))
                else:
                    other_lines_to_write.append(line)

            sorted = len(lines) != len(other_lines_to_write)
            if not sorted:
                for old_line, new_lines in zip(lines, other_lines_to_write):
                    if old_line != new_lines:
                        sorted = True

            write_file(filename=other_property_file, content=other_lines_to_write)

            logger.ok("Processing file '{file}' with {num_keys} Keys".format(file=filename, num_keys=num_keys))
            if num_fixed != 0:
                logger.ok("\tfixed {} unambiguous duplicates".format(num_fixed))
                if extended:
                    logger.neutral(u"\t\t{}".format(", ".join(fixed)))

            if num_keys_missing != 0:
                logger.ok("\tadded {} missing keys".format(num_keys_missing))
                if extended:
                    logger.neutral(u"\t\t{}".format(", ".join(keys_missing)))

            if num_keys_obsolete != 0:
                logger.ok("\tdeleted {} obsolete keys".format(num_keys_obsolete))
                if extended:
                    logger.neutral(u"\t\t{}".format(", ".join(keys_obsolete)))

            if sorted:
                logger.ok("\thas been sorted successfully")
Exemple #3
0
if len(sys.argv) == 2 and sys.argv[1] == "markdown":
    status_create_markdown()

elif (len(sys.argv) == 2 or len(sys.argv) == 3) and sys.argv[1] == "update":
    update(extended=len(sys.argv) == 3 and (sys.argv[2] == "-e" or sys.argv[2] == "--extended"))

elif (len(sys.argv) == 2 or len(sys.argv) == 3) and sys.argv[1] == "status":
    status(extended=len(sys.argv) == 3 and (sys.argv[2] == "-e" or sys.argv[2] == "--extended"))

else:
    logger.neutral("""This program must be run from the JabRef base directory.

Usage: syncLang.py {markdown, status [-e | --extended], update [-e | --extended]}
Option can be one of the following:

    status [-e | --extended]:
        prints the current status to the terminal
        [-e | --extended]:
            if the translations keys which create problems should be printed

    markdown:
        Creates a markdown file of the current status and opens it

    update [-e | --extended]:
        compares all the localization files against the English one and fixes unambiguous duplicates,
        removes obsolete keys, adds missing keys, and sorts them
        [-e | --extended]:
            if the translations keys which create problems should be printed
""")