From d60dc7718ec586280778f0b6f0dec565b5bb2757 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Sat, 31 Oct 2015 14:13:35 -0200 Subject: [PATCH] Implementando métodos python. --- src/python/get.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/python/init.cc | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/python/misc.cc | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/python/private.h | 19 ++++++++++++++++++- src/python/py3270.cbp | 3 +++ src/python/py3270.cc | 59 ++++++++++------------------------------------------------- src/python/sample.py | 15 +++++++++++++-- 7 files changed, 278 insertions(+), 52 deletions(-) create mode 100644 src/python/get.cc create mode 100644 src/python/init.cc create mode 100644 src/python/misc.cc diff --git a/src/python/get.cc b/src/python/get.cc new file mode 100644 index 0000000..29aa964 --- /dev/null +++ b/src/python/get.cc @@ -0,0 +1,63 @@ +/* + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a + * aplicativos mainframe. Registro no INPI sob o nome G3270. + * + * Copyright (C) <2008> + * + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela + * Free Software Foundation. + * + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para + * obter mais detalhes. + * + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin + * St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Este programa está nomeado como get.cc e possui - linhas de código. + * + * Contatos: + * + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) + * + * Referências: + * + * + * + * + */ + + #include "private.h" + + +/*---[ Implement ]----------------------------------------------------------------------------------*/ + +PyObject * terminal_get_version(PyObject *self, PyObject *args) { + + return PyString_FromString( ((pw3270_TerminalObject *) self)->session->get_version().c_str() ); + +} + +PyObject * terminal_get_revision(PyObject *self, PyObject *args) { + + return PyString_FromString( ((pw3270_TerminalObject *) self)->session->get_revision().c_str() ); + +} + +PyObject * terminal_is_connected(PyObject *self, PyObject *args) { + + return PyBool_FromLong( ((pw3270_TerminalObject *) self)->session->is_connected() ); + +} + +PyObject * terminal_is_ready(PyObject *self, PyObject *args) { + + return PyBool_FromLong( ((pw3270_TerminalObject *) self)->session->is_ready() ); + +} + diff --git a/src/python/init.cc b/src/python/init.cc new file mode 100644 index 0000000..aacd4fe --- /dev/null +++ b/src/python/init.cc @@ -0,0 +1,88 @@ +/* + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a + * aplicativos mainframe. Registro no INPI sob o nome G3270. + * + * Copyright (C) <2008> + * + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela + * Free Software Foundation. + * + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para + * obter mais detalhes. + * + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin + * St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Este programa está nomeado como py3270.cc e possui - linhas de código. + * + * Contatos: + * + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) + * + * Implementa métodos básicos inicio/final do objeto python + * + * Referências: + * + * + * + * + */ + + #include "private.h" + + +/*---[ Implement ]----------------------------------------------------------------------------------*/ + +PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + + pw3270_TerminalObject *self = (pw3270_TerminalObject *) type->tp_alloc(type, 0); + + trace("%s: self=%p",__FUNCTION__,self); + + self->session = NULL; + + return (PyObject *)self; +} + + +int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds) { + + const char *id = ""; + + if (!PyArg_ParseTuple(args, "s", &id)) { + id = ""; + } + + trace("%s(%s)",__FUNCTION__,id); + + try { + + self->session = PW3270_NAMESPACE::session::create(id); + + } catch(std::exception &e) { + + trace("%s failed: %s",__FUNCTION__,e.what()); + PyErr_SetString(terminalError, e.what()); + + } + + + return 0; + +} + +void terminal_dealloc(pw3270_TerminalObject * self) { + + trace("%s",__FUNCTION__); + + delete self->session; + + self->ob_type->tp_free((PyObject*)self); + +} diff --git a/src/python/misc.cc b/src/python/misc.cc new file mode 100644 index 0000000..7df1c32 --- /dev/null +++ b/src/python/misc.cc @@ -0,0 +1,83 @@ +/* + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a + * aplicativos mainframe. Registro no INPI sob o nome G3270. + * + * Copyright (C) <2008> + * + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela + * Free Software Foundation. + * + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para + * obter mais detalhes. + * + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin + * St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Este programa está nomeado como misc.cc e possui - linhas de código. + * + * Contatos: + * + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) + * + * Implementa métodos básicos inicio/final do objeto python + * + * Referências: + * + * + * + * + */ + + #include "private.h" + + +/*---[ Implement ]----------------------------------------------------------------------------------*/ + + PyObject * terminal_connect(PyObject *self, PyObject *args) { + + int rc = -1; + int wait = 60; + const char * host = ""; + + if (!PyArg_ParseTuple(args, "s|i", &host, &wait)) { + PyErr_SetString(terminalError, "connect requires a host URL"); + return NULL; + } + + try { + + rc = ((pw3270_TerminalObject *) self)->session->connect(host,wait); + + } catch(std::exception &e) { + + PyErr_SetString(terminalError, e.what()); + return NULL; + } + + return PyLong_FromLong(rc); + + } + + PyObject * terminal_disconnect(PyObject *self, PyObject *args) { + + int rc = -1; + + try { + + rc = ((pw3270_TerminalObject *) self)->session->disconnect(); + + } catch(std::exception &e) { + + PyErr_SetString(terminalError, e.what()); + return NULL; + } + + return PyLong_FromLong(rc); + + } diff --git a/src/python/private.h b/src/python/private.h index 4482bfd..cafa0f5 100644 --- a/src/python/private.h +++ b/src/python/private.h @@ -18,7 +18,7 @@ * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin * St, Fifth Floor, Boston, MA 02110-1301 USA * - * Este programa está nomeado como main.cc e possui - linhas de código. + * Este programa está nomeado como private.h e possui - linhas de código. * * Contatos: * @@ -46,4 +46,21 @@ extern PyObject * terminalError; + extern "C" { + + PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds); + int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds); + void terminal_dealloc(pw3270_TerminalObject * self); + + PyObject * terminal_get_version(PyObject *self, PyObject *args); + PyObject * terminal_get_revision(PyObject *self, PyObject *args); + + PyObject * terminal_is_connected(PyObject *self, PyObject *args); + PyObject * terminal_is_ready(PyObject *self, PyObject *args); + + PyObject * terminal_connect(PyObject *self, PyObject *args); + PyObject * terminal_disconnect(PyObject *self, PyObject *args); + + } + #endif // PRIVATE_H_INCLUDED diff --git a/src/python/py3270.cbp b/src/python/py3270.cbp index 879133d..bb1250f 100644 --- a/src/python/py3270.cbp +++ b/src/python/py3270.cbp @@ -53,6 +53,9 @@ + + + diff --git a/src/python/py3270.cc b/src/python/py3270.cc index ffadd87..a12dd04 100644 --- a/src/python/py3270.cc +++ b/src/python/py3270.cc @@ -18,7 +18,7 @@ * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin * St, Fifth Floor, Boston, MA 02110-1301 USA * - * Este programa está nomeado como main.cc e possui - linhas de código. + * Este programa está nomeado como py3270.cc e possui - linhas de código. * * Contatos: * @@ -48,55 +48,16 @@ static PyObject * get_revision(PyObject *self, PyObject *args) { } -static PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - - pw3270_TerminalObject *self = (pw3270_TerminalObject *) type->tp_alloc(type, 0); - - trace("%s: self=%p",__FUNCTION__,self); - - self->session = NULL; - - return (PyObject *)self; -} - - -static int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds) { - - const char *id = ""; - - if (!PyArg_ParseTuple(args, "s", &id)) { - id = ""; - } - - trace("%s(%s)",__FUNCTION__,id); - - try { - - self->session = PW3270_NAMESPACE::session::create(id); - - } catch(std::exception &e) { - - trace("%s failed: %s",__FUNCTION__,e.what()); - PyErr_SetString(terminalError, e.what()); - - } - - - return 0; - -} - -static void terminal_dealloc(pw3270_TerminalObject * self) { - - trace("%s",__FUNCTION__); - - delete self->session; +static PyMethodDef terminal_methods[] = { - self->ob_type->tp_free((PyObject*)self); + { "Version", terminal_get_version, METH_NOARGS, "Get the lib3270 version string." }, + { "Revision", terminal_get_revision, METH_NOARGS, "Get the lib3270 revision number." }, -} + { "IsConnected", terminal_is_connected, METH_NOARGS, "True if the terminal is connected to the host." }, + { "IsReady", terminal_is_ready, METH_NOARGS, "True if the terminal has finished network activity." }, -static PyMethodDef terminal_methods[] = { + { "Connect", terminal_connect, METH_VARARGS, "Connect to the host." }, + { "Disconnect", terminal_disconnect, METH_NOARGS, "Disconnect from host." }, {NULL} // Sentinel @@ -155,7 +116,7 @@ static PyTypeObject pw3270_TerminalType = { static PyMethodDef MyMethods[] = { - { "revision", get_revision, METH_VARARGS, "Get module revision." }, + { "Revision", get_revision, METH_VARARGS, "Get module revision." }, {NULL, NULL, 0, NULL} /* Sentinel */ @@ -181,6 +142,6 @@ PyMODINIT_FUNC initpy3270(void) { return (void) Py_INCREF(&pw3270_TerminalType); - PyModule_AddObject(m, "terminal", (PyObject *)&pw3270_TerminalType); + PyModule_AddObject(m, "Terminal", (PyObject *)&pw3270_TerminalType); } diff --git a/src/python/sample.py b/src/python/sample.py index 4d00bc0..357ee73 100644 --- a/src/python/sample.py +++ b/src/python/sample.py @@ -5,8 +5,19 @@ import py3270 print "Teste extensão pw3270" -print py3270.revision() +print py3270.Revision() + +term = py3270.Terminal("") + +print "Using pw3270 version " + term.Version() + " revision " + term.Revision() + +term.Connect("tn3270://zos.efglobe.com:telnet",10); + +print term.IsConnected() +print term.IsReady() + + + -term = py3270.terminal("") -- libgit2 0.21.2