def generate_tool_elem(self, tool_shed, repository_name, changeset_revision, owner, tool_file_path, tool, tool_section): """Create and return an ElementTree tool Element.""" if tool_section is not None: tool_elem = etree.SubElement(tool_section, 'tool') else: tool_elem = etree.Element('tool') tool_elem.attrib['file'] = tool_file_path if not tool.guid: raise ValueError("tool has no guid") tool_elem.attrib['guid'] = tool.guid tool_shed_elem = etree.SubElement(tool_elem, 'tool_shed') tool_shed_elem.text = tool_shed repository_name_elem = etree.SubElement(tool_elem, 'repository_name') repository_name_elem.text = repository_name repository_owner_elem = etree.SubElement(tool_elem, 'repository_owner') repository_owner_elem.text = owner changeset_revision_elem = etree.SubElement( tool_elem, 'installed_changeset_revision') changeset_revision_elem.text = changeset_revision id_elem = etree.SubElement(tool_elem, 'id') id_elem.text = tool.id version_elem = etree.SubElement(tool_elem, 'version') version_elem.text = tool.version return tool_elem
def create_element(tag, attributes=None, sub_elements=None): """ Create a new element whose tag is the value of the received tag, and whose attributes are all key / value pairs in the received attributes and sub_elements. """ if tag: elem = etree.Element(tag) if attributes: # The received attributes is an odict to preserve ordering. for k, v in attributes.items(): elem.set(k, v) if sub_elements: # The received attributes is an odict. These handle information that tends to be # long text including paragraphs (e.g., description and long_description. for k, v in sub_elements.items(): # Don't include fields that are blank. if v: if k == 'packages': # The received sub_elements is an odict whose key is 'packages' and whose # value is a list of ( name, version ) tuples. for v_tuple in v: sub_elem = etree.SubElement(elem, 'package') sub_elem_name, sub_elem_version = v_tuple sub_elem.set('name', sub_elem_name) sub_elem.set('version', sub_elem_version) elif isinstance(v, list): sub_elem = etree.SubElement(elem, k) # If v is a list, then it must be a list of tuples where the first # item is the tag and the second item is the text value. for v_tuple in v: if len(v_tuple) == 2: v_tag = v_tuple[0] v_text = v_tuple[1] # Don't include fields that are blank. if v_text: v_elem = etree.SubElement(sub_elem, v_tag) v_elem.text = v_text else: sub_elem = etree.SubElement(elem, k) sub_elem.text = v return elem return None
def generate_repository_info_elem(self, tool_shed, repository_name, changeset_revision, owner, parent_elem=None, **kwd): """Create and return an ElementTree repository info Element.""" if parent_elem is None: elem = etree.Element('tool_shed_repository') else: elem = etree.SubElement(parent_elem, 'tool_shed_repository') tool_shed_elem = etree.SubElement(elem, 'tool_shed') tool_shed_elem.text = tool_shed repository_name_elem = etree.SubElement(elem, 'repository_name') repository_name_elem.text = repository_name repository_owner_elem = etree.SubElement(elem, 'repository_owner') repository_owner_elem.text = owner changeset_revision_elem = etree.SubElement( elem, 'installed_changeset_revision') changeset_revision_elem.text = changeset_revision # add additional values # TODO: enhance additional values to allow e.g. use of dict values that will recurse for key, value in kwd.items(): new_elem = etree.SubElement(elem, key) new_elem.text = value return elem