More split work for the config.

This commit is contained in:
poljar (Damir Jelić) 2018-01-29 17:47:47 +01:00
parent 48f17e14d4
commit 44fb7a1913
8 changed files with 154 additions and 189 deletions

View file

@ -74,18 +74,21 @@ from matrix.utils import (
server_buffer_set_title
)
from matrix.config import (
from matrix.plugin_options import (
DebugType,
RedactType,
ServerBufferType
ServerBufferType,
)
import matrix.globals
from matrix.config import (
matrix_config_init,
matrix_config_read,
matrix_config_free,
matrix_config_change_cb,
matrix_config_reload_cb
)
W = matrix.globals.W
GLOBAL_OPTIONS = matrix.globals.OPTIONS
CONFIG = matrix.globals.CONFIG
SERVERS = matrix.globals.SERVERS
from matrix.globals import W, OPTIONS, CONFIG, SERVERS
WEECHAT_SCRIPT_NAME = "matrix" # type: str
@ -344,7 +347,7 @@ def room_input_cb(server_name, buffer, input_data):
"room_id": room_id
}
message = MatrixMessage(server, GLOBAL_OPTIONS, MessageType.SEND,
message = MatrixMessage(server, OPTIONS, MessageType.SEND,
data=body, room_id=room_id,
extra_data=extra_data)
@ -389,11 +392,6 @@ def matrix_timer_cb(server_name, remaining_calls):
return W.WEECHAT_RC_OK
@utf8_decode
def matrix_config_reload_cb(data, config_file):
return W.WEECHAT_RC_OK
@utf8_decode
def matrix_config_server_read_cb(
data, config_file, section,
@ -434,54 +432,9 @@ def matrix_config_server_write_cb(data, config_file, section_name):
return W.WEECHAT_CONFIG_WRITE_OK
@utf8_decode
def matrix_config_change_cb(data, option):
option_name = key_from_value(GLOBAL_OPTIONS.options, option)
if option_name == "redactions":
GLOBAL_OPTIONS.redaction_type = RedactType(W.config_integer(option))
elif option_name == "server_buffer":
GLOBAL_OPTIONS.look_server_buf = ServerBufferType(
W.config_integer(option))
elif option_name == "max_initial_sync_events":
GLOBAL_OPTIONS.sync_limit = W.config_integer(option)
elif option_name == "max_backlog_sync_events":
GLOBAL_OPTIONS.backlog_limit = W.config_integer(option)
elif option_name == "fetch_backlog_on_pgup":
GLOBAL_OPTIONS.enable_backlog = W.config_boolean(option)
if GLOBAL_OPTIONS.enable_backlog:
if not GLOBAL_OPTIONS.page_up_hook:
hook_page_up(CONFIG)
else:
if GLOBAL_OPTIONS.page_up_hook:
W.unhook(GLOBAL_OPTIONS.page_up_hook)
GLOBAL_OPTIONS.page_up_hook = None
return 1
def read_matrix_config():
# type: () -> bool
return_code = W.config_read(CONFIG)
if return_code == W.WEECHAT_CONFIG_READ_OK:
return True
elif return_code == W.WEECHAT_CONFIG_READ_MEMORY_ERROR:
return False
elif return_code == W.WEECHAT_CONFIG_READ_FILE_NOT_FOUND:
return True
return False
@utf8_decode
def matrix_unload_cb():
for section in ["network", "look", "color", "server"]:
section_pointer = W.config_search_section(CONFIG, section)
W.config_section_free_options(section_pointer)
W.config_section_free(section_pointer)
W.config_free(CONFIG)
matrix_config_free(CONFIG)
return W.WEECHAT_RC_OK
@ -510,8 +463,8 @@ if __name__ == "__main__":
''):
# TODO if this fails we should abort and unload the script.
CONFIG = matrix.globals.init_matrix_config()
read_matrix_config()
CONFIG = matrix_config_init()
matrix_config_read(CONFIG)
hook_commands()
init_bar_items()

View file

@ -25,7 +25,7 @@ from matrix.utf import utf8_decode
from matrix.api import MatrixMessage, MessageType
from matrix.utils import key_from_value, tags_from_line_data
from matrix.socket import send_or_queue, disconnect, connect
from matrix.config import DebugType
from matrix.plugin_options import DebugType
from matrix.server import MatrixServer

View file

@ -16,54 +16,148 @@
from __future__ import unicode_literals
from collections import namedtuple
from enum import Enum, unique
from matrix.plugin_options import (
Option,
RedactType,
ServerBufferType
)
from matrix.globals import W, OPTIONS, CONFIG
from matrix.utf import utf8_decode
from matrix.utils import key_from_value
from matrix.commands import hook_page_up
@unique
class RedactType(Enum):
STRIKETHROUGH = 0
NOTICE = 1
DELETE = 2
@utf8_decode
def matrix_config_reload_cb(data, config_file):
return W.WEECHAT_RC_OK
@unique
class ServerBufferType(Enum):
MERGE_CORE = 0
MERGE = 1
INDEPENDENT = 2
@utf8_decode
def matrix_config_change_cb(data, option):
option_name = key_from_value(OPTIONS.options, option)
if option_name == "redactions":
OPTIONS.redaction_type = RedactType(W.config_integer(option))
elif option_name == "server_buffer":
OPTIONS.look_server_buf = ServerBufferType(
W.config_integer(option))
elif option_name == "max_initial_sync_events":
OPTIONS.sync_limit = W.config_integer(option)
elif option_name == "max_backlog_sync_events":
OPTIONS.backlog_limit = W.config_integer(option)
elif option_name == "fetch_backlog_on_pgup":
OPTIONS.enable_backlog = W.config_boolean(option)
if OPTIONS.enable_backlog:
if not OPTIONS.page_up_hook:
hook_page_up(CONFIG)
else:
if OPTIONS.page_up_hook:
W.unhook(OPTIONS.page_up_hook)
OPTIONS.page_up_hook = None
return 1
@unique
class DebugType(Enum):
MESSAGING = 0
NETWORK = 1
TIMING = 2
def matrix_config_init():
config_file = W.config_new("matrix", "matrix_config_reload_cb", "")
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"
)
]
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"
)
)
]
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", "", "", "")
section = W.config_new_section(config_file, "color", 0, 0, "", "", "", "",
"", "", "", "", "", "")
# TODO color options
section = W.config_new_section(config_file, "look", 0, 0, "", "", "", "",
"", "", "", "", "", "")
add_global_options(section, look_options)
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",
"", "", "", "", "", "", ""
)
return config_file
Option = namedtuple(
'Option', [
'name',
'type',
'string_values',
'min',
'max',
'value',
'description'
])
def matrix_config_read(config):
# type: () -> bool
return_code = W.config_read(config)
if return_code == W.WEECHAT_CONFIG_READ_OK:
return True
elif return_code == W.WEECHAT_CONFIG_READ_MEMORY_ERROR:
return False
elif return_code == W.WEECHAT_CONFIG_READ_FILE_NOT_FOUND:
return True
return False
class PluginOptions:
def __init__(self):
self.redaction_type = RedactType.STRIKETHROUGH # type: RedactType
self.look_server_buf = ServerBufferType.MERGE_CORE # type: ServerBufferType
def matrix_config_free(config):
for section in ["network", "look", "color", "server"]:
section_pointer = W.config_search_section(config, section)
W.config_section_free_options(section_pointer)
W.config_section_free(section_pointer)
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]
W.config_free(config)

