The GSM-7 character set is defined as the base mapping table + extension character mapping table ( https://en.wikipedia.org/wiki/GSM_03.38#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_.2F_GSM_03.38 ). The value u'@' must be mapped to b'\x00' (a byte string of length 1), but u'[' must be mapped to b'\x1b<' or b'\x1b\x3c' (a byte string of length 2).
I managed to get the coding part to work by expanding encoding_table , but I'm not sure what to do with decoding_table ..?
Here is the full-text codec code:
import codecs from encodings import normalize_encoding class GSM7Codec(codecs.Codec): def encode(self, input, errors='strict'): return codecs.charmap_encode(input, errors, encoding_table) def decode(self, input, errors='strict'): return codecs.charmap_decode(input, errors, decoding_table) class GSM7IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input, final=False): return codecs.charmap_encode(input, self.errors, encoding_table)[0] class GSM7IncrementalDecoder(codecs.IncrementalDecoder): def decode(self, input, final=False): return codecs.charmap_decode(input, self.errors, decoding_table)[0] class GSM7StreamWriter(codecs.Codec, codecs.StreamWriter): pass class GSM7StreamReader(codecs.Codec, codecs.StreamReader): pass _cache = {} def search_function(encoding): """Register the gsm-7 encoding with Python codecs API. This involves adding a search function that takes in an encoding name, and returns a codec for that encoding if it knows one, or None if it doesn't. """ if encoding in _cache: return _cache[encoding] norm_encoding = normalize_encoding(encoding) if norm_encoding in ('gsm_7', 'g7', 'gsm7'): cinfo = codecs.CodecInfo( name='gsm-7', encode=GSM7Codec().encode, decode=GSM7Codec().decode, incrementalencoder=GSM7IncrementalEncoder, incrementaldecoder=GSM7IncrementalDecoder, streamreader=GSM7StreamReader, streamwriter=GSM7StreamWriter, ) _cache[norm_encoding] = cinfo return cinfo return None codecs.register(search_function)
and here are the table definitions:
decoding_table = ( u"@£$¥èéùìòÇ\nØø\rÅå" + u"Δ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ" + u" !\"#¤%&'()*+,-./" + u"0123456789:;<=>?" + u"¡ABCDEFGHIJKLMNO" + u"PQRSTUVWXYZÄÖÑܧ" + u"¿abcdefghijklmno" + u"pqrstuvwxyzäöñüà" ) encoding_table = codecs.charmap_build( decoding_table + '\0' * (256 - len(decoding_table)) )
Now part of the encoding works, but decoding is not performed:
>>> u'['.encode('g7') '\x1b<' >>> _.decode('g7') u'\x1b<' >>>
I was not able to find a good source for coding documentation.