Commit 3162c84e953fb440525b4ba2b51a2401f5ddb3b4
1 parent
ead2d48e
Exists in
master
Implementing session attribute getter.
Showing
9 changed files
with
165 additions
and
31 deletions
Show diff stats
.gitignore
py3270.cbp
| ... | ... | @@ -51,6 +51,7 @@ |
| 51 | 51 | <Unit filename="src/module/properties.cc" /> |
| 52 | 52 | <Unit filename="src/module/tools.cc" /> |
| 53 | 53 | <Unit filename="src/terminal/actions.cc" /> |
| 54 | + <Unit filename="src/terminal/attributes.cc" /> | |
| 54 | 55 | <Unit filename="src/terminal/get.cc" /> |
| 55 | 56 | <Unit filename="src/terminal/init.cc" /> |
| 56 | 57 | <Unit filename="src/terminal/network.cc" /> | ... | ... |
src/include/py3270.h
| ... | ... | @@ -97,6 +97,8 @@ |
| 97 | 97 | Host *host; |
| 98 | 98 | } pySession; |
| 99 | 99 | |
| 100 | + DLL_PRIVATE void py3270_session_type_init(PyTypeObject *type); | |
| 101 | + | |
| 100 | 102 | DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args); |
| 101 | 103 | DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args); |
| 102 | 104 | |
| ... | ... | @@ -106,6 +108,8 @@ |
| 106 | 108 | DLL_PRIVATE int py3270_session_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 107 | 109 | DLL_PRIVATE void py3270_session_finalize(PyObject *self); |
| 108 | 110 | |
| 111 | + DLL_PRIVATE PyObject * py3270_session_getter(PyObject *self, void *name); | |
| 112 | + DLL_PRIVATE int py3270_session_setter(PyObject *self, PyObject *value, void *name); | |
| 109 | 113 | |
| 110 | 114 | // DLL_PRIVATE PyObject * py3270_session_getattr(PyObject *self, char *attr_name); |
| 111 | 115 | ... | ... |
src/module/init.c
| ... | ... | @@ -31,6 +31,8 @@ |
| 31 | 31 | |
| 32 | 32 | #include <py3270.h> |
| 33 | 33 | |
| 34 | + static void cleanup(PyObject *module); | |
| 35 | + | |
| 34 | 36 | /*---[ Globals ]------------------------------------------------------------------------------------*/ |
| 35 | 37 | |
| 36 | 38 | static PyMethodDef methods[] = { |
| ... | ... | @@ -64,7 +66,8 @@ static struct PyModuleDef definition = { |
| 64 | 66 | .m_name = "tn3270", // name of module |
| 65 | 67 | .m_doc = PACKAGE_DESCRIPTION, // module documentation, may be NUL |
| 66 | 68 | .m_size = -1, // size of per-interpreter state of the module or -1 if the module keeps state in global variables. |
| 67 | - .m_methods = methods // Module methods | |
| 69 | + .m_methods = methods, // Module methods | |
| 70 | + .m_free = (freefunc) cleanup | |
| 68 | 71 | }; |
| 69 | 72 | |
| 70 | 73 | //# Tornar essa tabela pública e testar em getattr, se for um método usar PyMethod_New para retornar um método. |
| ... | ... | @@ -106,15 +109,15 @@ static PyTypeObject SessionType = { |
| 106 | 109 | |
| 107 | 110 | .tp_methods = py3270_session_methods, |
| 108 | 111 | |
| 109 | -// .tp_alloc = | |
| 110 | -// .tp_free = | |
| 111 | -// .tp_getattr = py3270_session_getattr | |
| 112 | 112 | }; |
| 113 | 113 | |
| 114 | 114 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
| 115 | 115 | |
| 116 | 116 | PyMODINIT_FUNC PyInit_tn3270(void) |
| 117 | 117 | { |
| 118 | + // Initialize custom attributes & methods. | |
| 119 | + py3270_session_type_init(&SessionType); | |
| 120 | + | |
| 118 | 121 | // |
| 119 | 122 | // Initialize module. |
| 120 | 123 | // |
| ... | ... | @@ -125,24 +128,11 @@ PyMODINIT_FUNC PyInit_tn3270(void) |
| 125 | 128 | |
| 126 | 129 | PyObject *module = PyModule_Create(&definition); |
| 127 | 130 | |
| 131 | + debug("Initializing module %p", module); | |
| 132 | + | |
| 128 | 133 | if(!module) |
| 129 | 134 | return NULL; |
| 130 | 135 | |
| 131 | - /* | |
| 132 | - // | |
| 133 | - // Create exception object | |
| 134 | - // | |
| 135 | - PyObject * except = PyErr_NewException("tn3270.Error", NULL, NULL); | |
| 136 | - | |
| 137 | - Py_XINCREF(except); | |
| 138 | - if (PyModule_AddObject(module, "error", except) < 0) { | |
| 139 | - Py_XDECREF(except); | |
| 140 | - Py_CLEAR(except); | |
| 141 | - Py_DECREF(module); | |
| 142 | - return NULL; | |
| 143 | - } | |
| 144 | - */ | |
| 145 | - | |
| 146 | 136 | // |
| 147 | 137 | // Create custom type |
| 148 | 138 | // |
| ... | ... | @@ -155,3 +145,15 @@ PyMODINIT_FUNC PyInit_tn3270(void) |
| 155 | 145 | |
| 156 | 146 | return module; |
| 157 | 147 | } |
| 148 | + | |
| 149 | +static void cleanup(PyObject *module) { | |
| 150 | + | |
| 151 | + debug("Cleaning up module %p", module); | |
| 152 | + | |
| 153 | + if(SessionType.tp_getset) { | |
| 154 | + free(SessionType.tp_getset); | |
| 155 | + SessionType.tp_getset = NULL; | |
| 156 | + } | |
| 157 | + | |
| 158 | +} | |
| 159 | + | ... | ... |
| ... | ... | @@ -0,0 +1,92 @@ |
| 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 <py3270.h> | |
| 36 | + | |
| 37 | +/*---[ Implement ]----------------------------------------------------------------------------------*/ | |
| 38 | + | |
| 39 | +PyObject * py3270_session_getter(PyObject *self, void *name) { | |
| 40 | + | |
| 41 | + debug("%s(%s)",__FUNCTION__,(const char *) name); | |
| 42 | + | |
| 43 | + return py3270_session_call(self, [name](TN3270::Host &host){ | |
| 44 | + | |
| 45 | + auto attribute = host[(const char *) name]; | |
| 46 | + | |
| 47 | + switch(attribute.getType()) { | |
| 48 | + case TN3270::Attribute::String: | |
| 49 | + return PyUnicode_FromString(attribute.toString().c_str()); | |
| 50 | + | |
| 51 | + case TN3270::Attribute::Boolean: | |
| 52 | + return PyBool_FromLong(attribute.getBoolean()); | |
| 53 | + | |
| 54 | + case TN3270::Attribute::Uchar: | |
| 55 | + throw std::system_error(ENOTSUP, std::system_category()); | |
| 56 | + | |
| 57 | + case TN3270::Attribute::Int16: | |
| 58 | + throw std::system_error(ENOTSUP, std::system_category()); | |
| 59 | + | |
| 60 | + case TN3270::Attribute::Uint16: | |
| 61 | + throw std::system_error(ENOTSUP, std::system_category()); | |
| 62 | + | |
| 63 | + case TN3270::Attribute::Int32: | |
| 64 | + return PyLong_FromLong(attribute.getInt32()); | |
| 65 | + | |
| 66 | + case TN3270::Attribute::Int32x: | |
| 67 | + throw std::system_error(ENOTSUP, std::system_category()); | |
| 68 | + | |
| 69 | + case TN3270::Attribute::Uint32: | |
| 70 | + return PyLong_FromLong(attribute.getUint32()); | |
| 71 | + | |
| 72 | + case TN3270::Attribute::Int64: | |
| 73 | + throw std::system_error(ENOTSUP, std::system_category()); | |
| 74 | + | |
| 75 | + case TN3270::Attribute::Uint64: | |
| 76 | + throw std::system_error(ENOTSUP, std::system_category()); | |
| 77 | + | |
| 78 | + default: | |
| 79 | + throw runtime_error("Unexpected atttribute type"); | |
| 80 | + } | |
| 81 | + | |
| 82 | + return PyLong_FromLong(0); | |
| 83 | + | |
| 84 | + }); | |
| 85 | + | |
| 86 | +} | |
| 87 | + | |
| 88 | +int py3270_session_setter(PyObject *self, PyObject *value, void *name) { | |
| 89 | + | |
| 90 | + return 0; | |
| 91 | +} | |
| 92 | + | ... | ... |
src/terminal/init.cc
| ... | ... | @@ -35,6 +35,7 @@ |
| 35 | 35 | */ |
| 36 | 36 | |
| 37 | 37 | #include <py3270.h> |
| 38 | + #include <lib3270/ipc.h> | |
| 38 | 39 | |
| 39 | 40 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
| 40 | 41 | |
| ... | ... | @@ -47,6 +48,35 @@ static void cleanup(pySession * session) { |
| 47 | 48 | |
| 48 | 49 | } |
| 49 | 50 | |
| 51 | +void py3270_session_type_init(PyTypeObject *type) { | |
| 52 | + | |
| 53 | + // Load lib3270's attributes | |
| 54 | + { | |
| 55 | + auto attributes = TN3270::getAttributes(); | |
| 56 | + size_t szData = sizeof(struct PyGetSetDef) * (attributes.size()+1); | |
| 57 | + | |
| 58 | + type->tp_getset = (struct PyGetSetDef *) malloc(szData); | |
| 59 | + memset(type->tp_getset,0,szData); | |
| 60 | + | |
| 61 | + size_t ix = 0; | |
| 62 | + for(auto attribute : attributes) { | |
| 63 | + | |
| 64 | + debug("Creating attribute %s",attribute->name); | |
| 65 | + | |
| 66 | + type->tp_getset[ix].name = (char *) attribute->name; | |
| 67 | + type->tp_getset[ix].doc = (char *) (attribute->description ? attribute->description : attribute->summary); | |
| 68 | + type->tp_getset[ix].get = py3270_session_getter; | |
| 69 | + type->tp_getset[ix].set = py3270_session_setter; | |
| 70 | + type->tp_getset[ix].closure = (void *) attribute->name; | |
| 71 | + ix++; | |
| 72 | + } | |
| 73 | + | |
| 74 | + | |
| 75 | + } | |
| 76 | + | |
| 77 | +} | |
| 78 | + | |
| 79 | + | |
| 50 | 80 | int py3270_session_init(PyObject *self, PyObject *args, PyObject *kwds) { |
| 51 | 81 | |
| 52 | 82 | pySession * session = (pySession *) self; | ... | ... |
src/terminal/network.cc
src/terminal/set.cc
testprograms/sample.py
| 1 | 1 | #!/usr/bin/python |
| 2 | 2 | #-*- coding: utf-8 |
| 3 | 3 | |
| 4 | -import weakref | |
| 4 | +import inspect | |
| 5 | 5 | import tn3270 |
| 6 | 6 | |
| 7 | 7 | print("Teste extensão pw3270") |
| ... | ... | @@ -11,20 +11,22 @@ print(tn3270.revision()) |
| 11 | 11 | |
| 12 | 12 | session = tn3270.Session("") |
| 13 | 13 | |
| 14 | -del session | |
| 14 | +print("Using tn3270 version " + session.version + " revision " + session.revision) | |
| 15 | 15 | |
| 16 | -input("Press enter to exit") | |
| 16 | +print(session.cstate) | |
| 17 | +print(session.width) | |
| 18 | +print(session.connected) | |
| 19 | +print(session.url) | |
| 20 | + | |
| 21 | +session.connect('') | |
| 17 | 22 | |
| 18 | -#print("Using tn3270 version " + session.version + " revision " + session.revision) | |
| 23 | +print(session.connected) | |
| 19 | 24 | |
| 20 | -#print(session.cstate) | |
| 21 | -#print(session.width) | |
| 22 | -#print(session.connected) | |
| 23 | -#print(session.url) | |
| 25 | +print('----------------------') | |
| 26 | +inspect.getmembers(session) | |
| 27 | +print('----------------------') | |
| 24 | 28 | |
| 25 | -# print(session.connect) | |
| 26 | 29 | |
| 27 | -#print(session.connected) | |
| 28 | 30 | |
| 29 | 31 | #print term.IsConnected() |
| 30 | 32 | #print term.IsReady() |
| ... | ... | @@ -35,6 +37,9 @@ input("Press enter to exit") |
| 35 | 37 | #print term |
| 36 | 38 | #print "-----------------------------------------------------------------------" |
| 37 | 39 | |
| 40 | +del session | |
| 41 | +input("Press enter to exit") | |
| 42 | + | |
| 38 | 43 | |
| 39 | 44 | |
| 40 | 45 | ... | ... |