Commit 3ef7dede0bcb424c33e755742ad77503aa9539e0

Authored by Perry Werneck
1 parent d9638802
Exists in master

Adding "set attribute" methods.

@@ -60,6 +60,7 @@ @@ -60,6 +60,7 @@
60 <Unit filename="src/session/attributes.cc" /> 60 <Unit filename="src/session/attributes.cc" />
61 <Unit filename="src/session/get.cc" /> 61 <Unit filename="src/session/get.cc" />
62 <Unit filename="src/session/init.cc" /> 62 <Unit filename="src/session/init.cc" />
  63 + <Unit filename="src/session/misc.cc" />
63 <Unit filename="src/session/network.cc" /> 64 <Unit filename="src/session/network.cc" />
64 <Unit filename="src/session/set.cc" /> 65 <Unit filename="src/session/set.cc" />
65 <Unit filename="src/session/tools.cc" /> 66 <Unit filename="src/session/tools.cc" />
src/include/py3270.h
@@ -116,6 +116,8 @@ @@ -116,6 +116,8 @@
116 DLL_PRIVATE PyTypeObject py3270_session_type; 116 DLL_PRIVATE PyTypeObject py3270_session_type;
117 DLL_PRIVATE PyTypeObject py3270_action_type; 117 DLL_PRIVATE PyTypeObject py3270_action_type;
118 118
  119 + DLL_PRIVATE const PyGetSetDef py3270_session_attributes[];
  120 +
119 DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args); 121 DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args);
120 DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args); 122 DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args);
121 123
@@ -133,12 +135,17 @@ @@ -133,12 +135,17 @@
133 DLL_PRIVATE PyObject * py3270_session_getter(PyObject *self, void *name); 135 DLL_PRIVATE PyObject * py3270_session_getter(PyObject *self, void *name);
134 DLL_PRIVATE int py3270_session_setter(PyObject *self, PyObject *value, void *name); 136 DLL_PRIVATE int py3270_session_setter(PyObject *self, PyObject *value, void *name);
135 137
  138 + DLL_PRIVATE PyObject * py3270_session_get_timeout(PyObject *self, void *dunno);
  139 + DLL_PRIVATE int py3270_session_set_timeout(PyObject *self, PyObject *value, void *dunno);
  140 +
136 DLL_PRIVATE PyObject * py3270_session_connect(PyObject *self, PyObject *args); 141 DLL_PRIVATE PyObject * py3270_session_connect(PyObject *self, PyObject *args);
137 142
138 DLL_PRIVATE PyObject * py3270_session_get(PyObject *self, PyObject *args); 143 DLL_PRIVATE PyObject * py3270_session_get(PyObject *self, PyObject *args);
139 DLL_PRIVATE PyObject * py3270_session_set(PyObject *self, PyObject *args); 144 DLL_PRIVATE PyObject * py3270_session_set(PyObject *self, PyObject *args);
140 DLL_PRIVATE PyObject * py3270_session_str(PyObject *self); 145 DLL_PRIVATE PyObject * py3270_session_str(PyObject *self);
141 DLL_PRIVATE PyObject * py3270_session_wait(PyObject *self, PyObject *args); 146 DLL_PRIVATE PyObject * py3270_session_wait(PyObject *self, PyObject *args);
  147 + DLL_PRIVATE PyObject * py3270_session_find(PyObject *self, PyObject *args);
  148 + DLL_PRIVATE PyObject * py3270_session_count(PyObject *self, PyObject *args);
