OutputFilesConfigurator.py 4.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
28
Created on May 21, 2015
29

30
:author: Eric C. Pellegrini
31
32
33
34
35
36
37
38
39
'''

import os

from MDANSE import PLATFORM, REGISTRY
from MDANSE.Framework.Configurators.IConfigurator import IConfigurator, ConfiguratorError
                
class OutputFilesConfigurator(IConfigurator):
    """
40
41
42
43
44
45
46
47
    This configurator allows to define the output directory, the basename, and the format(s) of the output file(s) resulting from an 
    analysis.
    
    Once configured, this configurator will provide a list of files built by joining the given output directory, the basename and the 
    extensions corresponding to the input file formats.
    
    Currently MDANSE supports ASCII, NetCDF and SVG file formats. To define a new output file format for an analysis, you must inherit from
    MDANSE.Framework.Formats.IFormat.IFormat interface.   
48
49
50
51
    """
    
    type = 'output_files'
    
52
    _default = ('.', "output", ["netcdf"])
53
54
                    
    def __init__(self, name, formats=None, **kwargs):
55
56
57
        '''
        Initializes the configurator.
        
58
        :param name: the name of the configurator as it will appear in the configuration.
59
60
61
        :type name: str
        :param formats: the list of output file formats suported.  
        :type formats: list of str
62
        '''
63
64
65
                        
        IConfigurator.__init__(self, name, **kwargs)

66
        self._formats = formats if formats is not None else OutputFilesConfigurator._default[2]
67
68
    
    def configure(self, configuration, value):
69
70
71
72
73
        '''
        Configure a set of output files for an analysis. 
                
        :param configuration: the current configuration.
        :type configuration: a MDANSE.Framework.Configurable.Configurable object
74
75
        :param value: the output files specifications. Must be a 3-tuple whose 1st element \
        if the output directory, 2nd element the basename and 3rd element a list of file formats.
76
77
        :type value: 3-tuple
        '''
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        
        dirname, basename, formats = value
                
        if not dirname:
            dirname = os.getcwd()
                
        if not basename:
            raise ConfiguratorError("empty basename for the output file.", self)
        
        root = os.path.join(dirname, basename)
        
        try:
            PLATFORM.create_directory(dirname)
        except:
            raise ConfiguratorError("the directory %r is not writable" % dirname)
                    
        if not formats:
            raise ConfiguratorError("no output formats specified", self)

        for fmt in formats:
            
            if not fmt in self._formats:
                raise ConfiguratorError("the output file format %r is not a valid output format" % fmt, self)
            
            if not REGISTRY["format"].has_key(fmt):
                raise ConfiguratorError("the output file format %r is not registered as a valid file format." % fmt, self)

        self["root"] = root
        self["formats"] = formats
        self["files"] = ["%s%s" % (root,REGISTRY["format"][f].extension) for f in formats]

    @property
    def formats(self):
111
112
113
114
115
116
        '''
        Returns the list of output file formats suported.
        
        :return: the list of file formats suported.
        :rtype: list of str
        '''
117
118
119
        return self._formats

    def get_information(self):
120
121
122
123
124
125
        '''
        Returns string information about this configurator.
        
        :return: the information about this configurator.
        :rtype: str
        '''
126
127
128
129
130
131
132
        
        info = ["Input files:\n"]
        for f in self["files"]:
            info.append(f)
            info.append("\n")
        
        return "".join(info)