def codegen(name, map, encodingname, comments=1):
 decoding_map_code = python_mapdef_code('decoding_map', map, comments=comments)
 decoding_table_code = python_tabledef_code('decoding_table', map, comments=comments)
 encoding_map_code = python_mapdef_code('encoding_map', codecs.make_encoding_map(map), comments=comments, precisions=(4, 2))
 if decoding_table_code:
  suffix = 'table'
 else:
  suffix = 'map'
 l = [('""" Python Character Mapping Codec %s generated from \'%s\' with gencodec.py.\n\n"""#"\n\nimport codecs\n\n### Codec APIs\n\nclass Codec(codecs.Codec):\n\n    def encode(self,input,errors=\'strict\'):\n        return codecs.charmap_encode(input,errors,encoding_%s)\n\n    def decode(self,input,errors=\'strict\'):\n        return codecs.charmap_decode(input,errors,decoding_%s)\n' % (encodingname, name, suffix, suffix))]
 l.append(('class IncrementalEncoder(codecs.IncrementalEncoder):\n    def encode(self, input, final=False):\n        return codecs.charmap_encode(input,self.errors,encoding_%s)[0]\n\nclass IncrementalDecoder(codecs.IncrementalDecoder):\n    def decode(self, input, final=False):\n        return codecs.charmap_decode(input,self.errors,decoding_%s)[0]' % (suffix, suffix)))
 l.append(('\nclass StreamWriter(Codec,codecs.StreamWriter):\n    pass\n\nclass StreamReader(Codec,codecs.StreamReader):\n    pass\n\n### encodings module API\n\ndef getregentry():\n    return codecs.CodecInfo(\n        name=%r,\n        encode=Codec().encode,\n        decode=Codec().decode,\n        incrementalencoder=IncrementalEncoder,\n        incrementaldecoder=IncrementalDecoder,\n        streamreader=StreamReader,\n        streamwriter=StreamWriter,\n    )\n' % encodingname.replace('_', '-')))
 if (not decoding_table_code):
  l.append('\n### Decoding Map\n')
  l.extend(decoding_map_code)
 else:
  l.append('\n### Decoding Table\n')
  l.extend(decoding_table_code)
 if decoding_table_code:
  l.append('\n### Encoding table\nencoding_table=codecs.charmap_build(decoding_table)\n')
 else:
  l.append('\n### Encoding Map\n')
  l.extend(encoding_map_code)
 l.append('')
 return '\n'.join(l).expandtabs()

def codegen ( name , map , encodingname , comments = 1 ) : decoding_map_code = python_mapdef_code ( 'decoding_map' , map , comments = comments ) decoding_table_code = python_tabledef_code ( 'decoding_table' , map , comments = comments ) encoding_map_code = python_mapdef_code ( 'encoding_map' , codecs . make_encoding_map ( map ) , comments = comments , precisions = ( 4 , 2 ) ) if decoding_table_code : suffix = 'table' else : suffix = 'map' l = [ ( '""" Python Character Mapping Codec %s generated from \'%s\' with gencodec.py.\n\n"""#"\n\nimport codecs\n\n### Codec APIs\n\nclass Codec(codecs.Codec):\n\n    def encode(self,input,errors=\'strict\'):\n        return codecs.charmap_encode(input,errors,encoding_%s)\n\n    def decode(self,input,errors=\'strict\'):\n        return codecs.charmap_decode(input,errors,decoding_%s)\n' % ( encodingname , name , suffix , suffix ) ) ] l . append ( ( 'class IncrementalEncoder(codecs.IncrementalEncoder):\n    def encode(self, input, final=False):\n        return codecs.charmap_encode(input,self.errors,encoding_%s)[0]\n\nclass IncrementalDecoder(codecs.IncrementalDecoder):\n    def decode(self, input, final=False):\n        return codecs.charmap_decode(input,self.errors,decoding_%s)[0]' % ( suffix , suffix ) ) ) l . append ( ( '\nclass StreamWriter(Codec,codecs.StreamWriter):\n    pass\n\nclass StreamReader(Codec,codecs.StreamReader):\n    pass\n\n### encodings module API\n\ndef getregentry():\n    return codecs.CodecInfo(\n        name=%r,\n        encode=Codec().encode,\n        decode=Codec().decode,\n        incrementalencoder=IncrementalEncoder,\n        incrementaldecoder=IncrementalDecoder,\n        streamreader=StreamReader,\n        streamwriter=StreamWriter,\n    )\n' % encodingname . replace ( '_' , '-' ) ) ) if ( not decoding_table_code ) : l . append ( '\n### Decoding Map\n' ) l . extend ( decoding_map_code ) else : l . append ( '\n### Decoding Table\n' ) l . extend ( decoding_table_code ) if decoding_table_code : l . append ( '\n### Encoding table\nencoding_table=codecs.charmap_build(decoding_table)\n' ) else : l . append ( '\n### Encoding Map\n' ) l . extend ( encoding_map_code ) l . append ( '' ) return '\n' . join ( l ) . expandtabs ( )
generate source code for expressions in a given language .