First, sorry for my english, I'm catalan.
- Change comment for existing entries (only if it is in previous line and it's /* */ type)
- Not UTF-16 files discarted. (you save changes with utf-16 always)
* dirOut: Folder for check .strings files and used with "genstrings" "-o" option
#! /usr/bin/env python
import os
import re
import sys
import time
import codecs
def combine_strings(previous,path_to):
# Charge new string files
string_files = {}
for f in os.listdir(path_to):
file_path = os.path.join(path_to,f)
if os.path.isfile(file_path) and f.endswith('.strings'):
try:
content = codecs.open(file_path,'r','utf-16').read()
entries = re.findall(ur'(.*?\n"(.*?)"\s*?=\s*?".*?"\s*?;[^\n]*)',content,re.S)
byname = {}
for e in entries:
byname[e[1]] = e
string_files[f] ={'content':content, 'entries':entries, 'byname':byname}
except:
pass
# Print old entries and replace comment if exists
for f,d in previous.items():
# Discard files with same content
if d['content'] == string_files[f]['content']: continue
old_entries = d['byname'].keys()
new_entries = string_files[f]['byname'].keys()
new_file_content = u''
# Add previous entries, and change comment if needed
for e in d['entries']:
if e[1] not in new_entries: continue;
new = string_files[f]['byname'][e[1]]
new_comment = re.findall(ur'\s*?/\*(.*?)\*/\s*?\n"',new[0],re.S)
old_comment = re.findall(ur'\s*?/\*(.*?)\*/\s*?\n"',e[0],re.S)
if len(old_comment) and new_comment[0] != old_comment[0]:
# re.sub delete extra '\'. Must duplicate
new_comment[0] = new_comment[0].replace('\\','\\\\')
new_file_content += re.sub(ur'(\s*?/\*).*?(\*/\s*?\n")',ur'\1%s\2'%new_comment[0],e[0],re.S)
else:
new_file_content += e[0]
new_file_content += '\n'*4
# Check for new entries
new_entries = set(new_entries).difference(old_entries)
if new_entries:
new_file_content += '// New entries: %s\n\n' % time.strftime('%F %T',time.localtime())
for k in string_files[f]['entries']:
if k[1] not in new_entries: continue;
new_file_content += k[0]
new_file_content += '\n'*2
file_path = os.path.join(path_to,f)
codecs.open(file_path,'w','utf-16').write(new_file_content)
def gen_strings(list_from,path_to):
os.system('genstrings -o %s %s' % (path_to,' '.join(list_from)))
def load_string_from(path):
string_files = {}
for f in os.listdir(path):
file_path = os.path.join(path,f)
if os.path.isfile(file_path) and f.endswith('.strings'):
try:
content = codecs.open(file_path,'r','utf-16').read()
entries = re.findall(ur'(.*?\n"(.*?)"\s*?=\s*?".*?"\s*?;[^\n]*)',content,re.S)
byname = {}
for e in entries:
byname[e[1]] = e
string_files[f] ={'content':content, 'entries':entries, 'byname':byname}
except Exception, e:
pass
return string_files
if __name__ == '__main__':
if len(sys.argv) < 3:
print "pyGenStrings usage: pyGenStrings pathOut files ..."
sys.exit(1)
load = load_string_from(sys.argv[1])
gen_strings(sys.argv[2:],sys.argv[1])
combine_strings(load,sys.argv[1])