Commit 871154d1 authored by okhin's avatar okhin
Browse files

Adding a clean arg parser and usage message

parent 9a648dd3
Pipeline #25 skipped
...@@ -10,6 +10,8 @@ import asyncio ...@@ -10,6 +10,8 @@ import asyncio
import re import re
import configparser import configparser
import argparse import argparse
import os.path
import sys
from operator import itemgetter from operator import itemgetter
import jwt import jwt
...@@ -17,8 +19,9 @@ import websockets ...@@ -17,8 +19,9 @@ import websockets
import requests import requests
from bottle import request, abort, Bottle, JSONPlugin from bottle import request, abort, Bottle, JSONPlugin
from bottle_sqlite import SQLitePlugin from bottle_sqlite import SQLitePlugin
from daemonize import Daemonize
config = ConfigParser() config = configparser.ConfigParser()
application = app = Bottle(autojson=False) application = app = Bottle(autojson=False)
app.install(SQLitePlugin(dbfile='call.db')) app.install(SQLitePlugin(dbfile='call.db'))
app.install(JSONPlugin(json_dumps=lambda s: json.dumps(s, cls=PiphoneJSONEncoder))) app.install(JSONPlugin(json_dumps=lambda s: json.dumps(s, cls=PiphoneJSONEncoder)))
...@@ -361,12 +364,19 @@ class Call(object): ...@@ -361,12 +364,19 @@ class Call(object):
phone_logger.exception(e) phone_logger.exception(e)
raise e raise e
def start():
global running
running = True
threads.submit(app.run)
loop.run_until_complete(listen())
def stop(): def stop():
global running global running
running = False running = False
ws.close() ws.close()
loop.close() loop.close()
threads.shutdown(wait=False) threads.shutdown(wait=False)
sys.exit(0)
@app.get('/calls/<callid>') @app.get('/calls/<callid>')
@app.get('/calls/') @app.get('/calls/')
...@@ -432,14 +442,27 @@ def originate(db, callid=None): ...@@ -432,14 +442,27 @@ def originate(db, callid=None):
abort(400, "Missing or incorrect fields, the call cannot be processed") abort(400, "Missing or incorrect fields, the call cannot be processed")
if __name__ == '__main__': if __name__ == '__main__':
global config
arg_parser = argparse.ArgumentParser(description='Manage the SIP Backend for the piphone') arg_parser = argparse.ArgumentParser(description='Manage the SIP Backend for the piphone')
arg_parser.add_argument('-c', '--config', help="Config file") arg_parser.add_argument('-c', '--config', help="Config file")
arg_parser.parse_args() arg_parser.parse_args()
config = ConfigParser(arg_parser.config)
try: try:
running = True logging.debug("Let's use {} as a config file".config(arg_parser.config,))
threads.submit(app.run) config.read(arg_parser.config)
loop.run_until_complete(listen()) except AttributeError:
try:
if os.path.isfile('config.ini'):
logging.debug("Let's use config.ini as a config file")
config.read('config.ini')
elif os.path.isfile('/etc/piphone/config.ini'):
logging.debug("Let's use /etc/iphone/config.ini as a config file")
config.read('/etc/piphone/config.ini')
else:
raise Exception("No configuration file found (tried ./config.ini and /etc/piphone/config.ini")
except Exception as e:
phone_logger.error("Config file cannot be read")
arg_parser.print_help()
sys.exit(1)
try:
start()
except: except:
stop() stop()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment