Commit 6cd66db1 authored by eric pellegrini's avatar eric pellegrini
Browse files

Added docstrings to MDANSE.Core classes

Added example_data_directory method to Platform class
Bug fix in mdanse console script
parent e950cb10
......@@ -164,5 +164,4 @@ class ClassRegistry(abc.ABCMeta):
:type cls: ClassRegistry
'''
return sorted(cls._registry.keys())
\ No newline at end of file
'''
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
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.
#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
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
Created on Mar 23, 2015
'''
Created on Mar 30, 2015
@author: pellegrini
'''
import collections
from MDANSE.Core.Error import Error
class ConfigurationError(Error):
'''
Handles the exception that may occurs when configuring an object that derives from MDANSE.Core.Configurable class.
'''
pass
import collections
class Configurable(object):
'''
This class allows any object that derives from it to be configurable within the MDANSE framework.
Within that framework, the object will also have to define the class attribute 'configurators' as
a MDANSE.Framework.Configurators.ConfiguratorsDict object to complete its configuration.
'''
configurators = collections.OrderedDict()
def __init__(self):
'''
Constructor
'''
self._configuration = {}
......@@ -47,30 +61,41 @@ class Configurable(object):
def __getitem__(self, name):
"""
Returns a configuration item given its name
Returns a configuration item given its name.
:param name: the name of the configuration item
:type name: str
If not found raise a ConfigurationError.
"""
if self.configurators.has_key(name):
return self._configuration.setdefault(name,{})
if not self.configurators.has_key(name):
raise ConfigurationError("The item %r is not valid for this configuration." % name)
raise ConfigurationError("The item %r is not valid for this configuration." % name)
return self._configuration.setdefault(name,{})
def setup(self,parameters):
"""
Setup the configuration given a set of parameters
"""
'''
Setup the configuration according to a set of input parameters.
:param parameters: the input parameters
:type parameters: dict
'''
# Cleans the previous configuration
self._configuration.clear()
self._configured=False
# If no configurator has to be configured, just return
if not self.configurators:
self._configured=True
return
if isinstance(parameters,dict):
# Loop over the configuration items
for k,v in self.configurators.items():
# If no input parameter has been set for this item, use its default value.
if not parameters.has_key(k):
parameters[k] = v.default
else:
......@@ -104,6 +129,12 @@ class Configurable(object):
self._configured=True
def __str__(self):
'''
Returns the informations about the current configuration in text form.
:return: the informations about the current configuration in text form
:rtype: str
'''
if not self._configured:
return "Not yet configured"
......
......@@ -210,6 +210,14 @@ class Platform(object):
return os.getpid()
def example_data_directory(self):
"""Returns the path for mdanse data.
@return: the path for mdanse package.
@rtype: string
"""
return os.path.dirname(self.package_directory())
def package_directory(self):
"""Returns the path for mdanse package.
......
......@@ -44,7 +44,7 @@ class MMTKNetCDFTrajectoryConfigurator(InputFileConfigurator):
type = 'mmtk_trajectory'
_default = ''
_default = os.path.join(PLATFORM.example_data_directory(),'Data','Trajectories','MMTK','waterbox_in_periodic_universe.nc')
def configure(self, configuration, value):
......
......@@ -244,11 +244,11 @@ class CommandLineParser(optparse.OptionParser):
filename = os.path.abspath('template_%s.py' % name.lower())
# Try to save the template for the job.
try:
jobs[name].save_job(filename)
jobs[name].save(filename)
# Case where an error occured when writing the template.
except IOError:
raise CommandLineParserError("Could not write the job template as %r" % filename)
# If the job class has no save_job method, thisis not a valid nMolDyn job.
# If the job class has no save method, thisis not a valid nMolDyn job.
except KeyError:
raise CommandLineParserError("The job %r is not a valid MDANSE job" % name)
# Otherwise, print some information about the saved template.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment