Apply PEP8 fixes to the source tree.

This commit is contained in:
poljar (Damir Jelić) 2018-01-27 16:21:10 +01:00
parent 0e4c29b0c6
commit 937ad993c5
7 changed files with 102 additions and 100 deletions

View file

@ -76,33 +76,33 @@ CONFIG = matrix.globals.CONFIG
SERVERS = matrix.globals.SERVERS SERVERS = matrix.globals.SERVERS
WEECHAT_SCRIPT_NAME = "matrix" # type: str WEECHAT_SCRIPT_NAME = "matrix" # type: str
WEECHAT_SCRIPT_DESCRIPTION = "matrix chat plugin" # type: str WEECHAT_SCRIPT_DESCRIPTION = "matrix chat plugin" # type: str
WEECHAT_SCRIPT_AUTHOR = "Damir Jelić <poljar@termina.org.uk>" # type: str WEECHAT_SCRIPT_AUTHOR = "Damir Jelić <poljar@termina.org.uk>" # type: str
WEECHAT_SCRIPT_VERSION = "0.1" # type: str WEECHAT_SCRIPT_VERSION = "0.1" # type: str
WEECHAT_SCRIPT_LICENSE = "ISC" # type: str WEECHAT_SCRIPT_LICENSE = "ISC" # type: str
class MatrixUser: class MatrixUser:
def __init__(self, name, display_name): def __init__(self, name, display_name):
self.name = name # type: str self.name = name # type: str
self.display_name = display_name # type: str self.display_name = display_name # type: str
self.power_level = 0 # type: int self.power_level = 0 # type: int
self.nick_color = "" # type: str self.nick_color = "" # type: str
self.prefix = "" # type: str self.prefix = "" # type: str
class MatrixRoom: class MatrixRoom:
def __init__(self, room_id): def __init__(self, room_id):
# type: (str) -> None # type: (str) -> None
self.room_id = room_id # type: str self.room_id = room_id # type: str
self.alias = room_id # type: str self.alias = room_id # type: str
self.topic = "" # type: str self.topic = "" # type: str
self.topic_author = "" # type: str self.topic_author = "" # type: str
self.topic_date = None # type: datetime.datetime self.topic_date = None # type: datetime.datetime
self.prev_batch = "" # type: str self.prev_batch = "" # type: str
self.users = dict() # type: Dict[str, MatrixUser] self.users = dict() # type: Dict[str, MatrixUser]
self.encrypted = False # type: bool self.encrypted = False # type: bool
@utf8_decode @utf8_decode
@ -921,10 +921,10 @@ def matrix_handle_message(
matrix_sync(server) matrix_sync(server)
elif message_type is MessageType.SEND: elif message_type is MessageType.SEND:
author = extra_data["author"] author = extra_data["author"]
message = extra_data["message"] message = extra_data["message"]
room_id = extra_data["room_id"] room_id = extra_data["room_id"]
date = int(time.time()) date = int(time.time())
# TODO the event_id can be missing if sending has failed for # TODO the event_id can be missing if sending has failed for
# some reason # some reason
event_id = response["event_id"] event_id = response["event_id"]
@ -1121,9 +1121,9 @@ def connect_cb(data, status, gnutls_rc, sock, error, ip_address):
server.name server.name
) )
server.fd_hook = hook server.fd_hook = hook
server.connected = True server.connected = True
server.connecting = False server.connecting = False
server.reconnect_count = 0 server.reconnect_count = 0
server.numeric_address = ip_address server.numeric_address = ip_address

View file

@ -28,15 +28,15 @@ MATRIX_API_PATH = "/_matrix/client/r0" # type: str
@unique @unique
class MessageType(Enum): class MessageType(Enum):
LOGIN = 0 LOGIN = 0
SYNC = 1 SYNC = 1
SEND = 2 SEND = 2
STATE = 3 STATE = 3
REDACT = 4 REDACT = 4
ROOM_MSG = 5 ROOM_MSG = 5
JOIN = 6 JOIN = 6
PART = 7 PART = 7
INVITE = 8 INVITE = 8
class MatrixMessage: class MatrixMessage:
@ -52,14 +52,14 @@ class MatrixMessage:
): ):
# type: (...) -> None # type: (...) -> None
# pylint: disable=dangerous-default-value # pylint: disable=dangerous-default-value
self.type = message_type # type: MessageType self.type = message_type # type: MessageType
self.request = None # type: HttpRequest self.request = None # type: HttpRequest
self.response = None # type: HttpResponse self.response = None # type: HttpResponse
self.extra_data = extra_data # type: Dict[str, Any] self.extra_data = extra_data # type: Dict[str, Any]
self.creation_time = time.time() # type: float self.creation_time = time.time() # type: float
self.send_time = None # type: float self.send_time = None # type: float
self.receive_time = None # type: float self.receive_time = None # type: float
if message_type == MessageType.LOGIN: if message_type == MessageType.LOGIN:
path = ("{api}/login").format(api=MATRIX_API_PATH) path = ("{api}/login").format(api=MATRIX_API_PATH)

