Commit d60dc7718ec586280778f0b6f0dec565b5bb2757

Authored by Perry Werneck
1 parent 7fbd26b0

Implementando métodos python.

src/python/get.cc 0 → 100644
... ... @@ -0,0 +1,63 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como get.cc e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + * Referências:
  29 + *
  30 + * <https://docs.python.org/2/extending/newtypes.html>
  31 + * <https://docs.python.org/2.7/extending/extending.html#a-simple-example>
  32 + *
  33 + */
  34 +
  35 + #include "private.h"
  36 +
  37 +
  38 +/*---[ Implement ]----------------------------------------------------------------------------------*/
  39 +
  40 +PyObject * terminal_get_version(PyObject *self, PyObject *args) {
  41 +
  42 + return PyString_FromString( ((pw3270_TerminalObject *) self)->session->get_version().c_str() );
  43 +
  44 +}
  45 +
  46 +PyObject * terminal_get_revision(PyObject *self, PyObject *args) {
  47 +
  48 + return PyString_FromString( ((pw3270_TerminalObject *) self)->session->get_revision().c_str() );
  49 +
  50 +}
  51 +
  52 +PyObject * terminal_is_connected(PyObject *self, PyObject *args) {
  53 +
  54 + return PyBool_FromLong( ((pw3270_TerminalObject *) self)->session->is_connected() );
  55 +
  56 +}
  57 +
  58 +PyObject * terminal_is_ready(PyObject *self, PyObject *args) {
  59 +
  60 + return PyBool_FromLong( ((pw3270_TerminalObject *) self)->session->is_ready() );
  61 +
  62 +}
  63 +
... ...
src/python/init.cc 0 → 100644
... ... @@ -0,0 +1,88 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como py3270.cc e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + * Implementa métodos básicos inicio/final do objeto python
  29 + *
  30 + * Referências:
  31 + *
  32 + * <https://docs.python.org/2/extending/newtypes.html>
  33 + * <https://docs.python.org/2.7/extending/extending.html#a-simple-example>
  34 + *
  35 + */
  36 +
  37 + #include "private.h"
  38 +
  39 +
  40 +/*---[ Implement ]----------------------------------------------------------------------------------*/
  41 +
  42 +PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
  43 +
  44 + pw3270_TerminalObject *self = (pw3270_TerminalObject *) type->tp_alloc(type, 0);
  45 +
  46 + trace("%s: self=%p",__FUNCTION__,self);
  47 +
  48 + self->session = NULL;
  49 +
  50 + return (PyObject *)self;
  51 +}
  52 +
  53 +
  54 +int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds) {
  55 +
  56 + const char *id = "";
  57 +
  58 + if (!PyArg_ParseTuple(args, "s", &id)) {
  59 + id = "";
  60 + }
  61 +
  62 + trace("%s(%s)",__FUNCTION__,id);
  63 +
  64 + try {
  65 +
  66 + self->session = PW3270_NAMESPACE::session::create(id);
  67 +
  68 + } catch(std::exception &e) {
  69 +
  70 + trace("%s failed: %s",__FUNCTION__,e.what());
  71 + PyErr_SetString(terminalError, e.what());
  72 +
  73 + }
  74 +
  75 +
  76 + return 0;
  77 +
  78 +}
  79 +
  80 +void terminal_dealloc(pw3270_TerminalObject * self) {
  81 +
  82 + trace("%s",__FUNCTION__);
  83 +
  84 + delete self->session;
  85 +
  86 + self->ob_type->tp_free((PyObject*)self);
  87 +
  88 +}
... ...
src/python/misc.cc 0 → 100644
... ... @@ -0,0 +1,83 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como misc.cc e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + * Implementa métodos básicos inicio/final do objeto python
  29 + *
  30 + * Referências:
  31 + *
  32 + * <https://docs.python.org/2/extending/newtypes.html>
  33 + * <https://docs.python.org/2.7/extending/extending.html#a-simple-example>
  34 + *
  35 + */
  36 +
  37 + #include "private.h"
  38 +
  39 +
  40 +/*---[ Implement ]----------------------------------------------------------------------------------*/
  41 +
  42 + PyObject * terminal_connect(PyObject *self, PyObject *args) {
  43 +
  44 + int rc = -1;
  45 + int wait = 60;
  46 + const char * host = "";
  47 +
  48 + if (!PyArg_ParseTuple(args, "s|i", &host, &wait)) {
  49 + PyErr_SetString(terminalError, "connect requires a host URL");
  50 + return NULL;
  51 + }
  52 +
  53 + try {
  54 +
  55 + rc = ((pw3270_TerminalObject *) self)->session->connect(host,wait);
  56 +
  57 + } catch(std::exception &e) {
  58 +
  59 + PyErr_SetString(terminalError, e.what());
  60 + return NULL;
  61 + }
  62 +
  63 + return PyLong_FromLong(rc);
  64 +
  65 + }
  66 +
  67 + PyObject * terminal_disconnect(PyObject *self, PyObject *args) {
  68 +
  69 + int rc = -1;
  70 +
  71 + try {
  72 +
  73 + rc = ((pw3270_TerminalObject *) self)->session->disconnect();
  74 +
  75 + } catch(std::exception &e) {
  76 +
  77 + PyErr_SetString(terminalError, e.what());
  78 + return NULL;
  79 + }
  80 +
  81 + return PyLong_FromLong(rc);
  82 +
  83 + }
... ...
src/python/private.h
... ... @@ -18,7 +18,7 @@
18 18 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
19 19 * St, Fifth Floor, Boston, MA 02110-1301 USA
20 20 *
21   - * Este programa está nomeado como main.cc e possui - linhas de código.
  21 + * Este programa está nomeado como private.h e possui - linhas de código.
