refactor: Split en/decoding helper into a separate method
Separate the code for wrapping a method so arguments and return values
are encoded/decoded into a new method in WeechatWrapper. This allowes us
to use it outside of __getattr__ as well, so we can handle some specific
methods differently.
@@ -137,16 +137,22 @@ class WeechatWrapper(object):
def __init__(self, wrapped_class):
self.wrapped_class = wrapped_class
- def __getattr__(self, attr):
- orig_attr = self.wrapped_class.__getattribute__(attr)
- if callable(orig_attr):
+ # Helper method used to encode/decode method calls.
+ def wrap_for_utf8(self, method):
def hooked(*args, **kwargs):
- result = orig_attr(*encode_to_utf8(args), **encode_to_utf8(kwargs))
+ result = method(*encode_to_utf8(args), **encode_to_utf8(kwargs))
# Prevent wrapped_class from becoming unwrapped
if result == self.wrapped_class:
return self
return decode_from_utf8(result)
return hooked
+
+ # Encode and decode everything sent to/received from weechat. We use the
+ # unicode type internally in wee-slack, but has to send utf8 to weechat.
+ def __getattr__(self, attr):
+ orig_attr = self.wrapped_class.__getattribute__(attr)
+ if callable(orig_attr):
+ return self.wrap_for_utf8(orig_attr)
else:
return decode_from_utf8(orig_attr)
