IUserDefinition.py 3.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#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
11
#aoun[at]ill.fr
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#
#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 Mar 27, 2015
29

30
:author: Eric C. Pellegrini
31
32
'''

33
import time
34

eric pellegrini's avatar
eric pellegrini committed
35
from MDANSE import REGISTRY
36
37
from MDANSE.Core.Error import Error

38
39
40
class UserDefinitionError(Error):
    pass

eric pellegrini's avatar
eric pellegrini committed
41
class IUserDefinition(dict):
42
    '''
43
44
45
46
47
    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. 
48
    '''
49
50
51
52
53
54
55
56
57
58
    
    # 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 = []
    
eric pellegrini's avatar
eric pellegrini committed
59
    def __init__(self, target, **kwargs):
60
61
62
63
64
65
66
67
        '''
        The constructor
        '''
        
        self._target = target
        
        self._creationTime = time.ctime()
        
eric pellegrini's avatar
eric pellegrini committed
68
69
        for k in self._keywords:
            try:
70
                self[k] = kwargs[k]
eric pellegrini's avatar
eric pellegrini committed
71
72
            except KeyError:
                raise UserDefinitionError("Incomplete user definable object: missing %r key" % k)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
                                                                                        
    @property
    def target(self):
        
        return self._target

    @classmethod
    def keywords(self):
        
        return self._keywords

    @property
    def creationTime(self):
        
        return self._creationTime

89
90
    def __setitem__(self, item, value):
        
91
92
        if item in self._keywords:
            dict.__setitem__(self,item,value)
93
94
95
96
97
98
        
    def __str__(self):
        '''
        Return the informal representation of a user definable object
        '''
        
99
        info = ["Created on: %s\n" % self.creationTime] + ["%s:\n%s\n" % (k,self[k]) for k in self._keywords] 
100
101
        
        return "".join(info)