Commit 9e4f94fa authored by eric pellegrini's avatar eric pellegrini
Browse files

Simplified the user definition store by removing the need of

IUserDefinitions interface when setting an ud
parent a3ab5dcd
#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
'''
Created on Mar 27, 2015
:author: Eric C. Pellegrini
'''
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
class AtomSelectionDefinition(IUserDefinition):
"""
This class defines a user definition for atom selection.
"""
type = "atom_selection"
_keywords = ['expression','indexes']
\ 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
#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
'''
Created on Mar 27, 2015
:author: Eric C. Pellegrini
'''
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
class AtomTransmutationDefinition(IUserDefinition):
'''
The user definable object used for storing atom transmutation.
'''
type = "atom_transmutation"
_keywords = ['expression','indexes','element']
#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
'''
Created on Mar 27, 2015
:author: Eric C. Pellegrini
'''
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
class AxisSelectionDefinition(IUserDefinition):
'''
The user definable object used for storing axis selection.
'''
type = "axis_selection"
_keywords = ['molecule','endpoint1','endpoint2']
#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
'''
Created on Mar 27, 2015
:author: Eric C. Pellegrini
'''
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
class BasisSelectionDefinition(IUserDefinition):
'''
The user definable object used for storing basis selection.
'''
type = "basis_selection"
_keywords = ['molecule','origin','x_axis','y_axis']
\ 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
#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
'''
Created on Mar 27, 2015
:author: Eric C. Pellegrini
'''
import time
from MDANSE import REGISTRY
from MDANSE.Core.Error import Error
class UserDefinitionError(Error):
pass
class IUserDefinition(dict):
'''
Base class for user definable objects. User definable objects are objects used in MDANSE for
storing information that can be reused later when setting up jobs (e.g. k-vectors defintion,
atom selection ...). A user definable object is a dictionary with a predefined set of keys that
depends on the user definable object. Object serialization is the underlying mechanism used to
save those objets and is done using cPickle module.
'''
# Any user definable object will be registered at MDANSE startup.
__metaclass__ = REGISTRY
# The base class has no type (hence it will not be registered)
type = "user_definition"
# The base class has no predefined set keywords
_keywords = []
def __init__(self, target, **kwargs):
'''
The constructor
'''
self._target = target
self._creationTime = time.ctime()
for k in self._keywords:
try:
self[k] = kwargs[k]
except KeyError:
raise UserDefinitionError("Incomplete user definable object: missing %r key" % k)
@property
def target(self):
return self._target
@classmethod
def keywords(self):
return self._keywords
@property
def creationTime(self):
return self._creationTime
def __setitem__(self, item, value):
if item in self._keywords:
dict.__setitem__(self,item,value)
def __str__(self):
'''
Return the informal representation of a user definable object
'''
info = ["Created on: %s\n" % self.creationTime] + ["%s:\n%s\n" % (k,self[k]) for k in self._keywords]
return "".join(info)
#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
'''
Created on Jun 10, 2015
:author: Eric C. Pellegrini
'''
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
class PartialChargesDefinition(IUserDefinition):
'''
The user definable object used for storing partial charges.
'''
type = "partial_charges"
_keywords = ['charges']
\ 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
#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
'''
Created on Mar 27, 2015
:author: Eric C. Pellegrini
'''
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
class QVectorsDefinition(IUserDefinition):
'''
The user definable object used for storing k vectors.
'''
type = "q_vectors"
_keywords = ['generator', 'parameters', 'q_vectors', 'is_lattice']
\ No newline at end of file
import os
import sys
sys.path.append(os.path.dirname(__file__))
\ No newline at end of file
...@@ -4,7 +4,6 @@ import os ...@@ -4,7 +4,6 @@ import os
from MDANSE import PLATFORM from MDANSE import PLATFORM
from MDANSE.Core.Error import Error from MDANSE.Core.Error import Error
from MDANSE.Core.Singleton import Singleton from MDANSE.Core.Singleton import Singleton
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
class UserDefinitionsStoreError(Error): class UserDefinitionsStoreError(Error):
pass pass
...@@ -76,7 +75,7 @@ class UserDefinitionsStore(dict): ...@@ -76,7 +75,7 @@ class UserDefinitionsStore(dict):
Returns a user definition given its target, category and its name. Returns a user definition given its target, category and its name.
:return: the user definition if it found or None otherwise :return: the user definition if it found or None otherwise
:rtype: a MDANSE.Framework.UserDefinitions.IUserDefinition.IUserDefinition derived class :rtype: any
''' '''
if not self.has_definition(target,section,name): if not self.has_definition(target,section,name):
...@@ -84,12 +83,6 @@ class UserDefinitionsStore(dict): ...@@ -84,12 +83,6 @@ class UserDefinitionsStore(dict):
ud = self[target][section][name] ud = self[target][section][name]
if ud.target != target:
raise UserDefinitionsStoreError('Target mismatch between %r and %r' % (target,ud.target))
if ud.type != section:
raise UserDefinitionsStoreError('Type mismatch between %r and %r' % (section,ud.type))
return ud return ud
def remove_definition(self,target,section,name): def remove_definition(self,target,section,name):
...@@ -99,9 +92,6 @@ class UserDefinitionsStore(dict): ...@@ -99,9 +92,6 @@ class UserDefinitionsStore(dict):
def set_definition(self, target, section, name, value): def set_definition(self, target, section, name, value):
if not isinstance(value,IUserDefinition):
raise UserDefinitionsStore('The input value is not a User definition.')
if self.has_definition(target, section, name): if self.has_definition(target, section, name):
raise UserDefinitionsStoreError('Item %s is already registered as an user definition. You must delete it before setting it.' % (target,section,name)) raise UserDefinitionsStoreError('Item %s is already registered as an user definition. You must delete it before setting it.' % (target,section,name))
......
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