More formatting fixes using yapf.

This commit is contained in:
poljar (Damir Jelić) 2018-02-22 14:35:14 +01:00
parent 6d4f69e3a2
commit 11076f9649
8 changed files with 172 additions and 349 deletions

View file

@ -43,21 +43,17 @@ def matrix_bar_item_name(data, item, window, buffer, extra_info):
for server in SERVERS.values():
if buffer in server.buffers.values():
color = ("status_name_ssl"
if server.ssl_context.check_hostname else
"status_name")
if server.ssl_context.check_hostname else "status_name")
room_id = key_from_value(server.buffers, buffer)
room = server.rooms[room_id]
return "{color}{name}".format(
color=W.color(color),
name=room.alias)
return "{color}{name}".format(color=W.color(color), name=room.alias)
elif buffer == server.server_buffer:
color = ("status_name_ssl"
if server.ssl_context.check_hostname else
"status_name")
if server.ssl_context.check_hostname else "status_name")
return "{color}server{del_color}[{color}{name}{del_color}]".format(
color=W.color(color),
@ -82,8 +78,7 @@ def matrix_bar_item_lag(data, item, window, buffer, extra_info):
lag_string = "Lag: {color}{lag}{ncolor}".format(
lag=lag.format((server.lag / 1000)),
color=color,
ncolor=W.color("reset")
)
ncolor=W.color("reset"))
return lag_string
return ""
@ -91,6 +86,6 @@ def matrix_bar_item_lag(data, item, window, buffer, extra_info):
def init_bar_items():
W.bar_item_new("(extra)buffer_plugin", "matrix_bar_item_plugin", "")
W.bar_item_new("(extra)buffer_plugin", "matrix_bar_item_plugin", "")
W.bar_item_new("(extra)buffer_name", "matrix_bar_item_name", "")
W.bar_item_new("(extra)lag", "matrix_bar_item_lag", "")

View file

@ -24,15 +24,9 @@ import matrix.globals
from matrix.globals import W, OPTIONS, SERVERS
from matrix.utf import utf8_decode
from matrix.api import (
MessageType,
MatrixTopicMessage,
MatrixRedactMessage,
MatrixBacklogMessage,
MatrixJoinMessage,
MatrixPartMessage,
MatrixInviteMessage
)
from matrix.api import (MessageType, MatrixTopicMessage, MatrixRedactMessage,
MatrixBacklogMessage, MatrixJoinMessage,
MatrixPartMessage, MatrixInviteMessage)
from matrix.utils import key_from_value, tags_from_line_data
from matrix.plugin_options import DebugType
from matrix.server import MatrixServer
@ -41,60 +35,52 @@ from matrix.server import MatrixServer
def hook_commands():
W.hook_command(
# Command name and short description
'matrix', 'Matrix chat protocol command',
'matrix',
'Matrix chat protocol command',
# Synopsis
(
'server add <server-name> <hostname>[:<port>] ||'
'server delete|list|listfull <server-name> ||'
'connect <server-name> ||'
'disconnect <server-name> ||'
'reconnect <server-name> ||'
'debug <debug-type> ||'
'help <matrix-command>'
),
('server add <server-name> <hostname>[:<port>] ||'
'server delete|list|listfull <server-name> ||'
'connect <server-name> ||'
'disconnect <server-name> ||'
'reconnect <server-name> ||'
'debug <debug-type> ||'
'help <matrix-command>'),
# Description
(
' server: list, add, or remove Matrix servers\n'
' connect: connect to Matrix servers\n'
'disconnect: disconnect from one or all Matrix servers\n'
' reconnect: reconnect to server(s)\n\n'
' help: show detailed command help\n\n'
' debug: enable or disable debugging\n\n'
'Use /matrix help [command] to find out more\n'
),
(' server: list, add, or remove Matrix servers\n'
' connect: connect to Matrix servers\n'
'disconnect: disconnect from one or all Matrix servers\n'
' reconnect: reconnect to server(s)\n\n'
' help: show detailed command help\n\n'
' debug: enable or disable debugging\n\n'
'Use /matrix help [command] to find out more\n'),
# Completions
(
'server %(matrix_server_commands)|%* ||'
'connect %(matrix_servers) ||'
'disconnect %(matrix_servers) ||'
'reconnect %(matrix_servers) ||'
'debug %(matrix_debug_types) ||'
'help %(matrix_commands)'
),
('server %(matrix_server_commands)|%* ||'
'connect %(matrix_servers) ||'
'disconnect %(matrix_servers) ||'
'reconnect %(matrix_servers) ||'
'debug %(matrix_debug_types) ||'
'help %(matrix_commands)'),
# Function name
'matrix_command_cb', '')
'matrix_command_cb',
'')
W.hook_command(
# Command name and short description
'redact', 'redact messages',
'redact',
'redact messages',
# Synopsis
(
'<message-number>[:<"message-part">] [<reason>]'
),
('<message-number>[:<"message-part">] [<reason>]'),
# Description
(
"message-number: number of the message to redact (message numbers"
"\n start from the last recieved as "
"1 and count up)\n"
" message-part: a shortened part of the message\n"
" reason: the redaction reason\n"
),
("message-number: number of the message to redact (message numbers"
"\n start from the last recieved as "
"1 and count up)\n"
" message-part: a shortened part of the message\n"
" reason: the redaction reason\n"),
# Completions
(
'%(matrix_messages)'
),
('%(matrix_messages)'),
# Function name
'matrix_redact_command_cb', '')
'matrix_redact_command_cb',
'')
W.hook_command_run('/topic', 'matrix_command_topic_cb', '')
W.hook_command_run('/buffer clear', 'matrix_command_buf_clear_cb', '')
@ -121,8 +107,7 @@ def matrix_fetch_old_messages(server, room_id):
server.client,
room_id=room_id,
token=prev_batch,
limit=OPTIONS.backlog_limit
)
limit=OPTIONS.backlog_limit)
room.backlog_pending = True
server.send_or_queue(message)
@ -140,11 +125,8 @@ def check_server_existence(server_name, servers):
def hook_page_up():
OPTIONS.page_up_hook = W.hook_command_run(
'/window page_up',
'matrix_command_pgup_cb',
''
)
OPTIONS.page_up_hook = W.hook_command_run('/window page_up',
'matrix_command_pgup_cb', '')
@utf8_decode
@ -171,8 +153,7 @@ def matrix_command_pgup_cb(data, buffer, command):
window = W.window_search_with_buffer(buffer)
first_line_displayed = bool(
W.window_get_integer(window, "first_line_displayed")
)
W.window_get_integer(window, "first_line_displayed"))
if first_line_displayed:
room_id = key_from_value(server.buffers, buffer)
@ -185,22 +166,19 @@ def matrix_command_pgup_cb(data, buffer, command):
@utf8_decode
def matrix_command_join_cb(data, buffer, command):
def join(server, args):
split_args = args.split(" ", 1)
# TODO handle join for non public rooms
if len(split_args) != 2:
message = ("{prefix}Error with command \"/join\" (help on "
"command: /help join)").format(
prefix=W.prefix("error"))
"command: /help join)").format(prefix=W.prefix("error"))
W.prnt("", message)
return
_, room_id = split_args
message = MatrixJoinMessage(
server.client,
room_id=room_id
)
message = MatrixJoinMessage(server.client, room_id=room_id)
server.send_or_queue(message)
for server in SERVERS.values():
@ -216,6 +194,7 @@ def matrix_command_join_cb(data, buffer, command):
@utf8_decode
def matrix_command_part_cb(data, buffer, command):
def part(server, buffer, args):
rooms = []
@ -223,9 +202,9 @@ def matrix_command_part_cb(data, buffer, command):
if len(split_args) == 1:
if buffer == server.server_buffer:
message = ("{prefix}Error with command \"/part\" (help on "
"command: /help part)").format(
prefix=W.prefix("error"))
message = (
"{prefix}Error with command \"/part\" (help on "
"command: /help part)").format(prefix=W.prefix("error"))
W.prnt("", message)
return
@ -236,10 +215,7 @@ def matrix_command_part_cb(data, buffer, command):
rooms = rooms.split(" ")
for room_id in rooms:
message = MatrixPartMessage(
server.client,
room_id=room_id
)
message = MatrixPartMessage(server.client, room_id=room_id)
server.send_or_queue(message)
for server in SERVERS.values():
@ -255,14 +231,15 @@ def matrix_command_part_cb(data, buffer, command):
@utf8_decode
def matrix_command_invite_cb(data, buffer, command):
def invite(server, buf, args):
split_args = args.split(" ", 1)
# TODO handle join for non public rooms
if len(split_args) != 2:
message = ("{prefix}Error with command \"/invite\" (help on "
"command: /help invite)").format(
prefix=W.prefix("error"))
message = (
"{prefix}Error with command \"/invite\" (help on "
"command: /help invite)").format(prefix=W.prefix("error"))
W.prnt("", message)
return
@ -270,10 +247,7 @@ def matrix_command_invite_cb(data, buffer, command):
room_id = key_from_value(server.buffers, buf)
message = MatrixInviteMessage(
server.client,
room_id=room_id,
user_id=invitee
)
server.client, room_id=room_id, user_id=invitee)
server.send_or_queue(message)
for server in SERVERS.values():
@ -288,28 +262,20 @@ def event_id_from_line(buf, target_number):
# type: (weechat.buffer, int) -> str
own_lines = W.hdata_pointer(W.hdata_get('buffer'), buf, 'own_lines')
if own_lines:
line = W.hdata_pointer(
W.hdata_get('lines'),
own_lines,
'last_line'
)
line = W.hdata_pointer(W.hdata_get('lines'), own_lines, 'last_line')
line_number = 1
while line:
line_data = W.hdata_pointer(
W.hdata_get('line'),
line,
'data'
)
line_data = W.hdata_pointer(W.hdata_get('line'), line, 'data')
if line_data:
tags = tags_from_line_data(line_data)
# Only count non redacted user messages
if ("matrix_message" in tags
and 'matrix_redacted' not in tags
and "matrix_new_redacted" not in tags):
if ("matrix_message" in tags and
'matrix_redacted' not in tags and
"matrix_new_redacted" not in tags):
if line_number == target_number:
for tag in tags:
@ -334,8 +300,8 @@ def matrix_redact_command_cb(data, buffer, args):
if not matches:
message = ("{prefix}matrix: Invalid command arguments (see "
"the help for the command /help redact)").format(
prefix=W.prefix("error"))
"the help for the command /help redact)"
).format(prefix=W.prefix("error"))
W.prnt("", message)
return W.WEECHAT_RC_ERROR
@ -347,8 +313,7 @@ def matrix_redact_command_cb(data, buffer, args):
if not event_id:
message = ("{prefix}matrix: No such message with number "
"{number} found").format(
prefix=W.prefix("error"),
number=line)
prefix=W.prefix("error"), number=line)
W.prnt("", message)
return W.WEECHAT_RC_OK
@ -356,16 +321,15 @@ def matrix_redact_command_cb(data, buffer, args):
server.client,
room_id=room_id,
event_id=event_id,
reason=reason
)
reason=reason)
server.send_or_queue(message)
return W.WEECHAT_RC_OK
elif buffer == server.server_buffer:
message = ("{prefix}matrix: command \"redact\" must be "
"executed on a Matrix channel buffer").format(
prefix=W.prefix("error"))
"executed on a Matrix channel buffer"
).format(prefix=W.prefix("error"))
W.prnt("", message)
return W.WEECHAT_RC_OK
@ -384,15 +348,13 @@ def matrix_command_debug(args):
if debug_type in OPTIONS.debug:
message = ("{prefix}matrix: Disabling matrix {t} "
"debugging.").format(
prefix=W.prefix("error"),
t=debug_type)
prefix=W.prefix("error"), t=debug_type)
W.prnt("", message)
OPTIONS.debug.remove(debug_type)
else:
message = ("{prefix}matrix: Enabling matrix {t} "
"debugging.").format(
prefix=W.prefix("error"),
t=debug_type)
prefix=W.prefix("error"), t=debug_type)
W.prnt("", message)
OPTIONS.debug.append(debug_type)
@ -406,8 +368,7 @@ def matrix_command_debug(args):
else:
message = ("{prefix}matrix: Unknown matrix debug "
"type \"{t}\".").format(
prefix=W.prefix("error"),
t=command)
prefix=W.prefix("error"), t=command)
W.prnt("", message)
@ -517,8 +478,7 @@ def matrix_command_help(args):
else:
message = ("{prefix}matrix: No help available, \"{command}\" "
"is not a matrix command").format(
prefix=W.prefix("error"),
command=command)
prefix=W.prefix("error"), command=command)
W.prnt("", "")
W.prnt("", message)
@ -527,6 +487,7 @@ def matrix_command_help(args):
def matrix_server_command_listfull(args):
def get_value_string(value, default_value):
if value == default_value:
if not value:
@ -691,11 +652,7 @@ def matrix_server_command_add(args):
except ValueError:
host, port = args[1], None
return_code = W.config_option_set(
server.options["address"],
host,
1
)
return_code = W.config_option_set(server.options["address"], host, 1)
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
remove_server(server)
@ -712,11 +669,7 @@ def matrix_server_command_add(args):
return
if port:
return_code = W.config_option_set(
server.options["port"],
port,
1
)
return_code = W.config_option_set(server.options["port"], port, 1)
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
remove_server(server)
message = ("{prefix}Failed to set port for server "
@ -733,11 +686,7 @@ def matrix_server_command_add(args):
if len(args) >= 3:
user = args[2]
return_code = W.config_option_set(
server.options["username"],
user,
1
)
return_code = W.config_option_set(server.options["username"], user, 1)
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
remove_server(server)
@ -756,11 +705,8 @@ def matrix_server_command_add(args):
if len(args) == 4:
password = args[3]
return_code = W.config_option_set(
server.options["password"],
password,
1
)
return_code = W.config_option_set(server.options["password"], password,
1)
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
remove_server(server)
message = ("{prefix}Failed to set password for server "
@ -783,14 +729,13 @@ def matrix_server_command_add(args):
def matrix_server_command(command, args):
def list_servers(_):
if SERVERS:
W.prnt("", "\nAll matrix servers:")
for server in SERVERS:
W.prnt("", " {color}{server}".format(
color=W.color("chat_server"),
server=server
))
color=W.color("chat_server"), server=server))
# TODO the argument for list and listfull is used as a match word to
# find/filter servers, we're currently match exactly to the whole name
@ -805,13 +750,13 @@ def matrix_server_command(command, args):
else:
message = ("{prefix}matrix: Error: unknown matrix server command, "
"\"{command}\" (type /matrix help server for help)").format(
prefix=W.prefix("error"),
command=command)
prefix=W.prefix("error"), command=command)
W.prnt("", message)
@utf8_decode
def matrix_command_cb(data, buffer, args):
def connect_server(args):
for server_name in args:
if check_server_existence(server_name, SERVERS):
@ -865,8 +810,7 @@ def matrix_command_cb(data, buffer, args):
else:
message = ("{prefix}matrix: Error: unknown matrix command, "
"\"{command}\" (type /help matrix for help)").format(
prefix=W.prefix("error"),
command=command)
prefix=W.prefix("error"), command=command)
W.prnt("", message)
return W.WEECHAT_RC_OK
@ -917,18 +861,15 @@ def matrix_command_topic_cb(data, buffer, command):
return W.WEECHAT_RC_OK_EAT
message = MatrixTopicMessage(
server.client,
room_id=room_id,
topic=topic
)
server.client, room_id=room_id, topic=topic)
server.send_or_queue(message)
return W.WEECHAT_RC_OK_EAT
elif buffer == server.server_buffer:
message = ("{prefix}matrix: command \"topic\" must be "
"executed on a Matrix channel buffer").format(
prefix=W.prefix("error"))
"executed on a Matrix channel buffer"
).format(prefix=W.prefix("error"))
W.prnt(buffer, message)
return W.WEECHAT_RC_OK_EAT

