From cdd3d4d0a911e484e35247892a827c17a48d328e Mon Sep 17 00:00:00 2001 From: Michael Andres Date: Wed, 5 Dec 2007 23:45:25 +0000 Subject: [PATCH] callback draft --- examples/python/callbacks.py | 44 +++++++++++++++++++++++++++ swig/RepoManager.i | 2 +- swig/python/callbacks.i | 71 ++++++++++++++++++++++++++++++++++++++++++++ swig/zypp.i | 3 ++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100755 examples/python/callbacks.py create mode 100644 swig/python/callbacks.i diff --git a/examples/python/callbacks.py b/examples/python/callbacks.py new file mode 100755 index 0000000..89149de --- /dev/null +++ b/examples/python/callbacks.py @@ -0,0 +1,44 @@ +#! /usr/bin/python +import zypp + +# --------------------------------------------------------- + +def myProgress( val ): + print "myProgress %d" % val + return True + +rec = zypp.ScanRpmDbReceive() +rec.set_pymethod( myProgress ) + +# --------------------------------------------------------- + +Z = zypp.ZYppFactory_instance().getZYpp() + +if True: + Z.initializeTarget( zypp.Pathname("/Local/ROOT") ) + Z.addResolvables( Z.target().resolvables(), True ); + +if False: + repoManager = zypp.RepoManager() + repos = repoManager.knownRepositories() + for repo in repos: + if not repo.enabled(): + continue + if not repoManager.isCached( repo ): + repoManager.buildCache( repo ) + Z.addResolvables( repoManager.createFromCache( repo ).resolvables() ) + +print "Items: %d" % ( Z.pool().size() ) + +if False: + for item in Z.pool(): + if item.status().isInstalled(): + t = "i" + else: + t = "*" + print "%s %s:%s-%s.%s\t(%s)" % ( t, + item.resolvable().kind(), + item.resolvable().name(), + item.resolvable().edition(), + item.resolvable().arch(), + item.resolvable().repository().info().alias() ) diff --git a/swig/RepoManager.i b/swig/RepoManager.i index ee8c7cd..23410e8 100644 --- a/swig/RepoManager.i +++ b/swig/RepoManager.i @@ -54,6 +54,6 @@ void removeRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() ); RepoInfo getRepositoryInfo( const std::string &alias, - const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() ); + const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() ); }; diff --git a/swig/python/callbacks.i b/swig/python/callbacks.i new file mode 100644 index 0000000..5367bfd --- /dev/null +++ b/swig/python/callbacks.i @@ -0,0 +1,71 @@ + +struct ScanRpmDbReceive +{ + void set_pymethod(PyObject *pyfunc); +}; + +%{ + namespace zypp + { + struct ScanRpmDbReceive : public callback::ReceiveReport + { + typedef target::rpm::ScanDBReport Report; + + ScanRpmDbReceive() + : _pyfunc( 0 ) + { connect(); } + + ~ScanRpmDbReceive() + { disconnect(); } + + virtual void start() + { + std::cerr << "ScanRpmDbReceive start" << std::endl; + _last = -1; + } + + virtual bool progress(int value) + { + if ( value == _last ) + return Report::progress( value ); + _last = value; + + if ( ! _pyfunc ) + return Report::progress( value ); + + bool ret = Report::progress( value ); + PyObject * arglist = Py_BuildValue( "(i)", value ); // Build argument list + PyObject * result = PyEval_CallObject( _pyfunc, arglist ); // Call Python + Py_DECREF( arglist ); // Trash arglist + if ( result ) { // If no errors, return bool + ret = PyLong_AsLong( result ); + } + Py_XDECREF( result ); + return ret; + } + + virtual Action problem( target::rpm::ScanDBReport::Error error, const std::string & description ) + { + //std::cerr << "ScanRpmDbReceive problem" << std::endl; + return Report::problem( error, description ); + } + + virtual void finish( Error error, const std::string & reason ) + { + //std::cerr << "ScanRpmDbReceive finish" << std::endl; + } + + void set_pymethod( PyObject * pyfunc_r ) + { + Py_INCREF( pyfunc_r ); + if ( _pyfunc ) + Py_DECREF( _pyfunc ); + _pyfunc = pyfunc_r; + } + + private: + int _last; + PyObject * _pyfunc; + }; + } +%} diff --git a/swig/zypp.i b/swig/zypp.i index 0ee203b..a0bb995 100644 --- a/swig/zypp.i +++ b/swig/zypp.i @@ -141,6 +141,9 @@ class intrusive_ptr { %include "TmpPath.i" %include "ItemCapKind.i" %include "Resolver.i" +#ifdef SWIGPYTHON +%include "python/callbacks.i" +#endif class ZYpp -- 2.7.4