diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2017-10-19 17:19:03 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2017-10-19 20:01:07 +0200 |
commit | 673f9be6f965d7febdaf4fe6dcd18686ecca7bc5 (patch) | |
tree | b97847a6abd35904df9f912cc098239564849910 /wee_slack.py | |
parent | 5bca925b5650e3dd3c41a9f36a5cc6d98d56d449 (diff) | |
download | wee-slack-673f9be6f965d7febdaf4fe6dcd18686ecca7bc5.tar.gz |
Create a decorator for decoding function arguments
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 75 |
1 files changed, 35 insertions, 40 deletions
diff --git a/wee_slack.py b/wee_slack.py index 2448c6e..6bc8aff 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -95,6 +95,17 @@ def slack_buffer_required(f): return wrapper +def utf8_decode(f): + """ + Decode all arguments from byte strings to unicode strings. Use this for + functions called from outside of this script, e.g. callbacks from weechat. + """ + @wraps(f) + def wrapper(*args, **kwargs): + return f(*decode_from_utf8(args), **decode_from_utf8(kwargs)) + return wrapper + + NICK_GROUP_HERE = "0|Here" NICK_GROUP_AWAY = "1|Away" @@ -571,13 +582,13 @@ def local_process_async_slack_api_request(request, event_router): ###### New Callbacks +@utf8_decode def receive_httprequest_callback(data, command, return_code, out, err): """ complete This is a dirty hack. There must be a better way. """ # def url_processor_cb(data, command, return_code, out, err): - data = decode_from_utf8(data) EVENTROUTER.receive_httprequest_callback(data, command, return_code, out, err) return w.WEECHAT_RC_OK @@ -598,6 +609,7 @@ def reconnect_callback(*args): return w.WEECHAT_RC_OK +@utf8_decode def buffer_closing_callback(signal, sig_type, data): """ complete @@ -606,11 +618,11 @@ def buffer_closing_callback(signal, sig_type, data): that is the only way we can do dependency injection via weechat callback, hence the eval. """ - data = decode_from_utf8(data) eval(signal).weechat_controller.unregister_buffer(data, True, False) return w.WEECHAT_RC_OK +@utf8_decode def buffer_input_callback(signal, buffer_ptr, data): """ incomplete @@ -618,7 +630,6 @@ def buffer_input_callback(signal, buffer_ptr, data): this includes add/remove reactions, modifying messages, and sending messages. """ - data = decode_from_utf8(data) eventrouter = eval(signal) channel = eventrouter.weechat_controller.get_channel_from_buffer_ptr(buffer_ptr) if not channel: @@ -662,6 +673,7 @@ def input_text_for_buffer_cb(data, modifier, current_buffer, string): return string +@utf8_decode def buffer_switch_callback(signal, sig_type, data): """ incomplete @@ -669,7 +681,6 @@ def buffer_switch_callback(signal, sig_type, data): 1) set read marker 2) determine if we have already populated channel history data """ - data = decode_from_utf8(data) eventrouter = eval(signal) prev_buffer_ptr = eventrouter.weechat_controller.get_previous_buffer_ptr() @@ -687,6 +698,7 @@ def buffer_switch_callback(signal, sig_type, data): return w.WEECHAT_RC_OK +@utf8_decode def buffer_list_update_callback(data, somecount): """ incomplete @@ -696,7 +708,6 @@ def buffer_list_update_callback(data, somecount): to indicate typing via "#channel" <-> ">channel" and user presence via " name" <-> "+name". """ - data = decode_from_utf8(data) eventrouter = eval(data) # global buffer_list_update @@ -715,8 +726,8 @@ def quit_notification_callback(signal, sig_type, data): stop_talking_to_slack() +@utf8_decode def typing_notification_cb(signal, sig_type, data): - data = decode_from_utf8(data) msg = w.buffer_get_string(data, "input") if len(msg) > 8 and msg[:1] != "/": global typing_timer @@ -732,14 +743,14 @@ def typing_notification_cb(signal, sig_type, data): return w.WEECHAT_RC_OK +@utf8_decode def typing_update_cb(data, remaining_calls): - data = decode_from_utf8(data) w.bar_item_update("slack_typing_notice") return w.WEECHAT_RC_OK +@utf8_decode def slack_never_away_cb(data, remaining_calls): - data = decode_from_utf8(data) if config.never_away: for t in EVENTROUTER.teams.values(): slackbot = t.get_channel_map()['slackbot'] @@ -783,13 +794,12 @@ def typing_bar_item_cb(data, current_buffer, args): return typing +@utf8_decode def nick_completion_cb(data, completion_item, current_buffer, completion): """ Adds all @-prefixed nicks to completion list """ - data = decode_from_utf8(data) - completion = decode_from_utf8(completion) current_buffer = w.current_buffer() current_channel = EVENTROUTER.weechat_controller.buffers.get(current_buffer, None) @@ -802,13 +812,12 @@ def nick_completion_cb(data, completion_item, current_buffer, completion): return w.WEECHAT_RC_OK +@utf8_decode def emoji_completion_cb(data, completion_item, current_buffer, completion): """ Adds all :-prefixed emoji to completion list """ - data = decode_from_utf8(data) - completion = decode_from_utf8(completion) current_buffer = w.current_buffer() current_channel = EVENTROUTER.weechat_controller.buffers.get(current_buffer, None) @@ -819,6 +828,7 @@ def emoji_completion_cb(data, completion_item, current_buffer, completion): return w.WEECHAT_RC_OK +@utf8_decode def complete_next_cb(data, current_buffer, command): """Extract current word, if it is equal to a nick, prefix it with @ and rely on nick_completion_cb adding the @-prefixed versions to the @@ -827,8 +837,6 @@ def complete_next_cb(data, current_buffer, command): """ - data = decode_from_utf8(data) - command = decode_from_utf8(data) current_buffer = w.current_buffer() current_channel = EVENTROUTER.weechat_controller.buffers.get(current_buffer, None) @@ -2836,9 +2844,8 @@ def tag(tagset, user=None): @slack_buffer_or_ignore +@utf8_decode def part_command_cb(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) e = EVENTROUTER args = args.split() if len(args) > 1: @@ -2876,13 +2883,12 @@ def parse_topic_command(command): @slack_buffer_or_ignore +@utf8_decode def topic_command_cb(data, current_buffer, command): """ Change the topic of a channel /topic [<channel>] [<topic>|-delete] """ - data = decode_from_utf8(data) - command = decode_from_utf8(command) channel_name, topic = parse_topic_command(command) @@ -2905,18 +2911,16 @@ def topic_command_cb(data, current_buffer, command): @slack_buffer_or_ignore +@utf8_decode def me_command_cb(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) message = "_{}_".format(args.split(' ', 1)[1]) buffer_input_callback("EVENTROUTER", current_buffer, message) return w.WEECHAT_RC_OK_EAT @slack_buffer_or_ignore +@utf8_decode def msg_command_cb(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) dbg("msg_command_cb") aargs = args.split(None, 2) who = aargs[1] @@ -2936,14 +2940,13 @@ def msg_command_cb(data, current_buffer, args): @slack_buffer_or_ignore +@utf8_decode def command_talk(data, current_buffer, args): """ Open a chat with the specified user /slack talk [user] """ - data = decode_from_utf8(data) - args = decode_from_utf8(args) e = EVENTROUTER team = e.weechat_controller.buffers[current_buffer].team channel_name = args.split(' ')[1] @@ -2973,9 +2976,8 @@ def command_showmuted(data, current_buffer, args): w.prnt(EVENTROUTER.weechat_controller.buffers[current].team.channel_buffer, str(EVENTROUTER.weechat_controller.buffers[current].team.muted_channels)) +@utf8_decode def thread_command_callback(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) current = w.current_buffer() channel = EVENTROUTER.weechat_controller.buffers.get(current) if channel: @@ -3005,9 +3007,8 @@ def thread_command_callback(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT +@utf8_decode def rehistory_command_callback(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) current = w.current_buffer() channel = EVENTROUTER.weechat_controller.buffers.get(current) channel.got_history = False @@ -3017,9 +3018,8 @@ def rehistory_command_callback(data, current_buffer, args): @slack_buffer_required +@utf8_decode def hide_command_callback(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) c = EVENTROUTER.weechat_controller.buffers.get(current_buffer, None) if c: name = c.formatted_name(style='long_default') @@ -3028,9 +3028,8 @@ def hide_command_callback(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT +@utf8_decode def slack_command_cb(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) a = args.split(' ', 1) if len(a) > 1: function_name, args = a[0], args @@ -3139,9 +3138,8 @@ def command_upload(data, current_buffer, args): w.hook_process(command, config.slack_timeout, '', '') +@utf8_decode def away_command_cb(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) # TODO: reimplement all.. maybe (all, message) = re.match("^/away(?:\s+(-all))?(?:\s+(.+))?", args).groups() if message is None: @@ -3199,9 +3197,8 @@ def command_back(data, current_buffer, args): @slack_buffer_required +@utf8_decode def label_command_cb(data, current_buffer, args): - data = decode_from_utf8(data) - args = decode_from_utf8(args) channel = EVENTROUTER.weechat_controller.buffers.get(current_buffer) if channel and channel.type == 'thread': aargs = args.split(None, 2) @@ -3210,18 +3207,16 @@ def label_command_cb(data, current_buffer, args): w.buffer_set(channel.channel_buffer, "short_name", new_name) +@utf8_decode def set_unread_cb(data, current_buffer, command): - data = decode_from_utf8(data) - command = decode_from_utf8(command) for channel in EVENTROUTER.weechat_controller.buffers.values(): channel.mark_read() return w.WEECHAT_RC_OK @slack_buffer_or_ignore +@utf8_decode def set_unread_current_buffer_cb(data, current_buffer, command): - data = decode_from_utf8(data) - command = decode_from_utf8(command) channel = EVENTROUTER.weechat_controller.buffers.get(current_buffer) channel.mark_read() return w.WEECHAT_RC_OK |