Poparse is a script that can fill empty .pot files with msgid strings from pre-existing .po files. It uses the gdbm database routine.
Gdbm is a database routine that allows you to use dictionary methods. I found it after I was pondering about using sqlite3 for this very script (project). I am not too sure if I will continue working on this. Anyway, have a look:
import shlex
import gdbm
dictionary = gdbm.open(‘dictionary’,‘c’)
def voraciousEater(filename):
"""This function saves the msgstrs corresponding to the msgids in a gdbm database
routine named `dictionary`."""
po_file = open(filename, ‘r’)
po_file_text = po_file.readlines() #We obtain a list of the lines in the po file.
for line in po_file_text:
if line.find("msgid") != -1:
number = po_file_text.index(line)
message_id = shlex.split(line)[1]
value_of_msg_id = shlex.split(po_file_text[number + 1])[1]
dictionary[message_id] = value_of_msg_id
def efficientFiller(filename):
"""This function creates a new po file and fills it using entries saved previously ."""
pot_file = open(filename,‘r’)
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if dictionary.has_key(message_id):
number = pot_file_text.index(line)
corresponding_crap = dictionary[message_id]
final_string = ‘msgstr’ + " " + ‘"’ + corresponding_crap + ‘"’ + ‘\n‘
pot_file_text[number+1] = final_string
yield pot_file_text #a very huge output of lists is generated
pot_file.close()
def writer(filename):
output_file = filename[:-1] #create the pot file
outfile = open(output_file,‘w’)
efficient_filler = efficientFiller(filename)
try:
new_list = list(efficient_filler)[-1] #picking the last list that the yield statement generates.
except IndexError:
pass
for line in new_list:
outfile.writelines(line)
outfile.close
filename = raw_input("Give me the pot file or the po file and I will do the rest: ")
if filename.endswith(".pot"):
writer(filename)
elif filename.endswith(".po"):
voraciousEater(filename)
else:
print "File type not recognised. Ensure that the file ends in .po or .pot"
Post a Comment