source: coopr.opt/trunk/coopr/opt/base/results.py @ 1897

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

Standardized the technique used for guessing a file's format from
its name.

Standardized plugin interfaces to sort the plugin names when they
are requested by the user.

File size: 2.1 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__all__ = [ 'IReaderRegistration', 'ReaderRegistration',
12            'IResultsReader', 'AbstractResultsReader',
13            'ReaderFactory' ]
14
15from pyutilib.plugin.core import *
16from pyutilib.plugin.config import *
17
18
19class IReaderRegistration(Interface):
20    """An interface for accessing"""
21
22    def create(self, name=None):
23        """Create a reader, optionally specifying the name"""
24
25    def type(self):
26        """The type of reader supported by this service"""
27
28
29def ReaderFactory(name=None, reader_name=None):
30    ep = ExtensionPoint(IReaderRegistration)
31    if name is None:
32        names = map(lambda x:x.name, ep())
33        names.sort()
34        return names
35    service = ep.service(name)
36    if service is None:
37        return None
38    return service.create(reader_name)
39
40
41class ReaderRegistration(Plugin):
42
43    implements(IReaderRegistration)
44
45    def __init__(self, type, cls):
46        self.name = type
47        self._type = type
48        self._cls = cls
49
50    def type(self):
51        return self._type
52
53    def create(self, name=None):
54        if name is None:
55            return self._cls()
56        return self._cls(name=name)
57
58
59class IResultsReader(Interface):
60    """Interface for classes that can read optimization results."""
61
62
63class AbstractResultsReader(ManagedPlugin):
64    """Base class that can read optimization results."""
65
66    implements(IResultsReader)
67
68    def __init__(self, results_format):
69        ManagedPlugin.__init__(self)
70        self.format=results_format
71
72    def __call__(self, filename, res=None): #pragma:nocover
73        raise TypeError, "Method __call__ undefined in reader for format "+str(self.format)
74
Note: See TracBrowser for help on using the repository browser.