| 1 |
// python-gphoto2 - Python interface to libgphoto2 |
| 2 |
// http://github.com/jim-easterbrook/python-gphoto2 |
| 3 |
// Copyright (C) 2014-25 Jim Easterbrook [email protected] |
| 4 |
// |
| 5 |
// This file is part of python-gphoto2. |
| 6 |
// |
| 7 |
// python-gphoto2 is free software: you can redistribute it and/or modify |
| 8 |
// it under the terms of the GNU Lesser General Public License as published |
| 9 |
// by the Free Software Foundation, either version 3 of the License, or |
| 10 |
// (at your option) any later version. |
| 11 |
// |
| 12 |
// python-gphoto2 is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU Lesser General Public License for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU Lesser General Public License |
| 18 |
// along with python-gphoto2. If not, see <https://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
%module(package="gphoto2") list |
| 21 |
|
| 22 |
%include "common/preamble.i" |
| 23 |
|
| 24 |
%rename(CameraList) _CameraList; |
| 25 |
|
| 26 |
// Deprecate some functions intended for camera drivers (2023-08-01) |
| 27 |
DEPRECATED(gp_list_populate,) |
| 28 |
DEPRECATED(_CameraList::populate, 1) |
| 29 |
|
| 30 |
// Make docstring parameter types more Pythonic |
| 31 |
%typemap(doc) CameraList * "$1_name: gphoto2.$*1_type" |
| 32 |
|
| 33 |
// Turn on default exception handling |
| 34 |
DEFAULT_EXCEPTION |
| 35 |
|
| 36 |
// gp_list_new() returns a pointer in an output parameter |
| 37 |
PLAIN_ARGOUT(CameraList **) |
| 38 |
|
| 39 |
// gp_list_find_by_name() returns the result in an integer, or NULL |
| 40 |
%typemap(in, numinputs=0) int *index (int temp=0) %{ |
| 41 |
$1 = &temp; |
| 42 |
%} |
| 43 |
%typemap(argout) int *index { |
| 44 |
$result = SWIG_AppendOutput($result, PyInt_FromLong(*$1)); |
| 45 |
} |
| 46 |
|
| 47 |
// Code fragments used later on |
| 48 |
%fragment("CameraList_accessor", "header") { |
| 49 |
%#include "gphoto2/gphoto2.h" |
| 50 |
typedef PyObject* (CameraList_get_func) (CameraList *, int); |
| 51 |
typedef struct _CameraList_accessor { |
| 52 |
CameraList *list; |
| 53 |
CameraList_get_func *func; |
| 54 |
} CameraList_accessor; |
| 55 |
} |
| 56 |
%fragment("new_CameraList_accessor", "header", |
| 57 |
fragment="CameraList_accessor") { |
| 58 |
// Constructor defined outside %extend as not callable from Python |
| 59 |
static CameraList_accessor |
| 60 |
*new_CameraList_accessor(CameraList *list, CameraList_get_func *func) { |
| 61 |
CameraList_accessor *self = malloc(sizeof(CameraList_accessor)); |
| 62 |
if (!self) return NULL; |
| 63 |
if (gp_list_ref(list) < GP_OK) { |
| 64 |
free(self); |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
self->list = list; |
| 68 |
self->func = func; |
| 69 |
return self; |
| 70 |
} |
| 71 |
} |
| 72 |
%fragment("CameraList_get_key", "header") { |
| 73 |
static PyObject* CameraList_get_key(CameraList *list, int idx) { |
| 74 |
const char *name = NULL; |
| 75 |
int error = gp_list_get_name(list, idx, &name); |
| 76 |
if (error < GP_OK) { |
| 77 |
GPHOTO2_ERROR(error); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
return name ? PyUnicode_FromString(name) : SWIG_Py_Void(); |
| 81 |
} |
| 82 |
} |
| 83 |
%fragment("CameraList_get_value", "header") { |
| 84 |
static PyObject* CameraList_get_value(CameraList *list, int idx) { |
| 85 |
const char *value = NULL; |
| 86 |
int error = gp_list_get_value(list, idx, &value); |
| 87 |
if (error < GP_OK) { |
| 88 |
GPHOTO2_ERROR(error); |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
return value ? PyUnicode_FromString(value) : SWIG_Py_Void(); |
| 92 |
} |
| 93 |
} |
| 94 |
%fragment("CameraList_get_item", "header", |
| 95 |
fragment="CameraList_get_key", fragment="CameraList_get_value") { |
| 96 |
static PyObject* CameraList_get_item(CameraList *list, int idx) { |
| 97 |
PyObject *name = CameraList_get_key(list, idx); |
| 98 |
if (!name) return NULL; |
| 99 |
PyObject *value = CameraList_get_value(list, idx); |
| 100 |
if (!value) { |
| 101 |
SWIG_Py_DECREF(name); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
return PyTuple_Pack(2, name, value); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Add constructor and destructor to _CameraList |
| 109 |
struct _CameraList {}; |
| 110 |
DEFAULT_CTOR(_CameraList, gp_list_new) |
| 111 |
DEFAULT_DTOR(_CameraList, gp_list_unref) |
| 112 |
|
| 113 |
// Simple accessor object |
| 114 |
%feature("docstring") _CameraList_accessor |
| 115 |
"List-like access to CameraList keys, values, or (key, value) items. |
| 116 |
|
| 117 |
This can be accessed or iterated over like any Python list or tuple." |
| 118 |
%feature("python:slot", "sq_length", functype="lenfunc") |
| 119 |
_CameraList_accessor::__len__; |
| 120 |
%feature("python:slot", "sq_item", functype="ssizeargfunc") |
| 121 |
_CameraList_accessor::__getitem__; |
| 122 |
// SWIG doesn't need to know about CameraList_accessor internals |
| 123 |
typedef struct _CameraList_accessor {} CameraList_accessor; |
| 124 |
%extend _CameraList_accessor { |
| 125 |
~_CameraList_accessor() { |
| 126 |
gp_list_unref($self->list); |
| 127 |
free($self); |
| 128 |
} |
| 129 |
int __len__() { |
| 130 |
return gp_list_count($self->list); |
| 131 |
} |
| 132 |
PyObject* __getitem__(int idx) { |
| 133 |
if (idx < 0 || idx >= gp_list_count($self->list)) { |
| 134 |
PyErr_SetString(PyExc_IndexError, |
| 135 |
"CameraList_accessor index out of range"); |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
return self->func($self->list, idx); |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
// Turn off default exception handling |
| 143 |
%noexception; |
| 144 |
|
| 145 |
// Make CameraList more like a Python list and/or dict |
| 146 |
%feature("python:slot", "mp_subscript", functype="binaryfunc") |
| 147 |
_CameraList::__getitem__; |
| 148 |
%feature("python:slot", "tp_iter", functype="getiterfunc") |
| 149 |
_CameraList::__iter__; |
| 150 |
// deprecated since 2025-06-03, v2.6.1 |
| 151 |
DEPRECATED(_CameraList::__iter__, 1) |
| 152 |
%feature("docstring") _CameraList::keys "Return an accessor for the names in the list." |
| 153 |
%feature("docstring") _CameraList::values "Return an accessor for the values in the list." |
| 154 |
%feature("docstring") _CameraList::items "Return an accessor for the (name, value) pairs in the list." |
| 155 |
%newobject _CameraList::keys; |
| 156 |
%newobject _CameraList::values; |
| 157 |
%newobject _CameraList::items; |
| 158 |
%extend _CameraList { |
| 159 |
%fragment("new_CameraList_accessor"); |
| 160 |
%fragment("CameraList_get_key"); |
| 161 |
%fragment("CameraList_get_value"); |
| 162 |
%fragment("CameraList_get_item"); |
| 163 |
PyObject *__getitem__(int idx) { |
| 164 |
if (idx < 0) |
| 165 |
idx += gp_list_count($self); |
| 166 |
if (idx < 0 || idx >= gp_list_count($self)) { |
| 167 |
PyErr_SetString(PyExc_IndexError, "CameraList index out of range"); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
return CameraList_get_item($self, idx); |
| 171 |
} |
| 172 |
PyObject *__getitem__(const char *name) { |
| 173 |
int idx = 0; |
| 174 |
if (gp_list_find_by_name($self, &idx, name) < GP_OK) { |
| 175 |
PyErr_SetString(PyExc_KeyError, name); |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
return CameraList_get_value($self, idx); |
| 179 |
} |
| 180 |
CameraList_accessor* keys() { |
| 181 |
return new_CameraList_accessor($self, CameraList_get_key); |
| 182 |
} |
| 183 |
CameraList_accessor* values() { |
| 184 |
return new_CameraList_accessor($self, CameraList_get_value); |
| 185 |
} |
| 186 |
CameraList_accessor* items() { |
| 187 |
return new_CameraList_accessor($self, CameraList_get_item); |
| 188 |
} |
| 189 |
PyObject* __iter__() { |
| 190 |
return PySeqIter_New(SWIG_Python_NewPointerObj( |
| 191 |
NULL, SWIG_as_voidptr(_CameraList_items($self)), |
| 192 |
$descriptor(CameraList_accessor*), SWIG_POINTER_OWN)); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
// Turn on default exception handling |
| 197 |
DEFAULT_EXCEPTION |
| 198 |
|
| 199 |
// Add member methods to _CameraList |
| 200 |
LEN_MEMBER_FUNCTION(_CameraList, gp_list_count) |
| 201 |
MEMBER_FUNCTION(_CameraList, |
| 202 |
int, count, (), |
| 203 |
gp_list_count, ($self), ) |
| 204 |
MEMBER_FUNCTION(_CameraList, |
| 205 |
void, append, (const char *name, const char *value), |
| 206 |
gp_list_append, ($self, name, value), ) |
| 207 |
MEMBER_FUNCTION(_CameraList, |
| 208 |
void, reset, (), |
| 209 |
gp_list_reset, ($self), ) |
| 210 |
MEMBER_FUNCTION(_CameraList, |
| 211 |
void, sort, (), |
| 212 |
gp_list_sort, ($self), ) |
| 213 |
MEMBER_FUNCTION(_CameraList, |
| 214 |
void, find_by_name, (int *index, const char *name), |
| 215 |
gp_list_find_by_name, ($self, index, name), ) |
| 216 |
MEMBER_FUNCTION(_CameraList, |
| 217 |
void, get_name, (int index, const char **name), |
| 218 |
gp_list_get_name, ($self, index, name), ) |
| 219 |
MEMBER_FUNCTION(_CameraList, |
| 220 |
void, get_value, (int index, const char **value), |
| 221 |
gp_list_get_value, ($self, index, value), ) |
| 222 |
MEMBER_FUNCTION(_CameraList, |
| 223 |
void, set_name, (int index, const char *name), |
| 224 |
gp_list_set_name, ($self, index, name), ) |
| 225 |
MEMBER_FUNCTION(_CameraList, |
| 226 |
void, set_value, (int index, const char *value), |
| 227 |
gp_list_set_value, ($self, index, value), ) |
| 228 |
MEMBER_FUNCTION(_CameraList, |
| 229 |
void, populate, (const char *format, int count), |
| 230 |
gp_list_populate, ($self, format, count), ) |
| 231 |
|
| 232 |
// Ignore some functions |
| 233 |
%ignore gp_list_free; |
| 234 |
%ignore gp_list_ref; |
| 235 |
%ignore gp_list_unref; |
| 236 |
|
| 237 |
// Turn off default exception handling |
| 238 |
%noexception; |
| 239 |
|
| 240 |
%include "gphoto2/gphoto2-list.h" |