Commit 090ddd31c871d7ba5076e7f0b2c6f555b7983752

Authored by Perry Werneck
1 parent 98721dff
Exists in master

Implementing python action object.

py3270.cbp
... ... @@ -43,6 +43,7 @@
43 43 <Add library="pw3270cpp" />
44 44 </Linker>
45 45 <Unit filename="src/action/init.cc" />
  46 + <Unit filename="src/action/methods.cc" />
46 47 <Unit filename="src/action/new.cc" />
47 48 <Unit filename="src/action/tools.cc" />
48 49 <Unit filename="src/action/type.c">
... ... @@ -65,6 +66,7 @@
65 66 <Unit filename="src/session/type.c">
66 67 <Option compilerVar="CC" />
67 68 </Unit>
  69 + <Unit filename="src/session/wait.cc" />
68 70 <Extensions>
69 71 <code_completion />
70 72 <envvars />
... ...
src/action/init.cc
... ... @@ -45,33 +45,4 @@ void py3270_action_type_init(PyTypeObject *type) {
45 45  
46 46 }
47 47  
48   -static void cleanup(pyAction * self) {
49   -
50   - if(self->action) {
51   - delete self->action;
52   - self->action = nullptr;
53   - }
54   -
55   -}
56   -
57   -void py3270_action_dealloc(PyObject * self) {
58   - debug("%s",__FUNCTION__);
59   - cleanup((pyAction *) self);
60   -}
61   -
62   -void py3270_action_finalize(PyObject *self) {
63   - debug("%s",__FUNCTION__);
64   - cleanup((pyAction *) self);
65   -}
66   -
67   -PyObject * py3270_action_activatable(PyObject *self, PyObject *args) {
68   -
69   - return py3270_action_call(self, [](TN3270::Action &action) {
70   -
71   - return PyBool_FromLong(action.activatable());
72   -
73   - });
74   -
75   -
76   -}
77 48  
... ...
src/action/methods.cc 0 → 100644
... ... @@ -0,0 +1,132 @@
  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 <py3270.h>
  38 + #include <lib3270/ipc.h>
  39 + #include <lib3270/ipc/action.h>
  40 +
  41 +/*---[ Implement ]----------------------------------------------------------------------------------*/
  42 +
  43 +PyObject * py3270_action_call(PyObject *self, PyObject *args, PyObject *kwargs) {
  44 +
  45 + return py3270_action_activate(self,args);
  46 +
  47 +}
  48 +
  49 +PyObject * py3270_action_describe(PyObject *self, PyObject *obj, PyObject *type) {
  50 +
  51 + return py3270_action_call(self, [](TN3270::Action &action) {
  52 +
  53 + return (PyObject *) PyUnicode_FromString(action.getDescription());
  54 +
  55 + });
  56 +
  57 +}
  58 +
  59 +PyObject * py3270_action_activate(PyObject *self, PyObject *args) {
  60 +
  61 + return py3270_action_call(self, [args, self](TN3270::Action &action) {
  62 +
  63 + if(PyTuple_Size(args) == 1) {
  64 +
  65 + unsigned int seconds;
  66 +
  67 + if (!PyArg_ParseTuple(args, "I", &seconds))
  68 + return (PyObject *) NULL;
  69 +
  70 + action.activate();
  71 + action.wait(seconds);
  72 +
  73 + } else {
  74 +
  75 + action.activate();
  76 +
  77 + }
  78 +
  79 + Py_INCREF(self);
  80 + return self;
  81 +
  82 + });
  83 +
  84 +}
  85 +
  86 +PyObject * py3270_action_get_activatable(PyObject *self, void *dunno) {
  87 +
  88 + return py3270_action_call(self, [](TN3270::Action &action) {
  89 +
  90 + return PyBool_FromLong(action.activatable());
  91 +
  92 + });
  93 +
  94 +}
  95 +
  96 +DLL_PRIVATE PyObject * py3270_action_wait(PyObject *self, PyObject *args) {
  97 +
  98 + return py3270_action_call(self, [args, self](TN3270::Action &action) {
  99 +
  100 + switch(PyTuple_Size(args)) {
  101 + case 0:
  102 + action.wait();
  103 + break;
  104 +
  105 + case 1:
  106 + {
  107 + unsigned int seconds;
  108 +
  109 + if (!PyArg_ParseTuple(args, "I", &seconds))
  110 + return (PyObject *) NULL;
  111 +
  112 + action.wait(seconds);
  113 +
  114 + }
  115 + break;
  116 +
  117 + default:
  118 + throw std::system_error(EINVAL, std::system_category());
  119 +
  120 + }
  121 +
  122 + debug("%s: ob_refcnt@%p=%ld",__FUNCTION__,self,self->ob_refcnt);
  123 + Py_INCREF(self);
  124 +
  125 + return self;
  126 +
  127 + });
  128 +
  129 +}
  130 +
  131 +
  132 +