View file

@ -23,16 +23,13 @@ from matrix.utils import tags_from_line_data
def add_servers_to_completion(completion):
for server_name in SERVERS:
W.hook_completion_list_add(
completion,
server_name,
0,
W.WEECHAT_LIST_POS_SORT
)
W.hook_completion_list_add(completion, server_name, 0,
W.WEECHAT_LIST_POS_SORT)
@utf8_decode
def matrix_server_command_completion_cb(data, completion_item, buffer, completion):
def matrix_server_command_completion_cb(data, completion_item, buffer,
completion):
buffer_input = W.buffer_get_string(buffer, "input").split()
args = buffer_input[1:]
@ -40,12 +37,8 @@ def matrix_server_command_completion_cb(data, completion_item, buffer, completio
def complete_commands():
for command in commands:
W.hook_completion_list_add(
completion,
command,
0,
W.WEECHAT_LIST_POS_SORT
)
W.hook_completion_list_add(completion, command, 0,
W.WEECHAT_LIST_POS_SORT)
if len(args) == 1:
complete_commands()
@ -74,29 +67,18 @@ def matrix_server_completion_cb(data, completion_item, buffer, completion):
@utf8_decode
def matrix_command_completion_cb(data, completion_item, buffer, completion):
for command in [
"connect",
"disconnect",
"reconnect",
"server",
"help",
"debug"
"connect", "disconnect", "reconnect", "server", "help", "debug"
]:
W.hook_completion_list_add(
completion,
command,
0,
W.WEECHAT_LIST_POS_SORT)
W.hook_completion_list_add(completion, command, 0,
W.WEECHAT_LIST_POS_SORT)
return W.WEECHAT_RC_OK
@utf8_decode
def matrix_debug_completion_cb(data, completion_item, buffer, completion):
for debug_type in ["messaging", "network", "timing"]:
W.hook_completion_list_add(
completion,
debug_type,
0,
W.WEECHAT_LIST_POS_SORT)
W.hook_completion_list_add(completion, debug_type, 0,
W.WEECHAT_LIST_POS_SORT)
return W.WEECHAT_RC_OK
@ -104,46 +86,31 @@ def matrix_debug_completion_cb(data, completion_item, buffer, completion):
def matrix_message_completion_cb(data, completion_item, buffer, completion):
own_lines = W.hdata_pointer(W.hdata_get('buffer'), buffer, 'own_lines')
if own_lines:
line = W.hdata_pointer(
W.hdata_get('lines'),
own_lines,
'last_line'
)
line = W.hdata_pointer(W.hdata_get('lines'), own_lines, 'last_line')
line_number = 1
while line:
line_data = W.hdata_pointer(
W.hdata_get('line'),
line,
'data'
)
line_data = W.hdata_pointer(W.hdata_get('line'), line, 'data')
if line_data:
message = W.hdata_string(W.hdata_get('line_data'), line_data,
'message')
message = W.hdata_string(
W.hdata_get('line_data'), line_data, 'message')
tags = tags_from_line_data(line_data)
# Only add non redacted user messages to the completion
if (message
and 'matrix_message' in tags
and 'matrix_redacted' not in tags):
if (message and 'matrix_message' in tags and
'matrix_redacted' not in tags):
if len(message) > OPTIONS.redaction_comp_len + 2:
message = (
message[:OPTIONS.redaction_comp_len]
+ '..')
message = (message[:OPTIONS.redaction_comp_len] + '..')
item = ("{number}:\"{message}\"").format(
number=line_number,
message=message)
number=line_number, message=message)
W.hook_completion_list_add(
completion,
item,
0,
W.WEECHAT_LIST_POS_END)
W.hook_completion_list_add(completion, item, 0,
W.WEECHAT_LIST_POS_END)
line_number += 1
line = W.hdata_move(W.hdata_get('line'), line, -1)
@ -152,37 +119,17 @@ def matrix_message_completion_cb(data, completion_item, buffer, completion):
def init_completion():
W.hook_completion(
"matrix_server_commands",
"Matrix server completion",
"matrix_server_command_completion_cb",
""
)
W.hook_completion("matrix_server_commands", "Matrix server completion",
"matrix_server_command_completion_cb", "")
W.hook_completion(
"matrix_servers",
"Matrix server completion",
"matrix_server_completion_cb",
""
)
W.hook_completion("matrix_servers", "Matrix server completion",
"matrix_server_completion_cb", "")
W.hook_completion(
"matrix_commands",
"Matrix command completion",
"matrix_command_completion_cb",
""
)
W.hook_completion("matrix_commands", "Matrix command completion",
"matrix_command_completion_cb", "")
W.hook_completion(
"matrix_messages",
"Matrix message completion",
"matrix_message_completion_cb",
""
)
W.hook_completion("matrix_messages", "Matrix message completion",
"matrix_message_completion_cb", "")
W.hook_completion(
"matrix_debug_types",
"Matrix debugging type completion",
"matrix_debug_completion_cb",
""
)
W.hook_completion("matrix_debug_types", "Matrix debugging type completion",
"matrix_debug_completion_cb", "")

