Better SWIG integration: autogen func & attributes

This commit is contained in:
slurdge
2020-06-14 11:32:37 +02:00
committed by Philippe Teuwen
parent 756b624668
commit 538ee4dabb
12 changed files with 887 additions and 1037 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
#!/bin/bash
swig -lua -o ../src/pm3_luawrap.c ../include/pm3.h
swig -python -o ../src/pm3_pywrap.c ../include/pm3.h
swig -lua -o ../src/pm3_luawrap.c ../include/pm3.i
swig -python -o ../src/pm3_pywrap.c ../include/pm3.i
+2 -2
View File
@@ -1,4 +1,4 @@
local pm3 = require("pm3")
p=pm3.device()
--p.console("hw status") ??
p.console(p, "hw status")
p:console("hw status")
print(p.name)
+1 -1
View File
@@ -3,4 +3,4 @@
import pm3
p=pm3.device()
p.console("hw status")
print("Device:", p.get_name())
print("Device:", p.name)
+5 -50
View File
@@ -1,56 +1,11 @@
#ifndef LIBPM3_H
#define LIBPM3_H
#ifdef SWIG
%module pm3
%{
/* Include the header in the wrapper code */
#include "pm3.h"
%}
/* Strip "pm3_" from API functions for SWIG */
%rename("%(strip:[pm3_])s") "";
%feature("immutable","1") pm3_current_dev;
struct pm3_device { };
%extend pm3_device {
pm3_device() {
printf("SWIG pm3_device constructor, get current pm3\n");
_embedded = 1;
return pm3_get_current_dev();
}
pm3_device(char *port) {
printf("SWIG pm3_device constructor with port, open pm3\n");
_embedded = 0;
return pm3_open(port);
}
~pm3_device() {
if (_embedded) {
printf("SWIG pm3_device destructor, nothing to do\n");
} else {
printf("SWIG pm3_device destructor, close pm3\n");
pm3_close($self);
}
}
int console(char *cmd) {
return pm3_console($self, cmd);
}
char *get_name() {
return pm3_get_name($self);
}
}
//%nodefaultctor pm3_device;
//%nodefaultdtor pm3_device;
/* Parse the header file to generate wrappers */
#endif // SWIG
// TODO better than this global?
int _embedded;
typedef struct pm3_device pm3_device;
pm3_device* pm3_open(char *port);
int pm3_console(pm3_device* dev, char *cmd);
char *pm3_get_name(pm3_device* dev);
void pm3_close(pm3_device* dev);
pm3_device* pm3_get_current_dev(void);
int pm3_device_console(pm3_device* dev, char *cmd);
char *pm3_device_name_get(pm3_device* dev);
void pm3_device_close(pm3_device* dev);
pm3_device* pm3_device_get_current_dev(void);
#endif // LIBPM3_H
+39
View File
@@ -0,0 +1,39 @@
%module pm3
%{
/* Include the header in the wrapper code */
#include "pm3.h"
#include "comms.h"
%}
/* Strip "pm3_" from API functions for SWIG */
%rename("%(strip:[pm3_])s") "";
%feature("immutable","1") pm3_current_dev;
typedef struct {
%extend {
pm3_device() {
printf("SWIG pm3_device constructor, get current pm3\n");
pm3_device * p = pm3_device_get_current_dev();
p->script_embedded = 1;
return p;
}
pm3_device(char *port) {
printf("SWIG pm3_device constructor with port, open pm3\n");
pm3_device * p = pm3_open(port);
p->script_embedded = 1;
return p;
}
~pm3_device() {
if ($self->script_embedded) {
printf("SWIG pm3_device destructor, nothing to do\n");
} else {
printf("SWIG pm3_device destructor, close pm3\n");
pm3_device_close($self);
}
}
int console(char *cmd);
char const * const name;
}
} pm3_device;
//%nodefaultctor device;
//%nodefaultdtor device;
/* Parse the header file to generate wrappers */
+2 -2
View File
@@ -2,5 +2,5 @@
local pm3 = require("pm3")
p=pm3.device("/dev/ttyACM0")
--p.console("hw status") ??
p.console(p, "hw status")
p:console("hw status")
print(p.name)
+1 -1
View File
@@ -3,4 +3,4 @@
import pm3
p=pm3.device("/dev/ttyACM0")
p.console("hw status")
print("Device:", p.get_name())
print("Device:", p.name)
+1
View File
@@ -68,6 +68,7 @@ extern communication_arg_t conn;
typedef struct pm3_device pm3_device;
struct pm3_device {
communication_arg_t *conn;
int script_embedded;
};
void *uart_receiver(void *targ);
+92 -66
View File
@@ -1,24 +1,85 @@
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 4.0.1
# Version 3.0.12
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
from sys import version_info as _swig_python_version_info
if _swig_python_version_info < (2, 7, 0):
raise RuntimeError("Python 2.7 or later required")
# Import the low-level C/C++ module
if __package__ or "." in __name__:
from . import _pm3
if _swig_python_version_info >= (2, 7, 0):
def swig_import_helper():
import importlib
pkg = __name__.rpartition('.')[0]
mname = '.'.join((pkg, '_pm3')).lstrip('.')
try:
return importlib.import_module(mname)
except ImportError:
return importlib.import_module('_pm3')
_pm3 = swig_import_helper()
del swig_import_helper
elif _swig_python_version_info >= (2, 6, 0):
def swig_import_helper():
from os.path import dirname
import imp
fp = None
try:
fp, pathname, description = imp.find_module('_pm3', [dirname(__file__)])
except ImportError:
import _pm3
return _pm3
try:
_mod = imp.load_module('_pm3', fp, pathname, description)
finally:
if fp is not None:
fp.close()
return _mod
_pm3 = swig_import_helper()
del swig_import_helper
else:
import _pm3
del _swig_python_version_info
try:
_swig_property = property
except NameError:
pass # Python < 2.2 doesn't have 'property'.
try:
import builtins as __builtin__
except ImportError:
import __builtin__
def _swig_setattr_nondynamic(self, class_type, name, value, static=1):
if (name == "thisown"):
return self.this.own(value)
if (name == "this"):
if type(value).__name__ == 'SwigPyObject':
self.__dict__[name] = value
return
method = class_type.__swig_setmethods__.get(name, None)
if method:
return method(self, value)
if (not static):
if _newclass:
object.__setattr__(self, name, value)
else:
self.__dict__[name] = value
else:
raise AttributeError("You cannot add attributes to %s" % self)
def _swig_setattr(self, class_type, name, value):
return _swig_setattr_nondynamic(self, class_type, name, value, 0)
def _swig_getattr(self, class_type, name):
if (name == "thisown"):
return self.this.own()
method = class_type.__swig_getmethods__.get(name, None)
if method:
return method(self)
raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name))
def _swig_repr(self):
try:
strthis = "proxy of " + self.this.__repr__()
@@ -26,73 +87,38 @@ def _swig_repr(self):
strthis = ""
return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
try:
_object = object
_newclass = 1
except __builtin__.Exception:
class _object:
pass
_newclass = 0
def _swig_setattr_nondynamic_instance_variable(set):
def set_instance_attr(self, name, value):
if name == "thisown":
self.this.own(value)
elif name == "this":
set(self, name, value)
elif hasattr(self, name) and isinstance(getattr(type(self), name), property):
set(self, name, value)
else:
raise AttributeError("You cannot add instance attributes to %s" % self)
return set_instance_attr
def _swig_setattr_nondynamic_class_variable(set):
def set_class_attr(cls, name, value):
if hasattr(cls, name) and not isinstance(getattr(cls, name), property):
set(cls, name, value)
else:
raise AttributeError("You cannot add class attributes to %s" % cls)
return set_class_attr
def _swig_add_metaclass(metaclass):
"""Class decorator for adding a metaclass to a SWIG wrapped class - a slimmed down version of six.add_metaclass"""
def wrapper(cls):
return metaclass(cls.__name__, cls.__bases__, cls.__dict__.copy())
return wrapper
class _SwigNonDynamicMeta(type):
"""Meta class to enforce nondynamic attributes (no new attributes) for a class"""
__setattr__ = _swig_setattr_nondynamic_class_variable(type.__setattr__)
class device(object):
thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag")
class device(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, device, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, device, name)
__repr__ = _swig_repr
def __init__(self, *args):
_pm3.device_swiginit(self, _pm3.new_device(*args))
this = _pm3.new_device(*args)
try:
self.this.append(this)
except __builtin__.Exception:
self.this = this
__swig_destroy__ = _pm3.delete_device
__del__ = lambda self: None
def console(self, cmd):
return _pm3.device_console(self, cmd)
__swig_getmethods__["name"] = _pm3.device_name_get
if _newclass:
name = _swig_property(_pm3.device_name_get)
device_swigregister = _pm3.device_swigregister
device_swigregister(device)
def get_name(self):
return _pm3.device_get_name(self)
# Register device in _pm3:
_pm3.device_swigregister(device)
# This file is compatible with both classic and new-style classes.
def open(port):
return _pm3.open(port)
def console(dev, cmd):
return _pm3.console(dev, cmd)
def get_name(dev):
return _pm3.get_name(dev)
def close(dev):
return _pm3.close(dev)
def get_current_dev():
return _pm3.get_current_dev()
cvar = _pm3.cvar
+74 -247
View File
File diff suppressed because it is too large Load Diff
+664 -662
View File
File diff suppressed because it is too large Load Diff
+4 -4
View File
@@ -727,7 +727,7 @@ pm3_device* pm3_open(char *port) {
return session.current_device;
}
void pm3_close(pm3_device* dev) {
void pm3_device_close(pm3_device* dev) {
// For now, there is no real device context:
(void) dev;
// Clean up the port
@@ -739,17 +739,17 @@ void pm3_close(pm3_device* dev) {
}
}
int pm3_console(pm3_device* dev, char *Cmd) {
int pm3_device_console(pm3_device* dev, char *Cmd) {
// For now, there is no real device context:
(void) dev;
return CommandReceived(Cmd);
}
char *pm3_get_name(pm3_device* dev) {
const char *pm3_device_name_get(pm3_device* dev) {
return dev->conn->serial_port_name;
}
pm3_device* pm3_get_current_dev(void) {
pm3_device* pm3_device_get_current_dev(void) {
return session.current_device;
}
/* ======================================================= */