142 149
143 // Action object 150 // Action object
144 DLL_PRIVATE PyObject * py3270_action_new_from_session(PyObject *session, void *action); 151 DLL_PRIVATE PyObject * py3270_action_new_from_session(PyObject *session, void *action);
src/session/attributes.cc
@@ -87,6 +87,39 @@ PyObject * py3270_session_getter(PyObject *self, void *name) { @@ -87,6 +87,39 @@ PyObject * py3270_session_getter(PyObject *self, void *name) {
87 87
88 int py3270_session_setter(PyObject *self, PyObject *value, void *name) { 88 int py3270_session_setter(PyObject *self, PyObject *value, void *name) {
89 89
  90 + try {
  91 +
  92 + auto attribute = ((pySession * ) self)->host->getAttribute((const char *) name);
  93 +
  94 + if(PyLong_Check(value)) {
  95 +
  96 + // Is a long, use PyLong_AsUnsignedLong
  97 + attribute = (int) PyLong_AsUnsignedLong(value);
  98 +
  99 + } else if(PyBool_Check(value)) {
  100 +
  101 + // Is a boolean, use PyLong_AsUnsignedLong != 0
  102 + attribute = (bool) (PyLong_AsUnsignedLong(value) != 0);
  103 +
  104 + } else if(PyUnicode_Check(value)) {
  105 +
  106 + // Is a unicode string
  107 + attribute = (const char *) PyUnicode_AsUTF8AndSize(value,NULL);
  108 +
  109 + }
  110 +
  111 + } catch(const exception &e) {
  112 +
  113 + PyErr_SetString(PyExc_RuntimeError, e.what());
  114 + return -1;
  115 +
  116 + } catch( ... ) {
  117 +
  118 + PyErr_SetString(PyExc_RuntimeError, "Unexpected error setting timeout");
  119 + return -1;
  120 +
  121 + }
  122 +
90 return 0; 123 return 0;
91 } 124 }
92 125
src/session/get.cc
@@ -93,3 +93,12 @@ @@ -93,3 +93,12 @@
93 93
94 } 94 }
95 95
  96 + PyObject * py3270_session_get_timeout(PyObject *self, void *dunno) {
  97 +
  98 + return py3270_session_call(self, [](TN3270::Host &host){
  99 +
  100 + return PyLong_FromLong(host.getTimeout());
  101 +
  102 + });
  103 +
  104 + }
