]> sigrok.org Git - libsigrok.git/blobdiff - bindings/python/sigrok/core/classes.i
bindings: Support per-language extensions to EnumValue wrappers.
[libsigrok.git] / bindings / python / sigrok / core / classes.i
index b755289052c842100116d25a4d66ad30f974d080..066dbf8264f8edc75b641d29c217ec5913b1b8bb 100644 (file)
@@ -287,14 +287,14 @@ std::map<std::string, std::string> dict_to_map_string(PyObject *dict)
 /* 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))
@@ -309,14 +309,14 @@ Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::Config
 Glib::VariantBase python_to_variant_by_option(PyObject *input,
     std::shared_ptr<sigrok::Option> option)
 {
-    GVariantType *type = option->get_default_value().get_type().gobj();
+    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<std::string>::create(PyString_AsString(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))
@@ -359,6 +359,31 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
 
 %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. */
@@ -377,7 +402,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
         {
             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;
         }
@@ -400,7 +425,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
     std::shared_ptr<sigrok::Input> _create_input_kwargs(PyObject *dict)
     {
         return $self->create_input(
-            dict_to_map_options(dict, $self->get_options()));
+            dict_to_map_options(dict, $self->options()));
     }
 }
 
@@ -419,7 +444,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
         std::shared_ptr<sigrok::Device> device, PyObject *dict)
     {
         return $self->create_output(device,
-            dict_to_map_options(dict, $self->get_options()));
+            dict_to_map_options(dict, $self->options()));
     }
 }