-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_util.py
72 lines (59 loc) · 2.3 KB
/
common_util.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
from os import (
listdir,
path
)
def chunks(array, max_size=100):
"""
Yield successive max_size chunks from array.
"""
for i in range(0, len(array), max_size):
yield array[i:i + max_size]
def find_files(directory):
"""
It returns list containing files in directory.
Args:
directory (str): Absolute location of folder
"""
file_paths = []
files = listdir(directory)
for file_name in files: # check if very object in the folder ...
if path.isfile(path.join(directory, file_name)):
file_paths.append(path.join(directory, file_name))
return file_paths
def validate_and_translate_file(**params):
"""
Takes input a .po file path and translate that file.
Args:
languages (list): Language code list i.e en for english
folder_path (str): The source folder containing .po files
dest_path (str): Destination folder path. I hard coded it to dest folder
action (def): a function need to perform
req_extension (str): extension need to be validate
"""
file_path = params.get('file_path', None)
action = params.get('action', None)
req_extension = params.get('req_extension', '.po')
if file_path:
file_name = path.basename(file_path)
file_base_name, extension = path.splitext(file_name)
if extension == req_extension:
print "Translating file path: {file_path}".format(file_path=file_path)
action(**params)
print "Translated file: {file_name}".format(file_name=file_name)
def extract_files_and_translate_folder(**params):
"""
Takes input a folder path and translate all .po files in that folder.
Args:
languages (list): Language code list i.e en for english
folder_path (str): The source folder containing .po files
dest_path (str): Destination folder path. I hard coded it to dest folder
action (def): a function need to perform
req_extension (str): extension need to be validate
"""
folder_path = params.get('folder_path', None)
if folder_path:
files = find_files(folder_path)
for file_path in files:
params['file_path'] = file_path
validate_and_translate_file(**params)
print "Folder: {} is translated".format(folder_path)