noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 507 forks source link

marshal_with no recupera atributo de telnetlib #502

Open ChristianBarrientos opened 6 years ago

ChristianBarrientos commented 6 years ago

Hola, el problema es el siguiente: se declara esto onu = api.model('ONU', { 'id': fields.String(required=True, attribute='idonu'), 'puerto_olt' :fields.String(required=True ), 'macaddress': fields.String(required=True ), 'estado' :fields.String(required=True ), })

olt = api.model('OLT', { 'id': fields.String(required=True, attribute='id_dispositivo'), 'host' :fields.String(required=True ), 'puerto': fields.String(required=True ), 'usuario' :fields.String(required=True ), 'clave': fields.String(required=True ), 'prompt': fields.String(required=True ), 'puertos': fields.String(required=True), 'onus': fields.List(fields.Nested(onu)), })

Y al utilizar esto @api.marshal_list_with(olt,envelope='olt')

Produce el siguiente error: AttributeError: 'Telnet' object has no attribute 'sock' El error completo es: Traceback (most recent call last): File "app.py", line 2, in <module> from apis import api File "PATH\repositorio\ApiRest\apis\__init__.py", line 3, in <module> from .namespace_olt import api as olt File "PATH\repositorio\ApiRest\apis\namespace_olt.py", line 112, in <module> class Olt(Resource): File "PATH\repositorio\ApiRest\apis\namespace_olt.py", line 114, in Olt @api.marshal_list_with(olt,envelope='olt') File "PATH\Python\Python35\lib\site-packages\flask_restplus\namespace.py", line 237, in wrapper func.__apidoc__ = merge(getattr(func, '__apidoc__', {}), doc) File "PATH\Python\Python35\lib\site-packages\flask_restplus\utils.py", line 39, in merge result[key] = deepcopy(value) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 224, in _deepcopy_tuple y = [deepcopy(a, memo) for a in x] File "PATH\Python\Python35\lib\copy.py", line 224, in <listcomp> y = [deepcopy(a, memo) for a in x] File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 219, in _deepcopy_list y.append(deepcopy(a, memo)) File "PATH\Python\Python35\lib\copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "PATH\Python\Python35\lib\copy.py", line 298, in _reconstruct state = deepcopy(state, memo) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "PATH\Python\Python35\lib\copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "PATH\Python\Python35\lib\copy.py", line 298, in _reconstruct state = deepcopy(state, memo) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "PATH\Python\Python35\lib\copy.py", line 174, in deepcopy rv = reductor(4) File "PATH\Python\Python35\lib\socket.py", line 175, in __getstate__ raise TypeError("Cannot serialize socket object") TypeError: Cannot serialize socket object Exception ignored in: <bound method Telnet.__del__ of <telnetlib.Telnet object at 0x00000284435CF080>> Traceback (most recent call last): File "PATH\Python\Python35\lib\telnetlib.py", line 238, in __del__ self.close() File "PATH\Python\Python35\lib\telnetlib.py", line 264, in close sock = self.sock AttributeError: 'Telnet' object has no attribute 'sock'

Al parecer no puede serializar esto:

{'lista_OLT': [{'tn': {'debuglevel': 0, 'cookedq': {'py/b64': ''}, 'timeout': {'py/object': '__builtin__.object'}, 'rawq': {'py/b64': ''}, 'irawq': 0, 'sbdataq': {'py/b64': ''}, 'py/object': 'telnetlib.Telnet', 'iacseq': {'py/b64': ''}, 'option_callback': None, 'sock': None, 'eof': False, 'host': '192.168.1.1', 'sb': 0, 'port': '555'}, 'prompt': 'prompt', 'clave': 'usuario', 'puerto': '555', 'host': '192.168.1.1', 'usuario': 'usuario', 'id_dispositivo': '2', 'py/object': 'core.olt.OLT'}, {'tn': {'debuglevel': 0, 'cookedq': {'py/b64': ''}, 'timeout': {'py/id': 5}, 'rawq': {'py/b64': ''}, 'irawq': 0, 'sbdataq': {'py/b64': ''}, 'py/object': 'telnetlib.Telnet', 'iacseq': {'py/b64': ''}, 'option_callback': None, 'sock': None, 'eof': False, 'host': '192.168.1.1', 'sb': 0, 'port': '666'}, 'prompt': 'prompt', 'clave': 'clave', 'puerto': '666', 'host': '192.168.1.1', 'usuario': 'usuario', 'id_dispositivo': '1', 'py/object': 'core.olt.OLT'}], 'py/object': 'apis.namespace_olt.namespace_olt'}

