Skip to content

Commit 1b89c2a

Browse files
committed
comply scripts with pylint
1 parent 22416a2 commit 1b89c2a

3 files changed

Lines changed: 117 additions & 97 deletions

File tree

opensips/event/__main__.py

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

20-
import argparse
21-
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
22-
from opensips.event import OpenSIPSEvent, OpenSIPSEventException
20+
""" OpenSIPS Event script """
21+
22+
import sys
2323
import json
2424
import time
2525
import signal
26+
import argparse
27+
from opensips.mi import OpenSIPSMI
28+
from opensips.event import OpenSIPSEvent, OpenSIPSEventException
2629

2730
parser = argparse.ArgumentParser()
2831

@@ -78,51 +81,58 @@
7881
help='OpenSIPS Event Expire Time',
7982
default=None)
8083

81-
args = parser.parse_args()
82-
83-
if args.type == 'fifo':
84-
fifo_args = {}
85-
if args.fifo_file:
86-
fifo_args['fifo_file'] = args.fifo_file
87-
if args.fifo_fallback:
88-
fifo_args['fifo_file_fallback'] = args.fifo_fallback
89-
if args.fifo_reply_dir:
90-
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
91-
mi = OpenSIPSMI('fifo', **fifo_args)
92-
elif args.type == 'http':
93-
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
94-
elif args.type == 'datagram':
95-
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
96-
else:
97-
print(f'Unknownt type: {args.type}')
98-
sys.exit(1)
99-
100-
event = OpenSIPSEvent(mi, args.transport, ip=args.listen_ip, port=args.listen_port)
101-
102-
def event_handler(message):
84+
def main():
85+
""" Main function of the opensips-event script """
86+
87+
args = parser.parse_args()
88+
89+
if args.type == 'fifo':
90+
fifo_args = {}
91+
if args.fifo_file:
92+
fifo_args['fifo_file'] = args.fifo_file
93+
if args.fifo_fallback:
94+
fifo_args['fifo_file_fallback'] = args.fifo_fallback
95+
if args.fifo_reply_dir:
96+
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
97+
mi = OpenSIPSMI('fifo', **fifo_args)
98+
elif args.type == 'http':
99+
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
100+
elif args.type == 'datagram':
101+
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
102+
else:
103+
print(f'Unknownt type: {args.type}')
104+
sys.exit(1)
105+
106+
ev = OpenSIPSEvent(mi, args.transport, ip=args.listen_ip, port=args.listen_port)
107+
108+
def event_handler(message):
109+
""" Event handler callback """
110+
try:
111+
message_json = json.loads(message.decode('utf-8'))
112+
print(json.dumps(message_json, indent=4))
113+
except json.JSONDecodeError as e:
114+
print(f"Failed to decode JSON: {e}")
115+
116+
def timer(*_):
117+
""" Timer to notify when the event expires """
118+
ev.unsubscribe(args.event)
119+
sys.exit(0) # successful
120+
121+
if args.expire:
122+
signal.signal(signal.SIGALRM, timer)
123+
signal.alarm(args.expire)
124+
125+
signal.signal(signal.SIGINT, timer)
126+
signal.signal(signal.SIGTERM, timer)
127+
103128
try:
104-
message_json = json.loads(message.decode('utf-8'))
105-
print(json.dumps(message_json, indent=4))
106-
except json.JSONDecodeError as e:
107-
print(f"Failed to decode JSON: {e}")
108-
109-
def timer(*_):
110-
""" Timer to notify when the event expires """
111-
event.unsubscribe(args.event)
112-
sys.exit(0) # successful
113-
114-
if args.expire:
115-
signal.signal(signal.SIGALRM, timer)
116-
signal.alarm(args.expire)
117-
118-
signal.signal(signal.SIGINT, timer)
119-
signal.signal(signal.SIGTERM, timer)
120-
121-
try:
122-
event.subscribe(args.event, event_handler, expire=args.expire)
123-
except OpenSIPSEventException as e:
124-
print(e)
125-
sys.exit(1)
126-
127-
while True:
128-
time.sleep(1)
129+
ev.subscribe(args.event, event_handler, expire=args.expire)
130+
except OpenSIPSEventException as e:
131+
print(e)
132+
sys.exit(1)
133+
134+
while True:
135+
time.sleep(1)
136+
137+
if __name__ == "__main__":
138+
main()

