system-config-printer/pycups-add-getJobs-requested-attrs.patch
Tim Waugh e00aa2ec64 - Specify requested attributes in getJobs if possible (bug #584806).
- Added optional requested_attributes argument to Connection.getJobs (bug
    #584806).
2010-04-22 15:20:06 +00:00

199 lines
6.0 KiB
Diff

diff -U0 pycups-1.9.49/ChangeLog.add-getJobs-requested-attrs pycups-1.9.49/ChangeLog
diff -up pycups-1.9.49/cupsconnection.c.add-getJobs-requested-attrs pycups-1.9.49/cupsconnection.c
--- pycups-1.9.49/cupsconnection.c.add-getJobs-requested-attrs 2010-02-24 15:07:28.000000000 +0000
+++ pycups-1.9.49/cupsconnection.c 2010-04-22 16:02:33.425457367 +0100
@@ -1382,6 +1382,52 @@ Connection_getDevices (Connection *self,
return result;
}
+static int
+get_requested_attrs (PyObject *requested_attrs, size_t *n_attrs, char ***attrs)
+{
+ int i;
+ size_t n;
+ char **as;
+
+ if (!PyList_Check (requested_attrs)) {
+ PyErr_SetString (PyExc_TypeError, "List required");
+ return -1;
+ }
+
+ n = PyList_Size (requested_attrs);
+ as = malloc ((n + 1) * sizeof (char *));
+ for (i = 0; i < n; i++) {
+ PyObject *val = PyList_GetItem (requested_attrs, i); // borrowed ref
+ if (!PyString_Check (val)) {
+ PyErr_SetString (PyExc_TypeError, "String required");
+ while (--i >= 0)
+ free (as[i]);
+ free (as);
+ return -1;
+ }
+
+ as[i] = strdup (PyString_AsString (val));
+ }
+ as[n] = NULL;
+
+ debugprintf ("Requested attributes:\n");
+ for (i = 0; as[i] != NULL; i++)
+ debugprintf (" %s\n", as[i]);
+
+ *n_attrs = n;
+ *attrs = as;
+ return 0;
+}
+
+static void
+free_requested_attrs (size_t n_attrs, char **attrs)
+{
+ int i;
+ for (i = 0; i < n_attrs; i++)
+ free (attrs[i]);
+ free (attrs);
+}
+
static PyObject *
Connection_getJobs (Connection *self, PyObject *args, PyObject *kwds)
{
@@ -1392,10 +1438,14 @@ Connection_getJobs (Connection *self, Py
int my_jobs = 0;
int limit = -1;
int first_job_id = -1;
+ PyObject *requested_attrs = NULL;
+ char **attrs = NULL; /* initialised to calm compiler */
+ size_t n_attrs = 0; /* initialised to calm compiler */
static char *kwlist[] = { "which_jobs", "my_jobs", "limit", "first_job_id",
- NULL };
- if (!PyArg_ParseTupleAndKeywords (args, kwds, "|siii", kwlist,
- &which, &my_jobs, &limit, &first_job_id))
+ "requested_attributes", NULL };
+ if (!PyArg_ParseTupleAndKeywords (args, kwds, "|siiiO", kwlist,
+ &which, &my_jobs, &limit, &first_job_id,
+ &requested_attrs))
return NULL;
debugprintf ("-> Connection_getJobs(%s,%d)\n",
@@ -1419,6 +1469,18 @@ Connection_getJobs (Connection *self, Py
ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
"first-job-id", first_job_id);
+ if (requested_attrs) {
+ if (get_requested_attrs (requested_attrs, &n_attrs, &attrs) == -1) {
+ ippDelete (request);
+ return NULL;
+ }
+
+ ippAddStrings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+ "requested-attributes", n_attrs, NULL,
+ (const char **) attrs);
+ free_requested_attrs (n_attrs, attrs);
+ }
+
debugprintf ("cupsDoRequest(\"/\")\n");
Connection_begin_allow_threads (self);
answer = cupsDoRequest (self->http, request, "/");
@@ -1474,6 +1536,18 @@ Connection_getJobs (Connection *self, Py
else if (!strcmp (attr->name, "job-preserved") &&
attr->value_tag == IPP_TAG_BOOLEAN)
val = PyBool_FromLong (attr->values[0].integer);
+ else {
+ if (attr->num_values > 1) {
+ int i;
+ val = PyList_New (0);
+ for (i = 0; i < attr->num_values; i++) {
+ PyObject *item = PyObject_from_attr_value (attr, i);
+ if (item)
+ PyList_Append (val, item);
+ }
+ } else
+ val = PyObject_from_attr_value (attr, 0);
+ }
if (val) {
debugprintf ("Adding %s to job dict\n", attr->name);
@@ -1500,52 +1574,6 @@ Connection_getJobs (Connection *self, Py
return result;
}
-static int
-get_requested_attrs (PyObject *requested_attrs, size_t *n_attrs, char ***attrs)
-{
- int i;
- size_t n;
- char **as;
-
- if (!PyList_Check (requested_attrs)) {
- PyErr_SetString (PyExc_TypeError, "List required");
- return -1;
- }
-
- n = PyList_Size (requested_attrs);
- as = malloc ((n + 1) * sizeof (char *));
- for (i = 0; i < n; i++) {
- PyObject *val = PyList_GetItem (requested_attrs, i); // borrowed ref
- if (!PyString_Check (val)) {
- PyErr_SetString (PyExc_TypeError, "String required");
- while (--i >= 0)
- free (as[i]);
- free (as);
- return -1;
- }
-
- as[i] = strdup (PyString_AsString (val));
- }
- as[n] = NULL;
-
- debugprintf ("Requested attributes:\n");
- for (i = 0; as[i] != NULL; i++)
- debugprintf (" %s\n", as[i]);
-
- *n_attrs = n;
- *attrs = as;
- return 0;
-}
-
-static void
-free_requested_attrs (size_t n_attrs, char **attrs)
-{
- int i;
- for (i = 0; i < n_attrs; i++)
- free (attrs[i]);
- free (attrs);
-}
-
static PyObject *
Connection_getJobAttributes (Connection *self, PyObject *args, PyObject *kwds)
{
@@ -4439,7 +4467,7 @@ PyMethodDef Connection_methods[] =
{ "getJobs",
(PyCFunction) Connection_getJobs, METH_VARARGS | METH_KEYWORDS,
- "getJobs(which_jobs='not-completed', my_jobs=False, limit=-1, first_job_id=-1) -> dict\n"
+ "getJobs(which_jobs='not-completed', my_jobs=False, limit=-1, first_job_id=-1, requested_attributes=None) -> dict\n"
"Fetch a list of jobs.\n"
"@type which_jobs: string\n"
"@param which_jobs: which jobs to fetch; possible values: \n"
@@ -4453,6 +4481,8 @@ PyMethodDef Connection_methods[] =
"@param limit: maximum number of jobs to return\n"
"@type first_job_id: integer\n"
"@param first_job_id: lowest job ID to return\n"
+ "@type requested_attributes: string list\n"
+ "@param requested_attributes: list of requested attribute names\n"
"@raise IPPError: IPP problem" },
{ "getJobAttributes",
diff -up pycups-1.9.49/NEWS.add-getJobs-requested-attrs pycups-1.9.49/NEWS
--- pycups-1.9.49/NEWS.add-getJobs-requested-attrs 2010-01-19 12:33:26.000000000 +0000
+++ pycups-1.9.49/NEWS 2010-04-22 16:01:37.816457416 +0100
@@ -1,6 +1,10 @@
NEWS
----
+New in 1.9.50:
+
+* cups.getJobs() now takes optional requested_attributes argument.
+
New in 1.9.48:
* cups.cancelJob()'s optional second argument is now a keyword.