View file

@ -19,93 +19,11 @@ from __future__ import unicode_literals
import sys
from matrix.utf import WeechatWrapper
from matrix.config import PluginOptions, Option
from matrix.plugin_options import PluginOptions
import weechat
def init_matrix_config():
config_file = W.config_new("matrix", "matrix_config_reload_cb", "")
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"
)
]
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"
)
)
]
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", "", "", "")
section = W.config_new_section(config_file, "color", 0, 0, "", "", "", "",
"", "", "", "", "", "")
# TODO color options
section = W.config_new_section(config_file, "look", 0, 0, "", "", "", "",
"", "", "", "", "", "")
add_global_options(section, look_options)
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",
"", "", "", "", "", "", ""
)
return config_file
W = weechat if sys.hexversion >= 0x3000000 else WeechatWrapper(weechat)
OPTIONS = PluginOptions() # type: PluginOptions

View file

@ -37,7 +37,7 @@ from matrix.api import (
from matrix.socket import send_or_queue, disconnect, close_socket
from matrix.utils import server_buffer_prnt, tags_from_line_data, prnt_debug
from matrix.config import RedactType, DebugType
from matrix.plugin_options import RedactType, DebugType
def strip_matrix_server(string):
# type: (str) -> str

View file

@ -21,7 +21,7 @@ import ssl
from collections import deque
from http_parser.pyparser import HttpParser
from matrix.config import Option
from matrix.plugin_options import Option
class MatrixServer:

View file

@ -22,7 +22,7 @@ import socket
from builtins import bytes, str
import matrix.globals
from matrix.config import DebugType
from matrix.plugin_options import DebugType
from matrix.utils import prnt_debug, server_buffer_prnt, create_server_buffer

View file

@ -20,7 +20,7 @@ import time
import matrix.globals
from matrix.config import ServerBufferType
from matrix.plugin_options import ServerBufferType
W = matrix.globals.W
GLOBAL_OPTIONS = matrix.globals.OPTIONS