src/session/init.cc
@@ -63,14 +63,25 @@ void py3270_session_type_init(PyTypeObject *type) { @@ -63,14 +63,25 @@ void py3270_session_type_init(PyTypeObject *type) {
63 auto actions = TN3270::getActions(); 63 auto actions = TN3270::getActions();
64 size_t ix = 0; 64 size_t ix = 0;
65 65
  66 + // Compute block size
66 size_t szData = sizeof(struct PyGetSetDef) * (attributes.size() + actions.size() + 1); 67 size_t szData = sizeof(struct PyGetSetDef) * (attributes.size() + actions.size() + 1);
67 68
  69 + for(size_t i = 0; py3270_session_attributes[i].name; i++) {
  70 + szData += sizeof(struct PyGetSetDef);
  71 + }
  72 +
  73 + // Allocate and clean
68 type->tp_getset = (struct PyGetSetDef *) malloc(szData); 74 type->tp_getset = (struct PyGetSetDef *) malloc(szData);
69 memset(type->tp_getset,0,szData); 75 memset(type->tp_getset,0,szData);
70 76
71 - for(auto attribute : attributes) { 77 + // Copy internal attributes
  78 + for(size_t i = 0; py3270_session_attributes[i].name; i++) {
  79 + type->tp_getset[ix] = py3270_session_attributes[i];
  80 + ix++;
  81 + }
72 82
73 -// debug("Creating attribute %s",attribute->name); 83 + // Copy lib3270's attributes
  84 + for(auto attribute : attributes) {
74 85
75 py3270_session_attribute_init(&type->tp_getset[ix], (const LIB3270_PROPERTY *) attribute); 86 py3270_session_attribute_init(&type->tp_getset[ix], (const LIB3270_PROPERTY *) attribute);
76 87
src/session/misc.cc 0 → 100644
@@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
  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_find(PyObject *self, PyObject *args) {
  40 +
  41 + return py3270_session_call(self, [self, args](TN3270::Host &host){
  42 +
  43 + const char *text;
  44 +
  45 + if(!PyArg_ParseTuple(args, "s", &text))
  46 + throw std::system_error(EINVAL, std::system_category());
  47 +
  48 + return PyLong_FromLong(host.find(text));
  49 +
  50 + });
  51 +
  52 + }
  53 +
  54 + PyObject * py3270_session_count(PyObject *self, PyObject *args) {
  55 +
  56 + return py3270_session_call(self, [self, args](TN3270::Host &host){
  57 +
  58 + const char *text;
  59 +
  60 + if(!PyArg_ParseTuple(args, "s", &text))
  61 + throw std::system_error(EINVAL, std::system_category());
  62 +
  63 + return PyLong_FromLong(host.count(text));
  64 +
  65 + });
  66 +
  67 + }
src/session/set.cc
@@ -89,53 +89,35 @@ @@ -89,53 +89,35 @@
89 89
90 } 90 }
91 91
92 -  
93 -/*  
94 - PyObject * terminal_set_string_at(PyObject *self, PyObject *args) {  
95 -  
96 - int row, col, rc;  
97 - const char *text;  
98 -  
99 - if (!PyArg_ParseTuple(args, "iis", &row, &col, &text)) {  
100 - PyErr_SetString(terminalError, strerror(EINVAL));  
101 - return NULL;  
102 - } 92 + int py3270_session_set_timeout(PyObject *self, PyObject *value, void *dunno) {
103 93
104 try { 94 try {
105 95
106 - rc = ((pw3270_TerminalObject *) self)->session->set_string_at(row,col,text);  
107 -  
108 - } catch(std::exception &e) { 96 + auto host = ((pySession * ) self)->host;
109 97
110 - PyErr_SetString(terminalError, e.what());  
111 - return NULL;  
112 - } 98 + if(PyLong_Check(value)) {
113 99
114 - return PyLong_FromLong(rc); 100 + host->setTimeout( (time_t) PyLong_AsUnsignedLong(value));
115 101
116 - } 102 + } else {
117 103
118 - PyObject * terminal_set_cursor_at(PyObject *self, PyObject *args) { 104 + throw std::system_error(EINVAL, std::system_category());
119 105
120 - int row, col, rc; 106 + }
121 107
122 - if (!PyArg_ParseTuple(args, "ii", &row, &col)) {  
123 - PyErr_SetString(terminalError, strerror(EINVAL));  
124 - return NULL;  
125 - } 108 + } catch(const exception &e) {
126 109
127 - try { 110 + PyErr_SetString(PyExc_RuntimeError, e.what());
  111 + return -1;
128 112
129 - rc = ((pw3270_TerminalObject *) self)->session->set_cursor_position(row,col); 113 + } catch( ... ) {
130 114
131 - } catch(std::exception &e) { 115 + PyErr_SetString(PyExc_RuntimeError, "Unexpected error setting timeout");
  116 + return -1;
132 117
133 - PyErr_SetString(terminalError, e.what());  
134 - return NULL;  
135 } 118 }
136 119
137 - return PyLong_FromLong(rc); 120 + return 0;
138 121
139 } 122 }
140 123
141 -*/  
src/session/type.c
@@ -66,10 +66,39 @@ static PyMethodDef py3270_session_methods[] = { @@ -66,10 +66,39 @@ static PyMethodDef py3270_session_methods[] = {
66 }, 66 },
67 67
68 { 68 {
  69 + "find",
  70 + (PyCFunction) py3270_session_find,
  71 + METH_VARARGS,
  72 + ""
  73 + },
  74 +
  75 + {
  76 + "count",
  77 + (PyCFunction) py3270_session_count,
  78 + METH_VARARGS,
  79 + ""
  80 + },
  81 +
  82 + {
69 NULL 83 NULL
70 } 84 }
71 }; 85 };
72 86
  87 +
  88 +const struct PyGetSetDef py3270_session_attributes[] = {
  89 +
  90 + {
  91 + .name = "timeout",
  92 + .doc = "Timeout (in seconds) for host access",
  93 + .get = py3270_session_get_timeout,
  94 + .set = py3270_session_set_timeout
  95 + },
  96 +
  97 + {
  98 + .name = NULL
  99 + }
  100 +};
  101 +