22 22 *
23 23 * Contatos:
24 24 *
... ... @@ -46,4 +46,21 @@
46 46  
47 47 extern PyObject * terminalError;
48 48  
  49 + extern "C" {
  50 +
  51 + PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
  52 + int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds);
  53 + void terminal_dealloc(pw3270_TerminalObject * self);
  54 +
  55 + PyObject * terminal_get_version(PyObject *self, PyObject *args);
  56 + PyObject * terminal_get_revision(PyObject *self, PyObject *args);
  57 +
  58 + PyObject * terminal_is_connected(PyObject *self, PyObject *args);
  59 + PyObject * terminal_is_ready(PyObject *self, PyObject *args);
  60 +
  61 + PyObject * terminal_connect(PyObject *self, PyObject *args);
  62 + PyObject * terminal_disconnect(PyObject *self, PyObject *args);
  63 +
  64 + }
  65 +
49 66 #endif // PRIVATE_H_INCLUDED
... ...
src/python/py3270.cbp
... ... @@ -53,6 +53,9 @@
53 53 <Unit filename="../classlib/session.cc" />
54 54 <Unit filename="../include/lib3270/config.h" />
55 55 <Unit filename="../include/pw3270/class.h" />
  56 + <Unit filename="get.cc" />
  57 + <Unit filename="init.cc" />
  58 + <Unit filename="misc.cc" />
56 59 <Unit filename="private.h" />
57 60 <Unit filename="py3270.cc" />
58 61 <Unit filename="sample.py" />
... ...
src/python/py3270.cc
... ... @@ -18,7 +18,7 @@
18 18 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
19 19 * St, Fifth Floor, Boston, MA 02110-1301 USA
20 20 *
21   - * Este programa está nomeado como main.cc e possui - linhas de código.
  21 + * Este programa está nomeado como py3270.cc e possui - linhas de código.
22 22 *
23 23 * Contatos:
24 24 *
... ... @@ -48,55 +48,16 @@ static PyObject * get_revision(PyObject *self, PyObject *args) {
48 48  
49 49 }
50 50  
51   -static PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
52   -
53   - pw3270_TerminalObject *self = (pw3270_TerminalObject *) type->tp_alloc(type, 0);
54   -
55   - trace("%s: self=%p",__FUNCTION__,self);
56   -
57   - self->session = NULL;
58   -
59   - return (PyObject *)self;
60   -}
61   -
62   -
63   -static int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds) {
64   -
65   - const char *id = "";
66   -
67   - if (!PyArg_ParseTuple(args, "s", &id)) {
68   - id = "";
69   - }
70   -
71   - trace("%s(%s)",__FUNCTION__,id);
72   -
73   - try {
74   -
75   - self->session = PW3270_NAMESPACE::session::create(id);
76   -
77   - } catch(std::exception &e) {
78   -
79   - trace("%s failed: %s",__FUNCTION__,e.what());
80   - PyErr_SetString(terminalError, e.what());
81   -
82   - }
83   -
84   -
85   - return 0;
86   -
87   -}
88   -
89   -static void terminal_dealloc(pw3270_TerminalObject * self) {
90   -
91   - trace("%s",__FUNCTION__);
92   -
93   - delete self->session;
  51 +static PyMethodDef terminal_methods[] = {
94 52  
95   - self->ob_type->tp_free((PyObject*)self);
  53 + { "Version", terminal_get_version, METH_NOARGS, "Get the lib3270 version string." },
  54 + { "Revision", terminal_get_revision, METH_NOARGS, "Get the lib3270 revision number." },
96 55  
97   -}
  56 + { "IsConnected", terminal_is_connected, METH_NOARGS, "True if the terminal is connected to the host." },
  57 + { "IsReady", terminal_is_ready, METH_NOARGS, "True if the terminal has finished network activity." },
98 58  
99   -static PyMethodDef terminal_methods[] = {
  59 + { "Connect", terminal_connect, METH_VARARGS, "Connect to the host." },
  60 + { "Disconnect", terminal_disconnect, METH_NOARGS, "Disconnect from host." },
100 61  
101 62 {NULL} // Sentinel
102 63  
... ... @@ -155,7 +116,7 @@ static PyTypeObject pw3270_TerminalType = {
155 116  
156 117 static PyMethodDef MyMethods[] = {
157 118  
158   - { "revision", get_revision, METH_VARARGS, "Get module revision." },
  119 + { "Revision", get_revision, METH_VARARGS, "Get module revision." },
159 120  
160 121 {NULL, NULL, 0, NULL} /* Sentinel */
161 122  
... ... @@ -181,6 +142,6 @@ PyMODINIT_FUNC initpy3270(void) {
181 142 return
182 143  
183 144 (void) Py_INCREF(&pw3270_TerminalType);
184   - PyModule_AddObject(m, "terminal", (PyObject *)&pw3270_TerminalType);
  145 + PyModule_AddObject(m, "Terminal", (PyObject *)&pw3270_TerminalType);
185 146  
186 147 }
... ...
src/python/sample.py
... ... @@ -5,8 +5,19 @@ import py3270
5 5  
6 6 print "Teste extensão pw3270"
7 7  
8   -print py3270.revision()
  8 +print py3270.Revision()
  9 +
  10 +term = py3270.Terminal("")
  11 +
  12 +print "Using pw3270 version " + term.Version() + " revision " + term.Revision()
  13 +
  14 +term.Connect("tn3270://zos.efglobe.com:telnet",10);
  15 +
  16 +print term.IsConnected()
  17 +print term.IsReady()
  18 +
  19 +
  20 +
9 21  
10   -term = py3270.terminal("")
11 22  
12 23  
... ...