View file

@ -16,11 +16,7 @@
from __future__ import unicode_literals
from matrix.plugin_options import (
Option,
RedactType,
ServerBufferType
)
from matrix.plugin_options import (Option, RedactType, ServerBufferType)
import matrix.globals
from matrix.globals import W, OPTIONS, SERVERS
@ -42,8 +38,7 @@ def matrix_config_change_cb(data, option):
OPTIONS.redaction_type = RedactType(W.config_integer(option))
elif option_name == "server_buffer":
OPTIONS.look_server_buf = ServerBufferType(
W.config_integer(option))
OPTIONS.look_server_buf = ServerBufferType(W.config_integer(option))
for server in SERVERS.values():
if server.server_buffer:
server_buffer_merge(server.server_buffer)
@ -70,56 +65,31 @@ def matrix_config_change_cb(data, option):
def matrix_config_init(config_file):
look_options = [
Option(
"redactions", "integer",
"strikethrough|notice|delete", 0, 0,
"strikethrough",
(
"Only notice redactions, strike through or delete "
"redacted messages"
)
),
Option(
"server_buffer", "integer",
"merge_with_core|merge_without_core|independent",
0, 0, "merge_with_core", "Merge server buffers"
)
Option("redactions", "integer", "strikethrough|notice|delete", 0, 0,
"strikethrough",
("Only notice redactions, strike through or delete "
"redacted messages")),
Option("server_buffer", "integer",
"merge_with_core|merge_without_core|independent", 0, 0,
"merge_with_core", "Merge server buffers")
]
network_options = [
Option(
"max_initial_sync_events", "integer",
"", 1, 10000,
"30",
(
"How many events to fetch during the initial sync"
)
),
Option(
"max_backlog_sync_events", "integer",
"", 1, 100,
"10",
(
"How many events to fetch during backlog fetching"
)
),
Option(
"fetch_backlog_on_pgup", "boolean",
"", 0, 0,
"on",
(
"Fetch messages in the backlog on a window page up event"
)
)
Option("max_initial_sync_events", "integer", "", 1, 10000, "30",
("How many events to fetch during the initial sync")),
Option("max_backlog_sync_events", "integer", "", 1, 100, "10",
("How many events to fetch during backlog fetching")),
Option("fetch_backlog_on_pgup", "boolean", "", 0, 0, "on",
("Fetch messages in the backlog on a window page up event"))
]
def add_global_options(section, options):
for option in options:
OPTIONS.options[option.name] = W.config_new_option(
config_file, section, option.name,
option.type, option.description, option.string_values,
option.min, option.max, option.value, option.value, 0, "",
"", "matrix_config_change_cb", "", "", "")
config_file, section, option.name, option.type,
option.description, option.string_values, option.min,
option.max, option.value, option.value, 0, "", "",
"matrix_config_change_cb", "", "", "")
section = W.config_new_section(config_file, "color", 0, 0, "", "", "", "",
"", "", "", "", "", "")
@ -131,19 +101,14 @@ def matrix_config_init(config_file):
add_global_options(section, look_options)
section = W.config_new_section(config_file, "network", 0, 0, "", "", "",
"", "", "", "", "", "", "")
section = W.config_new_section(config_file, "network", 0, 0, "", "", "", "",
"", "", "", "", "", "")
add_global_options(section, network_options)
W.config_new_section(
config_file, "server",
0, 0,
"matrix_config_server_read_cb",
"",
"matrix_config_server_write_cb",
"", "", "", "", "", "", ""
)
config_file, "server", 0, 0, "matrix_config_server_read_cb", "",
"matrix_config_server_write_cb", "", "", "", "", "", "", "")
return config_file