View file

@ -23,22 +23,22 @@ from enum import Enum, unique
@unique @unique
class RedactType(Enum): class RedactType(Enum):
STRIKETHROUGH = 0 STRIKETHROUGH = 0
NOTICE = 1 NOTICE = 1
DELETE = 2 DELETE = 2
@unique @unique
class ServerBufferType(Enum): class ServerBufferType(Enum):
MERGE_CORE = 0 MERGE_CORE = 0
MERGE = 1 MERGE = 1
INDEPENDENT = 2 INDEPENDENT = 2
@unique @unique
class DebugType(Enum): class DebugType(Enum):
MESSAGING = 0 MESSAGING = 0
NETWORK = 1 NETWORK = 1
TIMING = 2 TIMING = 2
Option = namedtuple( Option = namedtuple(
@ -55,15 +55,15 @@ Option = namedtuple(
class PluginOptions: class PluginOptions:
def __init__(self): 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.look_server_buf = ServerBufferType.MERGE_CORE # type: ServerBufferType
self.sync_limit = 30 # type: int self.sync_limit = 30 # type: int
self.backlog_limit = 10 # type: int self.backlog_limit = 10 # type: int
self.enable_backlog = True # type: bool self.enable_backlog = True # type: bool
self.page_up_hook = None # type: weechat.hook self.page_up_hook = None # type: weechat.hook
self.redaction_comp_len = 50 # type: int self.redaction_comp_len = 50 # type: int
self.options = dict() # type: Dict[str, weechat.config_option] self.options = dict() # type: Dict[str, weechat.config_option]
self.debug = [] # type: List[DebugType] self.debug = [] # type: List[DebugType]

View file

@ -108,6 +108,6 @@ def init_matrix_config():
W = weechat if sys.hexversion >= 0x3000000 else WeechatWrapper(weechat) W = weechat if sys.hexversion >= 0x3000000 else WeechatWrapper(weechat)
OPTIONS = PluginOptions() # type: PluginOptions OPTIONS = PluginOptions() # type: PluginOptions
SERVERS = dict() # type: Dict[str, MatrixServer] SERVERS = dict() # type: Dict[str, MatrixServer]
CONFIG = None # type: weechat.config CONFIG = None # type: weechat.config

View file

@ -22,16 +22,16 @@ from enum import Enum, unique
@unique @unique
class RequestType(Enum): class RequestType(Enum):
GET = 0 GET = 0
POST = 1 POST = 1
PUT = 2 PUT = 2
class HttpResponse: class HttpResponse:
def __init__(self, status, headers, body): def __init__(self, status, headers, body):
self.status = status # type: int self.status = status # type: int
self.headers = headers # type: Dict[str, str] self.headers = headers # type: Dict[str, str]
self.body = body # type: bytes self.body = body # type: bytes
class HttpRequest: class HttpRequest:
@ -46,24 +46,24 @@ class HttpRequest:
version="0.1") # type: str version="0.1") # type: str
): ):
# type: (...) -> None # type: (...) -> None
host_string = ':'.join([host, str(port)]) host_string = ':'.join([host, str(port)])
user_agent = 'User-Agent: {agent}'.format(agent=user_agent) user_agent = 'User-Agent: {agent}'.format(agent=user_agent)
host_header = 'Host: {host}'.format(host=host_string) host_header = 'Host: {host}'.format(host=host_string)
request_list = [] # type: List[str] request_list = [] # type: List[str]
accept_header = 'Accept: */*' # type: str accept_header = 'Accept: */*' # type: str
end_separator = '\r\n' # type: str end_separator = '\r\n' # type: str
payload = None # type: str payload = None # type: str
if request_type == RequestType.GET: if request_type == RequestType.GET:
get = 'GET {location} HTTP/1.1'.format(location=location) get = 'GET {location} HTTP/1.1'.format(location=location)
request_list = [get, host_header, request_list = [get, host_header,
user_agent, accept_header, end_separator] user_agent, accept_header, end_separator]
elif (request_type == RequestType.POST or elif (request_type == RequestType.POST or
request_type == RequestType.PUT): request_type == RequestType.PUT):
json_data = json.dumps(data, separators=(',', ':')) json_data = json.dumps(data, separators=(',', ':'))
if request_type == RequestType.POST: if request_type == RequestType.POST:
method = "POST" method = "POST"
@ -75,15 +75,15 @@ class HttpRequest:
location=location location=location
) )
type_header = 'Content-Type: application/x-www-form-urlencoded' type_header = 'Content-Type: application/x-www-form-urlencoded'
length_header = 'Content-Length: {length}'.format( length_header = 'Content-Length: {length}'.format(
length=len(json_data) length=len(json_data)
) )
request_list = [request_line, host_header, request_list = [request_line, host_header,
user_agent, accept_header, user_agent, accept_header,
length_header, type_header, end_separator] length_header, type_header, end_separator]
payload = json_data payload = json_data
request = '\r\n'.join(request_list) request = '\r\n'.join(request_list)

