Exemple #1
0
    def to_xml(self, doc, element):
        """Create XML elements for each model.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the model XML elements to.
        @type element:  XML element object
        """

        # Loop over the models.
        for i in range(len(self)):
            # Create an XML element for this model and add it to the higher level element.
            model_element = doc.createElement('model')
            element.appendChild(model_element)

            # Set the model attributes.
            model_element.setAttribute('desc', 'Model container')
            model_element.setAttribute('num', str(self[i].num))

            # Add all simple python objects within the ModelContainer to the XML element.
            fill_object_contents(doc,
                                 model_element,
                                 object=self[i],
                                 blacklist=['num', 'mol'] +
                                 list(self[i].__class__.__dict__.keys()))

            # Add the molecule data.
            self[i].mol.to_xml(doc, model_element)
Exemple #2
0
    def to_xml(self, doc, element):
        """Create an XML element for the alignment tensors.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the alignment tensors XML element to.
        @type element:  XML element object
        """

        # Create the alignment tensors element and add it to the higher level element.
        tensor_list_element = doc.createElement('align_tensors')
        element.appendChild(tensor_list_element)

        # Set the alignment tensor attributes.
        tensor_list_element.setAttribute('desc', 'Alignment tensor list')

        # Add all simple python objects within the PipeContainer to the pipe element.
        fill_object_contents(doc, tensor_list_element, object=self, blacklist=list(self.__class__.__dict__.keys())+list(list.__dict__.keys()))

        # Loop over the tensors.
        for i in range(len(self)):
            # Create an XML element for a single tensor.
            tensor_element = doc.createElement('align_tensor')
            tensor_list_element.appendChild(tensor_element)
            tensor_element.setAttribute('index', repr(i))
            tensor_element.setAttribute('desc', 'Alignment tensor')

            # The blacklist.
            blacklist = ['type', 'is_empty'] + list(self[i].__class__.__dict__.keys())
            for name in dir(self):
                if name not in self[i]._mod_attr:
                    blacklist.append(name)

            # Add all simple python objects within the PipeContainer to the pipe element.
            fill_object_contents(doc, tensor_element, object=self[i], blacklist=blacklist)
Exemple #3
0
    def to_xml(self, doc, element):
        """Create an XML element for the diffusion tensor.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the diffusion tensor element to.
        @type element:  XML element object
        """

        # Create the diffusion tensor element and add it to the higher level element.
        tensor_element = doc.createElement('diff_tensor')
        element.appendChild(tensor_element)

        # Set the diffusion tensor attributes.
        tensor_element.setAttribute('desc', 'Diffusion tensor')
        tensor_element.setAttribute('type', self.type)

        # The blacklist.
        blacklist = ['type', 'is_empty'] + list(self.__class__.__dict__.keys())
        for name in dir(self):
            if name not in self._mod_attr:
                blacklist.append(name)

        # Add all simple python objects within the PipeContainer to the pipe element.
        fill_object_contents(doc, tensor_element, object=self, blacklist=blacklist)
Exemple #4
0
    def to_xml(self, doc, element, pipe_type=None):
        """Create XML elements for each molecule.

        @param doc:         The XML document object.
        @type doc:          Xml.dom.minidom.Document instance
        @param element:     The element to add the molecule XML elements to.
        @type element:      XML element object
        @keyword pipe_type: The type of the pipe being converted to XML.
        @type pipe_type:    str
        """

        # Loop over the molecules.
        for i in range(len(self)):
            # Create an XML element for this molecule and add it to the higher level element.
            mol_element = doc.createElement('mol')
            element.appendChild(mol_element)

            # Set the molecule attributes.
            mol_element.setAttribute('desc', 'Molecule container')
            mol_element.setAttribute('name', str(self[i].name))
            mol_element.setAttribute('type', str(self[i].type))

            # Add all simple python objects within the MoleculeContainer to the XML element.
            fill_object_contents(doc,
                                 mol_element,
                                 object=self[i],
                                 blacklist=['name', 'res', 'type'] +
                                 list(self[i].__class__.__dict__.keys()))

            # Add the residue data.
            self[i].res.to_xml(doc, mol_element, pipe_type=pipe_type)
