Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/galaxy/util/tool_shed/xml_util.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import logging | |
2 import os | |
3 import tempfile | |
4 | |
5 from galaxy.util import ( | |
6 etree, | |
7 parse_xml as galaxy_parse_xml, | |
8 unicodify, | |
9 xml_to_string, | |
10 ) | |
11 | |
12 log = logging.getLogger(__name__) | |
13 | |
14 | |
15 def create_and_write_tmp_file(elem): | |
16 tmp_str = xml_to_string(elem, pretty=True) | |
17 with tempfile.NamedTemporaryFile(prefix="tmp-toolshed-cawrf", delete=False) as fh: | |
18 tmp_filename = fh.name | |
19 with open(tmp_filename, mode='w', encoding='utf-8') as fh: | |
20 fh.write(tmp_str) | |
21 return tmp_filename | |
22 | |
23 | |
24 def create_element(tag, attributes=None, sub_elements=None): | |
25 """ | |
26 Create a new element whose tag is the value of the received tag, and whose attributes are all | |
27 key / value pairs in the received attributes and sub_elements. | |
28 """ | |
29 if tag: | |
30 elem = etree.Element(tag) | |
31 if attributes: | |
32 # The received attributes is an odict to preserve ordering. | |
33 for k, v in attributes.items(): | |
34 elem.set(k, v) | |
35 if sub_elements: | |
36 # The received attributes is an odict. These handle information that tends to be | |
37 # long text including paragraphs (e.g., description and long_description. | |
38 for k, v in sub_elements.items(): | |
39 # Don't include fields that are blank. | |
40 if v: | |
41 if k == 'packages': | |
42 # The received sub_elements is an odict whose key is 'packages' and whose | |
43 # value is a list of ( name, version ) tuples. | |
44 for v_tuple in v: | |
45 sub_elem = etree.SubElement(elem, 'package') | |
46 sub_elem_name, sub_elem_version = v_tuple | |
47 sub_elem.set('name', sub_elem_name) | |
48 sub_elem.set('version', sub_elem_version) | |
49 elif isinstance(v, list): | |
50 sub_elem = etree.SubElement(elem, k) | |
51 # If v is a list, then it must be a list of tuples where the first | |
52 # item is the tag and the second item is the text value. | |
53 for v_tuple in v: | |
54 if len(v_tuple) == 2: | |
55 v_tag = v_tuple[0] | |
56 v_text = v_tuple[1] | |
57 # Don't include fields that are blank. | |
58 if v_text: | |
59 v_elem = etree.SubElement(sub_elem, v_tag) | |
60 v_elem.text = v_text | |
61 else: | |
62 sub_elem = etree.SubElement(elem, k) | |
63 sub_elem.text = v | |
64 return elem | |
65 return None | |
66 | |
67 | |
68 def parse_xml(file_name, check_exists=True): | |
69 """Returns a parsed xml tree with comments intact.""" | |
70 error_message = '' | |
71 if check_exists and not os.path.exists(file_name): | |
72 return None, "File does not exist %s" % str(file_name) | |
73 try: | |
74 tree = galaxy_parse_xml(file_name, remove_comments=False, strip_whitespace=False) | |
75 except OSError: | |
76 raise | |
77 except Exception as e: | |
78 error_message = "Exception attempting to parse {}: {}".format(str(file_name), unicodify(e)) | |
79 log.exception(error_message) | |
80 return None, error_message | |
81 return tree, error_message |