La idea es generar una lista de objetos (OLT) en donde estos poseen en un atributo una sesion Telnet. Esta sesion Telnet posee un atributo llamado 'sock' que al parecer la siguiente funcion: def marshal_with(self, fields, as_list=False, code=HTTPStatus.OK, description=None, **kwargs): ''' A decorator specifying the fields to use for serialization.

   `

:param bool as_list: Indicate that the return type is a list (for the documentation) :param int code: Optionally give the expected HTTP response code if its different from 200

    '''
    def wrapper(func):
        doc = {
            'responses': {
                code: (description, [fields]) if as_list else (description, fields)
            },
            '__mask__': kwargs.get('mask', True),  # Mask values can't be determined outside app context
        }
        func.__apidoc__ = merge(getattr(func, '__apidoc__', {}), doc)
        return marshal_with(fields, ordered=self.ordered, **kwargs)(func)
    return wrapper` 

No puede recuperar.

Las aplicaciones instaladas en el entorno virtual son las siguientes: aniso8601==3.0.2 click==6.7 Django==2.0.6 Flask==1.0.2 flask-restplus==0.11.0 itsdangerous==0.24 Jinja2==2.10 jsonpickle==0.9.6 jsonschema==2.6.0 MarkupSafe==1.0 psycopg2==2.6.2 pytz==2018.4 six==1.11.0 virtualenv==16.0.0 Werkzeug==0.14.1

Adjunto los tres archivos relevantes al caso.

ChristianBarrientos commented 6 years ago

Archivos namespace_oltpy.TXT sesion_tnpy.TXT oltpy.TXT

mhayk commented 6 years ago

Hi!

Could try using 'onus': fields.Nested(onu, as_list=True)

olt = api.model('OLT', { 'id': fields.String(required=True, attribute='id_dispositivo'), 'host' :fields.String(required=True ), 'puerto': fields.String(required=True ), 'usuario' :fields.String(required=True ), 'clave': fields.String(required=True ), 'prompt': fields.String(required=True ), 'puertos': fields.String(required=True), 'onus': fields.Nested(onu, as_list=True) })

Best regards,

On 27 July 2018 at 07:58, Christian Barrientos notifications@github.com wrote:

Hola, el problema es el siguiente: se declara esto onu = api.model('ONU', { 'id': fields.String(required=True, attribute='idonu'), 'puerto_olt' :fields.String(required=True ), 'macaddress': fields.String(required=True ), 'estado' :fields.String(required=True ), })

olt = api.model('OLT', { 'id': fields.String(required=True, attribute='id_dispositivo'), 'host' :fields.String(required=True ), 'puerto': fields.String(required=True ), 'usuario' :fields.String(required=True ), 'clave': fields.String(required=True ), 'prompt': fields.String(required=True ), 'puertos': fields.String(required=True), 'onus': fields.List(fields.Nested(onu)), })

Y al utilizar esto @api.marshal_list_with(olt,envelope='olt')

Produce el siguiente error: AttributeError: 'Telnet' object has no attribute 'sock' El error completo es: Traceback (most recent call last): File "app.py", line 2, in from apis import api File "PATH\repositorio\ApiRest\apis__init.py", line 3, in from .namespace_olt import api as olt File "PATH\repositorio\ApiRest\apis\namespace_olt.py", line 112, in class Olt(Resource): File "PATH\repositorio\ApiRest\apis\namespace_olt.py", line 114, in Olt @api.marshal_list_with(olt,envelope='olt') File "PATH\Python\Python35\lib\site-packages\flask_restplus\namespace.py", line 237, in wrapper func.apidoc = merge(getattr(func, 'apidoc__', {}), doc) File "PATH\Python\Python35\lib\site-packages\flask_restplus\utils.py", line 39, in merge result[key] = deepcopy(value) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 224, in _deepcopy_tuple y = [deepcopy(a, memo) for a in x] File "PATH\Python\Python35\lib\copy.py", line 224, in y = [deepcopy(a, memo) for a in x] File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 219, in _deepcopy_list y.append(deepcopy(a, memo)) File "PATH\Python\Python35\lib\copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "PATH\Python\Python35\lib\copy.py", line 298, in _reconstruct state = deepcopy(state, memo) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "PATH\Python\Python35\lib\copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "PATH\Python\Python35\lib\copy.py", line 298, in _reconstruct state = deepcopy(state, memo) File "PATH\Python\Python35\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "PATH\Python\Python35\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "PATH\Python\Python35\lib\copy.py", line 174, in deepcopy rv = reductor(4) File "PATH\Python\Python35\lib\socket.py", line 175, in getstate raise TypeError("Cannot serialize socket object") TypeError: Cannot serialize socket object Exception ignored in: <bound method Telnet.del of <telnetlib.Telnet object at 0x00000284435CF080>> Traceback (most recent call last): File "PATH\Python\Python35\lib\telnetlib.py", line 238, in del self.close() File "PATH\Python\Python35\lib\telnetlib.py", line 264, in close sock = self.sock AttributeError: 'Telnet' object has no attribute 'sock'

Al parecer no puede serializar esto:

{'lista_OLT': [{'tn': {'debuglevel': 0, 'cookedq': {'py/b64': ''}, 'timeout': {'py/object': 'builtin.object'}, 'rawq': {'py/b64': ''}, 'irawq': 0, 'sbdataq': {'py/b64': ''}, 'py/object': 'telnetlib.Telnet', 'iacseq': {'py/b64': ''}, 'option_callback': None, 'sock': None, 'eof': False, 'host': '192.168.1.1', 'sb': 0, 'port': '555'}, 'prompt': 'prompt', 'clave': 'usuario', 'puerto': '555', 'host': '192.168.1.1', 'usuario': 'usuario', 'id_dispositivo': '2', 'py/object': 'core.olt.OLT'}, {'tn': {'debuglevel': 0, 'cookedq': {'py/b64': ''}, 'timeout': {'py/id': 5}, 'rawq': {'py/b64': ''}, 'irawq': 0, 'sbdataq': {'py/b64': ''}, 'py/object': 'telnetlib.Telnet', 'iacseq': {'py/b64': ''}, 'option_callback': None, 'sock': None, 'eof': False, 'host': '192.168.1.1', 'sb': 0, 'port': '666'}, 'prompt': 'prompt', 'clave': 'clave', 'puerto': '666', 'host': '192.168.1.1', 'usuario': 'usuario', 'id_dispositivo': '1', 'py/object': 'core.olt.OLT'}], 'py/object': 'apis.namespace_olt.namespace_olt'}

La idea es generar una lista de objetos (OLT) en donde estos poseen en un atributo una sesion Telnet. Esta sesion Telnet posee un atributo llamado 'sock' que al parecer la siguiente funcion: def marshal_with(self, fields, as_list=False, code=HTTPStatus.OK, description=None, **kwargs): ''' A decorator specifying the fields to use for serialization.

`