View file

@ -23,9 +23,8 @@ from matrix.plugin_options import PluginOptions
import weechat
W = weechat if sys.hexversion >= 0x3000000 else WeechatWrapper(weechat)
OPTIONS = PluginOptions() # type: PluginOptions
SERVERS = dict() # type: Dict[str, MatrixServer]
CONFIG = None # type: weechat.config
SERVERS = dict() # type: Dict[str, MatrixServer]
CONFIG = None # type: weechat.config

View file

@ -18,6 +18,7 @@ from __future__ import unicode_literals
from collections import namedtuple
from enum import Enum, unique
@unique
class RedactType(Enum):
STRIKETHROUGH = 0
@ -40,29 +41,22 @@ class DebugType(Enum):
Option = namedtuple(
'Option', [
'name',
'type',
'string_values',
'min',
'max',
'value',
'description'
])
'Option',
['name', 'type', 'string_values', 'min', 'max', 'value', 'description'])
class PluginOptions:
def __init__(self):
self.redaction_type = RedactType.STRIKETHROUGH # type: RedactType
self.redaction_type = RedactType.STRIKETHROUGH # type: RedactType
self.look_server_buf = ServerBufferType.MERGE_CORE # type: ServerBufferType
self.sync_limit = 30 # type: int
self.backlog_limit = 10 # type: int
self.enable_backlog = True # type: bool
self.page_up_hook = None # type: weechat.hook
self.sync_limit = 30 # type: int
self.backlog_limit = 10 # type: int
self.enable_backlog = True # type: bool
self.page_up_hook = None # type: weechat.hook
self.redaction_comp_len = 50 # type: int
self.options = dict() # type: Dict[str, weechat.config_option]
self.debug = [] # type: List[DebugType]
self.options = dict() # type: Dict[str, weechat.config_option]
self.debug = [] # type: List[DebugType]

