Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of syncLang to python2 #3420

Merged
merged 2 commits into from
Nov 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions scripts/syncLang.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# coding=utf-8
from __future__ import division

import codecs
import datetime
import logging
import os
import subprocess
import sys
from io import TextIOWrapper

logging.basicConfig(level=logging.INFO)

Expand All @@ -14,19 +15,19 @@


class Git:
def get_current_branch(self) -> str:
def get_current_branch(self):
"""
:return: the current git branch
"""
return self.__call_command('git rev-parse --abbrev-ref HEAD')

def get_current_hash_short(self) -> str:
def get_current_hash_short(self):
"""
:return: the current git hash (short)
"""
return self.__call_command('git rev-parse --short HEAD')

def __call_command(self, command: str) -> str:
def __call_command(self, command):
"""
:param command: a shell command
:return: the output of the shell command
Expand All @@ -38,7 +39,7 @@ class Keys:
def __init__(self, lines):
self.lines = lines

def duplicates(self) -> list:
def duplicates(self):
"""
return: list of unicode strings
"""
Expand All @@ -48,15 +49,15 @@ def duplicates(self) -> list:
key, value = self.__extract_key_and_value(line=line)
if key:
if key in keys_checked:
duplicates.append("{key}={value}".format(key=key, value=value))
translation_in_list = "{key}={value}".format(key=key, value=keys_checked[key])
duplicates.append(u"{key}={value}".format(key=key, value=value))
translation_in_list = "u{key}={value}".format(key=key, value=keys_checked[key])
if translation_in_list not in duplicates:
duplicates.append(translation_in_list)
else:
keys_checked[key] = value
return duplicates

def fix_duplicates(self) -> tuple:
def fix_duplicates(self):
"""
Fixes all unambiguous duplicates
:return: (list of unicode strings, list of unicode strings): not fixed ambiguous duplicates, fixed unambiguous duplicates
Expand All @@ -83,7 +84,7 @@ def fix_duplicates(self) -> tuple:

return keys, not_fixed, fixed

def keys_from_lines(self) -> list:
def keys_from_lines(self):
"""
Builds a list of all translation keys in the list of lines.
Expand All @@ -97,7 +98,7 @@ def keys_from_lines(self) -> list:
return keys

@staticmethod
def key_from_line(line) -> str:
def key_from_line(line):
"""
Tries to extract the key from the line
Expand All @@ -112,7 +113,7 @@ def key_from_line(line) -> str:
return line[0:index_key_end].strip()
return None

def empty_keys(self) -> list:
def empty_keys(self):
"""
:return: list of unicode strings: the keys with empty values
"""
Expand All @@ -123,7 +124,7 @@ def empty_keys(self) -> list:
not_translated.append(key)
return not_translated

def translations_as_dict(self) -> dict:
def translations_as_dict(self):
"""
:return: dict of unicode strings:
"""
Expand All @@ -135,7 +136,7 @@ def translations_as_dict(self) -> dict:
return translations

@staticmethod
def __extract_key_and_value(line) -> tuple:
def __extract_key_and_value(line):
"""
Tries to extract the key and value from the line
:param line: unicode string
Expand All @@ -151,7 +152,7 @@ def __extract_key_and_value(line) -> tuple:


class SyncLang:
def __init__(self, extended: bool, out_file='status.md'):
def __init__(self, extended, out_file='status.md'):
"""
:param extended: boolean: if the keys with problems should be printed
Expand Down Expand Up @@ -237,30 +238,30 @@ def __check_properties(self, main_property_file, property_files):
if self.extended and num_keys_duplicate != 0:
logging.info("\t\t{}".format(", ".join(keys_duplicate)))

def __all_menu_properties(self) -> list:
def __all_menu_properties(self):
"""
:return: list of strings: all the Menu_*.preferences files with the english at the beginning
"""
menu_property_files = sorted(self.__other_menu_properties())
menu_property_files.insert(0, self.main_menu_preferences)
return menu_property_files

def __other_menu_properties(self) -> list:
def __other_menu_properties(self):
"""
:return: list of strings: all the Menu_*.preferences files without the english one
"""
menu_property_files = [s for s in os.listdir(RES_DIR) if (s.startswith('Menu_') and not (s.startswith('Menu_en')))]
return [os.path.join(RES_DIR, file) for file in menu_property_files]

