+ All Categories
Home > Technology > OpenSAF Symposium_Python Bindings_9.21.11

OpenSAF Symposium_Python Bindings_9.21.11

Date post: 19-May-2015
Category:
Upload: opensaf-foundation
View: 2,389 times
Download: 1 times
Share this document with a friend
Description:
Python bindings for SAF-AIS APIs offer many advantages to middleware developers, application developers, tool developers and testers. The bindings help to speed up the software development lifecycle and enable rapid deployment of architecture-independent components and services. This session will describe main principles guiding Python bindings implementation, and will have extensive in-depth application Python code examples using SAF-AIS services.
Popular Tags:
24
Python Bindings for SA Forum AIS APIs Hans Feldt Ericsson Currie Reid Linux Product Division Wind River
Transcript
Page 1: OpenSAF Symposium_Python Bindings_9.21.11

Python Bindings for SA Forum AIS APIs

Hans Feldt

Ericsson

Currie Reid

Linux Product Division

Wind River

Page 2: OpenSAF Symposium_Python Bindings_9.21.11

Outline of Presentation

• Objectives

• What is Python?

• Mapping Python to Objectives

• How Python bindings are implemented

• (Demonstration)

• Future Works

Page 3: OpenSAF Symposium_Python Bindings_9.21.11

Objectives

• Enable testing in all phases of the software

lifecycle.

• Simplify development across different

architectures.

• Simplify presentation to developers and

engineers.

• Quickly create tools that make it easy to

investigate and monitor the system.

• Make it easy to create and modify prototypes.

Page 4: OpenSAF Symposium_Python Bindings_9.21.11

What is Python?

• General purpose, high-level, interpreted language.

• Design philosophy emphasizes code readability.

• Designed to be extensible

• Large, comprehensive standard library.

• Supports object-oriented, imperative, and (lesser degree) functional programming paradigms.

• Dynamic type system, automatic memory management.

• Free, open-source, many 3rd party packages.

Page 5: OpenSAF Symposium_Python Bindings_9.21.11

Popularity and Usage

• Twice awarded as “Programming language of the

Year” by TIOBE (2007 & 2010)

• Ranked number 8 in the TIOBE popularity index

• Used as a scripting language for web applications

(Django, Pylons, …)

• Used as an embedded scripting language in other

programs (GIMP, GDB, ..)

• OpenSAF related: Mercurial, Buildbot, Trac, Yum

• Standard component in Linux distributions

Page 6: OpenSAF Symposium_Python Bindings_9.21.11

Enable testing in all phases of software

lifecycle

• Test coverage more complete if easy to write

tests; test-driven development encouraged!

• Tests are portable across architectures.

• Built-in modules for testing include unittest and

doctest.

• Modules available for unit testing, mock testing,

fuzz testing, web testing, gui testing, etc.

• i.e.: nose, py.test, zope.testing.

Page 7: OpenSAF Symposium_Python Bindings_9.21.11

Simplify development across different

architectures

• Python scripts are portable across different

architectures.

• Edit-compile-run cycle is greatly reduced.

• Python code can be edited/patched on the target

system if required.

• Huge collection of native and 3rd-party bindings

available to aid/speed development.

Page 8: OpenSAF Symposium_Python Bindings_9.21.11

Simplify presentation to developers and

engineers

• Python syntax reads like pseudo-code.

• Most Python programs much smaller than lower-

level implementations.

• Easier to examine the problem domain when

you abstract-away programming details and

challenges associated with the machine.

• Do more with less

Page 9: OpenSAF Symposium_Python Bindings_9.21.11

Make it easy to create and modify

prototypes

• Components and applications can be rapidly

prototyped in Python.

• Designs can then be hardened in another

implementation language.

• Ideal glue language.

• Working code can be developed much faster

than lower-level languages.

• Identify “hot spots” in application and possibly

optimize by extending with C/C++

Page 10: OpenSAF Symposium_Python Bindings_9.21.11

Ctypes module: Wrap Libraries in Python

• Advanced FFI (Foreign Function Interface) for

Python 2.3 and higher.

• Included in Python 2.5

• Provides C compatible data types

• Call functions in shared libraries (DLLs).

• Create, access and manipulate simple and

complicated C data types in Python.

• Enables C callbacks to be implemented in

Python.

Page 11: OpenSAF Symposium_Python Bindings_9.21.11

Python Bindings Implementation

• Conversion of SAF types to ctypes

• Definition of Const

• Definition of Enumeration

• Definition of Struct

• Definition of Union

• Dynamic Loading of Libraries and CDLL