View file

@ -31,7 +31,6 @@ from builtins import bytes, str
from collections import Mapping, Iterable
from functools import wraps
# These functions were written by Trygve Aaberge for wee-slack and are under a
# MIT License.
# More info can be found in the wee-slack repository under the commit:
@ -40,17 +39,20 @@ from functools import wraps
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
@ -67,11 +69,7 @@ class WeechatWrapper(object):
def prnt_date_tags(self, buffer, date, tags, message):
message = message.replace("\n", "\n \t")
return self.wrap_for_utf8(self.wrapped_class.prnt_date_tags)(
buffer,
date,
tags,
message
)
buffer, date, tags, message)
def utf8_decode(function):
@ -79,6 +77,7 @@ def utf8_decode(function):
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(function)
def wrapper(*args, **kwargs):
@ -87,6 +86,7 @@ def utf8_decode(function):
return function(*args, **kwargs)
return function(*decode_from_utf8(args), **decode_from_utf8(kwargs))
return wrapper

View file

@ -44,29 +44,21 @@ def server_buffer_prnt(server, string):
def tags_from_line_data(line_data):
# type: (weechat.hdata) -> List[str]
tags_count = W.hdata_get_var_array_size(
W.hdata_get('line_data'),
line_data,
'tags_array')
W.hdata_get('line_data'), line_data, 'tags_array')
tags = [
W.hdata_string(
W.hdata_get('line_data'),
line_data,
'%d|tags_array' % i
) for i in range(tags_count)]
W.hdata_get('line_data'), line_data, '%d|tags_array' % i)
for i in range(tags_count)
]
return tags
def create_server_buffer(server):
# type: (MatrixServer) -> None
server.server_buffer = W.buffer_new(
server.name,
"server_buffer_cb",
server.name,
"",
""
)
server.server_buffer = W.buffer_new(server.name, "server_buffer_cb",
server.name, "", "")
server_buffer_set_title(server)
W.buffer_set(server.server_buffer, "localvar_set_type", 'server')
@ -107,9 +99,7 @@ def server_buffer_set_title(server):
ip_string = ""
title = ("Matrix: {address}:{port}{ip}").format(
address=server.address,
port=server.port,
ip=ip_string)
address=server.address, port=server.port, ip=ip_string)
W.buffer_set(server.server_buffer, "title", title)
@ -155,17 +145,9 @@ def sender_to_nick_and_color(room, sender):
def tags_for_message(message_type):
default_tags = {
"message": [
"matrix_message",
"notify_message",
"log1"
],
"backlog": [
"matrix_message",
"notify_message",
"no_log",
"no_highlight"
]
"message": ["matrix_message", "notify_message", "log1"],
"backlog":
["matrix_message", "notify_message", "no_log", "no_highlight"]
}
return default_tags[message_type]