| 102 | |
| 103 | |
| 104 | def make(filename, outfile): |
| 105 | ID = 1 |
| 106 | STR = 2 |
| 107 | |
| 108 | # Compute .mo name from .po name and arguments |
| 109 | if filename.endswith('.po'): |
| 110 | infile = filename |
| 111 | else: |
| 112 | infile = filename + '.po' |
| 113 | if outfile is None: |
| 114 | outfile = os.path.splitext(infile)[0] + '.mo' |
| 115 | |
| 116 | try: |
| 117 | lines = open(infile, 'rb').readlines() |
| 118 | except IOError as msg: |
| 119 | print(msg, file=sys.stderr) |
| 120 | sys.exit(1) |
| 121 | |
| 122 | section = None |
| 123 | fuzzy = 0 |
| 124 | |
| 125 | # Start off assuming Latin-1, so everything decodes without failure, |
| 126 | # until we know the exact encoding |
| 127 | encoding = 'latin-1' |
| 128 | |
| 129 | # Parse the catalog |
| 130 | lno = 0 |
| 131 | for l in lines: |
| 132 | l = l.decode(encoding) |
| 133 | lno += 1 |
| 134 | # If we get a comment line after a msgstr, this is a new entry |
| 135 | if l[0] == '#' and section == STR: |
| 136 | add(msgid, msgstr, fuzzy) |
| 137 | section = None |
| 138 | fuzzy = 0 |
| 139 | # Record a fuzzy mark |
| 140 | if l[:2] == '#,' and 'fuzzy' in l: |
| 141 | fuzzy = 1 |
| 142 | # Skip comments |
| 143 | if l[0] == '#': |
| 144 | continue |
| 145 | # Now we are in a msgid section, output previous section |
| 146 | if l.startswith('msgid') and not l.startswith('msgid_plural'): |
| 147 | if section == STR: |
| 148 | add(msgid, msgstr, fuzzy) |
| 149 | if not msgid: |
| 150 | # See whether there is an encoding declaration |
| 151 | p = HeaderParser() |
| 152 | charset = p.parsestr(msgstr.decode(encoding)).get_content_charset() |
| 153 | if charset: |
| 154 | encoding = charset |
| 155 | section = ID |
| 156 | l = l[5:] |
| 157 | msgid = msgstr = b'' |
| 158 | is_plural = False |
| 159 | # This is a message with plural forms |
| 160 | elif l.startswith('msgid_plural'): |
| 161 | if section != ID: |