home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1382 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import struct
  7. import array
  8. __version__ = '1.2'
  9.  
  10. def usage(code, msg = ''):
  11.     print >>sys.stderr, __doc__
  12.     if msg:
  13.         print >>sys.stderr, msg
  14.     
  15.     sys.exit(code)
  16.  
  17.  
  18. def add(id, str, fuzzy, MESSAGES):
  19.     if not fuzzy and str:
  20.         MESSAGES[id] = str
  21.     
  22.  
  23.  
  24. def generate(MESSAGES):
  25.     keys = MESSAGES.keys()
  26.     keys.sort()
  27.     offsets = []
  28.     ids = strs = ''
  29.     for id in keys:
  30.         offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
  31.         ids += id + '\x00'
  32.         strs += MESSAGES[id] + '\x00'
  33.     
  34.     keystart = 28 + 16 * len(keys)
  35.     valuestart = keystart + len(ids)
  36.     koffsets = []
  37.     voffsets = []
  38.     for o1, l1, o2, l2 in offsets:
  39.         koffsets += [
  40.             l1,
  41.             o1 + keystart]
  42.         voffsets += [
  43.             l2,
  44.             o2 + valuestart]
  45.     
  46.     offsets = koffsets + voffsets
  47.     output = struct.pack('Iiiiiii', 0x950412DEL, 0, len(keys), 28, 28 + len(keys) * 8, 0, 0)
  48.     output += array.array('i', offsets).tostring()
  49.     output += ids
  50.     output += strs
  51.     return output
  52.  
  53.  
  54. def make(filename, outfile):
  55.     MESSAGES = { }
  56.     ID = 1
  57.     STR = 2
  58.     if filename.endswith('.po'):
  59.         infile = filename
  60.     else:
  61.         infile = filename + '.po'
  62.     if outfile is None:
  63.         outfile = os.path.splitext(infile)[0] + '.mo'
  64.     
  65.     
  66.     try:
  67.         lines = open(infile).readlines()
  68.     except IOError:
  69.         msg = None
  70.         print >>sys.stderr, msg
  71.         sys.exit(1)
  72.  
  73.     section = None
  74.     fuzzy = 0
  75.     lno = 0
  76.     msgid = msgstr = ''
  77.     for l in lines:
  78.         lno += 1
  79.         if l[0] == '#' and section == STR:
  80.             add(msgid, msgstr, fuzzy, MESSAGES)
  81.             section = None
  82.             fuzzy = 0
  83.         
  84.         if l[:2] == '#,' and 'fuzzy' in l:
  85.             fuzzy = 1
  86.         
  87.         if l[0] == '#':
  88.             continue
  89.         
  90.         if l.startswith('msgid'):
  91.             if section == STR:
  92.                 add(msgid, msgstr, fuzzy, MESSAGES)
  93.             
  94.             section = ID
  95.             l = l[5:]
  96.             msgid = msgstr = ''
  97.         elif l.startswith('msgstr'):
  98.             section = STR
  99.             l = l[6:]
  100.         
  101.         l = l.strip()
  102.         if not l:
  103.             continue
  104.         
  105.         l = eval(l)
  106.         if section == ID:
  107.             msgid += l
  108.             continue
  109.         if section == STR:
  110.             msgstr += l
  111.             continue
  112.         print >>sys.stderr, 'Syntax error on %s:%d' % (infile, lno), 'before:'
  113.         print >>sys.stderr, l
  114.         sys.exit(1)
  115.     
  116.     if section == STR:
  117.         add(msgid, msgstr, fuzzy, MESSAGES)
  118.     
  119.     output = generate(MESSAGES)
  120.     
  121.     try:
  122.         outfile.write(output)
  123.     except IOError:
  124.         msg = None
  125.         print >>sys.stderr, msg
  126.  
  127.  
  128.  
  129. def main(outfile, args = sys.argv[1:]):
  130.     for filename in args:
  131.         make(filename, outfile)
  132.     
  133.     return 0
  134.  
  135. if __name__ == '__main__':
  136.     sys.exit(main(sys.stdout))
  137.  
  138.