Exemple #5
0
    def to_xml(self, doc, element, pipe_type=None):
        """Create XML elements for each molecule.

        @param doc:         The XML document object.
        @type doc:          Xml.dom.minidom.Document instance
        @param element:     The element to add the molecule XML elements to.
        @type element:      XML element object
        @keyword pipe_type: The type of the pipe being converted to XML.
        @type pipe_type:    str
        """

        # Loop over the molecules.
        for i in range(len(self)):
            # Create an XML element for this molecule and add it to the higher level element.
            mol_element = doc.createElement('mol')
            element.appendChild(mol_element)

            # Set the molecule attributes.
            mol_element.setAttribute('desc', 'Molecule container')
            mol_element.setAttribute('name', str(self[i].name))
            mol_element.setAttribute('type', str(self[i].type))

            # Add all simple python objects within the MoleculeContainer to the XML element.
            fill_object_contents(doc, mol_element, object=self[i], blacklist=['name', 'res', 'type'] + list(self[i].__class__.__dict__.keys()))

            # Add the residue data.
            self[i].res.to_xml(doc, mol_element, pipe_type=pipe_type)
Exemple #6
0
    def to_xml(self, doc, element):
        """Create an XML element for the diffusion tensor.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the diffusion tensor element to.
        @type element:  XML element object
        """

        # Create the diffusion tensor element and add it to the higher level element.
        tensor_element = doc.createElement('diff_tensor')
        element.appendChild(tensor_element)

        # Set the diffusion tensor attributes.
        tensor_element.setAttribute('desc', 'Diffusion tensor')
        tensor_element.setAttribute('type', self.type)

        # The blacklist.
        blacklist = ['type', 'is_empty'] + list(self.__class__.__dict__.keys())
        for name in dir(self):
            if name not in self._mod_attr:
                blacklist.append(name)

        # Add all simple python objects within the PipeContainer to the pipe element.
        fill_object_contents(doc,
                             tensor_element,
                             object=self,
                             blacklist=blacklist)
Exemple #7
0
    def to_xml(self, doc, element):
        """Create an XML element for the list data structure.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the list data structure XML element to.
        @type element:  XML element object
        """

        # Create the element and add it to the higher level element.
        list_element = doc.createElement(self.list_name)
        element.appendChild(list_element)

        # Set the list attributes.
        list_element.setAttribute('desc', self.list_desc)

        # Blacklisted objects.
        blacklist = ['list_name', 'list_desc', 'element_name', 'element_desc', 'blacklist'] + list(self.__dict__.keys()) + list(RelaxListType.__dict__.keys()) + list(self.__class__.__dict__.keys()) + list(list.__dict__.keys()) + list(list.__dict__.keys())

        # Add all simple python objects within the list to the list element.
        fill_object_contents(doc, list_element, object=self, blacklist=blacklist)

        # Loop over the list.
        for i in range(len(self)):
            # The element has its own to_xml() method.
            if hasattr(self[i], 'to_xml'):
                self[i].to_xml(doc, list_element)

            # Normal element.
            else:
                # Create an XML element for each container.
                list_item_element = doc.createElement(self.element_name)
                list_element.appendChild(list_item_element)
                list_item_element.setAttribute('index', repr(i))
                list_item_element.setAttribute('desc', self.element_desc)

                # Blacklisted objects.
                blacklist = list(self[i].__class__.__dict__.keys())

                # Add objects which have to_xml() methods.
                for name in dir(self[i]):
                    # Skip blacklisted objects.
                    if name in blacklist:
                        continue

                    # Skip special objects.
                    if search('^_', name):
                        continue

                    # Execute any to_xml() methods, and add that object to the blacklist.
                    obj = getattr(self[i], name)
                    if hasattr(obj, 'to_xml'):
                        obj.to_xml(doc, list_item_element)
                        blacklist = blacklist + [name]

                # Add all simple python objects within the container to the XML element.
                fill_object_contents(doc, list_item_element, object=self[i], blacklist=blacklist)
