Skip to content

Commit 2e1736f

Browse files
committed
rework files to pass pylint
1 parent 132dd6a commit 2e1736f

16 files changed

Lines changed: 246 additions & 154 deletions

opensips/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20-
from .version import __version__
20+
""" Main package of OpenSIPS """
2121

22+
from .mi import OpenSIPSMI
23+
from .event import OpenSIPSEvent
24+
from .version import __version__

opensips/event/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
""" Event package of OpenSIPS """
21+
2022
from .subscriber import OpenSIPSEvent, OpenSIPSEventException

opensips/event/datagram.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,52 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
""" Implements Datagram Connection """
21+
2022
import socket
2123
from .generic_socket import GenericSocket
2224

2325
class Datagram(GenericSocket):
26+
27+
""" Datagram implementation of a socket """
28+
2429
def __init__(self, **kwargs):
2530
self.ip = None
2631
self.port = None
32+
self.sock = None
2733

2834
if "unix_path" in kwargs:
2935
self.sock_name = kwargs["unix_path"]
3036
elif "ip" in kwargs and "port" in kwargs:
3137
self.ip = kwargs["ip"]
3238
self.port = int(kwargs["port"])
33-
self.sock_name = f"udp:{self.ip}:{self.port}"
3439
else:
3540
raise ValueError("ip and port or unix_path is required for Datagram connector")
36-
41+
3742
def create(self):
3843
if self.ip is not None:
3944
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4045
self.sock.bind((self.ip, self.port))
46+
self.ip, self.port = self.sock.getsockname()
47+
if self.ip == "0.0.0.0":
48+
hostname = socket.gethostname()
49+
self.ip = socket.gethostbyname(hostname)
50+
self.sock_name = f"udp:{self.ip}:{self.port}"
4151
else:
4252
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
4353
self.sock.bind(self.sock_name)
44-
self.sock.setblocking(False)
54+
# we are waiting blocking, so we don't have to change this
55+
self.sock.settimeout(0.1)
56+
return self.sock_name
4557

46-
def handle(self, callback, stop):
47-
while not stop.is_set():
48-
try:
49-
data = self.sock.recv(1024)
50-
callback(data)
51-
except BlockingIOError:
52-
pass
58+
def read(self):
59+
try:
60+
return self.sock.recv(65535)
61+
except socket.timeout:
62+
return None
5363

5464
def destroy(self):
65+
if not self.sock:
66+
return
5567
self.sock.close()
5668
self.sock = None

opensips/event/generic_socket.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
""" Defines a generic socket """
21+
2022
from abc import ABC, abstractmethod
2123

2224
class GenericSocket(ABC):
25+
26+
""" Abstract class for a socket generic implementation """
27+
2328
@abstractmethod
2429
def __init__(self, **kwargs):
2530
pass
2631

2732
@abstractmethod
28-
def create(self):
29-
pass
33+
def create(self) -> str:
34+
""" Creates a socket """
3035

3136
@abstractmethod
32-
def handle(self, callback, stop):
33-
pass
37+
def read(self):
38+
""" Reads data on the socket """
3439

3540
@abstractmethod
3641
def destroy(self):
37-
pass
42+
""" Destroys the socket """

opensips/event/stream.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,46 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
""" Implements TCP/Stream Connection """
21+
2022
import socket
2123
from .generic_socket import GenericSocket
2224

2325
class Stream(GenericSocket):
26+
27+
""" TCP/Stream implementation of a socket """
28+
2429
def __init__(self, **kwargs):
2530
if "ip" not in kwargs:
2631
raise ValueError("ip is required for Stream connector")
2732
if "port" not in kwargs:
2833
raise ValueError("port is required for Stream connector")
29-
34+
3035
self.ip = kwargs["ip"]
3136
self.port = int(kwargs["port"])
32-
self.sock_name = f"tcp:{self.ip}:{self.port}"
37+
self.sock = None
38+
self.sock_name = None
3339

