callback draft
authorMichael Andres <ma@suse.de>
Wed, 5 Dec 2007 23:45:25 +0000 (23:45 +0000)
committerMichael Andres <ma@suse.de>
Wed, 5 Dec 2007 23:45:25 +0000 (23:45 +0000)
examples/python/callbacks.py [new file with mode: 0755]
swig/RepoManager.i
swig/python/callbacks.i [new file with mode: 0644]
swig/zypp.i

diff --git a/examples/python/callbacks.py b/examples/python/callbacks.py
new file mode 100755 (executable)
index 0000000..89149de
--- /dev/null
@@ -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() )
index ee8c7cd..23410e8 100644 (file)
@@ -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 (file)
index 0000000..5367bfd
--- /dev/null
@@ -0,0 +1,71 @@
+
+struct ScanRpmDbReceive
+{
+  void set_pymethod(PyObject *pyfunc);
+};
+
+%{
+  namespace zypp
+  {
+    struct ScanRpmDbReceive : public callback::ReceiveReport<target::rpm::ScanDBReport>
+    {
+      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;
+    };
+  }
+%}
index 0ee203b..a0bb995 100644 (file)
@@ -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