Problems encountered in the CMDB project

Client


Call the class

class Response(object):
    def __init__(self):
        self.status = True
        self.msg = None
        self.data = None

    @property
    def dict(self):
        return self.__dict__


response = Response()




class Disk(object):
    def __init__(self):
        self.response = response

    @property
    def info(self):
        self.response.status = 100
        self.response.msg = '魏新雨咋软'
        self.response.data = {'1':{'capatablities': '100GB', 'model': 'hp2344'}}

        return self.response.dict



class Memory(object):
    def __init__(self):
        self.response = response

    @property
    def info(self):
        self.response.status = 100
        self.response.msg = '陈骏啊, 萨宁啊'
        self.response.data = {'#DISM1':{'capatablities': '8GB', 'model': 'kingstong222'}}
        return self.response.dict


disk = Disk().info
memory = Memory().info


print(disk)  # {'status': 100, 'msg': '陈骏啊, 萨宁啊', 'data': {'#DISM1': {'capatablities': '8GB', 'model': 'kingstong222'}}}
print(memory)  # {'status': 100, 'msg': '陈骏啊, 萨宁啊', 'data': {'#DISM1': {'capatablities': '8GB', 'model': 'kingstong222'}}}


2's final output is the same, because both the Disk Memory object or objects, their attributes self.responseresults point is response to this object, so the final value is assigned by the last time


TypeError: Object of type 'Response' is not JSON serializable


The reason: The returned object can not be instantiated (json.dumps (ret)), and finally the return of objects rather than use json dictionary


lib.response.py

class Response(object):
    def __init__(self):
        self.status = None
        self.msg = None
        self.data = None

    @property
    def dict(self):
        return self.__dict__


src.plugins.disk.py

from .base import BasePlugin
import os
import traceback
from lib.response import Response



class Disk(BasePlugin):
    def __init__(self, handler):
        super().__init__(handler)
        self.disk_path = os.path.join(self.filepath, 'disk.out')
        # self.response = response_obj
        self.error = traceback.format_exc()
        self.response = Response()

    def Windows(self):
        return 'windows'

    def Linux(self, handler):
        # print('disk', response)
        if not self.debug:
            data = handler.cmd('sudo MegaCli -PDList -aALL')
        else:
            try:
                data = open(self.disk_path, 'r', encoding='utf-8').read().split('Enclosure Device ID: 32')[1:]
                disk_dict = self.LinuxParse(data)

                self.response.status = 1000
                self.response.msg = '采集磁盘成功'
                self.response.data = disk_dict

            except Exception:
                self.response.status = 4000
                self.response.msg = self.error

        return  self.response


    def LinuxParse(self, data):
        key_map = {
            'PD Type': 'iface',
            'Slot Number': 'slot',
            'Inquiry Data': 'model',
            'Raw Size': 'capacity',
        }
        disk_dict = {}
        for item in data:
            res = item.strip().split('\n')
            info = {}
            for row in res:
                if len(row.strip().split(':')) == 2:
                    key, value = row.strip().split(':')
                    if key in key_map:
                        info[key_map[key]] = value.strip()

            disk_dict[info['slot']] = info

            return disk_dict


API


TypeError: init() missing 1 required positional argument: 'app_module'



The reason: the app is registered in the list MIDDLEWARE


Guess you like

Origin www.cnblogs.com/cjwnb/p/11846359.html