3440
def create(self):
3541
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3642
self.sock.bind((self.ip, self.port))
37-
self.sock.setblocking(False)
43+
self.ip, self.port = self.sock.getsockname()
44+
if self.ip == "0.0.0.0":
45+
hostname = socket.gethostname()
46+
self.ip = socket.gethostbyname(hostname)
47+
self.sock_name = f"udp:{self.ip}:{self.port}"
48+
self.sock.settimeout(0.1)
3849
self.sock.listen(1)
50+
return self.sock_name
51+
52+
def read(self):
53+
try:
54+
conn, _ = self.sock.accept()
55+
with conn:
56+
return conn.recv(65536)
57+
except socket.timeout:
58+
return None
3959

40-
def handle(self, callback, stop):
41-
while not stop.is_set():
42-
try:
43-
conn, _ = self.sock.accept()
44-
conn.setblocking(True)
45-
with conn:
46-
data = conn.recv(1024)
47-
if not data:
48-
continue
49-
callback(data)
50-
except BlockingIOError:
51-
pass
52-
5360
def destroy(self):
5461
self.sock.close()
5562
self.sock = None

opensips/event/subscriber.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,77 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
21+
""" Module that implements OpenSIPS Event behavior """
22+
23+
from threading import Thread, Event
2024
from ..mi import OpenSIPSMI, OpenSIPSMIException
2125
from .datagram import Datagram
2226
from .stream import Stream
23-
from threading import Thread, Event
2427

2528
class OpenSIPSEventException(Exception):
26-
pass
29+
""" Exceptions generated by OpenSIPS Events """
2730

2831
class OpenSIPSEvent():
29-
def __init__(self, mi: OpenSIPSMI, type: str, **kwargs):
32+
33+
""" Implementation of the OpenSIPS Event """
34+
35+
def __init__(self, mi: OpenSIPSMI, _type: str, **kwargs):
3036
self.mi = mi
3137
self.kwargs = kwargs
38+
self.thread = None
39+
self.thread_stop = Event()
40+
self.thread_stop.clear()
3241

33-
if type == "datagram":
42+
if _type == "datagram":
3443
self.socket = Datagram(**kwargs)
35-
elif type == "stream":
44+
elif _type == "stream":
3645
self.socket = Stream(**kwargs)
3746
else:
3847
raise ValueError("Invalid event type")
3948

49+
def handle(self, callback):
50+
""" Handles the event callbacks """
51+
while not self.thread_stop.is_set():
52+
data = self.socket.read()
53+
if data:
54+
callback(data)
55+
4056
def subscribe(self, event: str, callback, expire=None):
57+
""" Subscribes for an event """
4158
try:
59+
sock_name = self.socket.create()
4260
if expire is None:
43-
ret_val = self.mi.execute("event_subscribe", [event, self.socket.sock_name])
61+
ret_val = self.mi.execute("event_subscribe", [event, sock_name])
4462
else:
45-
ret_val = self.mi.execute("event_subscribe", [event, self.socket.sock_name, expire])
63+
ret_val = self.mi.execute("event_subscribe", [event, sock_name, expire])
4664

4765
if ret_val != "OK":
4866
raise OpenSIPSEventException("Failed to subscribe to event")
49-
50-
self.socket.create()
51-
self.thread_stop = Event()
52-
self.thread_stop.clear()
53-
self.thread = Thread(target=self.socket.handle, args=(callback, self.thread_stop))
67+
68+
self.thread = Thread(target=self.handle, args=(callback,))
5469
self.thread.start()
5570

5671
except OpenSIPSMIException as e:
5772
raise e
5873
except Exception as e:
59-
raise OpenSIPSEventException("Failed to subscribe to event: {}".format(e))
60-
74+
raise OpenSIPSEventException(f"Failed to subscribe to event: {e}") from e
75+
6176
def unsubscribe(self, event: str):
77+
""" Unsubscribes for an event """
6278
try:
6379
ret_val = self.mi.execute("event_subscribe", [event, self.socket.sock_name, 0])
6480

