jaraco / irc

Full-featured Python IRC library for Python.
MIT License
392 stars 87 forks source link

Set encoding strings as properties to allow override #131

Closed ricrogz closed 6 years ago

ricrogz commented 6 years ago

Decoding of incoming messages could be handled as mentioned in the documentation.

But encoding of client's outgoing messages is hardcoded to 'utf-8'. To allow for compatibility with servers configured to work with other encodings, it should be possible to use other encodings. Therefore, I suggest to set the encoding as a property that can be overridden.

jaraco commented 6 years ago

Consider 4e023039f415b01cf1b0d4b493d3fc95649d045d, which I think serves the use-case you presented. By exposing the value as a class attribute, it also enables other ways to override encoding:

class MyConnection(ServerConnection):
    transmit_encoding = 'latin-1'

or

class MyComplexEncodingConnection(ServerConnection):
    def encode(self, msg):
        return msg.encode('utf-8') if random.randint(1,2) == 1 else msg.encode('latin-1')

But of course, you can still simply set the transmit_encoding attribute on an instance of the class and all messages transmitted subsequently will use that encoding:

conn = ServerConnection()
conn.transmit_encoding = 'latin-1'

Let me know if that works for you and I'll cut a release, or if it doesn't how it could be improved.

ricrogz commented 6 years ago

Works for me, thanks!