# -*- coding: utf-8 -*-
import  xdrlib ,sys
import xlwt
import string
import re

import sys   
sys.setrecursionlimit(100000000)

global dataMap
global languageList

def Filter(pattern, s, pos):
    result=[]
    find=pattern.search(s, pos)
    if not find:
        return result
    else:
        result.append(find)
    if find.end() < len(s):
        result.extend(Filter(pattern, s, find.end()))
    #s=re.findall(pattern, s)
    return result

def readDB(stringFile, languageNAME):
	global dataMap
	global languageList
	languageList.append(languageNAME)
	file = open(stringFile, "r")
	s = file.read()
	#pattern=re.compile(r'""(?P<keyName>.*)""[ |\t]+=[ |\t]+""(?P<textContent>.*)""')
	pattern=re.compile(r'\n[\W]+(?P<keyName>[\w]+):[^"]*"(?P<textContent>[^"]*)"')
	find=Filter(pattern, s, 0)
	for info in find:
		key = info.group("keyName")
		value = info.group("textContent")
		if key not in dataMap:
			dataMap[key] = dict()
		dataMap[key][languageNAME] = value

def writeExcel():
	global dataMap
	global languageList
	data = xlwt.Workbook('utf-8')
	table = data.add_sheet('MENU')
	table.write(0, 0, 'Section')
	table.write(0, 1, 'ID')
	table.write(0, 2, 'KEY')
	for i in range(0, len(languageList)):
		table.write(0, 3 + i, languageList[i])
	rowNum = len(dataMap)
	i = 1
	for key in dataMap.keys():
		table.write(i, 0, 'MENU')
		table.write(i, 1, OnlyCharNum(key))
		table.write(i, 2, key)
		for j in range(0, len(languageList)):
			table.write(i, 3 + j, GetExcelValue(key, languageList[j]))
		i = i + 1
	data.save('outServer.xls')

def OnlyCharNum(s,oth=''):
	s2 = s.upper();
	fomart = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789'
	for c in s2:
		if not c in fomart:
			if ' ' == c:
				s2 = s2.replace(c,'_');
			else:
				s2 = s2.replace(c,'');
	return s2;

def GetExcelValue(key, language):
	global dataMap
	if key not in dataMap:
		print("Can't find this key %s, lan %s" % (key, language))
		return ""
	childData = dataMap[key]
	if language not in childData:
		print("Can't find this language text %s, lan %s" % (key, language))
		return ""
	return childData[language]

if __name__=="__main__":
	global dataMap
	global languageList
	dataMap = dict()
	languageList = []
	if (len(sys.argv) %2 == 1):
		for i in range((len(sys.argv)-1)/2):
			readDB(sys.argv[i*2+1], sys.argv[i*2+2])
		writeExcel()
		print("done")
	else:
		print("wrong when export, require [strings language] pair")
# -*- coding: utf-8 -*-
import  xdrlib ,sys
import xlwt
import string
import re

import sys   
sys.setrecursionlimit(100000000)

global dataMap
global languageList

def Filter(pattern, s, pos):
    result=[]
    find=pattern.search(s, pos)
    if not find:
        return result
    else:
        result.append(find)
    if find.end() < len(s):
        result.extend(Filter(pattern, s, find.end()))
    #s=re.findall(pattern, s)
    return result

def readDB(stringFile, languageNAME):
	global dataMap
	global languageList
	languageList.append(languageNAME)
	file = open(stringFile, "r")
	s = file.read()
	#pattern=re.compile(r'""(?P<keyName>.*)""[ |\t]+=[ |\t]+""(?P<textContent>.*)""')
	pattern=re.compile(r'\n[\W]+(?P<keyName>[\w]+):[^"]*"(?P<textContent>[^"]*)"')
	find=Filter(pattern, s, 0)
	for info in find:
		key = info.group("keyName")
		value = info.group("textContent")
		if key not in dataMap:
			dataMap[key] = dict()
		dataMap[key][languageNAME] = value

def writeExcel():
	global dataMap
	global languageList
	data = xlwt.Workbook('utf-8')
	table = data.add_sheet('MENU')
	table.write(0, 0, 'Section')
	table.write(0, 1, 'ID')
	table.write(0, 2, 'KEY')
	for i in range(0, len(languageList)):
		table.write(0, 3 + i, languageList[i])
	rowNum = len(dataMap)
	i = 1
	for key in dataMap.keys():
		table.write(i, 0, 'MENU')
		table.write(i, 1, OnlyCharNum(key))
		table.write(i, 2, key)
		for j in range(0, len(languageList)):
			table.write(i, 3 + j, GetExcelValue(key, languageList[j]))
		i = i + 1
	data.save('outServer.xls')

def OnlyCharNum(s,oth=''):
	s2 = s.upper();
	fomart = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789'
	for c in s2:
		if not c in fomart:
			if ' ' == c:
				s2 = s2.replace(c,'_');
			else:
				s2 = s2.replace(c,'');
	return s2;

def GetExcelValue(key, language):
	global dataMap
	if key not in dataMap:
		print("Can't find this key %s, lan %s" % (key, language))
		return ""
	childData = dataMap[key]
	if language not in childData:
		print("Can't find this language text %s, lan %s" % (key, language))
		return ""
	return childData[language]

if __name__=="__main__":
	global dataMap
	global languageList
	dataMap = dict()
	languageList = []
	if (len(sys.argv) %2 == 1):
		for i in range((len(sys.argv)-1)/2):
			readDB(sys.argv[i*2+1], sys.argv[i*2+2])
		writeExcel()
		print("done")
	else:
		print("wrong when export, require [strings language] pair")
# -*- coding: utf-8 -*- #s=re.findall(pattern, s) #pattern=re.compile(r'""(?P<keyName>.*)""[ |\t]+=[ |\t]+""(?P<textContent>.*)""')