David Keegan avatar David Keegan committed b9fbe39

Removing dependency on json, this should fix issue #169.

Comments (0)

Files changed (3)



-    "English.lproj/FileViewer.xib": "89b3ce19501369084592522616927d3e", 
-    "English.lproj/MainMenu.xib": "481b5b20475f1aedcf89bfc408754937", 
-    "English.lproj/Projects.xib": "770fab372e314ce3a66f4c501d11d8ee", 
-    "English.lproj/Repo.xib": "22ec2f7574365381f41d796e0ba0b439", 
-    "French.lproj/FileViewer.strings": "3180f066f393da424f8f32f60384cedf", 
-    "French.lproj/MainMenu.strings": "d54b312e76454c8742206c78acb4a7e8", 
-    "French.lproj/Projects.strings": "7f3632a7668f39432d8dbf4ffb770f55", 
-    "French.lproj/Repo.strings": "1c882f55c1bc1e7629827090aae1f4ed", 
-    "German.lproj/FileViewer.strings": "3180f066f393da424f8f32f60384cedf", 
-    "German.lproj/MainMenu.strings": "aa69dae50fd262857497ba56606ac537", 
-    "German.lproj/Projects.strings": "8554bdd35568c0d598d3f5b322836067", 
-    "German.lproj/Repo.strings": "8bb4aeacd068f7b82e02806076095383"
 Wraps the ibtool commandline to generate nibs from .strings files.
-An md5 checksum of the base nibs is stored in a Localize.json file,
+An md5 checksum of the base nibs is stored in a Localize.ini file,
 if a checksum for the file does not exist or the check does not match
 a new localized nib is created.
+from __future__ import with_statement
 import time
 import codecs
-import os, sys, glob
+import os, re
+import sys, glob
 import subprocess
 from optparse import OptionParser
 from shutil import copyfile
-    #python2.6+
-    import json
-except ImportError:
-    #python2.5
-    import simplejson as json
+k_valueParse = re.compile('(?P<key>.+)=(?P<value>.+)$', re.UNICODE)
+k_localizePath = os.path.abspath('Localize.ini')
 class LocalizationError(Exception):
     def __init__(self, value):
         stringsEncoding = detectEncoding(stringFile)
         #if the file is not already utf-8 re-encode it
         if stringsEncoding != 'utf_8_sig':
-            fromFile = codecs.open(stringFile, 'r', stringsEncoding)
+            fromFile = codecs.open(stringFile, 'rU', stringsEncoding)
             toFile = codecs.open(tempStrings, 'w', 'utf_8')
             for eachLine in fromFile:
         print '  ', localizableStrings, 'updated'
+def getDict():
+    '''Read the values from Localize.ini and return a dictionary'''
+    localizeDict = {}
+    if not os.path.isfile(k_localizePath):
+        return localizeDict
+    with open(k_localizePath, 'rU') as localizeFile:
+        for line in localizeFile:
+            line = line.strip()
+            match = k_valueParse.match(line)
+            if match:
+                localizeDict[match.group('key')] = match.group('value')
+    return localizeDict
+def writeDict(dict):
+    '''Write a dictionary to Localize.ini'''
+    with open(k_localizePath, 'w') as localizeFile:
+        for key, value in sorted(dict.iteritems()):
+            localizeFile.write('%s=%s\n' % (key, value))
 def localizeNibs(fromLang, toLangs, nibs=None, utf8=False, ignore=False):
     '''Localize nibs from one language to others'''
-    #get the data from the json file
-    localizeJson = os.path.abspath('Localize.json')
-    if not(ignore) and os.path.isfile(localizeJson):
-        localizeData = open(localizeJson, 'r')
-        jsonData = json.load(localizeData)
-        localizeData.close()
-    else:
-        jsonData = {}
+    #get the data from the ini file
+    iniData = getDict()
     fromLangLproj = langProjName(fromLang)
     #if nibs is none, get all the nibs in the from language project
-    if nibs == None:
+    if nibs is None:
         nibs = []
         for eachNib in glob.glob('%s/*.xib' % fromLangLproj):
     for eachNib in nibs:
         eachNib = eachNib.strip()
-        if not(eachNib.endswith('.xib')):
+        if not eachNib.endswith('.xib'):
             eachNib += '.xib'
         fromNib = os.path.join(fromLangLproj, eachNib)
-        #get md5 and update the json data
+        #get md5 and update the ini data
         fromNibMd5 = md5(fromNib)
         #check if the strings for the fromNib need to the updated
-        if not(os.path.isfile(nibToStringFileName(fromNib))) or not(fromNib in jsonData) or jsonData[fromNib] != fromNibMd5:
+        if not os.path.isfile(nibToStringFileName(fromNib)) or fromNib not in iniData or iniData[fromNib] != fromNibMd5:
             ibtoolsGenerateStringsFile(fromNib, utf8)
         #write the localized nibs
         for eachToLang in toLangs:
             toLangLproj = langProjName(eachToLang)
-            toNib =  os.path.join(toLangLproj, eachNib)
+            toNib = os.path.join(toLangLproj, eachNib)
             toStrings = nibToStringFileName(toNib)
             #if there is no localized string file for the nib copy it from the 'from language'
-            if not(os.path.isfile(toStrings)):
+            if not os.path.isfile(toStrings):
                 fromStrings = nibToStringFileName(fromNib)
                 copyfile(fromStrings, toStrings)
             toStringsMd5 = md5(toStrings)
-            if (not(os.path.isfile(toNib)) or not(fromNib in jsonData) or jsonData[fromNib] != fromNibMd5 or
-                not(toStrings in jsonData) or jsonData[toStrings] != toStringsMd5):
+            if (not os.path.isfile(toNib) or fromNib not in iniData or iniData[fromNib] != fromNibMd5 or
+                toStrings not in iniData or iniData[toStrings] != toStringsMd5):
                 ibtoolsWriteNib(fromNib, toNib, utf8)
-                jsonData[toStrings] = toStringsMd5
+                iniData[toStrings] = toStringsMd5
-        jsonData[fromNib] = fromNibMd5
+        iniData[fromNib] = fromNibMd5
-    #update Localize.json
-    localizeData = open(localizeJson, 'w')
-    json.dump(jsonData, localizeData, sort_keys=True, indent=4)
-    #add newline for diffs
-    localizeData.write('\n')
-    localizeData.close()
+    #update Localize.ini
+    writeDict(iniData)
 if __name__ == '__main__':
     '''Command line options'''
     startTime = time.time()
     opts = OptionParser()
         if nibs != None:
             nibs = options.nibs.split('|')
         localizeNibs(options.fromLang, options.toLangs.split('|'), nibs, options.utf8, options.ignore)
-        print 'Nibs updated in %.2f seconds' % (time.time()-startTime)
+        print 'Nibs updated in %.2f seconds' % (time.time()-startTime)    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.