Exemple #8
0
    def to_xml(self, doc, element):
        """Create an XML element for the list data structure.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the list data structure XML element to.
        @type element:  XML element object
        """

        # Create the element and add it to the higher level element.
        list_element = doc.createElement(self.list_name)
        element.appendChild(list_element)

        # Set the list attributes.
        list_element.setAttribute('desc', self.list_desc)

        # Blacklisted objects.
        blacklist = ['list_name', 'list_desc', 'element_name', 'element_desc', 'blacklist'] + list(self.__dict__.keys()) + list(RelaxListType.__dict__.keys()) + list(self.__class__.__dict__.keys()) + list(list.__dict__.keys()) + list(list.__dict__.keys())

        # Add all simple python objects within the list to the list element.
        fill_object_contents(doc, list_element, object=self, blacklist=blacklist)

        # Loop over the list.
        for i in range(len(self)):
            # The element has its own to_xml() method.
            if hasattr(self[i], 'to_xml'):
                self[i].to_xml(doc, list_element)

            # Normal element.
            else:
                # Create an XML element for each container.
                list_item_element = doc.createElement(self.element_name)
                list_element.appendChild(list_item_element)
                list_item_element.setAttribute('index', repr(i))
                list_item_element.setAttribute('desc', self.element_desc)

                # Blacklisted objects.
                blacklist = list(self[i].__class__.__dict__.keys())

                # Add objects which have to_xml() methods.
                for name in dir(self[i]):
                    # Skip blacklisted objects.
                    if name in blacklist:
                        continue

                    # Skip special objects.
                    if search('^_', name):
                        continue

                    # Execute any to_xml() methods, and add that object to the blacklist.
                    obj = getattr(self[i], name)
                    if hasattr(obj, 'to_xml'):
                        obj.to_xml(doc, list_item_element)
                        blacklist = blacklist + [name]

                # Add all simple python objects within the container to the XML element.
                fill_object_contents(doc, list_item_element, object=self[i], blacklist=blacklist)
Exemple #9
0
    def to_xml(self, doc, element, pipe_type=None):
        """Create a XML element for the current data pipe.

        @param doc:         The XML document object.
        @type doc:          xml.dom.minidom.Document instance
        @param element:     The XML element to add the pipe XML element to.
        @type element:      XML element object
        @keyword pipe_type: The type of the pipe being converted to XML.
        @type pipe_type:    str
        """

        # Add all simple python objects within the PipeContainer to the global element.
        global_element = doc.createElement('global')
        element.appendChild(global_element)
        global_element.setAttribute(
            'desc', 'Global data located in the top level of the data pipe')
        fill_object_contents(
            doc,
            global_element,
            object=self,
            blacklist=[
                'align_tensors', 'diff_tensor', 'exp_info', 'interatomic',
                'hybrid_pipes', 'mol', 'pipe_type', 'structure'
            ] + list(self.__class__.__dict__.keys()))

        # Hybrid info.
        self.xml_create_hybrid_element(doc, element)

        # Add the experimental information.
        if hasattr(self, 'exp_info'):
            self.exp_info.to_xml(doc, element)

        # Add the diffusion tensor data.
        if hasattr(self, 'diff_tensor'):
            self.diff_tensor.to_xml(doc, element)

        # Add the alignment tensor data.
        if hasattr(self, 'align_tensors'):
            self.align_tensors.to_xml(doc, element)

        # Add the molecule-residue-spin data.
        self.mol.to_xml(doc, element, pipe_type=pipe_type)

        # Add the interatomic data.
        self.interatomic.to_xml(doc, element, pipe_type=pipe_type)

        # Add the structural data, if it exists.
        if hasattr(self, 'structure'):
            self.structure.to_xml(doc, element)
Exemple #10
0
    def to_xml(self, doc, element):
        """Create an XML element for the alignment tensors.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the alignment tensors XML element to.
        @type element:  XML element object
        """

        # Create the alignment tensors element and add it to the higher level element.
        tensor_list_element = doc.createElement('align_tensors')
        element.appendChild(tensor_list_element)

        # Set the alignment tensor attributes.
        tensor_list_element.setAttribute('desc', 'Alignment tensor list')

        # Add all simple python objects within the PipeContainer to the pipe element.
        fill_object_contents(doc,
                             tensor_list_element,
                             object=self,
                             blacklist=list(self.__class__.__dict__.keys()) +
                             list(list.__dict__.keys()))

        # Loop over the tensors.
        for i in range(len(self)):
            # Create an XML element for a single tensor.
            tensor_element = doc.createElement('align_tensor')
            tensor_list_element.appendChild(tensor_element)
            tensor_element.setAttribute('index', repr(i))
            tensor_element.setAttribute('desc', 'Alignment tensor')

            # The blacklist.
            blacklist = ['type', 'is_empty'] + list(
                self[i].__class__.__dict__.keys())
            for name in dir(self):
                if name not in self[i]._mod_attr:
                    blacklist.append(name)

            # Add all simple python objects within the PipeContainer to the pipe element.
            fill_object_contents(doc,
                                 tensor_element,
                                 object=self[i],
                                 blacklist=blacklist)