• Definition of Functions

• Definition of callbacks and CFUNCTYPE

Page 12: OpenSAF Symposium_Python Bindings_9.21.11

Conversion of SAF types to ctypes

SaInt8T = c_char SaInt16T = c_short SaInt32T = c_int SaInt64T = c_longlong SaUint8T = c_ubyte SaUint16T = c_ushort SaUint32T = c_uint SaUint64T = c_ulonglong ...

myuint = SaUint64T(12)

Page 13: OpenSAF Symposium_Python Bindings_9.21.11

Definition of Const

saAis = Const()

saAis.SA_TIME_END = 0x7FFFFFFFFFFFFFFF

saAis.SA_TIME_BEGIN = 0x0

saAis.SA_TIME_UNKNOWN = 0x8000000000000000

time_start = saAis.SA_TIME_BEGIN

saAis.SA_TIME_BEGIN = 5 #ERROR: EXCEPTION

Page 14: OpenSAF Symposium_Python Bindings_9.21.11

Definition of Enumeration

SaDispatchFlagsT = SaEnumT eSaDispatchFlagsT = Enumeration((

('SA_DISPATCH_ONE', 1), ('SA_DISPATCH_ALL', 2),

('SA_DISPATCH_BLOCKING', 3),

))

flag = eSaDispatchFlagsT.SA_DISPATCH_ALL

flag_str = eSaDispatchFlagsT.whatis(flag)

Page 15: OpenSAF Symposium_Python Bindings_9.21.11

Definition of Structs

class SaNameT(Structure):

_fields_ = [('length', SaUint16T),

('value',(SaInt8T*saAis.SA_MAX_NAME_LENGTH))]

def __init__(self, name=''):

super(SaNameT, self).__init__(len(name), name)

dn = SaNameT(‘safApp=OpenSAF’)

Page 16: OpenSAF Symposium_Python Bindings_9.21.11

Definition of Unions

class SaLimitValueT(Union):

_fields_ = [('int64Value', SaInt64T),

('uint64Value', SaUint64T),

('timeValue', SaTimeT),

('floatValue', SaFloatT),

('doubleValue', SaDoubleT)]

un = SaLimitValueT()

un.timeValue = 5000000000

Page 17: OpenSAF Symposium_Python Bindings_9.21.11

Dynamic Loading of Libraries using CDLL

amfdll = CDLL('libSaAmf.so.0')

Functions in the library accessible as

attributes of the amfdll object.

Page 18: OpenSAF Symposium_Python Bindings_9.21.11

Definition of Functions

def saAmfInitialize(amfHandle,

amfCallbacks, version):

return amfdll.saAmfInitialize(

BYREF(amfHandle),

BYREF(amfCallbacks),

BYREF(version))

Page 19: OpenSAF Symposium_Python Bindings_9.21.11

Example usage

amfHandle = SaAmfHandleT()

amfCallbacks = None

version = SaVersionT(‘B’, 1, 1)

rc = saAmfInitialize(amfHandle,

amfCallbacks, version)

Page 20: OpenSAF Symposium_Python Bindings_9.21.11

Definition of callbacks and CFUNCTYPE

SaAmfCSISetCallbackT = CFUNCTYPE(None,

SaInvocationT, POINTER(SaNameT),

SaAmfHAStateT, SaAmfCSIDescriptorT)

def myCSISetCallback(invoc,

comp, hastate, descr):

callbacks = SaAmfCallbacksT()

callbacks.saAmfCSISetCallbackT(

SaAmfCSISetCallbackT(myCSISetCallback))

Page 21: OpenSAF Symposium_Python Bindings_9.21.11

Bindings in 4.2

• A 1:1 mapping primarily aimed for OpenSAF

services testing

• Samples using the bindings

Page 22: OpenSAF Symposium_Python Bindings_9.21.11

Future Work

• Pythonic interfaces (inspired by Java bindings?)

• OpenSAF test framework using Python bindings

• Simple GUI for management

• Bindings for IMM A.02.11 – OpenSAF

extensions

• Complete SMF bindings

Page 23: OpenSAF Symposium_Python Bindings_9.21.11

Example of Pythonic Interface

def foo(parent, values, mods, dn1, dn2):

owner = SaImmOmOwner(‘Hans’)

owner.set(parent)

ccb = SaImmOmCcb(owner)

ccb.objectCreate(‘TestClass’, parent, values)

ccb.objectModify(dn1, mods)

ccb.objectDelete(dn2)

ccb.apply()

Page 24: OpenSAF Symposium_Python Bindings_9.21.11

Questions?


Recommended