* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-%module classes
+%define DOCSTRING
+"@mainpage API Reference
+
+Introduction
+------------
+
+The pysigrok API provides an object-oriented Python interface to the
+functionality in libsigrok. It is built on top of the sigrok++ C++ API.
+
+Getting started
+---------------
+
+Usage of the pysigrok API needs to begin with a call to Context.create().
+This will create the global libsigrok context and returns a Context object.
+Methods on this object provide access to the hardware drivers, input and output
+formats supported by the library, as well as means of creating other objects
+such as sessions and triggers.
+
+Error handling
+--------------
+
+When any libsigrok C API call returns an error, an Error exception is raised,
+which provides access to the error code and description."
+%enddef
+
+%module(docstring=DOCSTRING) classes
%{
#include <pygobject.h>
PyTypeObject *IOChannel;
PyTypeObject *PollFD;
+#include "config.h"
+
+#if PYGOBJECT_FLAGS_SIGNED
+typedef gint pyg_flags_type;
+#else
+typedef guint pyg_flags_type;
+#endif
+
%}
%init %{
/* Map from Glib::IOCondition to GLib.IOCondition. */
%typecheck(SWIG_TYPECHECK_POINTER) Glib::IOCondition {
- gint flags;
+ pyg_flags_type flags;
$1 = pygobject_check($input, &PyGFlags_Type) &&
(pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) != -1);
}
%typemap(in) Glib::IOCondition {
if (!pygobject_check($input, &PyGFlags_Type))
SWIG_exception(SWIG_TypeError, "Expected GLib.IOCondition value");
- gint flags;
+ pyg_flags_type flags;
if (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) == -1)
SWIG_exception(SWIG_TypeError, "Not a valid Glib.IOCondition value");
$1 = (Glib::IOCondition) flags;
SWIG_exception(SWIG_TypeError, "Expected GLib.PollFD");
PyObject *fd_obj = PyObject_GetAttrString($input, "fd");
PyObject *events_obj = PyObject_GetAttrString($input, "events");
- gint flags;
+ pyg_flags_type flags;
pyg_flags_get_value(G_TYPE_IO_CONDITION, events_obj, &flags);
int fd = PyInt_AsLong(fd_obj);
Glib::IOCondition events = (Glib::IOCondition) flags;
#include "libsigrok/libsigrok.hpp"
/* Convert from a Python dict to a std::map<std::string, std::string> */
-std::map<std::string, std::string> dict_to_map(PyObject *dict)
+std::map<std::string, std::string> dict_to_map_string(PyObject *dict)
{
if (!PyDict_Check(dict))
throw sigrok::Error(SR_ERR_ARG);
/* Convert from a Python type to Glib::Variant, according to config key data type. */
Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::ConfigKey *key)
{
- enum sr_datatype type = key->get_data_type()->get_id();
+ enum sr_datatype type = (enum sr_datatype) key->data_type()->id();
if (type == SR_T_UINT64 && PyInt_Check(input))
return Glib::Variant<guint64>::create(PyInt_AsLong(input));
if (type == SR_T_UINT64 && PyLong_Check(input))
return Glib::Variant<guint64>::create(PyLong_AsLong(input));
else if (type == SR_T_STRING && PyString_Check(input))
- return Glib::Variant<std::string>::create(PyString_AsString(input));
+ return Glib::Variant<Glib::ustring>::create(PyString_AsString(input));
else if (type == SR_T_BOOL && PyBool_Check(input))
return Glib::Variant<bool>::create(input == Py_True);
else if (type == SR_T_FLOAT && PyFloat_Check(input))
throw sigrok::Error(SR_ERR_ARG);
}
+/* Convert from a Python type to Glib::Variant, according to Option data type. */
+Glib::VariantBase python_to_variant_by_option(PyObject *input,
+ std::shared_ptr<sigrok::Option> option)
+{
+ GVariantType *type = option->default_value().get_type().gobj();
+
+ if (type == G_VARIANT_TYPE_UINT64 && PyInt_Check(input))
+ return Glib::Variant<guint64>::create(PyInt_AsLong(input));
+ if (type == G_VARIANT_TYPE_UINT64 && PyLong_Check(input))
+ return Glib::Variant<guint64>::create(PyLong_AsLong(input));
+ else if (type == G_VARIANT_TYPE_STRING && PyString_Check(input))
+ return Glib::Variant<Glib::ustring>::create(PyString_AsString(input));
+ else if (type == G_VARIANT_TYPE_BOOLEAN && PyBool_Check(input))
+ return Glib::Variant<bool>::create(input == Py_True);
+ else if (type == G_VARIANT_TYPE_DOUBLE && PyFloat_Check(input))
+ return Glib::Variant<double>::create(PyFloat_AsDouble(input));
+ else if (type == G_VARIANT_TYPE_INT32 && PyInt_Check(input))
+ return Glib::Variant<gint32>::create(PyInt_AsLong(input));
+ else
+ throw sigrok::Error(SR_ERR_ARG);
+}
+
+/* Convert from a Python dict to a std::map<std::string, std::string> */
+std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
+ std::map<std::string, std::shared_ptr<sigrok::Option> > options)
+{
+ if (!PyDict_Check(dict))
+ throw sigrok::Error(SR_ERR_ARG);
+
+ std::map<std::string, Glib::VariantBase> output;
+
+ PyObject *py_key, *py_value;
+ Py_ssize_t pos = 0;
+
+ while (PyDict_Next(dict, &pos, &py_key, &py_value)) {
+ if (!PyString_Check(py_key))
+ throw sigrok::Error(SR_ERR_ARG);
+ auto key = PyString_AsString(py_key);
+ auto value = python_to_variant_by_option(py_value, options[key]);
+ output[key] = value;
+ }
+
+ return output;
+}
+
%}
/* Ignore these methods, we will override them below. */
%ignore sigrok::Driver::scan;
-%ignore sigrok::InputFormat::open_file;
+%ignore sigrok::InputFormat::create_input;
%ignore sigrok::OutputFormat::create_output;
+%include "doc.i"
+
+%define %attributevector(Class, Type, Name, Get)
+%rename(_ ## Get) sigrok::Class::Get;
+%extend sigrok::Class
+{
+%pythoncode
+{
+ Name = property(_ ## Get)
+}
+}
+%enddef
+
+%define %attributemap(Class, Type, Name, Get)
+%rename(_ ## Get) sigrok::Class::Get;
+%extend sigrok::Class
+{
+%pythoncode
+{
+ Name = property(fget = lambda x: x._ ## Get().asdict(), doc=_ ## Get.__doc__)
+}
+}
+%enddef
+
+%define %enumextras(Class)
+%enddef
+
%include "../../../swig/classes.i"
/* Support Driver.scan() with keyword arguments. */
{
if (!PyString_Check(py_key))
throw sigrok::Error(SR_ERR_ARG);
- auto key = sigrok::ConfigKey::get(PyString_AsString(py_key));
+ auto key = sigrok::ConfigKey::get_by_identifier(PyString_AsString(py_key));
auto value = python_to_variant_by_key(py_value, key);
options[key] = value;
}
Driver.scan = _Driver_scan
}
-/* Support InputFormat.open_file() with keyword arguments. */
+/* Support InputFormat.create_input() with keyword arguments. */
%extend sigrok::InputFormat
{
- std::shared_ptr<sigrok::InputFileDevice> _open_file_kwargs(std::string filename, PyObject *dict)
+ std::shared_ptr<sigrok::Input> _create_input_kwargs(PyObject *dict)
{
- return $self->open_file(filename, dict_to_map(dict));
+ return $self->create_input(
+ dict_to_map_options(dict, $self->options()));
}
}
%pythoncode
{
- def _InputFormat_open_file(self, filename, **kwargs):
- return self._open_file_kwargs(filename, kwargs)
+ def _InputFormat_create_input(self, **kwargs):
+ return self._create_input(kwargs)
- InputFormat.open_file = _InputFormat_open_file
+ InputFormat.create_input = _InputFormat_create_input
}
/* Support OutputFormat.create_output() with keyword arguments. */
std::shared_ptr<sigrok::Output> _create_output_kwargs(
std::shared_ptr<sigrok::Device> device, PyObject *dict)
{
- return $self->create_output(device, dict_to_map(dict));
+ return $self->create_output(device,
+ dict_to_map_options(dict, $self->options()));
}
}