(filename, outfile)
| 98 | |
| 99 | |
| 100 | def make(filename, outfile): |
| 101 | ID = 1 |
| 102 | STR = 2 |
| 103 | CTXT = 3 |
| 104 | |
| 105 | # Compute .mo name from .po name and arguments |
| 106 | if filename.endswith('.po'): |
| 107 | infile = filename |
| 108 | else: |
| 109 | infile = filename + '.po' |
| 110 | if outfile is None: |
| 111 | outfile = os.path.splitext(infile)[0] + '.mo' |
| 112 | |
| 113 | try: |
| 114 | with open(infile, 'rb') as f: |
| 115 | lines = f.readlines() |
| 116 | except OSError as msg: |
| 117 | print(msg, file=sys.stderr) |
| 118 | sys.exit(1) |
| 119 | |
| 120 | if lines[0].startswith(codecs.BOM_UTF8): |
| 121 | print( |
| 122 | f"The file {infile} starts with a UTF-8 BOM which is not allowed in .po files.\n" |
| 123 | "Please save the file without a BOM and try again.", |
| 124 | file=sys.stderr |
| 125 | ) |
| 126 | sys.exit(1) |
| 127 | |
| 128 | section = msgctxt = None |
| 129 | msgid = msgstr = b'' |
| 130 | fuzzy = 0 |
| 131 | |
| 132 | # Start off assuming Latin-1, so everything decodes without failure, |
| 133 | # until we know the exact encoding |
| 134 | encoding = 'latin-1' |
| 135 | |
| 136 | # Parse the catalog |
| 137 | lno = 0 |
| 138 | for l in lines: |
| 139 | l = l.decode(encoding) |
| 140 | lno += 1 |
| 141 | # If we get a comment line after a msgstr, this is a new entry |
| 142 | if l[0] == '#' and section == STR: |
| 143 | add(msgctxt, msgid, msgstr, fuzzy) |
| 144 | section = msgctxt = None |
| 145 | fuzzy = 0 |
| 146 | # Record a fuzzy mark |
| 147 | if l[:2] == '#,' and 'fuzzy' in l: |
| 148 | fuzzy = 1 |
| 149 | # Skip comments |
| 150 | if l[0] == '#': |
| 151 | continue |
| 152 | # Now we are in a msgid or msgctxt section, output previous section |
| 153 | if l.startswith('msgctxt'): |
| 154 | if section == STR: |
| 155 | add(msgctxt, msgid, msgstr, fuzzy) |
| 156 | section = CTXT |
| 157 | l = l[7:] |
searching dependent graphs…