Skip to content

Commit 4dd09e8

Browse files
committed
adapt to pylint
1 parent 30c6b30 commit 4dd09e8

18 files changed

+554
-531
lines changed

opensips/event/__init__.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
#!/usr/bin/env python
2-
##
3-
## This file is part of the OpenSIPS Python Package
4-
## (see https://github.com/OpenSIPS/python-opensips).
5-
##
6-
## This program is free software: you can redistribute it and/or modify
7-
## it under the terms of the GNU General Public License as published by
8-
## the Free Software Foundation, either version 3 of the License, or
9-
## (at your option) any later version.
10-
##
11-
## This program is distributed in the hope that it will be useful,
12-
## but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
## GNU General Public License for more details.
15-
##
16-
## You should have received a copy of the GNU General Public License
17-
## along with this program. If not, see <http://www.gnu.org/licenses/>.
18-
##
2+
#
3+
# This file is part of the OpenSIPS Python Package
4+
# (see https://github.com/OpenSIPS/python-opensips).
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
1919

2020
""" Event package of OpenSIPS """
2121

2222
from .event import OpenSIPSEvent, OpenSIPSEventException
2323
from .handler import OpenSIPSEventHandler
2424
from .asyncevent import AsyncOpenSIPSEvent
25-
from .asynchandler import AsyncOpenSIPSEventHandler
25+
26+
__all__ = [
27+
'OpenSIPSEvent',
28+
'OpenSIPSEventException',
29+
'OpenSIPSEventHandler',
30+
'AsyncOpenSIPSEvent',
31+
]
32+
33+
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

opensips/event/__main__.py

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#!/usr/bin/env python
2-
##
3-
## This file is part of the OpenSIPS Python Package
4-
## (see https://github.com/OpenSIPS/python-opensips).
5-
##
6-
## This program is free software: you can redistribute it and/or modify
7-
## it under the terms of the GNU General Public License as published by
8-
## the Free Software Foundation, either version 3 of the License, or
9-
## (at your option) any later version.
10-
##
11-
## This program is distributed in the hope that it will be useful,
12-
## but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
## GNU General Public License for more details.
15-
##
16-
## You should have received a copy of the GNU General Public License
17-
## along with this program. If not, see <http://www.gnu.org/licenses/>.
18-
##
2+
#
3+
# This file is part of the OpenSIPS Python Package
4+
# (see https://github.com/OpenSIPS/python-opensips).
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
1919

2020
""" OpenSIPS Event script """
2121

@@ -32,54 +32,55 @@
3232
communication = parser.add_argument_group('communication')
3333

3434
communication.add_argument('-t', '--type',
35-
type=str,
36-
default='fifo',
37-
choices=['fifo', 'http', 'datagram'],
38-
help='OpenSIPS MI Communication Type')
35+
type=str,
36+
default='fifo',
37+
choices=['fifo', 'http', 'datagram'],
38+
help='OpenSIPS MI Communication Type')
3939
communication.add_argument('-i', '--ip',
40-
type=str,
41-
help='OpenSIPS MI IP Address',
42-
default='127.0.0.1')
40+
type=str,
41+
help='OpenSIPS MI IP Address',
42+
default='127.0.0.1')
4343
communication.add_argument('-p', '--port',
44-
type=int,
45-
help='OpenSIPS MI Port',
46-
default=8888)
44+
type=int,
45+
help='OpenSIPS MI Port',
46+
default=8888)
4747
communication.add_argument('-f', '--fifo-file',
48-
metavar='FIFO_FILE',
49-
type=str,
50-
help='OpenSIPS MI FIFO File')
48+
metavar='FIFO_FILE',
49+
type=str,
50+
help='OpenSIPS MI FIFO File')
5151
communication.add_argument('-fb', '--fifo-fallback',
52-
metavar='FIFO_FALLBACK_FILE',
53-
type=str,
54-
help='OpenSIPS MI Fallback FIFO File')
52+
metavar='FIFO_FALLBACK_FILE',
53+
type=str,
54+
help='OpenSIPS MI Fallback FIFO File')
5555
communication.add_argument('-fd', '--fifo-reply-dir',
56-
metavar='FIFO_DIR',
57-
type=str,
58-
help='OpenSIPS MI FIFO Reply Directory')
56+
metavar='FIFO_DIR',
57+
type=str,
58+
help='OpenSIPS MI FIFO Reply Directory')
5959

6060
event = parser.add_argument_group('event')
6161

6262
event.add_argument('event',
63-
type=str,
64-
help='OpenSIPS Event Name')
63+
type=str,
64+
help='OpenSIPS Event Name')
6565

6666
event.add_argument('-T', '--transport',
67-
type=str,
68-
choices=['datagram', 'stream'],
69-
help='OpenSIPS Event Transport',
70-
default='datagram')
67+
type=str,
68+
choices=['datagram', 'stream'],
69+
help='OpenSIPS Event Transport',
70+
default='datagram')
7171
event.add_argument('-li', '--listen-ip',
72-
type=str,
73-
help='OpenSIPS Event Listen IP Address',
74-
default='0.0.0.0')
72+
type=str,
73+
help='OpenSIPS Event Listen IP Address',
74+
default='0.0.0.0')
7575
event.add_argument('-lp', '--listen-port',
76-
type=int,
77-
help='OpenSIPS Event Listen Port',
78-
default=0)
76+
type=int,
77+
help='OpenSIPS Event Listen Port',
78+
default=0)
7979
event.add_argument('-e', '--expire',
80-
type=int,
81-
help='OpenSIPS Event Expire Time',
82-
default=None)
80+
type=int,
81+
help='OpenSIPS Event Expire Time',
82+
default=None)
83+
8384

