-
Notifications
You must be signed in to change notification settings - Fork 29
/
create_stdm.pro.py
157 lines (138 loc) · 5.41 KB
/
create_stdm.pro.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : Create STDM Pro
Description : A tool used to list all files of stdm with
translatable text into stdm.pro file.
Date : 30/June/2016
copyright : (C) 2016 by UN-Habitat and implementing partners.
See the accompanying file CONTRIBUTORS.txt in the root
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import io
import os
import os.path
import sys
# Memory file
mem_file = io.StringIO()
def translate_in_file(file_path):
"""
Checks if the word translate exists in a file.
:param file_path: The path of a file
:type file_path: String
:return: True if there is translate work
or False if it doesn't exist.
:rtype: Boolean
"""
with open(file_path, "r") as file_inst:
array = []
for line in file_inst:
array.append(line)
if 'translate' in str(array):
return True
elif 'self.tr(' in str(array):
return True
else:
return False
def format_file(file_path):
"""
Removes forward slash at the end of a block of lines.
:param file_path: The path of the file to be formatted
:type file_path: String
:return: None
:rtype: NoneType
"""
mem_file_lines = mem_file.getvalue().split('\n')
# Lines at the end of file type list.
last_line_indexes = []
# Loop through memory file to get the lines before empty line
for position, line1 in enumerate(mem_file_lines):
# Get the index of the lines before empty line
if line1 == '':
last_line_indexes.append(position - 1)
file = open(file_path, "w+")
# add lines of the memory file to actual file
# replace backward slashes if they are last line
# as backward slash is not needed there.
for position, line1 in enumerate(mem_file_lines):
if position in last_line_indexes:
print(line1.replace(' \\', ''), file=file)
else:
print(line1, file=file)
file.close()
mem_file.close()
def create_pro_file(dir):
"""
Creates a stdm.pro file to be used for translation.
:param dir: Directory
:type dir: String
:return: None
:rtype: NoneType
"""
file = mem_file
# Print into file.
print('SOURCES = \\', file=file)
space_length = len('SOURCES = ')
space = ' '
current_file_name = os.path.basename(sys.argv[0])
# Set indentation based on the length on the heading.
indentation = space_length * space
# loop through dir recursively except third party
# folder to get py files
for dirpath, dirnames, filenames in os.walk(dir):
# print dirpath, filenames
if 'third_party' not in dirpath:
# List of py files
py_files = [f for f in filenames if f.endswith('.py')]
# print last_loop
for i, filename in enumerate(py_files):
# exclude init files
if '__init__' not in filename and \
not current_file_name in filename and 'ui_' not in filename:
file_path = os.path.join(dirpath, filename)
if translate_in_file(file_path):
print('{}{} \\'.format(
indentation,
file_path[len(dir) + 1:]
), file=file)
print('', file=file)
print('FORMS = \\', file=file)
space_length = len('FORMS = ')
# Set indentation based on the length on the heading.
indentation = space_length * space
# loop through the current directory
# recursively get ui files
for dirpath, dirnames, filenames in os.walk(dir):
# List of ui files
ui_files = [f for f in filenames if f.endswith('.ui')]
last_loop = len(ui_files) - 1
for j, filename in enumerate(ui_files):
file_path = os.path.join(dirpath, filename)
print('{}{} \\'.format(
indentation,
file_path[len(dir) + 1:]
), file=file)
print('', file=file)
print('TRANSLATIONS = i18n/stdm_fr.ts \\', file=file)
print(' i18n/stdm_pt.ts \\', file=file)
print(' i18n/stdm_de.ts \\', file=file)
print(' i18n/stdm_sw.ts \\', file=file)
print(' i18n/stdm_fi.ts \\', file=file)
print(' i18n/stdm_ar.ts \\', file=file)
print(' i18n/stdm_es.ts', file=file)
print('', file=file)
print('CODECFORTR = UTF-8', file=file)
print('', file=file)
print('CODECFORSRC = UTF-8', file=file)
# file.close()
format_file(dir + '\stdm.pro')
create_pro_file('.')