73 // https://docs.python.org/3/c-api/typeobj.html 102 // https://docs.python.org/3/c-api/typeobj.html
74 PyTypeObject py3270_session_type = { 103 PyTypeObject py3270_session_type = {
75 104
src/session/wait.cc
@@ -87,7 +87,6 @@ @@ -87,7 +87,6 @@
87 87
88 } 88 }
89 89
90 -  
91 PyObject * py3270_session_wait(PyObject *self, PyObject *args) { 90 PyObject * py3270_session_wait(PyObject *self, PyObject *args) {
92 91
93 return py3270_session_call(self, [self, args](TN3270::Host &host){ 92 return py3270_session_call(self, [self, args](TN3270::Host &host){
@@ -100,4 +99,3 @@ @@ -100,4 +99,3 @@
100 }); 99 });
101 100
102 } 101 }
103 -  
testprograms/sample.py
@@ -7,25 +7,32 @@ import tn3270 @@ -7,25 +7,32 @@ import tn3270
7 print("Using TN3270 Version " + tn3270.version()) 7 print("Using TN3270 Version " + tn3270.version())
8 print(tn3270.revision()) 8 print(tn3270.revision())
9 9
10 -session = tn3270.Session(":a") 10 +session = tn3270.Session("")
  11 +session.timeout = 10
11 12
12 print("Using tn3270 version " + session.version + " revision " + session.revision) 13 print("Using tn3270 version " + session.version + " revision " + session.revision)
13 14
  15 +print(session.timeout)
  16 +
14 #print(session.cstate) 17 #print(session.cstate)
15 #print(session.width) 18 #print(session.width)
16 #print(session.connected) 19 #print(session.connected)
17 -#print(session.url) 20 +print(session.url)
  21 +session.url = "http://www.google.com"
  22 +print(session.url)
18 23
19 -print(session.reconnect) 24 +#print(session.reconnect)
20 25
21 # 26 #
22 # Can reconnect? If yes do it! 27 # Can reconnect? If yes do it!
23 # 28 #
24 -if session.reconnect.activatable:  
25 - print("Reconnecting...")  
26 - session.reconnect().wait(10) 29 +#if session.reconnect.activatable:
  30 +# print("Reconnecting...")
  31 +# session.reconnect().wait(10)
27 32
28 -print(session.connected) 33 +#print(session.connected)
  34 +#print(session.find('sisbb'))
  35 +#print(session.count('sisbb'))
29 36
30 #print('----------------------') 37 #print('----------------------')
31 #print(dir(session)) 38 #print(dir(session))
@@ -33,17 +40,17 @@ print(session.connected) @@ -33,17 +40,17 @@ print(session.connected)
33 40
34 #print(session.get(14,22,38)) 41 #print(session.get(14,22,38))
35 42
36 -print("-----------------------------------------------------------------------")  
37 -print(session)  
38 -print("-----------------------------------------------------------------------") 43 +#print("-----------------------------------------------------------------------")
  44 +#print(session)
  45 +#print("-----------------------------------------------------------------------")
39 46
40 -session.enter().wait(14,2,"Senha") 47 +#session.enter().wait(14,2,"Senha")
41 48
42 -session.set("value") 49 +#session.set("value")
43 50
44 -print("-----------------------------------------------------------------------")  
45 -print(session)  
46 -print("-----------------------------------------------------------------------") 51 +#print("-----------------------------------------------------------------------")
  52 +#print(session)
  53 +#print("-----------------------------------------------------------------------")
47 54
48 input("Press enter to exit") 55 input("Press enter to exit")
49 56