Skip to content

Commit 1d242ce

Browse files
authored
Optimize output in parser scripts (#987)
1 parent aa2f0f1 commit 1d242ce

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

app/scripts/tmx/tmx_products.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ def __init__(self, storage_path, locale,
6868
# Define the local storage filenames
6969
self.storage_file = os.path.join(
7070
storage_path, locale,
71-
'cache_{0}_{1}'.format(locale, repository_name))
71+
'cache_{}_{}'.format(locale, repository_name))
7272

7373
self.reference_storage_file = os.path.join(
7474
storage_path, reference_locale,
75-
'cache_{0}_{1}'.format(reference_locale, repository_name))
75+
'cache_{}_{}'.format(reference_locale, repository_name))
7676

7777
def setRepositoryPath(self, path):
7878
'''Set path to repository.'''
@@ -107,8 +107,8 @@ def getRelativePath(self, file_name):
107107
relative_path = file_name[len(self.repository_path) + 1:]
108108
# Prepend storage_prefix if defined
109109
if self.storage_prefix != '':
110-
relative_path = '{0}/{1}'.format(self.storage_prefix,
111-
relative_path)
110+
relative_path = '{}/{}'.format(self.storage_prefix,
111+
relative_path)
112112

113113
return relative_path
114114

@@ -138,14 +138,14 @@ def extractStrings(self):
138138
# Ignore Junk
139139
if isinstance(entity, parser.Junk):
140140
continue
141-
string_id = u'{0}:{1}'.format(
141+
string_id = u'{}:{}'.format(
142142
self.getRelativePath(file_name), six.text_type(entity))
143143
if file_extension == '.ftl':
144144
if entity.raw_val is not None:
145145
self.translations[string_id] = entity.raw_val
146146
# Store attributes
147147
for attribute in entity.attributes:
148-
attr_string_id = u'{0}:{1}.{2}'.format(
148+
attr_string_id = u'{}:{}.{}'.format(
149149
self.getRelativePath(file_name),
150150
six.text_type(entity),
151151
six.text_type(attribute))
@@ -154,7 +154,7 @@ def extractStrings(self):
154154
else:
155155
self.translations[string_id] = entity.raw_val
156156
except Exception as e:
157-
print('Error parsing file: {0}'.format(file_name))
157+
print('Error parsing file: {}'.format(file_name))
158158
print(e)
159159

160160
# Remove extra strings from locale
@@ -179,24 +179,28 @@ def storeTranslations(self, output_format):
179179

180180
if output_format != 'php':
181181
# Store translations in JSON format
182+
json_output = json.dumps(self.translations, sort_keys=True)
182183
with open('{}.json'.format(self.storage_file), 'w') as f:
183-
f.write(json.dumps(self.translations, sort_keys=True))
184+
f.write(json_output)
184185

185186
if output_format != 'json':
186187
# Store translations in PHP format (array)
187188
string_ids = list(self.translations.keys())
188189
string_ids.sort()
189190

191+
# Generate output before creating an handle for the file
192+
output_php = []
193+
output_php.append('<?php\n$tmx = [\n')
194+
for string_id in string_ids:
195+
translation = self.escape(self.translations[string_id])
196+
string_id = self.escape(string_id)
197+
output_php.append(
198+
u"'{}' => '{}',\n".format(string_id, translation))
199+
output_php.append('];\n')
200+
190201
file_name = '{}.php'.format(self.storage_file)
191202
with codecs.open(file_name, 'w', encoding='utf-8') as f:
192-
f.write('<?php\n$tmx = [\n')
193-
for string_id in string_ids:
194-
translation = self.escape(
195-
self.translations[string_id])
196-
string_id = self.escape(string_id)
197-
line = u"'{0}' => '{1}',\n".format(string_id, translation)
198-
f.write(line)
199-
f.write('];\n')
203+
f.writelines(output_php)
200204

201205
def escape(self, translation):
202206
'''

app/scripts/tmx/tmx_projectconfig.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,36 @@ def storeTranslations(self, output_format):
114114
storage_folder = os.path.join(self.storage_path, locale)
115115
storage_file = os.path.join(
116116
storage_folder,
117-
'cache_{0}_{1}'.format(locale, self.repository_name))
117+
'cache_{}_{}'.format(locale, self.repository_name))
118118

119119
# Make sure that the TMX folder exists
120120
if not os.path.exists(storage_folder):
121121
os.mkdir(storage_folder)
122122

123123
if output_format != 'php':
124124
# Store translations in JSON format
125+
json_output = json.dumps(translations, sort_keys=True)
125126
with open('{}.json'.format(storage_file), 'w') as f:
126-
f.write(json.dumps(translations, sort_keys=True))
127+
f.write(json_output)
127128

128129
if output_format != 'json':
129130
# Store translations in PHP format (array)
130131
string_ids = list(translations.keys())
131132
string_ids.sort()
132133

134+
# Generate output before creating an handle for the file
135+
output_php = []
136+
output_php.append('<?php\n$tmx = [\n')
137+
for string_id in string_ids:
138+
translation = self.escape(translations[string_id])
139+
string_id = self.escape(string_id)
140+
output_php.append(u"'{}' => '{}',\n".format(
141+
string_id, translation))
142+
output_php.append('];\n')
143+
133144
file_name = '{}.php'.format(storage_file)
134145
with codecs.open(file_name, 'w', encoding='utf-8') as f:
135-
f.write('<?php\n$tmx = [\n')
136-
for string_id in string_ids:
137-
translation = self.escape(translations[string_id])
138-
string_id = self.escape(string_id)
139-
line = u"'{0}' => '{1}',\n".format(
140-
string_id, translation)
141-
f.write(line)
142-
f.write('];\n')
146+
f.writelines(output_php)
143147

144148
def escape(self, translation):
145149
'''

0 commit comments

Comments
 (0)