8485
def main():
8586
""" Main function of the opensips-event script """
@@ -98,12 +99,16 @@ def main():
9899
elif args.type == 'http':
99100
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
100101
elif args.type == 'datagram':
101-
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
102+
mi = OpenSIPSMI('datagram',
103+
datagram_ip=args.ip,
104+
datagram_port=args.port)
102105
else:
103106
print(f'Unknown type: {args.type}')
104107
sys.exit(1)
105108

106-
hdl = OpenSIPSEventHandler(mi, args.transport, ip=args.listen_ip, port=args.listen_port)
109+
hdl = OpenSIPSEventHandler(mi, args.transport,
110+
ip=args.listen_ip,
111+
port=args.listen_port)
107112

108113
def event_handler(message):
109114
""" Event handler callback """
@@ -121,7 +126,7 @@ def event_handler(message):
121126
def timer(*_):
122127
""" Timer to notify when the event expires """
123128
ev.unsubscribe()
124-
sys.exit(0) # successful
129+
sys.exit(0) # successful
125130

126131
if args.expire:
127132
signal.signal(signal.SIGALRM, timer)
@@ -139,5 +144,6 @@ def timer(*_):
139144
while True:
140145
time.sleep(1)
141146

147+
142148
if __name__ == "__main__":
143149
main()

opensips/event/asyncevent.py

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
#!/usr/bin/env python
2-
##
3-
## This file is part of the OpenSIPS Python Package
4-
## (see https://github.com/OpenSIPS/python-opensips).
5-
##
6-
## This program is free software: you can redistribute it and/or modify
7-
## it under the terms of the GNU General Public License as published by
8-
## the Free Software Foundation, either version 3 of the License, or
9-
## (at your option) any later version.
10-
##
11-
## This program is distributed in the hope that it will be useful,
12-
## but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
## GNU General Public License for more details.
15-
##
16-
## You should have received a copy of the GNU General Public License
17-
## along with this program. If not, see <http://www.gnu.org/licenses/>.
18-
##
2+
#
3+
# This file is part of the OpenSIPS Python Package
4+
# (see https://github.com/OpenSIPS/python-opensips).
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
1919

2020

2121
""" Module that implements OpenSIPS Event behavior with asyncio """
2222

23+
import asyncio
2324
from ..mi import OpenSIPSMIException
24-
from .json_helper import extract_json
25+
from .json_helper import JsonBuffer, JsonBufferMaxAttempts
2526
from .event import OpenSIPSEventException
26-
import asyncio
2727

28-
class AsyncOpenSIPSEvent():
28+
29+
class AsyncOpenSIPSEvent(): # pylint: disable=too-many-instance-attributes
2930

3031
""" Asyncio implementation of the OpenSIPS Event """
3132

3233
def __init__(self, handler, name: str, callback, expire=None):
3334
self._handler = handler
3435
self.name = name
3536
self.callback = callback
36-
self.buf = b""
37-
self.json_queue = []
38-
self.retries = 0
37+
self.buf = JsonBuffer()
3938
if expire is not None:
4039
self.expire = expire
4140
self.reregister = False
@@ -49,41 +48,37 @@ def __init__(self, handler, name: str, callback, expire=None):
4948
self._handler.events[self.name] = self
5049
self.resubscribe_task = asyncio.create_task(self.resubscribe())
5150
loop = asyncio.get_running_loop()
52-
loop.add_reader(self.socket.sock.fileno(), self.handle, self.callback)
51+
loop.add_reader(self.socket.sock.fileno(),
52+
self.handle, self.callback)
5353

5454
except ValueError as e:
55-
raise OpenSIPSEventException("Invalid arguments for socket creation: {}".format(e))
55+
raise OpenSIPSEventException("Invalid arguments") from e
5656

5757
def handle(self, callback):
5858
""" Handles the event callbacks """
5959
data = self.socket.read()
6060
if not data:
6161
return
62-
63-
self.buf += data
64-
self.json_queue, self.buf = extract_json(self.json_queue, self.buf)
6562

66-
if not self.json_queue:
67-
self.retries += 1
68-
69-
if self.retries > 10:
63+
try:
64+
self.buf.push(data)
65+
while j := self.buf.pop():
66+
callback(j)
67+
except JsonBufferMaxAttempts:
7068
callback(None)
7169
return
7270

73-
while self.json_queue:
74-
self.retries = 0
75-
json_obj = self.json_queue.pop(0)
76-
callback(json_obj)
77-
7871
async def resubscribe(self):
7972
""" Resubscribes for the event """
8073
try:
8174
while True:
8275
try:
83-
self._handler.__mi_subscribe__(self.name, self.socket.sock_name, self.expire)
84-
except OpenSIPSEventException as e:
76+
self._handler.__mi_subscribe__(self.name,
77+
self.socket.sock_name,
78+
self.expire)
79+
except OpenSIPSEventException:
8580
return
86-
except OpenSIPSMIException as e:
81+
except OpenSIPSMIException:
8782
return
8883
await asyncio.sleep(self.expire - 60)
8984
if not self.reregister:

0 commit comments

Comments
 (0)