Optimize imports
This commit is contained in:
parent
2e7a4ad132
commit
e588c30044
29 changed files with 730 additions and 924 deletions
|
|
@ -16,11 +16,8 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import calendar
|
||||
from datetime import datetime, timezone
|
||||
import ipaddress
|
||||
import re
|
||||
import time
|
||||
import shlex
|
||||
|
||||
from nemubot.channel import Channel
|
||||
|
|
@ -82,6 +79,7 @@ class IRC(SocketServer):
|
|||
def _ctcp_dcc(msg, cmds):
|
||||
"""Response to DCC CTCP message"""
|
||||
try:
|
||||
import ipaddress
|
||||
ip = ipaddress.ip_address(int(cmds[3]))
|
||||
port = int(cmds[4])
|
||||
conn = DCC(srv, msg.sender)
|
||||
|
|
@ -333,6 +331,7 @@ class IRCMessage:
|
|||
|
||||
# Treat special tags
|
||||
if key == "time":
|
||||
import calendar, time
|
||||
value = datetime.fromtimestamp(calendar.timegm(time.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")), timezone.utc)
|
||||
|
||||
# Store tag
|
||||
|
|
|
|||
|
|
@ -16,11 +16,6 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import io
|
||||
import imp
|
||||
import logging
|
||||
import socket
|
||||
import queue
|
||||
|
||||
# Lists for select
|
||||
_rlist = []
|
||||
|
|
@ -28,132 +23,12 @@ _wlist = []
|
|||
_xlist = []
|
||||
|
||||
|
||||
# Extends from IOBase in order to be compatible with select function
|
||||
class AbstractServer(io.IOBase):
|
||||
|
||||
"""An abstract server: handle communication with an IM server"""
|
||||
|
||||
def __init__(self, send_callback=None):
|
||||
"""Initialize an abstract server
|
||||
|
||||
Keyword argument:
|
||||
send_callback -- Callback when developper want to send a message
|
||||
"""
|
||||
|
||||
if not hasattr(self, "id"):
|
||||
raise Exception("No id defined for this server. Please set one!")
|
||||
|
||||
self.logger = logging.getLogger("nemubot.server." + self.id)
|
||||
self._sending_queue = queue.Queue()
|
||||
if send_callback is not None:
|
||||
self._send_callback = send_callback
|
||||
else:
|
||||
self._send_callback = self._write_select
|
||||
|
||||
|
||||
# Open/close
|
||||
|
||||
def __enter__(self):
|
||||
self.open()
|
||||
return self
|
||||
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.close()
|
||||
|
||||
|
||||
def open(self):
|
||||
"""Generic open function that register the server un _rlist in case
|
||||
of successful _open"""
|
||||
self.logger.info("Opening connection to %s", self.id)
|
||||
if not hasattr(self, "_open") or self._open():
|
||||
_rlist.append(self)
|
||||
_xlist.append(self)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def close(self):
|
||||
"""Generic close function that register the server un _{r,w,x}list in
|
||||
case of successful _close"""
|
||||
self.logger.info("Closing connection to %s", self.id)
|
||||
if not hasattr(self, "_close") or self._close():
|
||||
if self in _rlist:
|
||||
_rlist.remove(self)
|
||||
if self in _wlist:
|
||||
_wlist.remove(self)
|
||||
if self in _xlist:
|
||||
_xlist.remove(self)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# Writes
|
||||
|
||||
def write(self, message):
|
||||
"""Asynchronymously send a message to the server using send_callback
|
||||
|
||||
Argument:
|
||||
message -- message to send
|
||||
"""
|
||||
|
||||
self._send_callback(message)
|
||||
|
||||
|
||||
def write_select(self):
|
||||
"""Internal function used by the select function"""
|
||||
try:
|
||||
_wlist.remove(self)
|
||||
while not self._sending_queue.empty():
|
||||
self._write(self._sending_queue.get_nowait())
|
||||
self._sending_queue.task_done()
|
||||
|
||||
except queue.Empty:
|
||||
pass
|
||||
|
||||
|
||||
def _write_select(self, message):
|
||||
"""Send a message to the server safely through select
|
||||
|
||||
Argument:
|
||||
message -- message to send
|
||||
"""
|
||||
|
||||
self._sending_queue.put(self.format(message))
|
||||
self.logger.debug("Message '%s' appended to Queue", message)
|
||||
if self not in _wlist:
|
||||
_wlist.append(self)
|
||||
|
||||
|
||||
def send_response(self, response):
|
||||
"""Send a formated Message class
|
||||
|
||||
Argument:
|
||||
response -- message to send
|
||||
"""
|
||||
|
||||
if response is None:
|
||||
return
|
||||
|
||||
elif isinstance(response, list):
|
||||
for r in response:
|
||||
self.send_response(r)
|
||||
|
||||
else:
|
||||
vprnt = self.printer()
|
||||
response.accept(vprnt)
|
||||
self.write(vprnt.pp)
|
||||
|
||||
|
||||
# Exceptions
|
||||
|
||||
def exception(self):
|
||||
"""Exception occurs in fd"""
|
||||
self.logger.warning("Unhandle file descriptor exception on server %s",
|
||||
self.id)
|
||||
|
||||
|
||||
def reload():
|
||||
import imp
|
||||
|
||||
import nemubot.server.abstract
|
||||
imp.reload(nemubot.server.abstract)
|
||||
|
||||
import nemubot.server.socket
|
||||
imp.reload(nemubot.server.socket)
|
||||
|
||||
|
|
|
|||
147
nemubot/server/abstract.py
Normal file
147
nemubot/server/abstract.py
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Nemubot is a smart and modulable IM bot.
|
||||
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import io
|
||||
import logging
|
||||
import queue
|
||||
|
||||
from nemubot.server import _rlist, _wlist, _xlist
|
||||
|
||||
# Extends from IOBase in order to be compatible with select function
|
||||
class AbstractServer(io.IOBase):
|
||||
|
||||
"""An abstract server: handle communication with an IM server"""
|
||||
|
||||
def __init__(self, send_callback=None):
|
||||
"""Initialize an abstract server
|
||||
|
||||
Keyword argument:
|
||||
send_callback -- Callback when developper want to send a message
|
||||
"""
|
||||
|
||||
if not hasattr(self, "id"):
|
||||
raise Exception("No id defined for this server. Please set one!")
|
||||
|
||||
self.logger = logging.getLogger("nemubot.server." + self.id)
|
||||
self._sending_queue = queue.Queue()
|
||||
if send_callback is not None:
|
||||
self._send_callback = send_callback
|
||||
else:
|
||||
self._send_callback = self._write_select
|
||||
|
||||
|
||||
# Open/close
|
||||
|
||||
def __enter__(self):
|
||||
self.open()
|
||||
return self
|
||||
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.close()
|
||||
|
||||
|
||||
def open(self):
|
||||
"""Generic open function that register the server un _rlist in case
|
||||
of successful _open"""
|
||||
self.logger.info("Opening connection to %s", self.id)
|
||||
if not hasattr(self, "_open") or self._open():
|
||||
_rlist.append(self)
|
||||
_xlist.append(self)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def close(self):
|
||||
"""Generic close function that register the server un _{r,w,x}list in
|
||||
case of successful _close"""
|
||||
self.logger.info("Closing connection to %s", self.id)
|
||||
if not hasattr(self, "_close") or self._close():
|
||||
if self in _rlist:
|
||||
_rlist.remove(self)
|
||||
if self in _wlist:
|
||||
_wlist.remove(self)
|
||||
if self in _xlist:
|
||||
_xlist.remove(self)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# Writes
|
||||
|
||||
def write(self, message):
|
||||
"""Asynchronymously send a message to the server using send_callback
|
||||
|
||||
Argument:
|
||||
message -- message to send
|
||||
"""
|
||||
|
||||
self._send_callback(message)
|
||||
|
||||
|
||||
def write_select(self):
|
||||
"""Internal function used by the select function"""
|
||||
try:
|
||||
_wlist.remove(self)
|
||||
while not self._sending_queue.empty():
|
||||
self._write(self._sending_queue.get_nowait())
|
||||
self._sending_queue.task_done()
|
||||
|
||||
except queue.Empty:
|
||||
pass
|
||||
|
||||
|
||||
def _write_select(self, message):
|
||||
"""Send a message to the server safely through select
|
||||
|
||||
Argument:
|
||||
message -- message to send
|
||||
"""
|
||||
|
||||
self._sending_queue.put(self.format(message))
|
||||
self.logger.debug("Message '%s' appended to Queue", message)
|
||||
if self not in _wlist:
|
||||
_wlist.append(self)
|
||||
|
||||
|
||||
def send_response(self, response):
|
||||
"""Send a formated Message class
|
||||
|
||||
Argument:
|
||||
response -- message to send
|
||||
"""
|
||||
|
||||
if response is None:
|
||||
return
|
||||
|
||||
elif isinstance(response, list):
|
||||
for r in response:
|
||||
self.send_response(r)
|
||||
|
||||
else:
|
||||
vprnt = self.printer()
|
||||
response.accept(vprnt)
|
||||
self.write(vprnt.pp)
|
||||
|
||||
|
||||
# Exceptions
|
||||
|
||||
def exception(self):
|
||||
"""Exception occurs in fd"""
|
||||
self.logger.warning("Unhandle file descriptor exception on server %s",
|
||||
self.id)
|
||||
|
|
@ -16,10 +16,7 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ssl
|
||||
import socket
|
||||
|
||||
from nemubot.server import AbstractServer
|
||||
from nemubot.server.abstract import AbstractServer
|
||||
|
||||
|
||||
class SocketServer(AbstractServer):
|
||||
|
|
@ -49,11 +46,14 @@ class SocketServer(AbstractServer):
|
|||
# Open/close
|
||||
|
||||
def _open(self):
|
||||
import os
|
||||
import socket
|
||||
# Create the socket
|
||||
self.socket = socket.socket()
|
||||
|
||||
# Wrap the socket for SSL
|
||||
if self.ssl:
|
||||
import ssl
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
||||
self.socket = ctx.wrap_socket(self.socket)
|
||||
|
||||
|
|
@ -71,6 +71,8 @@ class SocketServer(AbstractServer):
|
|||
|
||||
|
||||
def _close(self):
|
||||
import socket
|
||||
|
||||
self._sending_queue.join()
|
||||
if self.connected:
|
||||
try:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue