Commit ead2d48eda7ecedf91f6cd98b1254dce4997b056
1 parent
96cee301
Exists in
master
Refactoring for python3.
Showing
9 changed files
with
477 additions
and
155 deletions
Show diff stats
py3270.cbp
... | ... | @@ -49,10 +49,11 @@ |
49 | 49 | <Option compilerVar="CC" /> |
50 | 50 | </Unit> |
51 | 51 | <Unit filename="src/module/properties.cc" /> |
52 | + <Unit filename="src/module/tools.cc" /> | |
52 | 53 | <Unit filename="src/terminal/actions.cc" /> |
53 | 54 | <Unit filename="src/terminal/get.cc" /> |
54 | 55 | <Unit filename="src/terminal/init.cc" /> |
55 | - <Unit filename="src/terminal/misc.cc" /> | |
56 | + <Unit filename="src/terminal/network.cc" /> | |
56 | 57 | <Unit filename="src/terminal/set.cc" /> |
57 | 58 | <Extensions> |
58 | 59 | <code_completion /> | ... | ... |
src/include/py3270.h
... | ... | @@ -59,6 +59,17 @@ |
59 | 59 | |
60 | 60 | #endif |
61 | 61 | |
62 | + #ifdef DEBUG | |
63 | + #include <stdio.h> | |
64 | + #undef trace | |
65 | + #define trace( fmt, ... ) fprintf(stderr, "%s(%d) " fmt "\n", __FILE__, __LINE__, __VA_ARGS__ ); fflush(stderr); | |
66 | + #define debug( fmt, ... ) fprintf(stderr, "%s(%d) " fmt "\n", __FILE__, __LINE__, __VA_ARGS__ ); fflush(stderr); | |
67 | + #else | |
68 | + #undef trace | |
69 | + #define trace(x, ...) // __VA_ARGS__ | |
70 | + #define debug(x, ...) // __VA_ARGS__ | |
71 | + #endif | |
72 | + | |
62 | 73 | #ifdef __cplusplus |
63 | 74 | |
64 | 75 | #include <functional> |
... | ... | @@ -70,6 +81,9 @@ |
70 | 81 | using std::runtime_error; |
71 | 82 | using TN3270::Host; |
72 | 83 | |
84 | + DLL_PRIVATE PyObject * py3270_session_call(PyObject *self, std::function<PyObject * (TN3270::Host &host)> worker) noexcept; | |
85 | + DLL_PRIVATE PyObject * py3270_session_call(PyObject *self, std::function<int (TN3270::Host &host)> worker) noexcept; | |
86 | + | |
73 | 87 | extern "C" { |
74 | 88 | |
75 | 89 | #else |
... | ... | @@ -83,13 +97,20 @@ |
83 | 97 | Host *host; |
84 | 98 | } pySession; |
85 | 99 | |
86 | - DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args); | |
87 | - DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args); | |
100 | + DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args); | |
101 | + DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args); | |
102 | + | |
103 | + DLL_PRIVATE PyObject * py3270_session_alloc(PyTypeObject *type, PyObject *args, PyObject *kwds); | |
104 | + DLL_PRIVATE void py3270_session_dealloc(PyObject * self); | |
105 | + | |
106 | + DLL_PRIVATE int py3270_session_init(PyObject *self, PyObject *args, PyObject *kwds); | |
107 | + DLL_PRIVATE void py3270_session_finalize(PyObject *self); | |
108 | + | |
88 | 109 | |
89 | - DLL_PRIVATE PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | |
90 | -// DLL_PRIVATE int py3270_session_init(pySession *self, PyObject *args, PyObject *kwds); | |
91 | - DLL_PRIVATE void py3270_session_dealloc(pySession * self); | |
110 | +// DLL_PRIVATE PyObject * py3270_session_getattr(PyObject *self, char *attr_name); | |
92 | 111 | |
112 | + DLL_PRIVATE PyObject * py3270_session_connect(PyObject *self, PyObject *args); | |
113 | + DLL_PRIVATE PyObject * py3270_session_disconnect(PyObject *self, PyObject *args); | |
93 | 114 | |
94 | 115 | /* |
95 | 116 | ... | ... |
src/module/init.c
... | ... | @@ -67,15 +67,48 @@ static struct PyModuleDef definition = { |
67 | 67 | .m_methods = methods // Module methods |
68 | 68 | }; |
69 | 69 | |
70 | +//# Tornar essa tabela pública e testar em getattr, se for um método usar PyMethod_New para retornar um método. | |
71 | + | |
72 | +static PyMethodDef py3270_session_methods[] = { | |
73 | + { | |
74 | + "connect", | |
75 | + (PyCFunction) py3270_session_connect, | |
76 | + METH_VARARGS, | |
77 | + "" | |
78 | + }, | |
79 | + | |
80 | + { | |
81 | + "disconnect", | |
82 | + (PyCFunction) py3270_session_disconnect, | |
83 | + METH_NOARGS, | |
84 | + "" | |
85 | + }, | |
86 | + | |
87 | + { | |
88 | + NULL | |
89 | + } | |
90 | +}; | |
91 | + | |
92 | +// https://docs.python.org/3/c-api/typeobj.html | |
70 | 93 | static PyTypeObject SessionType = { |
71 | 94 | PyVarObject_HEAD_INIT(NULL, 0) |
72 | 95 | .tp_name = "tn3270.Session", |
73 | 96 | .tp_doc = "TN3270 Session Object", |
74 | 97 | .tp_basicsize = sizeof(pySession), |
75 | 98 | .tp_itemsize = 0, |
76 | - .tp_flags = Py_TPFLAGS_DEFAULT, | |
99 | + .tp_flags = Py_TPFLAGS_HAVE_FINALIZE|Py_TPFLAGS_DEFAULT, | |
100 | + | |
101 | + .tp_new = py3270_session_alloc, | |
77 | 102 | .tp_dealloc = py3270_session_dealloc, |
78 | - .tp_new = py3270_session_new, | |
103 | + | |
104 | + .tp_init = py3270_session_init, | |
105 | + .tp_finalize = py3270_session_finalize, | |
106 | + | |
107 | + .tp_methods = py3270_session_methods, | |
108 | + | |
109 | +// .tp_alloc = | |
110 | +// .tp_free = | |
111 | +// .tp_getattr = py3270_session_getattr | |
79 | 112 | }; |
80 | 113 | |
81 | 114 | /*---[ Implement ]----------------------------------------------------------------------------------*/ | ... | ... |
... | ... | @@ -0,0 +1,82 @@ |
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 <py3270.h> | |
38 | + | |
39 | +/*---[ Implement ]----------------------------------------------------------------------------------*/ | |
40 | + | |
41 | +PyObject * py3270_session_call(PyObject *self, std::function<PyObject * (TN3270::Host &host)> worker) noexcept { | |
42 | + | |
43 | + try { | |
44 | + | |
45 | + TN3270::Host *host = ((pySession * ) self)->host; | |
46 | + return worker(*host); | |
47 | + | |
48 | + } catch(const exception &e) { | |
49 | + | |
50 | + PyErr_SetString(PyExc_RuntimeError, e.what()); | |
51 | + | |
52 | + } catch( ... ) { | |
53 | + | |
54 | + PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module"); | |
55 | + | |
56 | + } | |
57 | + | |
58 | + return NULL; | |
59 | + | |
60 | +} | |
61 | + | |
62 | +PyObject * py3270_session_call(PyObject *self, std::function<int (TN3270::Host &host)> worker) noexcept { | |
63 | + | |
64 | + try { | |
65 | + | |
66 | + TN3270::Host *host = ((pySession * ) self)->host; | |
67 | + return PyLong_FromLong(worker(*host)); | |
68 | + | |
69 | + } catch(const exception &e) { | |
70 | + | |
71 | + PyErr_SetString(PyExc_RuntimeError, e.what()); | |
72 | + | |
73 | + } catch( ... ) { | |
74 | + | |
75 | + PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module"); | |
76 | + | |
77 | + } | |
78 | + | |
79 | + return NULL; | |
80 | + | |
81 | +} | |
82 | + | ... | ... |
src/terminal/get.cc
... | ... | @@ -36,6 +36,89 @@ |
36 | 36 | |
37 | 37 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
38 | 38 | |
39 | + /* | |
40 | +DLL_PRIVATE PyObject * py3270_session_getattr(PyObject *self, char *attr_name) { | |
41 | + | |
42 | + PyObject * rc = NULL; | |
43 | + | |
44 | + printf("\n\n*************%s(%s)\n\n",__FUNCTION__,attr_name); | |
45 | + | |
46 | + try { | |
47 | + | |
48 | + TN3270::Property * property = ((pySession * ) self)->host->getProperty(attr_name); | |
49 | + | |
50 | + try { | |
51 | + | |
52 | + switch(property->getType()) { | |
53 | + case TN3270::Property::String: | |
54 | + rc = PyUnicode_FromString(property->toString().c_str()); | |
55 | + break; | |
56 | + | |
57 | + case TN3270::Property::Boolean: | |
58 | + rc = PyBool_FromLong(property->toBool()); | |
59 | + break; | |
60 | + | |
61 | + case TN3270::Property::Uchar: | |
62 | + throw std::system_error(ENOTSUP, std::system_category()); | |
63 | + break; | |
64 | + | |
65 | + case TN3270::Property::Int16: | |
66 | + throw std::system_error(ENOTSUP, std::system_category()); | |
67 | + break; | |
68 | + | |
69 | + case TN3270::Property::Uint16: | |
70 | + throw std::system_error(ENOTSUP, std::system_category()); | |
71 | + break; | |
72 | + | |
73 | + case TN3270::Property::Int32: | |
74 | + rc = PyLong_FromLong(property->toInt32()); | |
75 | + break; | |
76 | + | |
77 | + case TN3270::Property::Int32x: | |
78 | + throw std::system_error(ENOTSUP, std::system_category()); | |
79 | + break; | |
80 | + | |
81 | + case TN3270::Property::Uint32: | |
82 | + rc = PyLong_FromLong(property->toUint32()); | |
83 | + break; | |
84 | + | |
85 | + case TN3270::Property::Int64: | |
86 | + throw std::system_error(ENOTSUP, std::system_category()); | |
87 | + break; | |
88 | + | |
89 | + case TN3270::Property::Uint64: | |
90 | + throw std::system_error(ENOTSUP, std::system_category()); | |
91 | + break; | |
92 | + | |
93 | + default: | |
94 | + throw runtime_error("Unexpected property type"); | |
95 | + } | |
96 | + | |
97 | + } catch(...) { | |
98 | + | |
99 | + delete property; | |
100 | + throw; | |
101 | + | |
102 | + } | |
103 | + | |
104 | + delete property; | |
105 | + | |
106 | + } catch(const exception &e) { | |
107 | + | |
108 | + PyErr_SetString(PyExc_RuntimeError, e.what()); | |
109 | + | |
110 | + } catch( ... ) { | |
111 | + | |
112 | + PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module"); | |
113 | + | |
114 | + } | |
115 | + | |
116 | + return rc; | |
117 | + | |
118 | +} | |
119 | +*/ | |
120 | + | |
121 | + | |
39 | 122 | /* |
40 | 123 | PyObject * terminal_get_version(PyObject *self, PyObject *args) { |
41 | 124 | ... | ... |
src/terminal/init.cc
... | ... | @@ -38,16 +38,77 @@ |
38 | 38 | |
39 | 39 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
40 | 40 | |
41 | -PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { | |
41 | +static void cleanup(pySession * session) { | |
42 | + | |
43 | + if(session->host) { | |
44 | + delete session->host; | |
45 | + session->host = nullptr; | |
46 | + } | |
47 | + | |
48 | +} | |
49 | + | |
50 | +int py3270_session_init(PyObject *self, PyObject *args, PyObject *kwds) { | |
51 | + | |
52 | + pySession * session = (pySession *) self; | |
53 | + debug("%s session=%p host=%p",__FUNCTION__,session,session->host); | |
54 | + | |
55 | + try { | |
56 | + | |
57 | + cleanup(session); | |
58 | + | |
59 | + const char *id = ""; | |
60 | + | |
61 | + if (!PyArg_ParseTuple(args, "s", &id)) | |
62 | + id = ""; | |
63 | + | |
64 | + session->host = new TN3270::Host(id); | |
65 | + | |
66 | + return 0; | |
67 | + | |
68 | + } catch(const std::exception &e) { | |
69 | + | |
70 | + PyErr_SetString(PyExc_RuntimeError, e.what()); | |
71 | + | |
72 | + } catch(...) { | |
73 | + | |
74 | + PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module"); | |
75 | + | |
76 | + } | |
77 | + | |
78 | + return -1; | |
79 | + | |
80 | +} | |
81 | + | |
82 | +void py3270_session_finalize(PyObject *self) { | |
83 | + | |
84 | + debug("%s",__FUNCTION__); | |
85 | + cleanup((pySession *) self); | |
86 | + | |
87 | +} | |
88 | + | |
89 | +PyObject * py3270_session_alloc(PyTypeObject *type, PyObject *args, PyObject *kwds) { | |
90 | + | |
91 | + debug("%s",__FUNCTION__); | |
92 | + return type->tp_alloc(type,0); | |
93 | + | |
94 | +} | |
95 | + | |
96 | +void py3270_session_dealloc(PyObject * self) { | |
97 | + | |
98 | + debug("%s",__FUNCTION__); | |
99 | + | |
100 | + cleanup((pySession *) self); | |
101 | + Py_TYPE(self)->tp_free(self); | |
102 | + | |
103 | +} | |
104 | + | |
105 | + /* | |
42 | 106 | |
43 | 107 | const char *id = ""; |
44 | 108 | |
45 | 109 | if (!PyArg_ParseTuple(args, "s", &id)) |
46 | 110 | id = ""; |
47 | 111 | |
48 | - pySession * session = (pySession *) type->tp_alloc(type,0); | |
49 | - | |
50 | - printf("---[%s]---\n",id); | |
51 | 112 | |
52 | 113 | if(session) { |
53 | 114 | |
... | ... | @@ -55,17 +116,12 @@ PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds |
55 | 116 | |
56 | 117 | session->host = new TN3270::Host(id); |
57 | 118 | |
58 | - printf("---[%s=%p]---\n",id,session->host); | |
59 | - return (PyObject *) session; | |
60 | - | |
61 | 119 | } catch(const exception &e) { |
62 | 120 | |
63 | - printf("---[%s]---\n",e.what()); | |
64 | 121 | PyErr_SetString(PyExc_RuntimeError, e.what()); |
65 | 122 | |
66 | 123 | } catch( ... ) { |
67 | 124 | |
68 | - printf("---[%s]---\n","???"); | |
69 | 125 | PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module"); |
70 | 126 | |
71 | 127 | } |
... | ... | @@ -87,6 +143,7 @@ void py3270_session_dealloc(pySession * self) { |
87 | 143 | Py_TYPE(self)->tp_free((PyObject *) self); |
88 | 144 | |
89 | 145 | } |
146 | + */ | |
90 | 147 | |
91 | 148 | |
92 | 149 | /* | ... | ... |
src/terminal/misc.cc
... | ... | @@ -1,135 +0,0 @@ |
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 <py3270.h> | |
38 | - | |
39 | -/*---[ Implement ]----------------------------------------------------------------------------------*/ | |
40 | - | |
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 | - } | |
84 | - | |
85 | - PyObject * terminal_wait_for_ready(PyObject *self, PyObject *args) { | |
86 | - | |
87 | - int rc; | |
88 | - int timeout = 60; | |
89 | - | |
90 | - if (!PyArg_ParseTuple(args, "|i", &timeout)) { | |
91 | - PyErr_SetString(terminalError, strerror(EINVAL)); | |
92 | - return NULL; | |
93 | - } | |
94 | - | |
95 | - try { | |
96 | - | |
97 | - rc = ((pw3270_TerminalObject *) self)->session->wait_for_ready(timeout); | |
98 | - | |
99 | - } catch(std::exception &e) { | |
100 | - | |
101 | - PyErr_SetString(terminalError, e.what()); | |
102 | - return NULL; | |
103 | - } | |
104 | - | |
105 | - return PyLong_FromLong(rc); | |
106 | - | |
107 | - } | |
108 | - | |
109 | - | |
110 | - PyObject * terminal_wait_for_string_at(PyObject *self, PyObject *args) { | |
111 | - | |
112 | - int row, col, rc; | |
113 | - int timeout = 10; | |
114 | - const char *text; | |
115 | - | |
116 | - if (!PyArg_ParseTuple(args, "iis|i", &row, &col, &text, &timeout)) { | |
117 | - PyErr_SetString(terminalError, strerror(EINVAL)); | |
118 | - return NULL; | |
119 | - } | |
120 | - | |
121 | - try { | |
122 | - | |
123 | - rc = ((pw3270_TerminalObject *) self)->session->wait_for_string_at(row,col,text,timeout); | |
124 | - | |
125 | - } catch(std::exception &e) { | |
126 | - | |
127 | - PyErr_SetString(terminalError, e.what()); | |
128 | - return NULL; | |
129 | - } | |
130 | - | |
131 | - return PyLong_FromLong(rc); | |
132 | - | |
133 | - } | |
134 | - | |
135 | -*/ |
... | ... | @@ -0,0 +1,168 @@ |
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 <py3270.h> | |
38 | + | |
39 | +/*---[ Implement ]----------------------------------------------------------------------------------*/ | |
40 | + | |
41 | + PyObject * py3270_session_connect(PyObject *self, PyObject *args) { | |
42 | + | |
43 | + printf("\n\n*************%s\n\n",__FUNCTION__); | |
44 | + | |
45 | + return py3270_session_call(self, [args](TN3270::Host &host){ | |
46 | + | |
47 | + const char * url = ""; | |
48 | + | |
49 | + if(!PyArg_ParseTuple(args, "s", &url)) { | |
50 | + throw runtime_error("connect requires a host URL"); | |
51 | + } | |
52 | + | |
53 | + host.connect(url); | |
54 | + | |
55 | + return 0; | |
56 | + | |
57 | + }); | |
58 | + | |
59 | + } | |
60 | + | |
61 | + PyObject * py3270_session_disconnect(PyObject *self, PyObject *args) { | |
62 | + | |
63 | + return py3270_session_call(self, [args](TN3270::Host &host){ | |
64 | + | |
65 | + host.disconnect(); | |
66 | + | |
67 | + return 0; | |
68 | + | |
69 | + }); | |
70 | + | |
71 | + } | |
72 | + | |
73 | + | |
74 | +/* | |
75 | + PyObject * terminal_connect(PyObject *self, PyObject *args) { | |
76 | + | |
77 | + int rc = -1; | |
78 | + int wait = 60; | |
79 | + const char * host = ""; | |
80 | + | |
81 | + if (!PyArg_ParseTuple(args, "s|i", &host, &wait)) { | |
82 | + PyErr_SetString(terminalError, "connect requires a host URL"); | |
83 | + return NULL; | |
84 | + } | |
85 | + | |
86 | + try { | |
87 | + | |
88 | + rc = ((pw3270_TerminalObject *) self)->session->connect(host,wait); | |
89 | + | |
90 | + } catch(std::exception &e) { | |
91 | + | |
92 | + PyErr_SetString(terminalError, e.what()); | |
93 | + return NULL; | |
94 | + } | |
95 | + | |
96 | + return PyLong_FromLong(rc); | |
97 | + | |
98 | + } | |
99 | + | |
100 | + PyObject * terminal_disconnect(PyObject *self, PyObject *args) { | |
101 | + | |
102 | + int rc = -1; | |
103 | + | |
104 | + try { | |
105 | + | |
106 | + rc = ((pw3270_TerminalObject *) self)->session->disconnect(); | |
107 | + | |
108 | + } catch(std::exception &e) { | |
109 | + | |
110 | + PyErr_SetString(terminalError, e.what()); | |
111 | + return NULL; | |
112 | + } | |
113 | + | |
114 | + return PyLong_FromLong(rc); | |
115 | + | |
116 | + } | |
117 | + | |
118 | + PyObject * terminal_wait_for_ready(PyObject *self, PyObject *args) { | |
119 | + | |
120 | + int rc; | |
121 | + int timeout = 60; | |
122 | + | |
123 | + if (!PyArg_ParseTuple(args, "|i", &timeout)) { | |
124 | + PyErr_SetString(terminalError, strerror(EINVAL)); | |
125 | + return NULL; | |
126 | + } | |
127 | + | |
128 | + try { | |
129 | + | |
130 | + rc = ((pw3270_TerminalObject *) self)->session->wait_for_ready(timeout); | |
131 | + | |
132 | + } catch(std::exception &e) { | |
133 | + | |
134 | + PyErr_SetString(terminalError, e.what()); | |
135 | + return NULL; | |
136 | + } | |
137 | + | |
138 | + return PyLong_FromLong(rc); | |
139 | + | |
140 | + } | |
141 | + | |
142 | + | |
143 | + PyObject * terminal_wait_for_string_at(PyObject *self, PyObject *args) { | |
144 | + | |
145 | + int row, col, rc; | |
146 | + int timeout = 10; | |
147 | + const char *text; | |
148 | + | |
149 | + if (!PyArg_ParseTuple(args, "iis|i", &row, &col, &text, &timeout)) { | |
150 | + PyErr_SetString(terminalError, strerror(EINVAL)); | |
151 | + return NULL; | |
152 | + } | |
153 | + | |
154 | + try { | |
155 | + | |
156 | + rc = ((pw3270_TerminalObject *) self)->session->wait_for_string_at(row,col,text,timeout); | |
157 | + | |
158 | + } catch(std::exception &e) { | |
159 | + | |
160 | + PyErr_SetString(terminalError, e.what()); | |
161 | + return NULL; | |
162 | + } | |
163 | + | |
164 | + return PyLong_FromLong(rc); | |
165 | + | |
166 | + } | |
167 | + | |
168 | +*/ | ... | ... |
testprograms/sample.py
1 | 1 | #!/usr/bin/python |
2 | 2 | #-*- coding: utf-8 |
3 | 3 | |
4 | +import weakref | |
4 | 5 | import tn3270 |
5 | 6 | |
6 | 7 | print("Teste extensão pw3270") |
... | ... | @@ -8,11 +9,22 @@ print("Teste extensão pw3270") |
8 | 9 | print("Using TN3270 Version " + tn3270.version()) |
9 | 10 | print(tn3270.revision()) |
10 | 11 | |
11 | -session = tn3270.Session(":a") | |
12 | +session = tn3270.Session("") | |
12 | 13 | |
13 | -#print "Using pw3270 version " + term.Version() + " revision " + term.Revision() | |
14 | +del session | |
14 | 15 | |
15 | -#term.Connect("tn3270://zos.efglobe.com:telnet",10); | |
16 | +input("Press enter to exit") | |
17 | + | |
18 | +#print("Using tn3270 version " + session.version + " revision " + session.revision) | |
19 | + | |
20 | +#print(session.cstate) | |
21 | +#print(session.width) | |
22 | +#print(session.connected) | |
23 | +#print(session.url) | |
24 | + | |
25 | +# print(session.connect) | |
26 | + | |
27 | +#print(session.connected) | |
16 | 28 | |
17 | 29 | #print term.IsConnected() |
18 | 30 | #print term.IsReady() | ... | ... |