1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#! /usr/bin/env python
# Author: Gustavo Carneiro
# Modified by: Alvaro J. Iradier
# Use: missing.py langXX
# lists all missing translations keys in language
import string
import sys
import getopt
#COLORS
NONE=0
BOLD=1
RED=31
GREEN=32
YELLOW=33
BLUE=34
MAGENTA=35
CYAN=36
WHITE=37
def esc(text,color,bold=False):
if nocolors: return text
seq="\33["+str(color)
if bold: seq=seq+";1"
seq=seq+"m"
return seq+text+"\33[0m"
def usage():
print "Usage: missing.py [--nocolor] [--hidetrans] langXX"
#By default, colors are shown
nocolors=False
hidetrans=False
msg_list = {}
#Get options
try:
opts,args=getopt.getopt(sys.argv[1:],"n",["nocolor","hidetrans"])
except getopt.GetoptError:
usage()
sys.exit(-1)
#Process options
for opt,arg in opts:
if opt in ("--nocolor"): nocolors=True
elif opt in ("--hidetrans"): hidetrans=True
#After parsing options, we should just have the language file
if len(args) != 1:
usage()
sys.exit(-1)
#Try to open the master file
try:
langen=open("langen")
except IOError:
print "'langen' master keys file is missing"
sys.exit(1)
#Try to open the language file to check
try:
f=open(args[0])
except IOError:
print "Couldn't open lang file '"+args[0]+"'"
sys.exit(2)
###################################
#Processing of the master keys
print
print esc("Loading master file...",CYAN)
#Skip version line
header=langen.next().strip()
#Load all master keys
for num,line in enumerate(langen):
line=line.strip()
if len(line)<=0:
print esc(" ERROR:",RED)+esc(" blank line in 'langen', you should remove it",BOLD)
print " -->",esc("Line "+str(num+2),YELLOW,True)
continue
i = string.find(line, ' ')
tabs = string.find(line,'\t')
if tabs>0:
print esc(" ERROR:",RED)+esc(" found TAB \\t character in 'langen'",BOLD)
print " -->",esc("Line "+str(num+2),YELLOW,True)
continue
if i < 0:
print esc(" ERROR:",RED)+esc(" invalid key in 'langen', you should remove it",BOLD)
print " -->",esc(line,YELLOW,True)
continue
key = line[:i]
val = line[i+1:]
msg_list[key] = val
print
###################################
#Now find missing keys
print esc("Checking for missing keys in "+args[0]+"...",CYAN)
loaded_keys=[]
#Skin version line
line=f.next().strip()
if not line==header:
print esc(" ERROR:",RED)+esc(" Header in file should be "+header,BOLD)
print " --> ",esc(line,YELLOW,True)
for num,line in enumerate(f):
line=line.strip()
i = string.find(line, ' ')
tabs = string.find(line,'\t')
if len(line)<=0:
print esc(" ERROR:",RED)+esc(" blank line, you should remove it",BOLD)
print " -->",esc("Line "+str(num+2),YELLOW,True)
continue
if tabs>0:
print esc(" ERROR:",RED)+esc(" found TAB \\t character",BOLD)
print " -->",esc("Line "+str(num+2),YELLOW,True)
continue
if i < 0:
print esc(" ERROR:",RED)+esc(" invalid key, you should remove it",BOLD)
print " Line "+str(num+2)+" -->",esc(line.strip(),YELLOW,True)
continue
key = line[:i]
if key in loaded_keys:
print esc(" ERROR:",RED)+esc(" found duplicated key",BOLD)
print " Line "+str(num+2)+" -->'"+esc(key,YELLOW,True)+"'. Please remove one of the ocurrences"
continue
else:
loaded_keys.append(key)
try:
del msg_list[key]
except KeyError:
print esc(" warning:",YELLOW)+esc(" found possibly deprecated key",BOLD)
print " Line "+str(num+2)+" --> '"+esc(key,YELLOW,True)+"'. Please remove it if it's not used in latest AMSN stable version"
###################################
#Print results
num_missing=len(msg_list.items())
if num_missing>0:
errormsg=str(num_missing)+" missing keys in "+args[0]+":"
print
print esc(errormsg,YELLOW,True)
print "-"*len(errormsg)
else:
print
print esc("** No missing keys in "+args[0]+" **",GREEN)
#Print sorted missing keys
keys=msg_list.keys()
keys.sort()
for key in keys:
if hidetrans: print esc(key,CYAN,True)
else:
print esc(key,CYAN,True)+": "+msg_list[key]
print
|