#!/usr/bin/env python
#-----------------------------------------------------
#- Simple transation tool -
#- www.unipex.it -
#- Michele Petrazzo - michele.petrazzo@unipex.it -
#- License: G.P.L. -
#-----------------------------------------------------
"""
This simple transation tool are made by me for transalte
the language file used into my application into differentes
language.
It's a very simple program able to handle .ini files with some
utils for speedup your translation.
If you want you can use it for all proupose, so edit all files editable
by ConfigParser standard library and not only for make transaltions !
"""
import wx, wx.grid
from ConfigParser import RawConfigParser
import os, sys, time
from shutil import copyfile
BT_SRC1 = 0
BT_SRC2 = 1
BT_SAV1 = 3
BT_SAV2 = 4
BT_DIFF1 = 6
BT_DIFF2 = 7
BT_INSERT1 = 8
BT_INSERT2 = 9
STR_INIT_SECT = "-- Sect:"
class BT(wx.Button):
def __init__(self, parent, label, btId = -1, eventBind=None):
"""
"""
wx.Button.__init__(self,parent, btId, label=label)
if eventBind:
self.Bind(wx.EVT_BUTTON, eventBind)
class tranlateFrame(wx.Frame):
def __init__(self, parent, wxId, title, size = wx.DefaultSize):
"""
"""
wx.Frame.__init__(self, parent, wxId, title)
self.SetBackgroundColour(wx.WHITE)
self.lblTitle = wx.StaticText(self, -1, 'Translation tool - by Unipex srl - www.unipex.it')
self.Bind(wx.EVT_CLOSE, self.onClose)
self.grLang1 = wx.grid.Grid(self)
self.grLang2 = wx.grid.Grid(self)
self.lstDiff = wx.ListBox(self)
self.lstDiff.Bind(wx.EVT_LISTBOX, self.onLstDiffClick)
#self.grLang3 = wx.grid.Grid(self)
self.btSearch1 = BT(self, "Load 1", BT_SRC1, self.onBtSearch)
self.btSearch2 = BT(self, "Load 2", BT_SRC2, self.onBtSearch)
self.btDifferences1 = BT(self, "Differences 1", BT_DIFF1, self.onBtDiff)
self.btDifferences2 = BT(self, "Differences 2", BT_DIFF2, self.onBtDiff)
self.btSave1 = BT(self, "Save 1", BT_SAV1, self.onBtSave)
self.btSave2 = BT(self, "Save 2", BT_SAV2, self.onBtSave)
self.btInsert1 = BT(self, "Insert line", BT_INSERT1, self.onBtInsert)
self.btInsert2 = BT(self, "Insert line", BT_INSERT2, self.onBtInsert)
self.lblFileName1 = wx.StaticText(self, label = 'File name')
self.lblFileName2 = wx.StaticText(self, label = 'File name')
self.chkAddSelection = wx.CheckBox(self, label='Add then select if not present')
self.chkDelSelection = wx.CheckBox(self, label='Delete the value wthen select')
szLbl = wx.BoxSizer(wx.HORIZONTAL)
szBtSearch1 = wx.BoxSizer(wx.HORIZONTAL)
szBtSearch2 = wx.BoxSizer(wx.HORIZONTAL)
szGrid1 = wx.BoxSizer(wx.VERTICAL)
szGrid2 = wx.BoxSizer(wx.VERTICAL)
szBtSave1 = wx.BoxSizer(wx.HORIZONTAL)
szBtSave2 = wx.BoxSizer(wx.HORIZONTAL)
szCheck = wx.BoxSizer(wx.VERTICAL)
self.frameSizer = wx.BoxSizer(wx.VERTICAL)
self.szWork = wx.FlexGridSizer(3,3)
flagGrBt = wx.ALL
propGrBt = 0
space = 5
flagGr = wx.ALL
propGr = 1
spaceGr = 2
szBtSearch1.Add(self.btSearch1, 0, flagGrBt, space)
szBtSearch1.Add(self.btDifferences1, 0, flagGrBt, space)
szBtSearch2.Add(self.btSearch2, 0, flagGrBt, space)
szBtSearch2.Add(self.btDifferences2, 0, flagGrBt, space)
szGrid1.Add(self.lblFileName1, 0, flagGr, spaceGr)
szGrid1.Add(self.grLang1, propGr, flagGr, spaceGr)
szGrid2.Add(self.lblFileName2, 0, flagGr, spaceGr)
szGrid2.Add(self.grLang2, propGr, flagGr, spaceGr)
szBtSave1.Add(self.btSave1, 0, flagGrBt, space)
szBtSave1.Add(self.btInsert1, 0, flagGrBt, space)
szBtSave2.Add(self.btSave2, 0, flagGrBt, space)
szBtSave2.Add(self.btInsert2, 0, flagGrBt, space)
szCheck.Add(self.chkAddSelection, 0, wx.EXPAND | wx.ALL, 2)
szCheck.Add(self.chkDelSelection, 0, wx.EXPAND| wx.ALL, 2)
szCheck.Add(self.lstDiff, 1, wx.EXPAND| wx.ALL, 2)
self.szWork.AddMany( [ (szBtSearch1, 0, wx.SHAPED),
(szBtSearch2, 0, wx.SHAPED),
(wx.Size(0,0), 0, 0),
(szGrid1, 0, wx.EXPAND),
(szGrid2, 0, wx.EXPAND),
(szCheck, 0, wx.EXPAND),
(szBtSave1, 0, wx.EXPAND),
(szBtSave2, 0, wx.EXPAND),
(wx.Size(0,0), 0, 0),
]
)
#szWork.AddGrowableCol(0)
#szWork.AddGrowableCol(1)
self.szWork.AddGrowableRow(1)
self.szWork.AddGrowableCol(2)
#szWork.AddGrowableRow(1)
szLbl.Add(self.lblTitle, 0 , wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5)
self.frameSizer.Add(szLbl, 0, wx.EXPAND)
self.frameSizer.Add(self.szWork, 1, wx.EXPAND)
self.dictValue1 = dict()
self.dictValue2 = dict()
self.dictDiff1 = dict()
self.dictDiff2 = dict()
self.grList = (self.grLang1, self.grLang2)
self.dictGrList = (self.dictValue1, self.dictValue2)
for i in self.grList:
i.CreateGrid(0,2)
i.SetRowLabelSize(30)
i.SetColLabelSize(20)
i.SetColSize(0, 100)
i.SetColSize(1, 160)
i.SetColLabelValue(0, "Key")
i.SetColLabelValue(1, "Value")
self.SetMinSize(wx.Size(400,550))
self.SetSizerAndFit(self.frameSizer)
self.Layout()
def onBtSearch(self, event):
"""
"""
fileName = ""
try:
dlg = wx.FileDialog(self, "Search file language", "./", "lang*.*")
dlg.ShowModal()
fileName = dlg.GetPath()
except:
pass
if not fileName: return
conf = RCP()
conf.read(fileName)
btId = event.GetEventObject().GetId()
if btId == BT_SRC1:
grForWork = self.grLang1
dictWork = self.dictGrList[0]
lblFileNameWork = self.lblFileName1
elif btId == BT_SRC2:
grForWork = self.grLang2
dictWork = self.dictGrList[1]
lblFileNameWork = self.lblFileName2
lblFileNameWork.SetLabel(fileName)
if grForWork.GetNumberRows():
grForWork.DeleteRows(0, grForWork.GetNumberRows())
rowNum = 0
sections = conf.sections()
sections.append('DEFAULT')
sections.sort()
optsDefault = [x for x in conf.defaults().keys()]
for sect in sections:
grForWork.AppendRows()
grForWork.SetCellValue(rowNum, 0, '%s %s' % (STR_INIT_SECT, sect.upper() ))
#grForWork.AppendRows()
rowNum += 1
if sect == 'DEFAULT':
opts = [x for x in conf.defaults().keys()]
opt_val = [ (opt, conf.get(sect, opt)) for opt in opts]
else:
opts = conf.options(sect)
opt_val = [ (opt, conf.get(sect, opt)) for opt in opts if opt not in optsDefault]
values = list()
opt_val.sort()
dictWork[sect] = opt_val
for opt, val in opt_val:
grForWork.AppendRows()
grForWork.SetCellValue(rowNum, 0, opt)
grForWork.SetCellValue(rowNum, 1, val)
rowNum += 1
grForWork.AppendRows()
rowNum += 1
self.szWork.Layout()
#self.Layout()
def onBtSave(self, event):
"""
"""
self.reloadDict()
bt = event.GetEventObject().GetId()
self._save(bt)
def onBtDiff(self, event):
"""
"""
self.reloadDict()
bt = event.GetEventObject()
if bt.GetId() == BT_DIFF1:
dictFirst = self.dictGrList[0]
dictSec = self.dictGrList[1]
lblFirst = self.lblFileName1
lblSec = self.lblFileName2
else:
dictFirst = self.dictGrList[1]
dictSec = self.dictGrList[0]
lblFirst = self.lblFileName2
lblSec = self.lblFileName1
lstDiff = self._makeDiff(dictFirst, dictSec)
lst2Diff = self._makeDiff(dictSec, dictFirst, 2)
lstDiff.insert(0, '%s -> %s' % (lblFirst.GetLabel(), lblSec.GetLabel() ) )
lstDiff.append('')
lst2Diff.insert(0, '%s -> %s' % (lblSec.GetLabel(), lblFirst.GetLabel() ) )
self.lstDiff.Clear()
self.lstDiff.AppendItems(lstDiff)
self.lstDiff.AppendItems(lst2Diff)
def onLstDiffClick(self, event):
"""
"""
stringClick = event.GetString()
if not stringClick.startswith('No key'): return
lstString = stringClick.split(' ')
k, sect = lstString[2], lstString[-1]
kDict1 = self.dictDiff1.keys()
kDict2 = self.dictDiff2.keys()
if k in self.dictDiff1.keys() and self.dictDiff1[k] == sect:
diff = 1
elif k in self.dictDiff2.keys() and self.dictDiff2[k] == sect:
diff = 2
#I don't found any values -> return
else:
return
if diff == 1:
gridWork = self.grLang1
gridSec = self.grLang2
else:
gridWork = self.grLang2
gridSec = self.grLang1
rowSect = self._getRowKey(gridWork, k, sect=sect)
rowSec = self._getRowSection(gridSec, sect)
#If check delete are present, remove the row and
#select the section
if self.chkDelSelection.GetValue():
rowSec = self._getRowSection(gridWork, sect)
gridWork.DeleteRows(rowSect)
gridWork.SelectBlock(rowSec, 0,rowSec, 0)
gridWork.MakeCellVisible(rowSec, 0)
return
gridWork.SelectBlock(rowSect, 0,rowSect, 0)
gridWork.MakeCellVisible(rowSect, 0)
colSelect = 0
if self.chkAddSelection.GetValue():
rowSec += 1
gridSec.InsertRows(rowSec)
valueWork = gridWork.GetCellValue(rowSect, 1)
gridSec.SetCellValue(rowSec, 0, k)
gridSec.SetCellValue(rowSec, 1, valueWork)
colSelect = 1
gridSec.SelectBlock(rowSec, 0,rowSec, colSelect)
gridSec.MakeCellVisible(rowSec, 0)
#print sect, k, rowSect, row
def reloadDict(self, ):
"""
"""
self.dictGrList = list()
for num, grid in enumerate(self.grList):
dictWork = dict()
sectWork = None
valueWork = None
for row in xrange( grid.GetNumberRows() ):
key = grid.GetCellValue(row, 0)
value = grid.GetCellValue(row, 1)
if (key == '' and value == ''): continue
#This is the new section, so update the previous
if key.startswith(STR_INIT_SECT):
oldSect = sectWork
sectWork = key[len(STR_INIT_SECT):].strip().lower()
if oldSect:
dictWork[oldSect] = valueWork
valueWork = list()
continue
valueWork.append( (key, value) )
#For the last section
dictWork[sectWork] = valueWork
self.dictGrList.append(dictWork)
def onBtInsert(self, event):
"""
"""
bt = event.GetEventObject()
if bt.GetId() == BT_INSERT1:
grWork = self.grLang1
elif bt.GetId() == BT_INSERT2:
grWork = self.grLang2
if grWork.GetSelectedRows():
selRows = grWork.GetSelectedRows()
grWork.InsertRows(selRows[0], len(selRows))
else:
grWork.AppendRows()
def onClose(self, event):
dlg = wx.MessageDialog(self, "Do you want to save", "Save?",
wx.ICON_QUESTION | wx.YES_NO)
#Go out !
if dlg.ShowModal() == wx.ID_YES:
self._save(BT_SAV1)
self._save(BT_SAV2)
dlg.Destroy()
event.Skip()
self.Destroy()
#pass
def _makeDiff(self,dictFirst, dictSec, state=1):
"""
"""
keyDictFirst = dictFirst.keys()
keyDictFirst.sort()
lstDiff = list()
if state == 1:
dictWork = self.dictDiff1
else:
dictWork = self.dictDiff2
for k in keyDictFirst:
#Section control
if k not in dictSec.keys():
lstDiff.append( 'No section %s' % k )
continue
kvListFirst = dictFirst[k]
kvListSec = dictSec[k]
if not kvListSec: return lstDiff
kListSec = [ x[0] for x in kvListSec]
kListSec.sort()
#Key - value control
for kv in kvListFirst:
key, value = kv
dictWork[key] = k
if not key in kListSec:
lstDiff.append( 'No key %s in section %s' % (key, k) )
return lstDiff
def _getRowSection(self, gridWork, sect):
""" Return the row where the specific section are present
"""
numRows = gridWork.GetNumberRows()
for row in xrange(numRows):
key = gridWork.GetCellValue(row, 0)
#I'm on the right section
if key.startswith(STR_INIT_SECT) and \
key[len(STR_INIT_SECT):].strip().lower() == sect:
break
return row
def _getRowKey(self, gridWork, keyToFound, row = 0, sect=None):
""" Return the row where the key are present, optionally
start at the specific section
"""
if sect:
row = self._getRowSection(gridWork, sect)
numRows = gridWork.GetNumberRows()
for rowSect in xrange(row, numRows):
key = gridWork.GetCellValue(rowSect, 0)
if key == keyToFound:
break
return rowSect
def _save(self, side):
""" Internal save
"""
if side == BT_SAV1:
dictFirst = self.dictGrList[0]
lblFileName = self.lblFileName1
dictWork = self.dictGrList[0]
elif side == BT_SAV2:
dictFirst = self.dictGrList[0]
lblFileName = self.lblFileName2
dictWork = self.dictGrList[1]
else:
return
fileName = lblFileName.GetLabel()
#No file load, return
if fileName == 'File name': return
copyfile( fileName, '%s_%d_old' % (fileName, time.time()) )
#Leave the os to write the file
time.sleep(0.2)
dictDefault = dict()
for k, v in dictWork['default']:
dictDefault[k] = v
conf = RCP(dictDefault)
f = open(fileName, 'w')
for sect in dictWork.keys():
if sect == 'default': continue
conf.add_section(sect)
for kv in dictWork[sect]:
key, value = kv
conf.set(sect, key, value)
conf.write(f)
f.close()
if __name__ == '__main__':
app = wx.App(0)
wx.InitAllImageHandlers()
frame = tranlateFrame(None, -1, "Translation tool - by Unipex srl - www.unipex.it")
frame.CenterOnScreen()
frame.Show()
app.MainLoop()
|