Back to index

python3.2  3.2.2
Functions | Variables
msgfmt Namespace Reference

Functions

def usage
def add
def generate
def make
def main

Variables

string __version__ = "1.1"
dictionary MESSAGES = {}

Function Documentation

def msgfmt.add (   id,
  str,
  fuzzy 
)

Definition at line 48 of file msgfmt.py.

00048 
00049 def add(id, str, fuzzy):
00050     "Add a non-fuzzy translation to the dictionary."
00051     global MESSAGES
00052     if not fuzzy and str:
00053         MESSAGES[id] = str
00054 
00055 


Here is the caller graph for this function:

def msgfmt.generate ( )

Definition at line 56 of file msgfmt.py.

00056 
00057 def generate():
00058     "Return the generated output."
00059     global MESSAGES
00060     # the keys are sorted in the .mo file
00061     keys = sorted(MESSAGES.keys())
00062     offsets = []
00063     ids = strs = b''
00064     for id in keys:
00065         # For each string, we need size and file offset.  Each string is NUL
00066         # terminated; the NUL does not count into the size.
00067         offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
00068         ids += id + b'\0'
00069         strs += MESSAGES[id] + b'\0'
00070     output = ''
00071     # The header is 7 32-bit unsigned integers.  We don't use hash tables, so
00072     # the keys start right after the index tables.
00073     # translated string.
00074     keystart = 7*4+16*len(keys)
00075     # and the values start after the keys
00076     valuestart = keystart + len(ids)
00077     koffsets = []
00078     voffsets = []
00079     # The string table first has the list of keys, then the list of values.
00080     # Each entry has first the size of the string, then the file offset.
00081     for o1, l1, o2, l2 in offsets:
00082         koffsets += [l1, o1+keystart]
00083         voffsets += [l2, o2+valuestart]
00084     offsets = koffsets + voffsets
00085     output = struct.pack("Iiiiiii",
00086                          0x950412de,       # Magic
00087                          0,                 # Version
00088                          len(keys),         # # of entries
00089                          7*4,               # start of key index
00090                          7*4+len(keys)*8,   # start of value index
00091                          0, 0)              # size and offset of hash table
00092     output += array.array("i", offsets).tostring()
00093     output += ids
00094     output += strs
00095     return output
00096 
00097 


Here is the call graph for this function:

Here is the caller graph for this function:

def msgfmt.main ( void  )

Definition at line 208 of file msgfmt.py.

00208 
00209 def main():
00210     try:
00211         opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
00212                                    ['help', 'version', 'output-file='])
00213     except getopt.error as msg:
00214         usage(1, msg)
00215 
00216     outfile = None
00217     # parse options
00218     for opt, arg in opts:
00219         if opt in ('-h', '--help'):
00220             usage(0)
00221         elif opt in ('-V', '--version'):
00222             print("msgfmt.py", __version__, file=sys.stderr)
00223             sys.exit(0)
00224         elif opt in ('-o', '--output-file'):
00225             outfile = arg
00226     # do it
00227     if not args:
00228         print('No input file given', file=sys.stderr)
00229         print("Try `msgfmt --help' for more information.", file=sys.stderr)
00230         return
00231 
00232     for filename in args:
00233         make(filename, outfile)
00234 

Here is the call graph for this function:

def msgfmt.make (   filename,
  outfile 
)

Definition at line 98 of file msgfmt.py.

00098 
00099 def make(filename, outfile):
00100     ID = 1
00101     STR = 2
00102 
00103     # Compute .mo name from .po name and arguments
00104     if filename.endswith('.po'):
00105         infile = filename
00106     else:
00107         infile = filename + '.po'
00108     if outfile is None:
00109         outfile = os.path.splitext(infile)[0] + '.mo'
00110 
00111     try:
00112         lines = open(infile, 'rb').readlines()
00113     except IOError as msg:
00114         print(msg, file=sys.stderr)
00115         sys.exit(1)
00116 
00117     section = None
00118     fuzzy = 0
00119 
00120     # Start off assuming Latin-1, so everything decodes without failure,
00121     # until we know the exact encoding
00122     encoding = 'latin-1'
00123 
00124     # Parse the catalog
00125     lno = 0
00126     for l in lines:
00127         l = l.decode(encoding)
00128         lno += 1
00129         # If we get a comment line after a msgstr, this is a new entry
00130         if l[0] == '#' and section == STR:
00131             add(msgid, msgstr, fuzzy)
00132             section = None
00133             fuzzy = 0
00134         # Record a fuzzy mark
00135         if l[:2] == '#,' and 'fuzzy' in l:
00136             fuzzy = 1
00137         # Skip comments
00138         if l[0] == '#':
00139             continue
00140         # Now we are in a msgid section, output previous section
00141         if l.startswith('msgid') and not l.startswith('msgid_plural'):
00142             if section == STR:
00143                 add(msgid, msgstr, fuzzy)
00144                 if not msgid:
00145                     # See whether there is an encoding declaration
00146                     p = HeaderParser()
00147                     charset = p.parsestr(msgstr.decode(encoding)).get_content_charset()
00148                     if charset:
00149                         encoding = charset
00150             section = ID
00151             l = l[5:]
00152             msgid = msgstr = b''
00153             is_plural = False
00154         # This is a message with plural forms
00155         elif l.startswith('msgid_plural'):
00156             if section != ID:
00157                 print('msgid_plural not preceeded by msgid on %s:%d' % (infile, lno),
00158                       file=sys.stderr)
00159                 sys.exit(1)
00160             l = l[12:]
00161             msgid += b'\0' # separator of singular and plural
00162             is_plural = True
00163         # Now we are in a msgstr section
00164         elif l.startswith('msgstr'):
00165             section = STR
00166             if l.startswith('msgstr['):
00167                 if not is_plural:
00168                     print('plural without msgid_plural on %s:%d' % (infile, lno),
00169                           file=sys.stderr)
00170                     sys.exit(1)
00171                 l = l.split(']', 1)[1]
00172                 if msgstr:
00173                     msgstr += b'\0' # Separator of the various plural forms
00174             else:
00175                 if is_plural:
00176                     print('indexed msgstr required for plural on  %s:%d' % (infile, lno),
00177                           file=sys.stderr)
00178                     sys.exit(1)
00179                 l = l[6:]
00180         # Skip empty lines
00181         l = l.strip()
00182         if not l:
00183             continue
00184         # XXX: Does this always follow Python escape semantics?
00185         l = eval(l)
00186         if section == ID:
00187             msgid += l.encode(encoding)
00188         elif section == STR:
00189             msgstr += l.encode(encoding)
00190         else:
00191             print('Syntax error on %s:%d' % (infile, lno), \
00192                   'before:', file=sys.stderr)
00193             print(l, file=sys.stderr)
00194             sys.exit(1)
00195     # Add last entry
00196     if section == STR:
00197         add(msgid, msgstr, fuzzy)
00198 
00199     # Compute output
00200     output = generate()
00201 
00202     try:
00203         open(outfile,"wb").write(output)
00204     except IOError as msg:
00205         print(msg, file=sys.stderr)
00206 
00207 


Here is the call graph for this function:

Here is the caller graph for this function:

def msgfmt.usage (   code,
  msg = '' 
)

Definition at line 40 of file msgfmt.py.

00040 
00041 def usage(code, msg=''):
00042     print(__doc__, file=sys.stderr)
00043     if msg:
00044         print(msg, file=sys.stderr)
00045     sys.exit(code)
00046 
00047 


Here is the caller graph for this function:


Variable Documentation

Definition at line 34 of file msgfmt.py.

Definition at line 36 of file msgfmt.py.