Exemple #11
0
    def to_xml(self, doc, element):
        """Create XML elements for the contents of this molecule container.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the molecule XML elements to.
        @type element:  XML element object
        """

        # Create an XML element for this molecule and add it to the higher level element.
        mol_element = doc.createElement('mol_cont')
        element.appendChild(mol_element)

        # Set the molecule attributes.
        mol_element.setAttribute('desc', 'Molecule container')
        mol_element.setAttribute('name', str(self.mol_name))

        # Add all simple python objects within the MolContainer to the XML element.
        fill_object_contents(doc, mol_element, object=self, blacklist=list(self.__class__.__dict__.keys()))
Exemple #12
0
    def to_xml(self, doc, element):
        """Create an XML element for the container.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the data container XML element to.
        @type element:  XML element object
        """

        # Create a new element for this container and add it to the higher level element.
        cont_element = doc.createElement(self.name)
        element.appendChild(cont_element)

        # Set the list attributes.
        cont_element.setAttribute('desc', self.desc)

        # Blacklisted objects.
        blacklist = self.blacklist + ['name', 'desc', 'blacklist'] + list(Element.__dict__.keys()) + list(self.__class__.__dict__.keys()) + list(object.__dict__.keys())

        # Store and blacklist the objects which have to_xml() methods.
        to_xml_list = []
        for name in dir(self):
            # Skip blacklisted objects.
            if name in blacklist:
                continue

            # Skip special objects.
            if search('^_', name):
                continue

            # Execute any to_xml() methods, and add that object to the blacklist.
            obj = getattr(self, name)
            if hasattr(obj, 'to_xml'):
                to_xml_list.append(obj)
                blacklist = blacklist + [name]

        # Add all simple python objects within the container to the XML element.
        fill_object_contents(doc, cont_element, object=self, blacklist=blacklist)

        # Execute the object to_xml() methods.
        for obj in to_xml_list:
            obj.to_xml(doc, cont_element)
Exemple #13
0
    def to_xml(self, doc, element):
        """Create an XML element for the container.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the data container XML element to.
        @type element:  XML element object
        """

        # Create a new element for this container and add it to the higher level element.
        cont_element = doc.createElement(self.name)
        element.appendChild(cont_element)

        # Set the list attributes.
        cont_element.setAttribute('desc', self.desc)

        # Blacklisted objects.
        blacklist = self.blacklist + ['name', 'desc', 'blacklist'] + list(Element.__dict__.keys()) + list(self.__class__.__dict__.keys()) + list(object.__dict__.keys())

        # Store and blacklist the objects which have to_xml() methods.
        to_xml_list = []
        for name in dir(self):
            # Skip blacklisted objects.
            if name in blacklist:
                continue

            # Skip special objects.
            if search('^_', name):
                continue

            # Execute any to_xml() methods, and add that object to the blacklist.
            obj = getattr(self, name)
            if hasattr(obj, 'to_xml'):
                to_xml_list.append(obj)
                blacklist = blacklist + [name]

        # Add all simple python objects within the container to the XML element.
        fill_object_contents(doc, cont_element, object=self, blacklist=blacklist)

        # Execute the object to_xml() methods.
        for obj in to_xml_list:
            obj.to_xml(doc, cont_element)
Exemple #14
0
    def to_xml(self, doc, element, pipe_type=None):
        """Create a XML element for the current data pipe.

        @param doc:         The XML document object.
        @type doc:          xml.dom.minidom.Document instance
        @param element:     The XML element to add the pipe XML element to.
        @type element:      XML element object
        @keyword pipe_type: The type of the pipe being converted to XML.
        @type pipe_type:    str
        """

        # Add all simple python objects within the PipeContainer to the global element.
        global_element = doc.createElement('global')
        element.appendChild(global_element)
        global_element.setAttribute('desc', 'Global data located in the top level of the data pipe')
        fill_object_contents(doc, global_element, object=self, blacklist=['align_tensors', 'diff_tensor', 'exp_info', 'interatomic', 'hybrid_pipes', 'mol', 'pipe_type', 'structure'] + list(self.__class__.__dict__.keys()))

        # Hybrid info.
        self.xml_create_hybrid_element(doc, element)

        # Add the experimental information.
        if hasattr(self, 'exp_info'):
            self.exp_info.to_xml(doc, element)

        # Add the diffusion tensor data.
        if hasattr(self, 'diff_tensor'):
            self.diff_tensor.to_xml(doc, element)

        # Add the alignment tensor data.
        if hasattr(self, 'align_tensors'):
            self.align_tensors.to_xml(doc, element)

        # Add the molecule-residue-spin data.
        self.mol.to_xml(doc, element, pipe_type=pipe_type)

        # Add the interatomic data.
        self.interatomic.to_xml(doc, element, pipe_type=pipe_type)

        # Add the structural data, if it exists.
        if hasattr(self, 'structure'):
            self.structure.to_xml(doc, element)
Exemple #15
0
    def to_xml(self, doc, element):
        """Create XML elements for the contents of this molecule container.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the molecule XML elements to.
        @type element:  XML element object
        """

        # Create an XML element for this molecule and add it to the higher level element.
        mol_element = doc.createElement('mol_cont')
        element.appendChild(mol_element)

        # Set the molecule attributes.
        mol_element.setAttribute('desc', 'Molecule container')
        mol_element.setAttribute('name', str(self.mol_name))

        # Add all simple python objects within the MolContainer to the XML element.
        fill_object_contents(doc,
                             mol_element,
                             object=self,
                             blacklist=list(self.__class__.__dict__.keys()))
Exemple #16
0
    def to_xml(self, doc, element):
        """Create XML elements for each model.

        @param doc:     The XML document object.
        @type doc:      xml.dom.minidom.Document instance
        @param element: The element to add the model XML elements to.
        @type element:  XML element object
        """

        # Loop over the models.
        for i in range(len(self)):
            # Create an XML element for this model and add it to the higher level element.
            model_element = doc.createElement('model')
            element.appendChild(model_element)

            # Set the model attributes.
            model_element.setAttribute('desc', 'Model container')
            model_element.setAttribute('num', str(self[i].num))

            # Add all simple python objects within the ModelContainer to the XML element.
            fill_object_contents(doc, model_element, object=self[i], blacklist=['num', 'mol'] + list(self[i].__class__.__dict__.keys()))

            # Add the molecule data.
            self[i].mol.to_xml(doc, model_element)
Exemple #17
0
    def to_xml(self, doc, element, pipe_type=None):
        """Create XML elements for each spin.

        @param doc:         The XML document object.
        @type doc:          xml.dom.minidom.Document instance
        @param element:     The element to add the spin XML elements to.
        @type element:      XML element object
        @keyword pipe_type: The type of the pipe being converted to XML.
        @type pipe_type:    str
        """

        # The specific analysis API object.
        api = specific_analyses.api.return_api(analysis_type=pipe_type)

        # Loop over the containers.
        for i in range(len(self)):
            # Create an XML element for this container and add it to the higher level element.
            interatom_element = doc.createElement('interatomic')
            element.appendChild(interatom_element)

            # Set the spin attributes.
            interatom_element.setAttribute('desc',
                                           'Interatomic data container')
            interatom_element.setAttribute('spin_id1', str(self[i].spin_id1))
            interatom_element.setAttribute('spin_id2', str(self[i].spin_id2))

            # Get the specific object names and loop over them to get their descriptions.
            object_info = []
            try:
                for name in api.data_names(error_names=True, sim_names=True):
                    # Get the description.
                    if hasattr(api, 'return_data_desc'):
                        desc = api.return_data_desc(name)
                    else:
                        desc = None

                    # Append the two.
                    object_info.append([name, desc])
            except RelaxImplementError:
                pass

            # Add the ordered objects.
            blacklist = []
            for name, desc in object_info:
                # Add the name to the blacklist.
                blacklist.append(name)

                # Skip the object if it is missing from the InteratomContainer.
                if not hasattr(self[i], name):
                    continue

                # Create a new element for this object, and add it to the main element.
                sub_element = doc.createElement(name)
                interatom_element.appendChild(sub_element)

                # Add the object description.
                if desc:
                    sub_element.setAttribute('desc', desc)

                # Get the object.
                object = getattr(self[i], name)

                # Convert to XML.
                object_to_xml(doc, sub_element, value=object)

            # Add all simple python objects within the InteratomContainer to the XML element.
            fill_object_contents(doc,
                                 interatom_element,
                                 object=self[i],
                                 blacklist=['spin_id1', 'spin_id2'] +
                                 blacklist +
                                 list(self[i].__class__.__dict__.keys()))
Exemple #18
0
    def to_xml(self, file, pipes=None):
        """Create a XML document representation of the current data pipe.

        This method creates the top level XML document including all the information needed
        about relax, calls the PipeContainer.xml_write() method to fill in the document contents,
        and writes the XML into the file object.

        @param file:        The open file object.
        @type file:         file
        @param pipes:       The name of the pipe, or list of pipes to place in the XML file.
        @type pipes:        str or list of str
        """

        # The pipes to include in the XML file.
        all = False
        if not pipes:
            all = True
            pipes = list(self.keys())
        elif isinstance(pipes, str):
            pipes = [pipes]

        # Sort the pipes.
        pipes.sort()

        # Create the XML document object.
        xmldoc = xml.dom.minidom.Document()

        # Create the top level element, including the relax URL.
        top_element = xmldoc.createElementNS('http://www.nmr-relax.com',
                                             'relax')
        top_element.setAttribute("xmlns", "http://www.nmr-relax.com")

        # Append the element.
        xmldoc.appendChild(top_element)

        # Set the relax version number, and add a creation time.
        top_element.setAttribute('version', version.version)
        top_element.setAttribute('time', asctime())
        top_element.setAttribute('file_version', "2")
        if version.repo_head:
            top_element.setAttribute('head', version.repo_head)
        if version.repo_url:
            top_element.setAttribute('url',
                                     version.repo_url.replace('\n', '; '))

        # Add all objects in the data store base object to the XML element.
        if all:
            blacklist = list(self.__class__.__dict__.keys()) + list(
                dict.__dict__.keys())
            for name in dir(self):
                # Skip blacklisted objects.
                if name in blacklist:
                    continue

                # Skip special objects.
                if search('^_', name):
                    continue

                # Execute any to_xml() methods, and add that object to the blacklist.
                obj = getattr(self, name)
                if hasattr(obj, 'to_xml'):
                    obj.to_xml(xmldoc, top_element)
                    blacklist = blacklist + [name]

            # Remove the current data pipe from the blacklist!
            blacklist.remove('current_pipe')

            # Add all simple python objects within the store.
            fill_object_contents(xmldoc,
                                 top_element,
                                 object=self,
                                 blacklist=blacklist)

        # Loop over the pipes.
        for pipe in pipes:
            # Create the pipe XML element and add it to the top level XML element.
            pipe_element = xmldoc.createElement('pipe')
            top_element.appendChild(pipe_element)

            # Set the data pipe attributes.
            pipe_element.setAttribute('desc',
                                      'The contents of a relax data pipe')
            pipe_element.setAttribute('name', pipe)
            pipe_element.setAttribute('type', self[pipe].pipe_type)

            # Fill the data pipe XML element.
            self[pipe].to_xml(xmldoc,
                              pipe_element,
                              pipe_type=self[pipe].pipe_type)

        # Write out the XML file.
        file.write(xmldoc.toprettyxml(indent='    '))
Exemple #19
0
    def to_xml(self, file, pipes=None):
        """Create a XML document representation of the current data pipe.

        This method creates the top level XML document including all the information needed
        about relax, calls the PipeContainer.xml_write() method to fill in the document contents,
        and writes the XML into the file object.

        @param file:        The open file object.
        @type file:         file
        @param pipes:       The name of the pipe, or list of pipes to place in the XML file.
        @type pipes:        str or list of str
        """

        # The pipes to include in the XML file.
        all = False
        if not pipes:
            all = True
            pipes = list(self.keys())
        elif isinstance(pipes, str):
            pipes = [pipes]

        # Sort the pipes.
        pipes.sort()

        # Create the XML document object.
        xmldoc = xml.dom.minidom.Document()

        # Create the top level element, including the relax URL.
        top_element = xmldoc.createElementNS('http://www.nmr-relax.com', 'relax')
        top_element.setAttribute("xmlns", "http://www.nmr-relax.com")

        # Append the element.
        xmldoc.appendChild(top_element)

        # Set the relax version number, and add a creation time.
        top_element.setAttribute('version', version.version)
        top_element.setAttribute('time', asctime())
        top_element.setAttribute('file_version', "2")
        if version.repo_revision:
            top_element.setAttribute('revision', version.repo_revision)
        if version.repo_url:
            top_element.setAttribute('url', version.repo_url)

        # Add all objects in the data store base object to the XML element.
        if all:
            blacklist = list(self.__class__.__dict__.keys()) + list(dict.__dict__.keys())
            for name in dir(self):
                # Skip blacklisted objects.
                if name in blacklist:
                    continue

                # Skip special objects.
                if search('^_', name):
                    continue

                # Execute any to_xml() methods, and add that object to the blacklist.
                obj = getattr(self, name)
                if hasattr(obj, 'to_xml'):
                    obj.to_xml(xmldoc, top_element)
                    blacklist = blacklist + [name]

            # Remove the current data pipe from the blacklist!
            blacklist.remove('current_pipe')

            # Add all simple python objects within the store.
            fill_object_contents(xmldoc, top_element, object=self, blacklist=blacklist)

        # Loop over the pipes.
        for pipe in pipes:
            # Create the pipe XML element and add it to the top level XML element.
            pipe_element = xmldoc.createElement('pipe')
            top_element.appendChild(pipe_element)

            # Set the data pipe attributes.
            pipe_element.setAttribute('desc', 'The contents of a relax data pipe')
            pipe_element.setAttribute('name', pipe)
            pipe_element.setAttribute('type', self[pipe].pipe_type)

            # Fill the data pipe XML element.
            self[pipe].to_xml(xmldoc, pipe_element, pipe_type=self[pipe].pipe_type)

        # Write out the XML file.
        file.write(xmldoc.toprettyxml(indent='    '))
Exemple #20
0
    def to_xml(self, doc, element, pipe_type=None):
        """Create XML elements for each spin.

        @param doc:         The XML document object.
        @type doc:          xml.dom.minidom.Document instance
        @param element:     The element to add the spin XML elements to.
        @type element:      XML element object
        @keyword pipe_type: The type of the pipe being converted to XML.
        @type pipe_type:    str
        """

        # The specific analysis API object.
        api = specific_analyses.api.return_api(analysis_type=pipe_type)

        # Loop over the spins.
        for i in range(len(self)):
            # Create an XML element for this spin and add it to the higher level element.
            spin_element = doc.createElement('spin')
            element.appendChild(spin_element)

            # Set the spin attributes.
            spin_element.setAttribute('desc', 'Spin container')
            spin_element.setAttribute('name', str(self[i].name))
            spin_element.setAttribute('num', str(self[i].num))

            # Get the spin specific object names and loop over them to get their descriptions.
            object_info = []
            try:
                for name in api.data_names(error_names=True, sim_names=True):
                    # Get the description.
                    if hasattr(api, 'return_data_desc'):
                        desc = api.return_data_desc(name)
                    else:
                        desc = None

                    # Append the two.
                    object_info.append([name, desc])
            except RelaxImplementError:
                pass

            # Add the ordered objects.
            blacklist = []
            for name, desc in object_info:
                # Add the name to the blacklist.
                blacklist.append(name)

                # Skip the object if it is missing from the SpinContainer.
                if not hasattr(self[i], name):
                    continue

                # Create a new element for this object, and add it to the main element.
                sub_element = doc.createElement(name)
                spin_element.appendChild(sub_element)

                # Add the object description.
                if desc:
                    sub_element.setAttribute('desc', desc)

                # Get the object.
                object = getattr(self[i], name)

                # Convert to XML.
                object_to_xml(doc, sub_element, value=object)

            # Add all simple python objects within the SpinContainer to the XML element.
            fill_object_contents(doc, spin_element, object=self[i], blacklist=['name', 'num', 'spin'] + blacklist + list(self[i].__class__.__dict__.keys()))