Skip to content

Commit

Permalink
Allow widget value to be accessed as an attribute
Browse files Browse the repository at this point in the history
See feature request #116. This is a bit kludgy, and is read-only so far,
but it seems to work and should be faster than calling
gp_widget_get_value or CameraWidget.get_value
  • Loading branch information
jim-easterbrook committed Mar 26, 2021
1 parent 9448eb3 commit 5cd8c94
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# python-gphoto2 - Python interface to libgphoto2
# http://github.com/jim-easterbrook/python-gphoto2
# Copyright (C) 2014-20 Jim Easterbrook jim@jim-easterbrook.me.uk
# Copyright (C) 2014-21 Jim Easterbrook jim@jim-easterbrook.me.uk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -26,7 +26,7 @@
import sys

# python-gphoto2 version
version = '2.2.5'
version = '2.3.0'

# get gphoto2 library config
cmd = ['pkg-config', '--modversion', 'libgphoto2']
Expand Down
42 changes: 41 additions & 1 deletion src/gphoto2/widget.i
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// python-gphoto2 - Python interface to libgphoto2
// http://github.com/jim-easterbrook/python-gphoto2
// Copyright (C) 2014-20 Jim Easterbrook jim@jim-easterbrook.me.uk
// Copyright (C) 2014-21 Jim Easterbrook jim@jim-easterbrook.me.uk
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand All @@ -18,6 +18,7 @@
%module(package="gphoto2") widget

%include "common/preamble.i"
%include <attribute.i>

%rename(CameraWidget) _CameraWidget;

Expand Down Expand Up @@ -199,6 +200,45 @@ int gp_widget_get_value(CameraWidget *widget, int *value);
// Ignore original void* version
%ignore gp_widget_get_value(CameraWidget *widget, void *value);

// Add a 'value' attribute to CameraWidget
%attribute(_CameraWidget, PyObject*, value, value_get);
%ignore _CameraWidget_value_get;

%{
// Undefine %attribute macro's version of _CameraWidget_value_get
#undef _CameraWidget_value_get

// Define our own _CameraWidget_value_get function
static PyObject* _CameraWidget_value_get(CameraWidget *widget) {
int error;
CameraWidgetType type;
union {
int int_val;
float flt_val;
char* str_val;
} value;
value.str_val = NULL;
error = gp_widget_get_value(widget, &value);
if (error < GP_OK) goto fail;
error = gp_widget_get_type(widget, &type);
if (error < GP_OK) goto fail;
switch (type) {
case GP_WIDGET_DATE:
case GP_WIDGET_TOGGLE:
return SWIG_From_int(value.int_val);
case GP_WIDGET_RANGE:
return SWIG_From_float(value.flt_val);
default:
if (value.str_val) return PyString_FromString(value.str_val);
Py_INCREF(Py_None);
return Py_None;
}
fail:
PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
return NULL;
}
%}

// function to allow python iter() to be called with python object
#if defined(SWIGPYTHON_BUILTIN)
%ignore make_iterator;
Expand Down

0 comments on commit 5cd8c94

Please sign in to comment.