Set encoding strings as properties to allow override#131
Set encoding strings as properties to allow override#131ricrogz wants to merge 2 commits intojaraco:masterfrom ricrogz:master
Conversation
jaraco
left a comment
There was a problem hiding this comment.
Thanks for describing your use-case. I'm rather surprised there are still servers that are not yet supporting UTF-8 encoding, but alas we live in a diverse world, so perhaps it makes sense to support it in the library.
Rather than storing properties on the classes, I think a better approach would be to provide a method for handling the encoding that may be overridden by a subclass. I'll present that in an alternative approach.
|
|
||
| def __init__(self, reactor): | ||
| super(ServerConnection, self).__init__(reactor) | ||
| self.outgoing_encoding = 'utf-8' |
There was a problem hiding this comment.
Since this behavior is repeated in another Connection subclass, the behavior should probably exist in the superclass.
I'd prefer the name transmit_encoding.
| self.buffer.feed(data) | ||
| for line in self.buffer: | ||
| line = line.decode('utf-8') | ||
| line = line.decode(self.incoming_encoding) |
There was a problem hiding this comment.
Here I prefer to keep the hard-coded value. I'm not aware of any use case that would demand any other encoding.
|
Consider 4e02303, 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: or 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: Let me know if that works for you and I'll cut a release, or if it doesn't how it could be improved. |
|
Works for me, thanks! |
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.