UserDefinitionStore.py 4 KB
Newer Older
eric pellegrini's avatar
eric pellegrini committed
1
2
3
4
5
6
7
import cPickle
import os

from MDANSE import PLATFORM
from MDANSE.Core.Error import Error
from MDANSE.Core.Singleton import Singleton

8
class UserDefinitionStoreError(Error):
eric pellegrini's avatar
eric pellegrini committed
9
10
11
    pass


12
class UserDefinitionStore(dict):
eric pellegrini's avatar
eric pellegrini committed
13
14
15
16
17
18
    '''
    This class is used to register, save and delete MDANSE user definitions (a.k.a. UD).
    
    Basically, user definitions are used to keep track of definitions made on a given target. The target can 
    be a file or any kind of object that has to be associated with the user definitions.
    This definitions can be selections of atoms, Q vectors definitions, axis definitions ... The 
19
    user definitions are loaded when MDANSE starts through a cPickle file that will store these definitions.    
eric pellegrini's avatar
eric pellegrini committed
20
21
22
23
24
25
26
27
    '''
    
    __metaclass__ = Singleton
        
    UD_PATH = os.path.join(PLATFORM.application_directory(),"user_definitions.ud")
    
    def __init__(self):
        
28
        dict.__init__(self)
eric pellegrini's avatar
eric pellegrini committed
29
30
31
32
33
34
35
36
        
        self.load()                                               
                       
    def load(self):
        '''
        Load the user definitions.
        '''

37
        if not os.path.exists(UserDefinitionStore.UD_PATH):
eric pellegrini's avatar
eric pellegrini committed
38
            return
39

40
41
        # Try to open the UD file.
        try:
42
            f = open(UserDefinitionStore.UD_PATH, "rb")
43
            UD = cPickle.load(f)
44
  
45
46
47
        # If for whatever reason the pickle file loading failed do not even try to restore it
        except:
            return
48
          
49
        else:
50
            self.update(UD)
51
            f.close()
52
                                    
eric pellegrini's avatar
eric pellegrini committed
53
54
55
56
57
58
59
60
61
    def save(self):
        '''
        Save the user definitions.
        
        :param path: the path of the user definitions file.
        :type path: str
        '''
                    
        try:                        
62
            f = open(UserDefinitionStore.UD_PATH, "wb")
eric pellegrini's avatar
eric pellegrini committed
63
64
65
66
67
68
        except IOError:
            return
        else:
            cPickle.dump(self, f, protocol=2)
            f.close()
                                    
69
    def get_definition(self, target, section, name):
eric pellegrini's avatar
eric pellegrini committed
70
71
72
73
        '''
        Returns a user definition given its target, category and its name.
                
        :return: the user definition if it found or None otherwise
74
        :rtype: any
eric pellegrini's avatar
eric pellegrini committed
75
76
        '''
            
77
        if not self.has_definition(target,section,name):
78
            raise UserDefinitionStoreError('The item %r could not be found' % (target,section,name))
79
80

        ud = self[target][section][name]
eric pellegrini's avatar
eric pellegrini committed
81
            
82
        return ud
83
84
85
86
87
88
89
90
91
92

    def remove_target(self,target):
        
        if self.has_target(target):
            del self[target]

    def remove_section(self,target,section):
        
        if self.has_section(target, section):
            del self[target][section]
eric pellegrini's avatar
eric pellegrini committed
93
        
94
    def remove_definition(self,target,section,name):
95
        
96
97
        if self.has_definition(target, section, name):
            del self[target][section][name]
eric pellegrini's avatar
eric pellegrini committed
98
                                                   
99
    def set_definition(self, target, section, name, value):
100
                        
101
        if self.has_definition(target, section, name):
102
            raise UserDefinitionStoreError('Item %s is already registered as an user definition. You must delete it before setting it.' % (target,section,name))
103
104

        self.setdefault(target,{}).setdefault(section,{})[name] = value                                          
eric pellegrini's avatar
eric pellegrini committed
105

106
    def filter(self,target,section):
eric pellegrini's avatar
eric pellegrini committed
107
        
108
        return self.get(target,{}).get(section,{}).keys()
109
110
111
112
113
114
115
116
117
118
119

    def has_target(self,target):
                
        return self.has_key(target)

    def has_section(self,target,section):
        
        if not self.has_key(target):
            return False
                
        return self[target].has_key(section)
120
            
121
122
123
124
    def has_definition(self,target,section,name):
        
        if not self.has_key(target):
            return False
125
126
                
        if not self[target].has_key(section):
127
128
129
            return False
        
        return self[target][section].has_key(name)
eric pellegrini's avatar
eric pellegrini committed
130
        
131
UD_STORE = UserDefinitionStore()