:param bool as_list: Indicate that the return type is a list (for the documentation) :param int code: Optionally give the expected HTTP response code if its different from 200

'''
def wrapper(func):
    doc = {
        'responses': {
            code: (description, [fields]) if as_list else (description, fields)
        },
        '__mask__': kwargs.get('mask', True),  # Mask values can't be determined outside app context
    }
    func.__apidoc__ = merge(getattr(func, '__apidoc__', {}), doc)
    return marshal_with(fields, ordered=self.ordered, **kwargs)(func)
return wrapper`

No puede recuperar.

Las aplicaciones instaladas en el entorno virtual son las siguientes: aniso8601==3.0.2 click==6.7 Django==2.0.6 Flask==1.0.2 flask-restplus==0.11.0 itsdangerous==0.24 Jinja2==2.10 jsonpickle==0.9.6 jsonschema==2.6.0 MarkupSafe==1.0 psycopg2==2.6.2 pytz==2018.4 six==1.11.0 virtualenv==16.0.0 Werkzeug==0.14.1

Adjunto los tres archivos relevantes al caso.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/noirbizarre/flask-restplus/issues/502, or mute the thread https://github.com/notifications/unsubscribe-auth/ABbmyblC0wRxJximFHd0XD9IXVdkOgT8ks5uKrosgaJpZM4VjAcS .

-- Mhayk Whandson Linux User #413723 E-mail: eu@mhayk.com.br

ChristianBarrientos commented 5 years ago

Hola Gracias!! Ahora tengo el siguiente problema. Tengo un objeto que posee una lista de objetos OLT con sus respectivas sesiones telnet. AL querer copiar ese objeto usando deepcopy de copy, me devuelve el siguiente error.

AttributeError: 'Telnet' object has no attribute 'sock'

Quiero ejecutar esto: namespace_olt_ch = copy.deepcopy(namespaceolt)

Traceback completo:

Traceback (most recent call last): File "app.py", line 6, in from apis import api,namespaceolt,namespace_olt_ch,LeerDatos,namespace_olt,sesiones_tn,namespace_olt_oh File "/home/christian/providers/ApiRest/apis/init.py", line 3, in from .namespace_olt import api as olt File "/home/christian/providers/ApiRest/apis/namespace_olt.py", line 405, in iniciarApp(); File "/home/christian/providers/ApiRest/apis/namespace_olt.py", line 356, in iniciarApp namespace_olt_ch = copy.deepcopy(namespaceolt) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 297, in _reconstruct state = deepcopy(state, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 155, in deepcopy y = copier(x, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 243, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 155, in deepcopy y = copier(x, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 218, in _deepcopy_list y.append(deepcopy(a, memo)) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 297, in _reconstruct state = deepcopy(state, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 155, in deepcopy y = copier(x, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 243, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 155, in deepcopy y = copier(x, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 218, in _deepcopy_list y.append(deepcopy(a, memo)) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 297, in _reconstruct state = deepcopy(state, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 155, in deepcopy y = copier(x, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 243, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 297, in _reconstruct state = deepcopy(state, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 155, in deepcopy y = copier(x, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 243, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/christian/providers/AppGpon/proyectoenv-deb/lib/python3.5/copy.py", line 174, in deepcopy rv = reductor(4) File "/usr/lib/python3.5/socket.py", line 175, in getstate raise TypeError("Cannot serialize socket object") TypeError: Cannot serialize socket object Exception ignored in: <bound method Telnet.del of <telnetlib.Telnet object at 0x7fc9aa448550>> Traceback (most recent call last): File "/usr/lib/python3.5/telnetlib.py", line 238, in del self.close() File "/usr/lib/python3.5/telnetlib.py", line 264, in close sock = self.sock AttributeError: 'Telnet' object has no attribute 'sock'

Gracias!