6581
if ret_val != "OK":
6682
raise OpenSIPSEventException("Failed to unsubscribe from event")
67-
83+
6884
self.stop()
6985

7086
except OpenSIPSMIException as e:
7187
raise e
7288

7389
def stop(self):
90+
""" Stops the current event processing """
7491
self.thread_stop.set()
7592
self.thread.join()
7693
self.socket.destroy()

opensips/mi/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
21+
""" OpenSIPS MI package """
22+
2023
from .connector import OpenSIPSMI, OpenSIPSMIException

opensips/mi/connection.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
21+
""" Abstract implementation of an MI connection """
22+
2023
from abc import ABC, abstractmethod
2124

2225
class Connection(ABC):
26+
27+
""" Abstract MI Connection """
28+
2329
@abstractmethod
2430
def __init__(self, **kwargs):
2531
pass
2632

2733
@abstractmethod
2834
def execute(self, method: str, params: dict):
29-
pass
35+
""" Executes an MI Command """
3036

3137
@abstractmethod
3238
def valid(self):
33-
pass
39+
""" Checks if an MI connection is valid """

opensips/mi/connector.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
""" Connector implementation for OpenSIPS MI """
21+
2022
from .fifo import FIFO
2123
from .datagram import Datagram
2224
from .http import HTTP
2325
from .jsonrpc_helper import JSONRPCError, JSONRPCException
2426

2527
class OpenSIPSMIException(Exception):
26-
pass
28+
""" Generic OpenSIPS MI Exception """
2729

2830
class OpenSIPSMI():
31+
""" OpenSIPS MI Implementation """
2932
def __init__(self, conn="fifo", **kwargs):
3033
if conn == "fifo":
3134
if "fifo_file" not in kwargs:
@@ -43,17 +46,19 @@ def __init__(self, conn="fifo", **kwargs):
4346
raise ValueError("Invalid connector type")
4447

4548
self.validated = None
46-
47-
def execute(self, cmd, params=[]):
49+
50+
def execute(self, cmd, params=None):
51+
""" Executes a command with the requested parameters """
4852
try:
49-
ret_val = self.conn.execute(cmd, params)
53+
ret_val = self.conn.execute(cmd, params if params else [])
5054
except JSONRPCError as e:
51-
raise OpenSIPSMIException("Error executing command: {}".format(e))
55+
raise OpenSIPSMIException(f"Error executing command: {e}") from e
5256
except JSONRPCException as e:
53-
raise OpenSIPSMIException("Error with connection: {}. Is OpenSIPS running?".format(e))
57+
raise OpenSIPSMIException(f"Error with connection: {e}. Is OpenSIPS running?") from e
5458
return ret_val
55-
59+
5660
def valid(self):
61+
""" Checks if the connector is valid """
5762
if self.validated is not None:
5863
return self.validated
5964
self.validated = self.conn.valid()

opensips/mi/datagram.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
##
1919

20+
""" MI Datagram implementation """
21+
22+
import socket
2023
from .connection import Connection
2124
from . import jsonrpc_helper
22-
import socket
2325

2426
class Datagram(Connection):
27+
28+
""" MI Datagram connection """
29+
2530
def __init__(self, **kwargs):
2631
if "datagram_ip" not in kwargs:
2732
raise ValueError("datagram_ip is required for Datagram connector")
28-
33+
2934
if "datagram_port" not in kwargs:
3035
raise ValueError("datagram_port is required for Datagram connector")
31-
36+
3237
self.ip = kwargs["datagram_ip"]
3338
self.port = int(kwargs["datagram_port"])
3439

@@ -45,6 +50,6 @@ def execute(self, method: str, params: dict):
4550
finally:
4651
udp_socket.close()
4752
return jsonrpc_helper.get_reply(reply)
48-
53+
4954
def valid(self):
5055
return (True, None)

0 commit comments

Comments
 (0)