Commit afc5535f authored by okhin's avatar okhin
Browse files

Doing a lot of preconfiguration at module loading

parent 871154d1
Pipeline #26 skipped
...@@ -21,30 +21,50 @@ from bottle import request, abort, Bottle, JSONPlugin ...@@ -21,30 +21,50 @@ from bottle import request, abort, Bottle, JSONPlugin
from bottle_sqlite import SQLitePlugin from bottle_sqlite import SQLitePlugin
from daemonize import Daemonize from daemonize import Daemonize
arg_parser = argparse.ArgumentParser(description='Manage the SIP Backend for the piphone')
config = configparser.ConfigParser() config = configparser.ConfigParser()
arg_parser.add_argument('-c', '--config', help="Config file")
arg_parser.parse_args()
try:
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:
arg_parser.print_help()
sys.exit(1)
application = app = Bottle(autojson=False) application = app = Bottle(autojson=False)
app.install(SQLitePlugin(dbfile='call.db')) app.install(SQLitePlugin(dbfile=config['piphone']['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)))
threads = concurrent.futures.ThreadPoolExecutor() threads = concurrent.futures.ThreadPoolExecutor()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
db = sqlite3.connect('call.db')
running = False running = False
ws = None ws = None
debug = True
# Loggers # Loggers
handler = logging.FileHandler('app.log') handler = logging.FileHandler(config['piphone']['log'])
verbosity = getattr(logging, config['piphone']['verbosity'].upper()) or logging.DEBUG
phone_logger = logging.getLogger('piphone') phone_logger = logging.getLogger('piphone')
phone_logger.addHandler(handler) phone_logger.addHandler(handler)
phone_logger.setLevel(logging.DEBUG) phone_logger.setLevel(verbosity)
ws_logger = logging.getLogger('asterisk') ws_logger = logging.getLogger('asterisk')
ws_logger.addHandler(handler) ws_logger.addHandler(handler)
ws_logger.setLevel(logging.DEBUG) ws_logger.setLevel(verbosity)
bottle_logger = logging.getLogger('bottle') bottle_logger = logging.getLogger('bottle')
bottle_logger.addHandler(handler) bottle_logger.addHandler(handler)
bottle_logger.setLevel(logging.DEBUG) bottle_logger.setLevel(verbosity)
class PiphoneJSONEncoder(json.JSONEncoder): class PiphoneJSONEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):
...@@ -442,26 +462,8 @@ def originate(db, callid=None): ...@@ -442,26 +462,8 @@ 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__':
arg_parser = argparse.ArgumentParser(description='Manage the SIP Backend for the piphone') db = sqlite3.connect(config['piphone']['db'])
arg_parser.add_argument('-c', '--config', help="Config file") phone_logger.info("Starting the piphone SIP backend")
arg_parser.parse_args()
try:
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: try:
start() start()
except: except:
......
[WebService]
base_url = ws://185.34.33.12:8088/ari/events
[asterisk]
server = 'http://182.34.33.12:8088/ari/'
app = piphone
key = piphone
password = passpiphone
[piphone]
db = call.db
log = call.log
verbosity = debug
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