[python] high level API patch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

as I used PJSUA Python bindings, I found it a bit C-like, not so much
"pythonic" :). It's probably a matter of perspective, but I took the time and
liberty and rewrote a bit of the pjsua.py library + added logging support from
Python's logging library. The code didn't change that much, although I broke
compatibility for the sake of readability and less coding for the developer.

Things I changed:
* removed most of the static class variables (put it into constructor =>
made them instance variable)

* simplifed Lib interface
 - merged __init__() and init()
 - used Lib._cb_* methods directly as callback therefore removing several
 module global functions (which were mere wrappers)
 - behaves almost like a singleton, still returns raises exception if
 __init__() is called twice, but no more global variables like _lib, _quit ...

* refactored _cvt_(from|to)_pjsua methods on several objects
 - now using Python inspect module, so no more typing all the transferred
 variables, although had to filter some different names or internal vars
 (__dict__, __class__, ...)

* minor tweaks
 - renamed id, type variables to id_ or _id, type_, on one occasion to
 type_name (big deal :P)
 - removed sometimes abundant '\'
 - all tabs removed, replaced with spaces, indentation 4; old code had some
 mixed use
 - removed trailing whitespace
 - consistent use of doc strings, always """
 - 'if var != "":' == 'if var:' => more readable
 - reformatting of long lines, just syntax sugar

* refactored UAConfig
 - accepts user_agent, max_calls directly in __init__()

* removed a bug in not much used method MediaConfig._cvt_from_pjsua() :)
 - lines 430-431 -> wrong order of arguments
 - modified AuthCred to take as first arguments username and password

* new (I hope cleaner) logging interface
 - removed _Trace and enable_trace
 - new logger objects for Python bindings and underlying PJSUA & PJSIP:
<code>
    logging.getLogger("PyPJSUA")
    logging.getLogger("PyPJSUA.C")
</code>
 - can be used by adding Handlers, setting level as usual in Python ([1])
 - LogConfig can map specific PJSIP log levels to Python levels (which are
 essentially just integers) and still has the ability to limit the underlying
 C layer logging events (level, console_level, msg_logging)

As attachment I added the modified file (pjsua.py), samples (derived from [2])
and a diff. I hope somebody likes my proposed modifications and the diff doesn't
just fade away (<=> time invested is not wasted).

I should note that
* the module is not fully PEP-8 compliant (needs more time and some critique)
* lacks the ability to control first 4 lines of log/debug output, one would
need to go through _pjsua.c -> pjsua_core.c -> <os|platform_specific>.c and
modify pj_init() and that would be a bold move :)
* should get rid of Lib._err_check() and raise exception directly, however no
idea how to do it elegantly (maybe some weird decorator?) ;)
* this "patch" should be considered a thank you to the developers who are
writing the library as a way to constructively help them...

Cheers,
Karol


[1] http://docs.python.org/library/logging.html#more-examples
[2] http://trac.pjsip.org/repos/wiki/Python_SIP_Tutorial

-------------- next part --------------
A non-text attachment was scrubbed...
Name: pypjsua_patch.tgz
Type: application/x-gtar
Size: 42991 bytes
Desc: not available
URL: <http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/attachments/20100713/12487d50/attachment-0001.tgz>


[Index of Archives]     [Asterisk Users]     [Asterisk App Development]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [Linux API]
  Powered by Linux