View file

@ -28,41 +28,43 @@ class MatrixServer:
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
def __init__(self, name, w, config_file): def __init__(self, name, w, config_file):
# type: (str, weechat, weechat.config) -> None # type: (str, weechat, weechat.config) -> None
self.name = name # type: str self.name = name # type: str
self.user_id = "" self.user_id = ""
self.address = "" # type: str self.address = "" # type: str
self.port = 8448 # type: int self.port = 8448 # type: int
self.options = dict() # type: Dict[str, weechat.config] self.options = dict() # type: Dict[str, weechat.config]
self.device_name = "Weechat Matrix" # type: str self.device_name = "Weechat Matrix" # type: str
self.user = "" # type: str self.user = "" # type: str
self.password = "" # type: str self.password = "" # type: str
self.rooms = dict() # type: Dict[str, MatrixRoom] self.rooms = dict() # type: Dict[str, MatrixRoom]
self.buffers = dict() # type: Dict[str, weechat.buffer] self.buffers = dict() # type: Dict[str, weechat.buffer]
self.server_buffer = None # type: weechat.buffer self.server_buffer = None # type: weechat.buffer
self.fd_hook = None # type: weechat.hook self.fd_hook = None # type: weechat.hook
self.timer_hook = None # type: weechat.hook self.timer_hook = None # type: weechat.hook
self.numeric_address = "" # type: str self.numeric_address = "" # type: str
self.autoconnect = False # type: bool self.autoconnect = False # type: bool
self.connected = False # type: bool self.connected = False # type: bool
self.connecting = False # type: bool self.connecting = False # type: bool
self.reconnect_count = 0 # type: int self.reconnect_count = 0 # type: int
self.socket = None # type: ssl.SSLSocket self.socket = None # type: ssl.SSLSocket
self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext
self.access_token = None # type: str self.access_token = None # type: str
self.next_batch = None # type: str self.next_batch = None # type: str
self.transaction_id = 0 # type: int self.transaction_id = 0 # type: int
self.http_parser = HttpParser() # type: HttpParser self.http_parser = HttpParser() # type: HttpParser
self.http_buffer = [] # type: List[bytes] self.http_buffer = [] # type: List[bytes]
# Queue of messages we need to send off. # Queue of messages we need to send off.
self.send_queue = deque() # type: Deque[MatrixMessage] self.send_queue = deque() # type: Deque[MatrixMessage]
# Queue of messages we send off and are waiting a response for # Queue of messages we send off and are waiting a response for
self.receive_queue = deque() # type: Deque[MatrixMessage] self.receive_queue = deque() # type: Deque[MatrixMessage]
self.message_queue = deque() # type: Deque[MatrixMessage] self.message_queue = deque() # type: Deque[MatrixMessage]
self.ignore_event_list = [] # type: List[str] self.ignore_event_list = [] # type: List[str]

View file

@ -33,9 +33,9 @@ def disconnect(server):
if server.fd_hook: if server.fd_hook:
W.unhook(server.fd_hook) W.unhook(server.fd_hook)
server.fd_hook = None server.fd_hook = None
server.socket = None server.socket = None
server.connected = False server.connected = False
server_buffer_prnt(server, "Disconnected") server_buffer_prnt(server, "Disconnected")