opensips/mi/__main__.py

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

20+
""" Script to run OpenSIPS MI commands """
21+
22+
import sys
23+
import json
2024
import argparse
2125
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
22-
import json
2326

2427
parser = argparse.ArgumentParser()
2528

2629
communication = parser.add_argument_group('communication')
2730

2831
communication.add_argument('-t', '--type',
2932
type=str,
33+
default='fifo',
34+
choices=['fifo', 'http', 'datagram'],
3035
help='OpenSIPS MI Communication Type')
3136
communication.add_argument('-i', '--ip',
3237
type=str,
@@ -70,47 +75,52 @@
7075
default=[],
7176
help='cmd args')
7277

73-
args = parser.parse_args()
74-
print(args)
75-
76-
if args.stats:
77-
print('Using get_statistics! Be careful not to use command after -s/--stats.')
78-
print(args.stats)
79-
args.command = 'get_statistics'
80-
81-
if args.json:
82-
print('Cannot use -s/--stats with -j/--json!')
83-
exit(1)
84-
85-
args.parameters = {'statistics': args.stats}
86-
else:
87-
if args.json:
88-
try:
89-
args.parameters = json.loads(args.json)
90-
print(args.parameters)
91-
except json.JSONDecodeError as e:
92-
print('Invalid JSON: ', e)
93-
exit(1)
94-
95-
if args.type == 'fifo':
96-
fifo_args = {}
97-
if args.fifo_file:
98-
fifo_args['fifo_file'] = args.fifo_file
99-
if args.fifo_fallback:
100-
fifo_args['fifo_file_fallback'] = args.fifo_fallback
101-
if args.fifo_reply_dir:
102-
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
103-
mi = OpenSIPSMI('fifo', **fifo_args)
104-
105-
if args.type == 'http':
106-
mi = OpenSIPSMI('http', url='http://{}:{}/mi'.format(args.ip, args.port))
107-
108-
if args.type == 'datagram':
109-
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
110-
111-
try:
112-
response = mi.execute(args.command, args.parameters)
113-
print(json.dumps(response, indent=4))
114-
except OpenSIPSMIException as e:
115-
print('Error: ', e)
116-
exit(1)
78+
def main():
79+
""" Main function of the opensips-mi script """
80+
args = parser.parse_args()
81+
82+
if args.stats:
83+
print('Using get_statistics! Be careful not to use command after -s/--stats.')
84+
print(args.stats)
85+
args.command = 'get_statistics'
86+
87+
if args.json:
88+
print('Cannot use -s/--stats with -j/--json!')
89+
sys.exit(1)
90+
91+
args.parameters = {'statistics': args.stats}
92+
else:
93+
if args.json:
94+
try:
95+
args.parameters = json.loads(args.json)
96+
print(args.parameters)
97+
except json.JSONDecodeError as e:
98+
print('Invalid JSON: ', e)
99+
sys.exit(1)
100+
101+
if args.type == 'fifo':
102+
fifo_args = {}
103+
if args.fifo_file:
104+
fifo_args['fifo_file'] = args.fifo_file
105+
if args.fifo_fallback:
106+
fifo_args['fifo_file_fallback'] = args.fifo_fallback
107+
if args.fifo_reply_dir:
108+
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
109+
mi = OpenSIPSMI('fifo', **fifo_args)
110+
elif args.type == 'http':
111+
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
112+
elif args.type == 'datagram':
113+
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
114+
else:
115+
print(f'Unknownt type: {args.type}')
116+
sys.exit(1)
117+
118+
try:
119+
response = mi.execute(args.command, args.parameters)
120+
print(json.dumps(response, indent=4))
121+
except OpenSIPSMIException as e:
122+
print('Error: ', e)
123+
sys.exit(1)
124+
125+
if __name__ == "__main__":
126+
main()

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
],
4545
entry_points = {
4646
'console_scripts': [
47-
'openisps-mi = opensips.mi',
48-
'openisps-event = opensips.event',
47+
'opensips-mi = opensips.mi.__main__:main',
48+
'opensips-event = opensips.event.__main__:main',
4949
],
5050
},
5151
python_requires=">=3.6"

0 commit comments

Comments
 (0)