Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/bin
File: msgfmt2.py
#! /usr/bin/python2.7
[0] Fix | Delete
# -*- coding: iso-8859-1 -*-
[1] Fix | Delete
[2] Fix | Delete
[3] Fix | Delete
"""Generate binary message catalog from textual translation description.
[4] Fix | Delete
[5] Fix | Delete
This program converts a textual Uniforum-style message catalog (.po file) into
[6] Fix | Delete
a binary GNU catalog (.mo file). This is essentially the same function as the
[7] Fix | Delete
GNU msgfmt program, however, it is a simpler implementation.
[8] Fix | Delete
[9] Fix | Delete
Usage: msgfmt.py [OPTIONS] filename.po
[10] Fix | Delete
[11] Fix | Delete
Options:
[12] Fix | Delete
-o file
[13] Fix | Delete
--output-file=file
[14] Fix | Delete
Specify the output file to write to. If omitted, output will go to a
[15] Fix | Delete
file named filename.mo (based off the input file name).
[16] Fix | Delete
[17] Fix | Delete
-h
[18] Fix | Delete
--help
[19] Fix | Delete
Print this message and exit.
[20] Fix | Delete
[21] Fix | Delete
-V
[22] Fix | Delete
--version
[23] Fix | Delete
Display version information and exit.
[24] Fix | Delete
"""
[25] Fix | Delete
[26] Fix | Delete
import os
[27] Fix | Delete
import sys
[28] Fix | Delete
import ast
[29] Fix | Delete
import getopt
[30] Fix | Delete
import struct
[31] Fix | Delete
import array
[32] Fix | Delete
[33] Fix | Delete
__version__ = "1.1"
[34] Fix | Delete
[35] Fix | Delete
MESSAGES = {}
[36] Fix | Delete
[37] Fix | Delete
[38] Fix | Delete
[39] Fix | Delete
def usage(code, msg=''):
[40] Fix | Delete
print >> sys.stderr, __doc__
[41] Fix | Delete
if msg:
[42] Fix | Delete
print >> sys.stderr, msg
[43] Fix | Delete
sys.exit(code)
[44] Fix | Delete
[45] Fix | Delete
[46] Fix | Delete
[47] Fix | Delete
def add(id, str, fuzzy):
[48] Fix | Delete
"Add a non-fuzzy translation to the dictionary."
[49] Fix | Delete
global MESSAGES
[50] Fix | Delete
if not fuzzy and str:
[51] Fix | Delete
MESSAGES[id] = str
[52] Fix | Delete
[53] Fix | Delete
[54] Fix | Delete
[55] Fix | Delete
def generate():
[56] Fix | Delete
"Return the generated output."
[57] Fix | Delete
global MESSAGES
[58] Fix | Delete
keys = MESSAGES.keys()
[59] Fix | Delete
# the keys are sorted in the .mo file
[60] Fix | Delete
keys.sort()
[61] Fix | Delete
offsets = []
[62] Fix | Delete
ids = strs = ''
[63] Fix | Delete
for id in keys:
[64] Fix | Delete
# For each string, we need size and file offset. Each string is NUL
[65] Fix | Delete
# terminated; the NUL does not count into the size.
[66] Fix | Delete
offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
[67] Fix | Delete
ids += id + '\0'
[68] Fix | Delete
strs += MESSAGES[id] + '\0'
[69] Fix | Delete
output = ''
[70] Fix | Delete
# The header is 7 32-bit unsigned integers. We don't use hash tables, so
[71] Fix | Delete
# the keys start right after the index tables.
[72] Fix | Delete
# translated string.
[73] Fix | Delete
keystart = 7*4+16*len(keys)
[74] Fix | Delete
# and the values start after the keys
[75] Fix | Delete
valuestart = keystart + len(ids)
[76] Fix | Delete
koffsets = []
[77] Fix | Delete
voffsets = []
[78] Fix | Delete
# The string table first has the list of keys, then the list of values.
[79] Fix | Delete
# Each entry has first the size of the string, then the file offset.
[80] Fix | Delete
for o1, l1, o2, l2 in offsets:
[81] Fix | Delete
koffsets += [l1, o1+keystart]
[82] Fix | Delete
voffsets += [l2, o2+valuestart]
[83] Fix | Delete
offsets = koffsets + voffsets
[84] Fix | Delete
output = struct.pack("Iiiiiii",
[85] Fix | Delete
0x950412deL, # Magic
[86] Fix | Delete
0, # Version
[87] Fix | Delete
len(keys), # # of entries
[88] Fix | Delete
7*4, # start of key index
[89] Fix | Delete
7*4+len(keys)*8, # start of value index
[90] Fix | Delete
0, 0) # size and offset of hash table
[91] Fix | Delete
output += array.array("i", offsets).tostring()
[92] Fix | Delete
output += ids
[93] Fix | Delete
output += strs
[94] Fix | Delete
return output
[95] Fix | Delete
[96] Fix | Delete
[97] Fix | Delete
[98] Fix | Delete
def make(filename, outfile):
[99] Fix | Delete
ID = 1
[100] Fix | Delete
STR = 2
[101] Fix | Delete
[102] Fix | Delete
# Compute .mo name from .po name and arguments
[103] Fix | Delete
if filename.endswith('.po'):
[104] Fix | Delete
infile = filename
[105] Fix | Delete
else:
[106] Fix | Delete
infile = filename + '.po'
[107] Fix | Delete
if outfile is None:
[108] Fix | Delete
outfile = os.path.splitext(infile)[0] + '.mo'
[109] Fix | Delete
[110] Fix | Delete
try:
[111] Fix | Delete
lines = open(infile).readlines()
[112] Fix | Delete
except IOError, msg:
[113] Fix | Delete
print >> sys.stderr, msg
[114] Fix | Delete
sys.exit(1)
[115] Fix | Delete
[116] Fix | Delete
section = None
[117] Fix | Delete
fuzzy = 0
[118] Fix | Delete
[119] Fix | Delete
# Parse the catalog
[120] Fix | Delete
lno = 0
[121] Fix | Delete
for l in lines:
[122] Fix | Delete
lno += 1
[123] Fix | Delete
# If we get a comment line after a msgstr, this is a new entry
[124] Fix | Delete
if l[0] == '#' and section == STR:
[125] Fix | Delete
add(msgid, msgstr, fuzzy)
[126] Fix | Delete
section = None
[127] Fix | Delete
fuzzy = 0
[128] Fix | Delete
# Record a fuzzy mark
[129] Fix | Delete
if l[:2] == '#,' and 'fuzzy' in l:
[130] Fix | Delete
fuzzy = 1
[131] Fix | Delete
# Skip comments
[132] Fix | Delete
if l[0] == '#':
[133] Fix | Delete
continue
[134] Fix | Delete
# Now we are in a msgid section, output previous section
[135] Fix | Delete
if l.startswith('msgid') and not l.startswith('msgid_plural'):
[136] Fix | Delete
if section == STR:
[137] Fix | Delete
add(msgid, msgstr, fuzzy)
[138] Fix | Delete
section = ID
[139] Fix | Delete
l = l[5:]
[140] Fix | Delete
msgid = msgstr = ''
[141] Fix | Delete
is_plural = False
[142] Fix | Delete
# This is a message with plural forms
[143] Fix | Delete
elif l.startswith('msgid_plural'):
[144] Fix | Delete
if section != ID:
[145] Fix | Delete
print >> sys.stderr, 'msgid_plural not preceded by msgid on %s:%d' %\
[146] Fix | Delete
(infile, lno)
[147] Fix | Delete
sys.exit(1)
[148] Fix | Delete
l = l[12:]
[149] Fix | Delete
msgid += '\0' # separator of singular and plural
[150] Fix | Delete
is_plural = True
[151] Fix | Delete
# Now we are in a msgstr section
[152] Fix | Delete
elif l.startswith('msgstr'):
[153] Fix | Delete
section = STR
[154] Fix | Delete
if l.startswith('msgstr['):
[155] Fix | Delete
if not is_plural:
[156] Fix | Delete
print >> sys.stderr, 'plural without msgid_plural on %s:%d' %\
[157] Fix | Delete
(infile, lno)
[158] Fix | Delete
sys.exit(1)
[159] Fix | Delete
l = l.split(']', 1)[1]
[160] Fix | Delete
if msgstr:
[161] Fix | Delete
msgstr += '\0' # Separator of the various plural forms
[162] Fix | Delete
else:
[163] Fix | Delete
if is_plural:
[164] Fix | Delete
print >> sys.stderr, 'indexed msgstr required for plural on %s:%d' %\
[165] Fix | Delete
(infile, lno)
[166] Fix | Delete
sys.exit(1)
[167] Fix | Delete
l = l[6:]
[168] Fix | Delete
# Skip empty lines
[169] Fix | Delete
l = l.strip()
[170] Fix | Delete
if not l:
[171] Fix | Delete
continue
[172] Fix | Delete
l = ast.literal_eval(l)
[173] Fix | Delete
if section == ID:
[174] Fix | Delete
msgid += l
[175] Fix | Delete
elif section == STR:
[176] Fix | Delete
msgstr += l
[177] Fix | Delete
else:
[178] Fix | Delete
print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
[179] Fix | Delete
'before:'
[180] Fix | Delete
print >> sys.stderr, l
[181] Fix | Delete
sys.exit(1)
[182] Fix | Delete
# Add last entry
[183] Fix | Delete
if section == STR:
[184] Fix | Delete
add(msgid, msgstr, fuzzy)
[185] Fix | Delete
[186] Fix | Delete
# Compute output
[187] Fix | Delete
output = generate()
[188] Fix | Delete
[189] Fix | Delete
try:
[190] Fix | Delete
open(outfile,"wb").write(output)
[191] Fix | Delete
except IOError,msg:
[192] Fix | Delete
print >> sys.stderr, msg
[193] Fix | Delete
[194] Fix | Delete
[195] Fix | Delete
[196] Fix | Delete
def main():
[197] Fix | Delete
try:
[198] Fix | Delete
opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
[199] Fix | Delete
['help', 'version', 'output-file='])
[200] Fix | Delete
except getopt.error, msg:
[201] Fix | Delete
usage(1, msg)
[202] Fix | Delete
[203] Fix | Delete
outfile = None
[204] Fix | Delete
# parse options
[205] Fix | Delete
for opt, arg in opts:
[206] Fix | Delete
if opt in ('-h', '--help'):
[207] Fix | Delete
usage(0)
[208] Fix | Delete
elif opt in ('-V', '--version'):
[209] Fix | Delete
print >> sys.stderr, "msgfmt.py", __version__
[210] Fix | Delete
sys.exit(0)
[211] Fix | Delete
elif opt in ('-o', '--output-file'):
[212] Fix | Delete
outfile = arg
[213] Fix | Delete
# do it
[214] Fix | Delete
if not args:
[215] Fix | Delete
print >> sys.stderr, 'No input file given'
[216] Fix | Delete
print >> sys.stderr, "Try `msgfmt --help' for more information."
[217] Fix | Delete
return
[218] Fix | Delete
[219] Fix | Delete
for filename in args:
[220] Fix | Delete
make(filename, outfile)
[221] Fix | Delete
[222] Fix | Delete
[223] Fix | Delete
if __name__ == '__main__':
[224] Fix | Delete
main()
[225] Fix | Delete
[226] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function