From ee446299b8e429243b605f4ec082a8b0bd5cf387 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Fri, 1 Aug 2014 16:29:07 +0200 Subject: [PATCH] Real Python program to test the Python wrapper. --- tools/python/test.py | 66 ++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/tools/python/test.py b/tools/python/test.py index 627f8cad9..53856e7d4 100644 --- a/tools/python/test.py +++ b/tools/python/test.py @@ -1,5 +1,7 @@ +import argparse import linphone import logging +import sys import threading import time @@ -29,12 +31,22 @@ class IntervalTimer(StoppableThread): time.sleep(self._interval) -# Configure logging module -logging.addLevelName(logging.DEBUG, "\033[1;37m%s\033[1;0m" % logging.getLevelName(logging.DEBUG)) -logging.addLevelName(logging.INFO, "\033[1;36m%s\033[1;0m" % logging.getLevelName(logging.INFO)) -logging.addLevelName(logging.WARNING, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.WARNING)) -logging.addLevelName(logging.ERROR, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.ERROR)) -logging.basicConfig(level=logging.INFO, format="%(asctime)s.%(msecs)03d %(levelname)s: %(message)s", datefmt="%H:%M:%S") +def setup_log_colors(): + logging.addLevelName(logging.DEBUG, "\033[1;37m%s\033[1;0m" % logging.getLevelName(logging.DEBUG)) + logging.addLevelName(logging.INFO, "\033[1;36m%s\033[1;0m" % logging.getLevelName(logging.INFO)) + logging.addLevelName(logging.WARNING, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.WARNING)) + logging.addLevelName(logging.ERROR, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.ERROR)) + +def setup_log(log, trace): + if log is None: + setup_log_colors() + format = "%(asctime)s.%(msecs)03d %(levelname)s: %(message)s" + datefmt = "%H:%M:%S" + if trace: + level = logging.DEBUG + else: + level = logging.INFO + logging.basicConfig(filename=log, level=level, format=format, datefmt=datefmt) # Define the linphone module log handler def log_handler(level, msg): @@ -59,14 +71,34 @@ def global_state_changed(core, state, message): if state == linphone.GlobalState.GlobalOn: logging.warning("[PYTHON] core version: " + str(core.version)) -# Create a linphone core and iterate every 20 ms -linphone.set_log_handler(log_handler) -callbacks = { - 'global_state_changed':global_state_changed -} -core = linphone.Core.new(callbacks, None, None) -interval = IntervalTimer(0.02, iterate, kwargs={'core':core}) -interval.start() -while interact(): - pass -interval.stop() +def registration_state_changed(core, proxy_cfg, state, message): + logging.warning("[PYTHON] registration_state_changed: " + str(state) + ", " + message) + +def run(args): + # Create a linphone core and iterate every 20 ms + setup_log(args.log, args.trace) + linphone.set_log_handler(log_handler) + callbacks = { + 'global_state_changed':global_state_changed, + 'registration_state_changed':registration_state_changed + } + core = linphone.Core.new(callbacks, args.config, args.factory_config) + interval = IntervalTimer(0.02, iterate, kwargs={'core':core}) + interval.start() + while interact(): + pass + interval.stop() + +def main(argv = None): + if argv is None: + argv = sys.argv + argparser = argparse.ArgumentParser(description="Linphone console interface in Python.") + argparser.add_argument('--config', default=None, help="Path to the linphonerc configuration file to use.") + argparser.add_argument('--factory_config', default=None, help="Path to the linphonerc factory configuration file to use.") + argparser.add_argument('--log', default=None, help="Path to the file used for logging (default is the standard output).") + argparser.add_argument('--trace', action='store_true', help="Output linphone Python module tracing logs (for debug purposes).") + args = argparser.parse_args() + run(args) + +if __name__ == "__main__": + sys.exit(main())