Commit 871154d1 authored by okhin's avatar okhin 🚴

Adding a clean arg parser and usage message

parent 9a648dd3
Pipeline #25 skipped
......@@ -10,6 +10,8 @@ import asyncio
import re
import configparser
import argparse
import os.path
import sys
from operator import itemgetter
import jwt
......@@ -17,8 +19,9 @@ import websockets
import requests
from bottle import request, abort, Bottle, JSONPlugin
from bottle_sqlite import SQLitePlugin
from daemonize import Daemonize
config = ConfigParser()
config = configparser.ConfigParser()
application = app = Bottle(autojson=False)
app.install(SQLitePlugin(dbfile='call.db'))
app.install(JSONPlugin(json_dumps=lambda s: json.dumps(s, cls=PiphoneJSONEncoder)))
......@@ -361,12 +364,19 @@ class Call(object):
phone_logger.exception(e)
raise e
def start():
global running
running = True
threads.submit(app.run)
loop.run_until_complete(listen())
def stop():
global running
running = False
ws.close()
loop.close()
threads.shutdown(wait=False)
sys.exit(0)
@app.get('/calls/<callid>')
@app.get('/calls/')
......@@ -432,14 +442,27 @@ def originate(db, callid=None):
abort(400, "Missing or incorrect fields, the call cannot be processed")
if __name__ == '__main__':
global config
arg_parser = argparse.ArgumentParser(description='Manage the SIP Backend for the piphone')
arg_parser.add_argument('-c', '--config', help="Config file")
arg_parser.parse_args()
config = ConfigParser(arg_parser.config)
try:
running = True
threads.submit(app.run)
loop.run_until_complete(listen())
logging.debug("Let's use {} as a config file".config(arg_parser.config,))
config.read(arg_parser.config)
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:
stop()
Markdown is supported
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