Esempio n. 1
0
    def _create_diffmet_instr(self):
        """
        Create the difmet instruction file according to the information contained
        in self.instructions for all the request Id linked to the file
        """

        def get_prefix(diff):
            if diff["DiffusionType"] == "FTP":
                prefix = "standby_ftp_"
            else:
                prefix = "standby_email_"

            return prefix


        date_str = strftime("%Y%m%d%H%M%S")
        # difmet instruction file name
        path_to_file = ",".join((SettingsManager.get("diffFileName"),
                                 self.incr,
                                 "",
                                 date_str,
                                 ))
        path_to_file += ".diffusions.xml"
        path_to_file = os.path.join(self.dir_b, path_to_file)

        # create the xml structure
        root = etree.Element("product_diffusion")
        product = etree.SubElement(root, "product")
        etree.SubElement(product,"file_name").text = Tools.ack_str(self.new_filename)
        etree.SubElement(product,"file_size").text = Tools.ack_str(self._get_file_size())
        etree.SubElement(product,"priority").text = Tools.ack_str(self._get_priority())
        etree.SubElement(product,"archive").text = "0"
        end_date = self._get_end_date()
        # add a end_date only if user specified one
        if end_date is not None:
            etree.SubElement(product,"end_to_live_date").text = Tools.ack_str(end_date)
        # loop on all the requests linked to the file to send to difmet
        for req_id in self.id_list:
            diffusion = etree.SubElement(product,"diffusion")
            # fetch the informations stored from json instruction file
            instr = self.instructions[req_id]
            diff = instr["diffusion"]
            etree.SubElement(diffusion,"diffusion_externalid").text = Database.get_external_id(req_id, self.new_filename)
            etree.SubElement(diffusion,"archive").text = "0"

            self.diff_info_to_xml(diffusion, diff)

            # if an alternative diffusion is requested, it is added
            if "alternativeDiffusion" in instr.keys():
                altdiff = instr["alternativeDiffusion"]
                prefix = get_prefix(altdiff)
                self.diff_info_to_xml(diffusion,altdiff, prefix=prefix)
                etree.SubElement(diffusion,"standby_media").text = altdiff["DiffusionType"]
                if altdiff["DiffusionType"] == "FTP" and diff["DiffusionType"] == "FTP":
                    etree.SubElement(diffusion, "switch_method_medias_ftp").text = "NTRY"

            etree.SubElement(diffusion,"standby_switch_try_number").text = "3"

        etree.SubElement(product, "diffusionnumber").text=str(len(self.id_list))

        etree.SubElement(root, "productnumber").text="1"
        # Dump the xml tree into a file
        etree.ElementTree(root).write(path_to_file,
                                      pretty_print=True,
                                      encoding='UTF-8',
                                      xml_declaration=True)
        return path_to_file