From 578f83f9be9795e16714641ae13909fa4714f37b Mon Sep 17 00:00:00 2001 From: pliablepixels Date: Thu, 2 Jun 2016 15:17:18 -0400 Subject: #261 - language file sorting, checking for completeness and pretty layout automation Former-commit-id: 46406e8c93331b9e26da581a0487216f4154cabe --- www/lang/checklang.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 5 deletions(-) (limited to 'www/lang/checklang.py') diff --git a/www/lang/checklang.py b/www/lang/checklang.py index ea910cad..86596bdd 100755 --- a/www/lang/checklang.py +++ b/www/lang/checklang.py @@ -1,25 +1,71 @@ #!/usr/bin/env python import json import os +import getopt +import sys globGood=0 globBad=0 +globBeautify=0 +globOverwrite=0 +globFile="" +# prints usage +def usage(): + print ( + 'Usage: '+sys.argv[0]+'\n' + ' -h|--help: this help\n' + ' -f|file : only processes that file\n' + ' -b|--beautify: beautifies the json file\n' + ' -o|--overwrite: when used with -b/--beautify overwrites the file without adding a pretty- prefix. Use with caution\n' + ) + + +#beautifies a given file +def beautify(fi,ki): + global globOverwrite + if globOverwrite: + prefix='' + else: + prefix='pretty-' + print "Beautifying %s, writing to %s" % (fi,prefix+fi) + w = len (max(ki, key=len)) + pretty=[] + for k,v in ki.iteritems(): + line = " \"%s\"%s:\"%s\"" %(k,' '*(w-len(k)+1),v) + pretty.append(line) + pFh=open (prefix+fi,"w") + pFh.write("{\n") + pFh.write(',\n'.join(pretty)) + pFh.write("\n}\n") + pFh.close() + +#Compares keys in language file def compare (fname): - global globGood, globBad + beaut="no" + global globGood, globBad,globOverwrite, globFile, globBeautify with open (i) as json_data: - newKeys = json.load(json_data) + try: + newKeys = json.load(json_data) + except ValueError: + print 'could not parse %s, skipping!' %fname + globBad+=1 + return json_data.close() diffOrig = set(origKeys.keys()) - set(newKeys.keys()) diffNew = set(newKeys.keys()) - set(origKeys.keys()) if len(diffOrig)==0 and len (diffNew)==0: status = "GOOD" globGood+=1 + if globBeautify and globFile == fname: + beaut="YES" else: status = "ERROR" globBad+=1 - print "\n-------Checking:%s:%s---------" % (fname,status) + print "\n-------Checking:%s:%s, beautify:%s---------" % (fname,status,beaut) print "master keys:%d, %s keys:%d" % (len(origKeys), i, len(newKeys)) + if beaut=="YES": + beautify(fname,newKeys) if len(diffOrig) > 0: print "Keys not present in :%s" %fname for x in diffOrig: @@ -31,17 +77,42 @@ def compare (fname): +#MAIN +try: + myopts,args=getopt.getopt(sys.argv[1:],"f:hob",["file=","help","overwrite","beautify"]) +except getopt.GetoptError as err: + print (err) + usage() + sys.exit(2) + +for o,a in myopts: + if o in ("-h","--help"): + usage() + sys.exit() + elif o in ("-b","--beautify"): + globBeautify=1 + elif o in ("-o","--overwrite"): + globOverwrite=1 + elif o in ("-f","--file"): + globFile=a + with open ('locale-en.json') as json_data: origKeys=json.load(json_data) json_data.close() print "total keys in master language: ", len(origKeys) +#iterate through all languages, using -en as the master for i in os.listdir(os.getcwd()): - if i.endswith("-en.json") or not i.endswith(".json"): + if not i.endswith(".json") or not i.startswith("locale-"): + #print "skipping ",i," as we will only process locale-*.json" continue else: - compare(i) + if globFile == "" or globFile == i: + compare(i) + else: + print "skipping ",i, " as its not ",globFile + print "=================================================" print "Good files:%d, Bad files:%d, Total files:%d" % (globGood, globBad, globGood+globBad) print "=================================================\n" -- cgit v1.2.3