summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2005-04-07 11:44:59 +0000
committerMichael Vogt <michael.vogt@ubuntu.com>2005-04-07 11:44:59 +0000
commit7e76103c2b13d27f4a9987b1f9fca50504177268 (patch)
tree5690b514e2c52155a231bae2e578b40a14f5470b /python
parenta567392950a174ccb85c7f6c5c4ad8de7c37773d (diff)
downloadpython-apt-7e76103c2b13d27f4a9987b1f9fca50504177268.tar.gz
* code cleanups, simple MediaChanged implemented
Diffstat (limited to 'python')
-rw-r--r--python/progress.cc118
-rw-r--r--python/progress.h33
2 files changed, 79 insertions, 72 deletions
diff --git a/python/progress.cc b/python/progress.cc
index c6f56ed1..673d7c0d 100644
--- a/python/progress.cc
+++ b/python/progress.cc
@@ -9,97 +9,110 @@
#include <iostream>
#include "progress.h"
-
-// OpProgress interface
-// FIXME: add "string Op, string SubOp" as attribute to the callbackInst
-void PyOpProgress::Update()
+// generic
+bool PyCallbackObj::RunSimpleCallback(const char* method_name,
+ PyObject *arglist)
{
if(callbackInst == 0)
- return;
-
- // Build up the argument list...
- PyObject *arglist = Py_BuildValue("(f)", Percent);
-
- // ...for calling the Python compare function.
- PyObject *method = PyObject_GetAttrString(callbackInst, "Update");
+ return false;
+
+ PyObject *method = PyObject_GetAttrString(callbackInst,(char*) method_name);
if(method == NULL) {
// FIXME: make this silent
- Py_DECREF(arglist);
- return;
+ std::cerr << "Can't find '" << method_name << "' method" << std::endl;
+ Py_XDECREF(arglist);
+ return false;
}
- PyObject *result = PyEval_CallObject(method,arglist);
-
+ PyObject *result = PyEval_CallObject(method, arglist);
+ Py_XDECREF(arglist);
+
+ if(result == NULL) {
+ // exception happend
+ std::cerr << "Error in function " << method_name << std::endl;
+ return NULL;
+ }
+
Py_XDECREF(result);
Py_XDECREF(method);
- Py_DECREF(arglist);
-
- return;
-};
+ return true;
+}
-void PyOpProgress::Done()
+
+// OpProgress interface
+// FIXME: add "string Op, string SubOp" as attribute to the callbackInst
+void PyOpProgress::Update()
{
- if(callbackInst == 0)
- return;
-
// Build up the argument list...
- PyObject *arglist = Py_BuildValue("()", NULL);
-
- // ...for calling the Python compare function.
- PyObject *method = PyObject_GetAttrString(callbackInst, "Done");
- if(method == NULL) {
- Py_DECREF(arglist);
- return;
- }
- PyObject *result = PyEval_CallObject(method,arglist);
-
- Py_XDECREF(result);
- Py_XDECREF(method);
- Py_DECREF(arglist);
+ PyObject *arglist = Py_BuildValue("(f)", Percent);
+ RunSimpleCallback("Update", arglist);
+};
+
+void PyOpProgress::Done()
+{
+ RunSimpleCallback("Done");
}
// fetcher interface
-// PyFetchProgress::
+
+enum {
+ DLDone, DLQueued, DLFailed, DLHit
+};
+
// apt interface
bool PyFetchProgress::MediaChange(string Media, string Drive)
{
- std::cout << "MediaChange" << std::endl;
+ //std::cout << "MediaChange" << std::endl;
+ PyObject *arglist = Py_BuildValue("(ss)", Media.c_str(), Drive.c_str());
+ RunSimpleCallback("MediaChange", arglist);
+
+ // FIXME: need to return depending on the python result
+ return true;
+}
+
+void PyFetchProgress::UpdateStatus(pkgAcquire::ItemDesc &Itm, int status)
+{
+ //std::cout << "UpdateStatus: " << Itm.URI << " " << status << std::endl;
+ PyObject *arglist = Py_BuildValue("(sssi)", Itm.URI.c_str(), Itm.Description.c_str(), Itm.ShortDesc.c_str(), status);
+ RunSimpleCallback("UpdateStatus", arglist);
}
void PyFetchProgress::IMSHit(pkgAcquire::ItemDesc &Itm)
{
- std::cout << "IMSHit" << std::endl;
+ UpdateStatus(Itm, DLHit);
}
void PyFetchProgress::Fetch(pkgAcquire::ItemDesc &Itm)
{
- std::cout << "Fetch" << std::endl;
+ UpdateStatus(Itm, DLQueued);
}
void PyFetchProgress::Done(pkgAcquire::ItemDesc &Itm)
{
- std::cout << "Done" << std::endl;
+ UpdateStatus(Itm, DLDone);
}
void PyFetchProgress::Fail(pkgAcquire::ItemDesc &Itm)
{
- std::cout << "Fail" << std::endl;
+ UpdateStatus(Itm, DLFailed);
}
void PyFetchProgress::Start()
{
- std::cout << "Start" << std::endl;
+ //std::cout << "Start" << std::endl;
pkgAcquireStatus::Start();
-
+ RunSimpleCallback("Start");
}
+
void PyFetchProgress::Stop()
{
- std::cout << "Stop" << std::endl;
+ //std::cout << "Stop" << std::endl;
pkgAcquireStatus::Stop();
+ RunSimpleCallback("Stop");
}
// FIXME: it should just set the attribute for
@@ -125,21 +138,8 @@ bool PyFetchProgress::Pulse(pkgAcquire * Owner)
o = Py_BuildValue("f", TotalBytes);
PyObject_SetAttrString(callbackInst, "TotalBytes", o);
- // Call the pulse method
- PyObject *arglist = Py_BuildValue("()");
- PyObject *method = PyObject_GetAttrString(callbackInst, "Pulse");
- if(method == NULL) {
- // FIXME: make this silent
- std::cerr << "Can't find 'Pulse' method" << std::endl;
- Py_DECREF(arglist);
- return false;
- }
- PyObject *result = PyEval_CallObject(method,arglist);
- // FIXME: throw some exception here if the method was unsuccessfull
+ RunSimpleCallback("Pulse");
- Py_XDECREF(result);
- Py_XDECREF(method);
- Py_DECREF(arglist);
// this can be canceld by returning false
return true;
diff --git a/python/progress.h b/python/progress.h
index 6ea0b16b..a39ae41a 100644
--- a/python/progress.h
+++ b/python/progress.h
@@ -13,34 +13,39 @@
#include <apt-pkg/acquire.h>
#include <Python.h>
-struct PyOpProgress : public OpProgress
-{
+class PyCallbackObj {
+ protected:
PyObject *callbackInst;
+ public:
void setCallbackInst(PyObject *o) {
+ Py_INCREF(o);
callbackInst = o;
}
- virtual void Update();
- virtual void Done();
+ bool RunSimpleCallback(const char *method, PyObject *arglist=NULL);
- PyOpProgress() : OpProgress(), callbackInst(0) {};
+ PyCallbackObj() : callbackInst(0) {};
+ ~PyCallbackObj() {Py_DECREF(callbackInst); };
};
-
-struct PyFetchProgress : public pkgAcquireStatus
+struct PyOpProgress : public OpProgress, public PyCallbackObj
{
- PyObject *callbackInst;
- void setCallbackInst(PyObject *o) {
- callbackInst = o;
- }
+ virtual void Update();
+ virtual void Done();
+ PyOpProgress() : OpProgress(), PyCallbackObj() {};
+};
- void updateStatus(pkgAcquire::ItemDesc & Itm, int status);
- // apt interface
+struct PyFetchProgress : public pkgAcquireStatus, public PyCallbackObj
+{
+ void UpdateStatus(pkgAcquire::ItemDesc & Itm, int status);
+
virtual bool MediaChange(string Media, string Drive);
+
+ /* apt stuff */
virtual void IMSHit(pkgAcquire::ItemDesc &Itm);
virtual void Fetch(pkgAcquire::ItemDesc &Itm);
virtual void Done(pkgAcquire::ItemDesc &Itm);
@@ -49,6 +54,8 @@ struct PyFetchProgress : public pkgAcquireStatus
virtual void Stop();
bool Pulse(pkgAcquire * Owner);
+ PyFetchProgress() : PyCallbackObj() {};
+
};