source: coopr.colin/trunk/coopr/colin/core/colin_xml_io.py @ 2201

Last change on this file since 2201 was 2201, checked in by wehart, 10 years ago

Update to Coopr to account for changes in PyUtilib? package names.

File size: 2.6 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11"""
12Define the plugin for COLIN XML IO
13"""
14
15from pyutilib.component.core import *
16from problem_io import *
17import xml.dom.minidom
18from pyutilib.misc import get_xml_text, tostr
19
20
21class ColinXmlIO(SingletonPlugin):
22    """The reader/writer for the COLIN XML IO Formats"""
23
24    implements(IOptProblemIO)
25
26    def __init__(self):
27        SingletonPlugin.__init__(self)
28        self.name = 'colin'
29
30    def read(self, filename, point):
31        """
32        Read a point and request information.
33        This method returns a tuple: point, requests
34        """
35        input_doc = xml.dom.minidom.parse(filename)
36        point = point.process(input_doc.getElementsByTagName("Domain")[0])
37        requests = self._handleRequests(input_doc.getElementsByTagName("Requests")[0])
38        return point, requests
39
40    def _handleRequests(self, node):
41        """
42        A function that processes the requests
43        """
44        requests = {}
45        for child in node.childNodes:
46            if child.nodeType == node.ELEMENT_NODE:
47                tmp = {}
48                for (name,value) in child.attributes.items():
49                    tmp[name]=value
50                requests[str(child.nodeName)] = tmp
51        return requests
52
53    def write(self, filename, response):
54        """
55        Write response information to a file.
56        """
57        output_doc = self._process(response)
58        OUTPUT = open(filename,"w")
59        output_doc.writexml(OUTPUT," "," ","\n","UTF-8")
60        OUTPUT.close()
61
62    def _process(self, response):
63        """
64        Process the XML document
65        """
66        doc = xml.dom.minidom.Document()
67        root = doc.createElement("ColinResponse")
68        doc.appendChild(root)
69        for key in response:
70            elt = doc.createElement(str(key))
71            root.appendChild(elt)
72            if isinstance(response[key], basestring):
73                text_elt = doc.createTextNode( response[key] )
74            elif type(response[key]) in [int,long,float]:
75                text_elt = doc.createTextNode( str(response[key]) )
76            else:
77                text_elt = doc.createTextNode( tostr(response[key]) )
78            elt.appendChild(text_elt)
79        return doc
80
81
Note: See TracBrowser for help on using the repository browser.