Output the per-prefix data in Python form to the given file
(prefixdata, outfilename, module_prefix, varprefix, per_locale, chunks)
| 162 | |
| 163 | |
| 164 | def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix, per_locale, chunks): |
| 165 | """Output the per-prefix data in Python form to the given file """ |
| 166 | sorted_keys = sorted(prefixdata.keys()) |
| 167 | total_keys = len(sorted_keys) |
| 168 | if chunks == -1: |
| 169 | chunk_size = PREFIXDATA_CHUNK_SIZE |
| 170 | total_chunks = int(math.ceil(total_keys / float(chunk_size))) |
| 171 | else: |
| 172 | chunk_size = int(math.ceil(total_keys / float(chunks))) |
| 173 | total_chunks = chunks |
| 174 | |
| 175 | outdirname = os.path.dirname(outfilename) |
| 176 | longest_prefix = 0 |
| 177 | for chunk_num in range(total_chunks): |
| 178 | chunk_index = chunk_size * chunk_num |
| 179 | chunk_keys = sorted_keys[chunk_index:chunk_index + chunk_size] |
| 180 | chunk_data = {} |
| 181 | for key in chunk_keys: |
| 182 | chunk_data[key] = prefixdata[key] |
| 183 | chunk_file = os.path.join(outdirname, 'data%d.py' % chunk_num) |
| 184 | chunk_longest = output_prefixdata_chunk( |
| 185 | chunk_data, chunk_file, module_prefix, per_locale) |
| 186 | if chunk_longest > longest_prefix: |
| 187 | longest_prefix = chunk_longest |
| 188 | |
| 189 | with open(outfilename, "w") as outfile: |
| 190 | if per_locale: |
| 191 | prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix}, file=outfile) |
| 192 | else: |
| 193 | prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix}, file=outfile) |
| 194 | prnt(COPYRIGHT_NOTICE, file=outfile) |
| 195 | prnt("%s_DATA = {}" % varprefix, file=outfile) |
| 196 | for chunk_num in range(total_chunks): |
| 197 | prnt("from .data%d import data" % chunk_num, file=outfile) |
| 198 | prnt("%s_DATA.update(data)" % varprefix, file=outfile) |
| 199 | prnt("del data", file=outfile) |
| 200 | prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix), file=outfile) |
| 201 | |
| 202 | # Emit corresponding typing info. |
| 203 | with open(outfilename + "i", "w") as pyifile: |
| 204 | if per_locale: |
| 205 | prnt("%s_DATA: dict[str, dict[str, str]]" % varprefix, file=pyifile) |
| 206 | else: |
| 207 | prnt("%s_DATA: dict[str, tuple[str, ...]]" % varprefix, file=pyifile) |
| 208 | prnt("%s_LONGEST_PREFIX: int" % varprefix, file=pyifile) |
| 209 | |
| 210 | |
| 211 | def output_prefixdata_chunk(prefixdata, outfilename, module_prefix, per_locale): |
no test coverage detected