Return the generated output.
()
| 52 | |
| 53 | |
| 54 | def generate(): |
| 55 | "Return the generated output." |
| 56 | global MESSAGES |
| 57 | # the keys are sorted in the .mo file |
| 58 | keys = sorted(MESSAGES.keys()) |
| 59 | offsets = [] |
| 60 | ids = strs = b'' |
| 61 | for id in keys: |
| 62 | # For each string, we need size and file offset. Each string is NUL |
| 63 | # terminated; the NUL does not count into the size. |
| 64 | offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id]))) |
| 65 | ids += id + b'\0' |
| 66 | strs += MESSAGES[id] + b'\0' |
| 67 | output = '' |
| 68 | # The header is 7 32-bit unsigned integers. We don't use hash tables, so |
| 69 | # the keys start right after the index tables. |
| 70 | # translated string. |
| 71 | keystart = 7*4+16*len(keys) |
| 72 | # and the values start after the keys |
| 73 | valuestart = keystart + len(ids) |
| 74 | koffsets = [] |
| 75 | voffsets = [] |
| 76 | # The string table first has the list of keys, then the list of values. |
| 77 | # Each entry has first the size of the string, then the file offset. |
| 78 | for o1, l1, o2, l2 in offsets: |
| 79 | koffsets += [l1, o1+keystart] |
| 80 | voffsets += [l2, o2+valuestart] |
| 81 | offsets = koffsets + voffsets |
| 82 | output = struct.pack("Iiiiiii", |
| 83 | 0x950412de, # Magic |
| 84 | 0, # Version |
| 85 | len(keys), # # of entries |
| 86 | 7*4, # start of key index |
| 87 | 7*4+len(keys)*8, # start of value index |
| 88 | 0, 0) # size and offset of hash table |
| 89 | try: |
| 90 | output += array.array("i", offsets).tostring() |
| 91 | except AttributeError: |
| 92 | output += array.array("i", offsets).tobytes() |
| 93 | output += ids |
| 94 | output += strs |
| 95 | return output |
| 96 | |
| 97 | |
| 98 | def reset(): |