예제 #1
0
    def _json_to_obj(cls, serialized_str):
        """
        @summary: Returns an instance of a Host or a collection of Hosts
         based on the json serialized_str passed in.
        @param serialized_str: json serialized string.
        @type serialized_str: String.
        @return: Host or List of Hosts.
        @rtype: Host or List.
         """
        json_dict = json.loads(serialized_str)
        if 'host' in json_dict.keys():
            resources = []
            for resource in json_dict.get("host"):
                resources.append(
                    Resource._dict_to_obj(resource.get("resource")))
            host = Host(resources=resources)
            return host

        if 'hosts' in json_dict.keys():
            hosts = []
            for host_dict in json_dict.get("hosts"):
                hosts.append(cls._dict_to_obj(host_dict))
            return hosts
예제 #2
0
    def _xml_to_obj(cls, serialized_str):
        """
        @summary: Returns an instance of a Host or a collection of Hosts
         based on the xml serialized_str passed in.
        @param serialized_str: xml serialized string.
        @type serialized_str: String.
        @return: Host or a List of Hosts.
        @rtype: Host or List.
         """
        element = ET.fromstring(serialized_str)

        if element.tag == 'host':
            resources = []
            for resource in element._children:
                resources.append(Resource._xml_to_obj(resource))
            host = Host(resources=resources)
            return host

        if element.tag == 'hosts':
            hosts = []
            for host in element.findall('host'):
                host = cls._xml_ele_to_obj(host)
                hosts.append(host)
            return hosts