2
* cachegroup.cc - Wrapper around pkgCache::GrpIterator
4
* Copyright 2011 Julian Andres Klode <jak@debian.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include "apt_pkgmodule.h"
24
#include <apt-pkg/pkgcache.h>
26
struct PyGroup : CppPyObject<pkgCache::GrpIterator> {
27
pkgCache::PkgIterator current;
31
static PyObject *group_new(PyTypeObject *type,PyObject *args,
36
char *kwlist[] = {"cache", "name", NULL};
37
if (PyArg_ParseTupleAndKeywords(args, kwds, "O!s", kwlist,
38
&PyCache_Type, &pyCache,
42
pkgCache *cache = GetCpp<pkgCache *>(pyCache);
44
pkgCache::GrpIterator grp = cache->FindGrp(name);
47
return PyGroup_FromCpp(grp, true, pyCache);
49
PyErr_SetString(PyExc_KeyError, name);
54
static const char group_find_package_doc[] =
55
"find_package(architecture: str) -> Package\n\n"
56
"Return a package for the given architecture, or None if none exists";
57
static PyObject *group_find_package(PyObject *self,PyObject *args)
59
pkgCache::GrpIterator grp = GetCpp<pkgCache::GrpIterator>(self);
60
PyObject *owner = GetOwner<pkgCache::GrpIterator>(self);
63
if (PyArg_ParseTuple(args, "s", &architecture) == 0)
66
pkgCache::PkgIterator pkg = grp.FindPkg(architecture);
71
return PyPackage_FromCpp(pkg, true, owner ? owner : self);
75
static const char group_find_preferred_package_doc[] =
76
"find_preferred_package(prefer_non_virtual: bool = True) -> Package\n\n"
77
"Return a package for the best architecture, either the native one\n"
78
"or the first found one. If none exists, return None. If non_virtual\n"
79
"is True, prefer non-virtual packages over virtual ones.";
80
static PyObject *group_find_preferred_package(PyObject *self,PyObject *args,
83
pkgCache::GrpIterator grp = GetCpp<pkgCache::GrpIterator>(self);
84
PyObject *owner = GetOwner<pkgCache::GrpIterator>(self);
86
char *kwlist[] = {"prefer_non_virtual", NULL};
87
if (PyArg_ParseTupleAndKeywords(args, kwds, "|b", kwlist, &nonvirtual) == 0)
89
pkgCache::PkgIterator pkg = grp.FindPreferredPkg(nonvirtual);
94
return PyPackage_FromCpp(pkg, true, owner);
98
static PyMethodDef group_methods[] = {
99
{"find_package",group_find_package,METH_VARARGS,group_find_package_doc},
100
{"find_preferred_package",(PyCFunction) group_find_preferred_package,
101
METH_VARARGS|METH_KEYWORDS,group_find_preferred_package_doc},
105
static PyObject *group_seq_item(PyObject *pySelf,Py_ssize_t index)
107
PyGroup *self = static_cast<PyGroup *>(pySelf);
108
pkgCache::GrpIterator grp = GetCpp<pkgCache::GrpIterator>(self);
109
PyObject *owner = GetOwner<pkgCache::GrpIterator>(self);
111
if (self->nextIndex > index || self->nextIndex == 0) {
113
new (&self->current) pkgCache::PkgIterator(grp.PackageList());
116
if (self->nextIndex != index + 1) {
117
while (self->nextIndex <= index && !self->current.end()) {
118
self->current = grp.NextPkg(self->current);
123
if (self->current.end())
124
return PyErr_Format(PyExc_IndexError, "Out of range: %zd", index);
126
return PyPackage_FromCpp(self->current, true, owner);
130
static PySequenceMethods group_as_sequence =
142
static const char group_doc[] = "Group(cache, name)\n\n"
143
"Group of packages with the same name.\n\n"
144
"Provides access to all packages sharing a name. Can be used this\n"
145
"like a list, or by using the special find_*() methods. If you use\n"
146
"it as a sequence, make sure to access it linearly, as this uses a\n"
147
"linked list internally.";
148
PyTypeObject PyGroup_Type = {
149
PyVarObject_HEAD_INIT(&PyType_Type, 0)
150
"apt_pkg.Group", // tp_name
151
sizeof(PyGroup), // tp_basicsize
154
CppDealloc<pkgCache::GrpIterator>, // tp_dealloc
161
&group_as_sequence, // tp_as_sequence
169
Py_TPFLAGS_DEFAULT, // tp_flags
174
0, // tp_weaklistoffset
177
group_methods, // tp_methods