def __all_jabref_properties(self) -> list:
def __all_jabref_properties(self):
"""
:return: list of strings: all the JabRef_*.preferences files with the english at the beginning
"""
jabref_property_files = sorted(self.__other_jabref_properties())
jabref_property_files.insert(0, os.path.join(RES_DIR, "JabRef_en.properties"))
return jabref_property_files

def __other_jabref_properties(self) -> list:
def __other_jabref_properties(self):
"""
:return: list of strings: all the JabRef_*.preferences files without the english one
"""
Expand Down Expand Up @@ -313,7 +314,7 @@ def __update_properties(self, main_property_file, other_property_files):
for line in main_lines:
key = main_keys.key_from_line(line)
if key is not None:
other_lines_to_write.append("{key}={value}\n".format(key=key, value=keys[key]))
other_lines_to_write.append(u"{key}={value}\n".format(key=key, value=keys[key]))
else:
other_lines_to_write.append(line)

Expand Down Expand Up @@ -345,7 +346,7 @@ def __update_properties(self, main_property_file, other_property_files):
logging.info("\thas been sorted successfully")

@staticmethod
def __format_filename(filepath) -> str:
def __format_filename(filepath):
"""
removes the res_dir path
Expand All @@ -361,20 +362,20 @@ def __write_file(filename, content):
:param filename: string
:param content: list of unicode unicode: the lines to write
"""
with open(filename, 'w', newline='\n', encoding='UTF-8') as f:
with codecs.open(filename, 'w', encoding="UTF-8") as f:
f.writelines(content)

@staticmethod
def __read_file_as_lines(filename, encoding="UTF-8") -> list:
def __read_file_as_lines(filename):
"""
:param filename: string
:param encoding: string: the encoding of the file to read (standard: `UTF-8`)
:return: list of unicode strings: the lines of the file
"""
with open(filename, 'r', newline='', encoding=encoding) as file:
return ["{}\n".format(line.strip()) for line in file.readlines()]
with codecs.open(filename, 'r', encoding="UTF-8") as file:
return [u"{}\n".format(line.strip()) for line in file.readlines()]

def __missing_keys(self, first_list: list, second_list: list) -> list:
def __missing_keys(self, first_list, second_list):
"""
Finds all keys in the first list that are not present in the second list
Expand All @@ -392,7 +393,8 @@ def status_create_markdown(self):
"""
Creates a markdown file of the current status.
"""
def _write_properties(output_file: TextIOWrapper, property_files: list):

def _write_properties(output_file, property_files):
output_file.write("\n| Property file | Keys | Keys translated | Keys not translated | % translated |\n")
output_file.write("| ------------- | ---- | --------------- | ------------------- | ------------ |\n")

Expand All @@ -403,26 +405,30 @@ def _write_properties(output_file: TextIOWrapper, property_files: list):
num_keys_missing_value = len(keys.empty_keys())
num_keys_translated = num_keys - num_keys_missing_value

output_file.write(f"| [{os.path.basename(file)}]({URL_BASE}{os.path.basename(file)}) | "
f"{num_keys} | "
f"{num_keys_translated} | "
f"{num_keys_missing_value} | "
f"{_percentage(num_keys, num_keys_translated)} |\n")

def _percentage(whole: int, part: int) -> int:
output_file.write("| [%s](%s%s) | %d | %d | %d | %d |\n" % (os.path.basename(file),
URL_BASE,
os.path.basename(file),
num_keys,
num_keys_translated,
num_keys_missing_value,
_percentage(num_keys, num_keys_translated)
)
)

def _percentage(whole, part):
if whole == 0:
return 0
return int(part / whole * 100.0)

with open(self.markdown_output, "w", newline="\n", encoding='utf-8') as status_file:
status_file.write(f'### Localization files status ({datetime.datetime.now().strftime("%Y-%m-%d %H:%M")} - '
f'Branch `{Git().get_current_branch()}` `{Git().get_current_hash_short()}`)\n\n')
with codecs.open(self.markdown_output, "w", encoding="UTF-8") as status_file:
status_file.write('### Localization files status (' + datetime.datetime.now().strftime(
"%Y-%m-%d %H:%M") + ' - Branch `' + Git().get_current_branch() + '` `' + Git().get_current_hash_short() + '`)\n\n')
status_file.write('Note: To get the current status from your local repository, run `python ./scripts/syncLang.py markdown`\n')

_write_properties(status_file, self.__all_menu_properties())
_write_properties(status_file, self.__all_jabref_properties())

logging.info(f'Current status written to {self.markdown_output}')
logging.info('Current status written to ' + self.markdown_output)


if '__main__' == __name__:
Expand Down