aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2017-09-22 16:10:49 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2017-09-22 16:16:46 +0200
commit61b4eefd38ff474684b384d57d388346113d94b4 (patch)
treecf864ca0aa68b7e68c12155ecc3c32bb49c77033 /wee_slack.py
parent2f43d1b6466c74e8774e2833214f847eb559f15a (diff)
downloadwee-slack-61b4eefd38ff474684b384d57d388346113d94b4.tar.gz
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.
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 90b56ac..d23be42 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -137,16 +137,22 @@ class WeechatWrapper(object):
def __init__(self, wrapped_class):
self.wrapped_class = wrapped_class
+ # Helper method used to encode/decode method calls.
+ def wrap_for_utf8(self, method):
+ def hooked(*args, **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):
- def hooked(*args, **kwargs):
- result = orig_attr(*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
+ return self.wrap_for_utf8(orig_attr)
else:
return decode_from_utf8(orig_attr)