Skip to content

Commit

Permalink
comply scripts with pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Oct 11, 2024
1 parent 22416a2 commit 1b89c2a
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 97 deletions.
110 changes: 60 additions & 50 deletions opensips/event/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

import argparse
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
from opensips.event import OpenSIPSEvent, OpenSIPSEventException
""" OpenSIPS Event script """

import sys
import json
import time
import signal
import argparse
from opensips.mi import OpenSIPSMI
from opensips.event import OpenSIPSEvent, OpenSIPSEventException

parser = argparse.ArgumentParser()

Expand Down Expand Up @@ -78,51 +81,58 @@
help='OpenSIPS Event Expire Time',
default=None)

args = parser.parse_args()

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
elif args.type == 'datagram':
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
else:
print(f'Unknownt type: {args.type}')
sys.exit(1)

event = OpenSIPSEvent(mi, args.transport, ip=args.listen_ip, port=args.listen_port)

def event_handler(message):
def main():
""" Main function of the opensips-event script """

args = parser.parse_args()

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
elif args.type == 'datagram':
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
else:
print(f'Unknownt type: {args.type}')
sys.exit(1)

ev = OpenSIPSEvent(mi, args.transport, ip=args.listen_ip, port=args.listen_port)

def event_handler(message):
""" Event handler callback """
try:
message_json = json.loads(message.decode('utf-8'))
print(json.dumps(message_json, indent=4))
except json.JSONDecodeError as e:
print(f"Failed to decode JSON: {e}")

def timer(*_):
""" Timer to notify when the event expires """
ev.unsubscribe(args.event)
sys.exit(0) # successful

if args.expire:
signal.signal(signal.SIGALRM, timer)
signal.alarm(args.expire)

signal.signal(signal.SIGINT, timer)
signal.signal(signal.SIGTERM, timer)

try:
message_json = json.loads(message.decode('utf-8'))
print(json.dumps(message_json, indent=4))
except json.JSONDecodeError as e:
print(f"Failed to decode JSON: {e}")

def timer(*_):
""" Timer to notify when the event expires """
event.unsubscribe(args.event)
sys.exit(0) # successful

if args.expire:
signal.signal(signal.SIGALRM, timer)
signal.alarm(args.expire)

signal.signal(signal.SIGINT, timer)
signal.signal(signal.SIGTERM, timer)

try:
event.subscribe(args.event, event_handler, expire=args.expire)
except OpenSIPSEventException as e:
print(e)
sys.exit(1)

while True:
time.sleep(1)
ev.subscribe(args.event, event_handler, expire=args.expire)
except OpenSIPSEventException as e:
print(e)
sys.exit(1)

while True:
time.sleep(1)

if __name__ == "__main__":
main()
100 changes: 55 additions & 45 deletions opensips/mi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

""" Script to run OpenSIPS MI commands """

import sys
import json
import argparse
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
import json

parser = argparse.ArgumentParser()

communication = parser.add_argument_group('communication')

communication.add_argument('-t', '--type',
type=str,
default='fifo',
choices=['fifo', 'http', 'datagram'],
help='OpenSIPS MI Communication Type')
communication.add_argument('-i', '--ip',
type=str,
Expand Down Expand Up @@ -70,47 +75,52 @@
default=[],
help='cmd args')

args = parser.parse_args()
print(args)

if args.stats:
print('Using get_statistics! Be careful not to use command after -s/--stats.')
print(args.stats)
args.command = 'get_statistics'

if args.json:
print('Cannot use -s/--stats with -j/--json!')
exit(1)

args.parameters = {'statistics': args.stats}
else:
if args.json:
try:
args.parameters = json.loads(args.json)
print(args.parameters)
except json.JSONDecodeError as e:
print('Invalid JSON: ', e)
exit(1)

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
mi = OpenSIPSMI('fifo', **fifo_args)

if args.type == 'http':
mi = OpenSIPSMI('http', url='http://{}:{}/mi'.format(args.ip, args.port))

if args.type == 'datagram':
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)

try:
response = mi.execute(args.command, args.parameters)
print(json.dumps(response, indent=4))
except OpenSIPSMIException as e:
print('Error: ', e)
exit(1)
def main():
""" Main function of the opensips-mi script """
args = parser.parse_args()

if args.stats:
print('Using get_statistics! Be careful not to use command after -s/--stats.')
print(args.stats)
args.command = 'get_statistics'

if args.json:
print('Cannot use -s/--stats with -j/--json!')
sys.exit(1)

args.parameters = {'statistics': args.stats}
else:
if args.json:
try:
args.parameters = json.loads(args.json)
print(args.parameters)
except json.JSONDecodeError as e:
print('Invalid JSON: ', e)
sys.exit(1)

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
elif args.type == 'datagram':
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
else:
print(f'Unknownt type: {args.type}')
sys.exit(1)

try:
response = mi.execute(args.command, args.parameters)
print(json.dumps(response, indent=4))
except OpenSIPSMIException as e:
print('Error: ', e)
sys.exit(1)

if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
],
entry_points = {
'console_scripts': [
'openisps-mi = opensips.mi',
'openisps-event = opensips.event',
'opensips-mi = opensips.mi.__main__:main',
'opensips-event = opensips.event.__main__:main',
],
},
python_requires=">=3.6"
Expand Down

0 comments on commit 1b89c2a

Please sign in to comment.