... ...
src/action/new.cc
... ... @@ -29,8 +29,8 @@
29 29 *
30 30 * Referências:
31 31 *
32   - * <https://docs.python.org/2/extending/newtypes.html>
33   - * <https://docs.python.org/2.7/extending/extending.html#a-simple-example>
  32 + * <https://pythonextensionpatterns.readthedocs.io/en/latest/refcount.html>
  33 + *
34 34 *
35 35 */
36 36  
... ... @@ -41,10 +41,29 @@
41 41  
42 42 DLL_PRIVATE PyObject * py3270_action_new_from_session(PyObject *session, void *action) {
43 43  
44   - pyAction * object = (pyAction *) _PyObject_New(&py3270_action_type);
  44 + pyAction * pObj = (pyAction *) _PyObject_New(&py3270_action_type);
  45 +
  46 + pObj->session = session;
  47 + pObj->action = ((pySession *) session)->host->getAction((const LIB3270_ACTION *) action);
  48 +
  49 + debug("%s: ob_refcnt@%p=%ld",__FUNCTION__,pObj,((PyObject *) pObj)->ob_refcnt);
45 50  
46   - object->action = ((pySession *) session)->host->getAction((const LIB3270_ACTION *) action);
  51 + Py_INCREF(pObj->session);
47 52  
48   - return (PyObject *) object;
  53 + return (PyObject *) pObj;
49 54  
50 55 }
  56 +
  57 +void py3270_action_dealloc(PyObject * self) {
  58 +
  59 + pyAction * pObj = (pyAction *) self;
  60 +
  61 + debug("%s: %p",__FUNCTION__,self);
  62 +
  63 + Py_DECREF(pObj->session);
  64 +
  65 + delete pObj->action;
  66 + pObj->action = nullptr;
  67 +
  68 +}
  69 +
