Skip to content

Commit

Permalink
Optimize output in parser scripts (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo authored Jun 26, 2020
1 parent aa2f0f1 commit 1d242ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
36 changes: 20 additions & 16 deletions app/scripts/tmx/tmx_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def __init__(self, storage_path, locale,
# Define the local storage filenames
self.storage_file = os.path.join(
storage_path, locale,
'cache_{0}_{1}'.format(locale, repository_name))
'cache_{}_{}'.format(locale, repository_name))

self.reference_storage_file = os.path.join(
storage_path, reference_locale,
'cache_{0}_{1}'.format(reference_locale, repository_name))
'cache_{}_{}'.format(reference_locale, repository_name))

def setRepositoryPath(self, path):
'''Set path to repository.'''
Expand Down Expand Up @@ -107,8 +107,8 @@ def getRelativePath(self, file_name):
relative_path = file_name[len(self.repository_path) + 1:]
# Prepend storage_prefix if defined
if self.storage_prefix != '':
relative_path = '{0}/{1}'.format(self.storage_prefix,
relative_path)
relative_path = '{}/{}'.format(self.storage_prefix,
relative_path)

return relative_path

Expand Down Expand Up @@ -138,14 +138,14 @@ def extractStrings(self):
# Ignore Junk
if isinstance(entity, parser.Junk):
continue
string_id = u'{0}:{1}'.format(
string_id = u'{}:{}'.format(
self.getRelativePath(file_name), six.text_type(entity))
if file_extension == '.ftl':
if entity.raw_val is not None:
self.translations[string_id] = entity.raw_val
# Store attributes
for attribute in entity.attributes:
attr_string_id = u'{0}:{1}.{2}'.format(
attr_string_id = u'{}:{}.{}'.format(
self.getRelativePath(file_name),
six.text_type(entity),
six.text_type(attribute))
Expand All @@ -154,7 +154,7 @@ def extractStrings(self):
else:
self.translations[string_id] = entity.raw_val
except Exception as e:
print('Error parsing file: {0}'.format(file_name))
print('Error parsing file: {}'.format(file_name))
print(e)

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

if output_format != 'php':
# Store translations in JSON format
json_output = json.dumps(self.translations, sort_keys=True)
with open('{}.json'.format(self.storage_file), 'w') as f:
f.write(json.dumps(self.translations, sort_keys=True))
f.write(json_output)

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

# Generate output before creating an handle for the file
output_php = []
output_php.append('<?php\n$tmx = [\n')
for string_id in string_ids:
translation = self.escape(self.translations[string_id])
string_id = self.escape(string_id)
output_php.append(
u"'{}' => '{}',\n".format(string_id, translation))
output_php.append('];\n')

file_name = '{}.php'.format(self.storage_file)
with codecs.open(file_name, 'w', encoding='utf-8') as f:
f.write('<?php\n$tmx = [\n')
for string_id in string_ids:
translation = self.escape(
self.translations[string_id])
string_id = self.escape(string_id)
line = u"'{0}' => '{1}',\n".format(string_id, translation)
f.write(line)
f.write('];\n')
f.writelines(output_php)

def escape(self, translation):
'''
Expand Down
24 changes: 14 additions & 10 deletions app/scripts/tmx/tmx_projectconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,32 +114,36 @@ def storeTranslations(self, output_format):
storage_folder = os.path.join(self.storage_path, locale)
storage_file = os.path.join(
storage_folder,
'cache_{0}_{1}'.format(locale, self.repository_name))
'cache_{}_{}'.format(locale, self.repository_name))

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

if output_format != 'php':
# Store translations in JSON format
json_output = json.dumps(translations, sort_keys=True)
with open('{}.json'.format(storage_file), 'w') as f:
f.write(json.dumps(translations, sort_keys=True))
f.write(json_output)

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

# Generate output before creating an handle for the file
output_php = []
output_php.append('<?php\n$tmx = [\n')
for string_id in string_ids:
translation = self.escape(translations[string_id])
string_id = self.escape(string_id)
output_php.append(u"'{}' => '{}',\n".format(
string_id, translation))
output_php.append('];\n')

file_name = '{}.php'.format(storage_file)
with codecs.open(file_name, 'w', encoding='utf-8') as f:
f.write('<?php\n$tmx = [\n')
for string_id in string_ids:
translation = self.escape(translations[string_id])
string_id = self.escape(string_id)
line = u"'{0}' => '{1}',\n".format(
string_id, translation)
f.write(line)
f.write('];\n')
f.writelines(output_php)

def escape(self, translation):
'''
Expand Down

0 comments on commit 1d242ce

Please sign in to comment.