Warning, file /pilot2/pilot/user/atlas/nordugrid.py was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 import re
0011 import logging
0012 from xml.dom import minidom
0013 from xml.etree import ElementTree
0014
0015 logger = logging.getLogger(__name__)
0016
0017
0018 class XMLDictionary(object):
0019 """
0020 This is a helper class that is used to create the dictionary which is converted to the special XML files for
0021 Nordugrid pilots.
0022 Example dictionary:
0023 dictionary = { "outfiles": [ { "file": { "surl": "some_surl", "size": "123", "ad32": "aaaaaaa",
0024 "guid": "ababa22", "lfn": "some_lfn", "dataset": "some_dataset",
0025 "date": "11/11/11" } },
0026 {}, {}, ..
0027 ]
0028 }
0029
0030 Usage:
0031 xmldic = XMLDictionary()
0032 xmldic.add_to_list({"surl": "some_surl1", "size": "123", "ad32": "aaaaaaa", "guid": "ababa22", "lfn": "some_lfn",
0033 "dataset": "some_dataset", "date": "11/11/11"})
0034 dictionary = xmldic.get_dictionary()
0035 """
0036
0037 _dictionary = None
0038
0039 def __init__(self, rootname="outfiles"):
0040 """
0041 Standard init function.
0042 :param rootname: name of the root key. There is only one root key in the Nordugrid XML file ('outfiles').
0043 """
0044 self._dictionary = {}
0045 self._dictionary[rootname] = []
0046
0047 def add_to_list(self, dictionary, rootname="outfiles", itemname="file"):
0048 """
0049 Add dictionary to itemname key. See example in class header.
0050 :param dictionary: dictionary to add to itemname key.
0051 :param rootname: name of the root key. There is only one root key in the Nordugrid XML file ('outfiles').
0052 :param itemname: name of the item key. In the Nordugrid XML it should be called 'file'.
0053 :return:
0054 """
0055 if isinstance(self._dictionary, dict):
0056 if isinstance(self._dictionary[rootname], list):
0057 _dic = {itemname: dictionary}
0058 self._dictionary[rootname].append(_dic)
0059 else:
0060 pass
0061 else:
0062 logger.info("not a dictionary: %s", str(self._dictionary))
0063
0064 def get_dictionary(self):
0065 """
0066 Return the dictionary to be converted to XML.
0067 It should be populated with the dictionary added to it in add_to_list().
0068 :return: dictionary
0069 """
0070 return self._dictionary
0071
0072
0073 def convert_to_xml(dictionary):
0074 """
0075 Convert a dictionary to XML.
0076 The dictionary is expected to follow the Nordugrid format. See the XMLDictionary helper class.
0077
0078 Example of XML (OutputFiles.xml):
0079
0080 <?xml version="1.0" ?>
0081 <outfiles>
0082 <file>
0083 <ad32>aaaaaaa</ad32>
0084 <surl>some_surl1</surl>
0085 <lfn>some_lfn</lfn>
0086 <dataset>some_dataset</dataset>
0087 <date>11/11/11</date>
0088 <guid>ababa22</guid>
0089 <size>123</size>
0090 </file>
0091 </outfiles>
0092
0093 :param dictionary: dictionary created with XMLDictionary.
0094 :return: xml (pretty printed for python >= 2.7 - for older python, use the convert_to_prettyprint() function).
0095 """
0096
0097 failed = False
0098
0099 single_file_tag = list(dictionary.keys())
0100 if len(single_file_tag) != 1:
0101 logger.warning("unexpected format - expected single entry, got %d entries", len(single_file_tag))
0102 logger.warning('dictionary = %s', str(dictionary))
0103 return None
0104
0105 file_tag = single_file_tag[0]
0106 root = ElementTree.Element(file_tag)
0107
0108 file_list = dictionary[file_tag]
0109 if isinstance(file_list, list):
0110 for file_entry in file_list:
0111 if isinstance(file_entry, dict) and len(file_entry) == 1:
0112 single_entry = list(file_entry.keys())[0]
0113
0114
0115 file_element = ElementTree.Element(single_entry)
0116 root.append(file_element)
0117
0118 file_dictionary = file_entry[single_entry]
0119 if isinstance(file_dictionary, dict):
0120 for dictionary_entry in list(file_dictionary.keys()):
0121
0122 entry = ElementTree.SubElement(file_element, dictionary_entry)
0123 entry.text = file_dictionary[dictionary_entry]
0124 else:
0125 logger.warning("unexpected format - expected a dictionary, got %s", str(file_dictionary))
0126 failed = True
0127 else:
0128 logger.warning("unexpected format - expected a length 1 dictionary, got %s", str(file_entry))
0129 failed = True
0130 else:
0131 logger.warning("unexpected format - expected a list, got %s", str(file_list))
0132 failed = True
0133
0134 if failed:
0135 return None
0136
0137
0138 return minidom.parseString(ElementTree.tostring(root)).toprettyxml(indent=" ")
0139
0140
0141 def convert_to_prettyprint(xmlstr):
0142 """
0143 Convert XML to pretty print for older python versions (< 2.7).
0144 :param xmlstr: input XML string
0145 :return: XML string (pretty printed)
0146 """
0147
0148 text_re = re.compile(r'>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL)
0149 return text_re.sub(r'>\g<1></', xmlstr)