Returns Python source for the given map. Comments are included in the source, if comments is true (default).
(name, map, encodingname, comments=1)
| 251 | return l |
| 252 | |
| 253 | def codegen(name, map, encodingname, comments=1): |
| 254 | |
| 255 | """ Returns Python source for the given map. |
| 256 | |
| 257 | Comments are included in the source, if comments is true (default). |
| 258 | |
| 259 | """ |
| 260 | # Generate code |
| 261 | decoding_map_code = python_mapdef_code( |
| 262 | 'decoding_map', |
| 263 | map, |
| 264 | comments=comments) |
| 265 | decoding_table_code = python_tabledef_code( |
| 266 | 'decoding_table', |
| 267 | map, |
| 268 | comments=comments) |
| 269 | encoding_map_code = python_mapdef_code( |
| 270 | 'encoding_map', |
| 271 | codecs.make_encoding_map(map), |
| 272 | comments=comments, |
| 273 | precisions=(4, 2)) |
| 274 | |
| 275 | if decoding_table_code: |
| 276 | suffix = 'table' |
| 277 | else: |
| 278 | suffix = 'map' |
| 279 | |
| 280 | l = [ |
| 281 | '''\ |
| 282 | """ Python Character Mapping Codec %s generated from '%s' with gencodec.py. |
| 283 | |
| 284 | """#" |
| 285 | |
| 286 | import codecs |
| 287 | |
| 288 | ### Codec APIs |
| 289 | |
| 290 | class Codec(codecs.Codec): |
| 291 | |
| 292 | def encode(self, input, errors='strict'): |
| 293 | return codecs.charmap_encode(input, errors, encoding_%s) |
| 294 | |
| 295 | def decode(self, input, errors='strict'): |
| 296 | return codecs.charmap_decode(input, errors, decoding_%s) |
| 297 | ''' % (encodingname, name, suffix, suffix)] |
| 298 | l.append('''\ |
| 299 | class IncrementalEncoder(codecs.IncrementalEncoder): |
| 300 | def encode(self, input, final=False): |
| 301 | return codecs.charmap_encode(input, self.errors, encoding_%s)[0] |
| 302 | |
| 303 | class IncrementalDecoder(codecs.IncrementalDecoder): |
| 304 | def decode(self, input, final=False): |
| 305 | return codecs.charmap_decode(input, self.errors, decoding_%s)[0]''' % |
| 306 | (suffix, suffix)) |
| 307 | |
| 308 | l.append(''' |
| 309 | class StreamWriter(Codec, codecs.StreamWriter): |
| 310 | pass |
no test coverage detected
searching dependent graphs…