| 1 |
// python-gphoto2 - Python interface to libgphoto2 |
| 2 |
// http://github.com/jim-easterbrook/python-gphoto2 |
| 3 |
// Copyright (C) 2014-23 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", threads="1") file |
| 21 |
%nothread; |
| 22 |
#pragma SWIG nowarn=321 |
| 23 |
|
| 24 |
%include "common/preamble.i" |
| 25 |
|
| 26 |
%rename(CameraFile) _CameraFile; |
| 27 |
|
| 28 |
// gp_file_get_mtime() returns a pointer in output params |
| 29 |
typedef long int time_t; |
| 30 |
%apply time_t *OUTPUT { time_t * }; |
| 31 |
%typemap(doc) time_t "$1_name: int" |
| 32 |
|
| 33 |
// Make docstring parameter types more Pythonic |
| 34 |
%typemap(doc) CameraFile * "$1_name: gphoto2.$*1_type" |
| 35 |
%typemap(doc) CameraFileHandler * "$1_name: gphoto2.$*1_type" |
| 36 |
%typemap(doc) enum CameraFileType "$1_name: $1_type (gphoto2.GP_FILE_TYPE_PREVIEW etc.)" |
| 37 |
|
| 38 |
// Allow other Python threads to continue during some function calls |
| 39 |
%thread gp_file_copy; |
| 40 |
%thread gp_file_get_data_and_size; |
| 41 |
%thread gp_file_save; |
| 42 |
%thread gp_file_set_data_and_size; |
| 43 |
|
| 44 |
// Turn on default exception handling |
| 45 |
DEFAULT_EXCEPTION |
| 46 |
|
| 47 |
// gp_file_new() returns a pointer in an output parameter |
| 48 |
PLAIN_ARGOUT(CameraFile **) |
| 49 |
|
| 50 |
// gp_file_get_data_and_size() returns a pointer to some data |
| 51 |
%typemap(in, numinputs=0) (const char **data, unsigned long int *size) |
| 52 |
(char *temp_data = NULL, |
| 53 |
unsigned long temp_size = 0) %{ |
| 54 |
$1 = &temp_data; |
| 55 |
$2 = &temp_size; |
| 56 |
%} |
| 57 |
%typemap(argout) (const char **data, unsigned long int *size) { |
| 58 |
$result = SWIG_AppendOutput( |
| 59 |
$result, PyMemoryView_FromMemory(*$1, *$2, PyBUF_READ)); |
| 60 |
} |
| 61 |
|
| 62 |
// gp_file_set_data_and_size() requires data allocated by malloc which it will free later |
| 63 |
%typemap(in, numinputs=1) (char * data, unsigned long int size) { |
| 64 |
Py_buffer view; |
| 65 |
if (PyObject_CheckBuffer($input) != 1) { |
| 66 |
PyErr_SetString( |
| 67 |
PyExc_TypeError, |
| 68 |
"in method '$symname', argument $argnum does not support the buffer interface"); |
| 69 |
SWIG_fail; |
| 70 |
} |
| 71 |
if (PyObject_GetBuffer($input, &view, PyBUF_SIMPLE) != 0) { |
| 72 |
PyErr_SetString( |
| 73 |
PyExc_TypeError, |
| 74 |
"in method '$symname', argument $argnum does not export a simple buffer"); |
| 75 |
SWIG_fail; |
| 76 |
} |
| 77 |
$1 = malloc(view.len); |
| 78 |
if (!$1) { |
| 79 |
PyErr_SetString( |
| 80 |
PyExc_MemoryError, "in method '$symname', insufficient memory"); |
| 81 |
SWIG_fail; |
| 82 |
} |
| 83 |
memcpy($1, view.buf, view.len); |
| 84 |
$2 = view.len; |
| 85 |
PyBuffer_Release(&view); |
| 86 |
} |
| 87 |
%exception gp_file_set_data_and_size %{ |
| 88 |
$action |
| 89 |
if (result < GP_OK) free(arg2); |
| 90 |
%} |
| 91 |
%exception _CameraFile::set_data_and_size { |
| 92 |
$action |
| 93 |
if (PyErr_Occurred()) { |
| 94 |
free(arg2); |
| 95 |
SWIG_fail; |
| 96 |
} |
| 97 |
} |
| 98 |
%typemap(doc) char * data, unsigned long int size "$1_name: readable buffer (e.g. bytes)" |
| 99 |
|
| 100 |
// Add default constructor and destructor to _CameraFile |
| 101 |
struct _CameraFile {}; |
| 102 |
DEFAULT_CTOR(_CameraFile, gp_file_new) |
| 103 |
DEFAULT_DTOR(_CameraFile, gp_file_unref) |
| 104 |
|
| 105 |
// Add constructor from file descriptor |
| 106 |
%extend _CameraFile { |
| 107 |
_CameraFile(int fd) { |
| 108 |
struct _CameraFile *result; |
| 109 |
int error = gp_file_new_from_fd(&result, fd); |
| 110 |
if (error < GP_OK) |
| 111 |
GPHOTO2_ERROR(error) |
| 112 |
return result; |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
// Add member methods to _CameraFile |
| 117 |
MEMBER_FUNCTION(_CameraFile, |
| 118 |
void, set_name, (const char *name), |
| 119 |
gp_file_set_name, ($self, name), ) |
| 120 |
MEMBER_FUNCTION(_CameraFile, |
| 121 |
void, get_name, (const char **name), |
| 122 |
gp_file_get_name, ($self, name), ) |
| 123 |
MEMBER_FUNCTION(_CameraFile, |
| 124 |
void, set_mime_type, (const char *mime_type), |
| 125 |
gp_file_set_mime_type, ($self, mime_type), ) |
| 126 |
MEMBER_FUNCTION(_CameraFile, |
| 127 |
void, get_mime_type, (const char **mime_type), |
| 128 |
gp_file_get_mime_type, ($self, mime_type), ) |
| 129 |
MEMBER_FUNCTION(_CameraFile, |
| 130 |
void, set_mtime, (time_t mtime), |
| 131 |
gp_file_set_mtime, ($self, mtime), ) |
| 132 |
MEMBER_FUNCTION(_CameraFile, |
| 133 |
void, get_mtime, (time_t *mtime), |
| 134 |
gp_file_get_mtime, ($self, mtime), ) |
| 135 |
MEMBER_FUNCTION(_CameraFile, |
| 136 |
void, detect_mime_type, (), |
| 137 |
gp_file_detect_mime_type, ($self), ) |
| 138 |
MEMBER_FUNCTION(_CameraFile, |
| 139 |
void, adjust_name_for_mime_type, (), |
| 140 |
gp_file_adjust_name_for_mime_type, ($self), ) |
| 141 |
MEMBER_FUNCTION(_CameraFile, |
| 142 |
void, get_name_by_type, (const char *basename, CameraFileType type, char **newname), |
| 143 |
gp_file_get_name_by_type, ($self, basename, type, newname), ) |
| 144 |
MEMBER_FUNCTION(_CameraFile, |
| 145 |
void, set_data_and_size, (char *data, unsigned long int size), |
| 146 |
gp_file_set_data_and_size, ($self, data, size), 1) |
| 147 |
MEMBER_FUNCTION(_CameraFile, |
| 148 |
void, get_data_and_size, (const char **data, unsigned long int *size), |
| 149 |
gp_file_get_data_and_size, ($self, data, size), 1) |
| 150 |
MEMBER_FUNCTION(_CameraFile, |
| 151 |
void, save, (const char *filename), |
| 152 |
gp_file_save, ($self, filename), 1) |
| 153 |
MEMBER_FUNCTION(_CameraFile, |
| 154 |
void, clean, (), |
| 155 |
gp_file_clean, ($self), ) |
| 156 |
MEMBER_FUNCTION(_CameraFile, |
| 157 |
void, copy, (CameraFile *source), |
| 158 |
gp_file_copy, ($self, source), 1) |
| 159 |
MEMBER_FUNCTION(_CameraFile, |
| 160 |
void, open, (const char *filename), |
| 161 |
gp_file_open, ($self, filename), 1) |
| 162 |
|
| 163 |
// These structures are private |
| 164 |
%ignore _CameraFileHandler; |
| 165 |
|
| 166 |
// These functions are internal |
| 167 |
%ignore gp_file_append; |
| 168 |
%ignore gp_file_slurp; |
| 169 |
%ignore gp_file_free; |
| 170 |
%ignore gp_file_ref; |
| 171 |
%ignore gp_file_unref; |
| 172 |
%ignore gp_file_new_from_handler; |
| 173 |
|
| 174 |
// Turn off default exception handling |
| 175 |
%noexception; |
| 176 |
|
| 177 |
%include "gphoto2/gphoto2-file.h" |