... ...
src/action/tools.cc
... ... @@ -42,8 +42,7 @@ PyObject * py3270_action_call(PyObject *self, std::function&lt;PyObject * (TN3270::
42 42  
43 43 try {
44 44  
45   - TN3270::Action *action = ((pyAction * ) self)->action;
46   - return worker(*action);
  45 + return worker(* (((pyAction * ) self)->action));
47 46  
48 47 } catch(const exception &e) {
49 48  
... ... @@ -51,7 +50,7 @@ PyObject * py3270_action_call(PyObject *self, std::function&lt;PyObject * (TN3270::
51 50  
52 51 } catch( ... ) {
53 52  
54   - PyErr_SetString(PyExc_RuntimeError, "Unexpected error in action object");
  53 + PyErr_SetString(PyExc_RuntimeError, "Unexpected error in action method");
55 54  
56 55 }
57 56  
... ... @@ -59,3 +58,4 @@ PyObject * py3270_action_call(PyObject *self, std::function&lt;PyObject * (TN3270::
59 58  
60 59 }
61 60  
  61 +
... ...
src/action/type.c
... ... @@ -39,17 +39,37 @@
39 39 static PyMethodDef py3270_action_methods[] = {
40 40  
41 41 {
42   - "activatable",
43   - (PyCFunction) py3270_action_activatable,
  42 + "activate",
  43 + (PyCFunction) py3270_action_activate,
44 44 METH_NOARGS,
45 45 ""
46 46 },
47 47  
48 48 {
  49 + "wait",
  50 + (PyCFunction) py3270_action_wait,
  51 + METH_VARARGS,
  52 + ""
  53 + },
  54 +
  55 + {
49 56 NULL
50 57 }
51 58 };
52 59  
  60 +static struct PyGetSetDef py3270_action_attributes[] = {
  61 +
  62 + {
  63 + .name = "activatable",
  64 + .doc = "True if the action can be activated",
  65 + .get = py3270_action_get_activatable
  66 + },
  67 +
  68 + {
  69 + NULL
  70 + }
  71 +};
  72 +
53 73 // https://docs.python.org/3/c-api/typeobj.html
54 74 PyTypeObject py3270_action_type = {
55 75  
... ... @@ -61,10 +81,13 @@ PyTypeObject py3270_action_type = {
61 81 .tp_itemsize = 0,
62 82 .tp_flags = Py_TPFLAGS_DEFAULT,
63 83  
  84 + .tp_call = py3270_action_call,
  85 + .tp_descr_get = py3270_action_describe,
  86 +
64 87 .tp_methods = py3270_action_methods,
  88 + .tp_getset = py3270_action_attributes,
65 89  
66 90 .tp_dealloc = py3270_action_dealloc,
67   - .tp_finalize = py3270_action_finalize,
68 91  
69 92  
70 93 };
... ...
src/include/py3270.h
... ... @@ -106,7 +106,8 @@
106 106  
107 107 typedef struct {
108 108 PyObject_HEAD
109   - Action *action;
  109 + PyObject * session;
  110 + Action * action;
110 111 } pyAction;
111 112  
112 113 DLL_PRIVATE PyTypeObject py3270_session_type;
... ... @@ -131,19 +132,22 @@
131 132 DLL_PRIVATE int py3270_session_setter(PyObject *self, PyObject *value, void *name);
132 133  
133 134 DLL_PRIVATE PyObject * py3270_session_connect(PyObject *self, PyObject *args);
134   - DLL_PRIVATE PyObject * py3270_session_disconnect(PyObject *self, PyObject *args);
135 135  
136 136 DLL_PRIVATE PyObject * py3270_session_get(PyObject *self, PyObject *args);
137 137 DLL_PRIVATE PyObject * py3270_session_set(PyObject *self, PyObject *args);
138 138 DLL_PRIVATE PyObject * py3270_session_str(PyObject *self);
  139 + DLL_PRIVATE PyObject * py3270_session_wait(PyObject *self, PyObject *args);
139 140  
140 141 // Action object
141 142 DLL_PRIVATE PyObject * py3270_action_new_from_session(PyObject *session, void *action);
142   -
143 143 DLL_PRIVATE void py3270_action_dealloc(PyObject * self);
144   - DLL_PRIVATE void py3270_action_finalize(PyObject *self);
145 144  
146   - DLL_PRIVATE PyObject * py3270_action_activatable(PyObject *self, PyObject *args);
  145 + DLL_PRIVATE PyObject * py3270_action_call(PyObject *callable, PyObject *args, PyObject *kwargs);
  146 + DLL_PRIVATE PyObject * py3270_action_describe(PyObject *self, PyObject *obj, PyObject *type);
  147 + DLL_PRIVATE PyObject * py3270_action_activate(PyObject *self, PyObject *args);
  148 + DLL_PRIVATE PyObject * py3270_action_wait(PyObject *self, PyObject *args);
  149 +
  150 + DLL_PRIVATE PyObject * py3270_action_get_activatable(PyObject *self, void *dunno);
147 151  
148 152 /*
149 153  
... ...
src/session/init.cc
... ... @@ -70,7 +70,7 @@ void py3270_session_type_init(PyTypeObject *type) {
70 70  
71 71 for(auto attribute : attributes) {
72 72  
73   - debug("Creating attribute %s",attribute->name);
  73 +// debug("Creating attribute %s",attribute->name);
74 74  
75 75 py3270_session_attribute_init(&type->tp_getset[ix], (const LIB3270_PROPERTY *) attribute);
76 76  
... ... @@ -84,7 +84,7 @@ void py3270_session_type_init(PyTypeObject *type) {
84 84  
85 85 for(auto action : actions) {
86 86  
87   - debug("Creating action %s",action->name);
  87 +// debug("Creating action %s",action->name);
88 88  
89 89 py3270_session_attribute_init(&type->tp_getset[ix], (const LIB3270_PROPERTY *) action);
90 90  
... ...
src/session/network.cc
... ... @@ -56,19 +56,6 @@
56 56  
57 57 }
58 58  
59   - PyObject * py3270_session_disconnect(PyObject *self, PyObject *args) {
60   -
61   - return py3270_session_call(self, [args](TN3270::Host &host){
62   -
63   - host.disconnect();
64   -
65   - return 0;
66   -
67   - });
68   -
69   - }
70   -
71   -
72 59 /*
73 60 PyObject * terminal_connect(PyObject *self, PyObject *args) {
74 61  
... ...
src/session/type.c
... ... @@ -45,9 +45,9 @@ static PyMethodDef py3270_session_methods[] = {
45 45 },
46 46  
47 47 {
48   - "disconnect",
49   - (PyCFunction) py3270_session_disconnect,
50   - METH_NOARGS,
  48 + "wait",
  49 + (PyCFunction) py3270_session_wait,
  50 + METH_VARARGS,
51 51 ""
52 52 },
53 53  
... ...
src/session/wait.cc 0 → 100644
... ... @@ -0,0 +1,119 @@
  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 set.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 <py3270.h>
  36 +
  37 +/*---[ Implement ]----------------------------------------------------------------------------------*/
  38 +
  39 + PyObject * py3270_session_wait(PyObject *self, PyObject *args) {
  40 +
  41 + return py3270_session_call(self, [args](TN3270::Host &host){
  42 +
  43 + switch(PyTuple_Size(args)) {
  44 + case 0: // No time defined, use the default one.
  45 + host.waitForReady();
  46 + break;
  47 +
  48 + case 1: // Has argument, wait for it.
  49 + {
  50 + unsigned int seconds;
  51 +
  52 + if(!PyArg_ParseTuple(args, "I", &seconds))
  53 + return (PyObject *) NULL;
  54 +
  55 + host.waitForReady(seconds);
  56 + }
  57 + break;
  58 +
  59 + default:
  60 + throw std::system_error(EINVAL, std::system_category());
  61 +
  62 + }
  63 +
  64 + return PyLong_FromLong(0);
  65 +
  66 + });
  67 +
  68 + }
  69 +
  70 +
  71 +/*
  72 + PyObject * terminal_set_string_at(PyObject *self, PyObject *args) {
  73 +
  74 + int row, col, rc;
  75 + const char *text;
  76 +
  77 + if (!PyArg_ParseTuple(args, "iis", &row, &col, &text)) {
  78 + PyErr_SetString(terminalError, strerror(EINVAL));
  79 + return NULL;
  80 + }
  81 +
  82 + try {
  83 +
  84 + rc = ((pw3270_TerminalObject *) self)->session->set_string_at(row,col,text);
  85 +
  86 + } catch(std::exception &e) {
  87 +
  88 + PyErr_SetString(terminalError, e.what());
  89 + return NULL;
  90 + }
  91 +
  92 + return PyLong_FromLong(rc);
  93 +
  94 + }
  95 +
  96 + PyObject * terminal_set_cursor_at(PyObject *self, PyObject *args) {
  97 +
  98 + int row, col, rc;
  99 +
  100 + if (!PyArg_ParseTuple(args, "ii", &row, &col)) {
  101 + PyErr_SetString(terminalError, strerror(EINVAL));
  102 + return NULL;
  103 + }
  104 +
  105 + try {
  106 +
  107 + rc = ((pw3270_TerminalObject *) self)->session->set_cursor_position(row,col);
  108 +
  109 + } catch(std::exception &e) {
  110 +
  111 + PyErr_SetString(terminalError, e.what());
  112 + return NULL;
  113 + }
  114 +
  115 + return PyLong_FromLong(rc);
  116 +
  117 + }
  118 +
  119 +*/
... ...
testprograms/sample.py
... ... @@ -11,10 +11,17 @@ session = tn3270.Session(&quot;&quot;)
11 11  
12 12 print("Using tn3270 version " + session.version + " revision " + session.revision)
13 13  
14   -print(session.cstate)
15   -print(session.width)
16   -print(session.connected)
17   -print(session.url)
  14 +#print(session.cstate)
  15 +#print(session.width)
  16 +#print(session.connected)
  17 +#print(session.url)
  18 +
  19 +#
  20 +# Can reconnect? If yes do it!
  21 +#
  22 +if session.reconnect.activatable:
  23 + print("Reconnecting...")
  24 + session.reconnect().wait(10)
18 25  
19 26 #session.connect('')
20 27  
... ... @@ -24,27 +31,21 @@ print(session.connected)
24 31 #print(dir(session))
25 32 #print('----------------------')
26 33  
27   -test = session.reconnect
28   -print('----------------------')
29   -print(dir(test))
30   -print('----------------------')
31   -
32   -print(test.activatable())
  34 +#print(session.get(14,22,38))
33 35  
  36 +print("-----------------------------------------------------------------------")
  37 +print(session)
  38 +print("-----------------------------------------------------------------------")
34 39  
35   -#print(session.get(14,22,38))
  40 +session.enter().wait(2)
36 41  
37   -#print("-----------------------------------------------------------------------")
38   -#print(session)
39   -#print("-----------------------------------------------------------------------")
40 42  
41 43 #session.set("value")
42 44  
43   -#print("-----------------------------------------------------------------------")
44   -#print(session)
45   -#print("-----------------------------------------------------------------------")
  45 +print("-----------------------------------------------------------------------")
  46 +print(session)
  47 +print("-----------------------------------------------------------------------")
46 48  
47   -del session
48 49 input("Press enter to exit")
49 50  
50 51  
... ...