UserDefinitionsStore.py 3.77 KB
Newer Older
eric pellegrini's avatar
eric pellegrini committed
1
2
3
4
5
6
7
8
9
10
11
import cPickle
import os

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

class UserDefinitionsStoreError(Error):
    pass


12
class UserDefinitionsStore(dict):
eric pellegrini's avatar
eric pellegrini committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    '''
    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 
    user definitions are loaded when MDANSE starts through a cPickle file that will store these definitions.
    
    They are stored internally as MDANSE.Framework.UserDefinition.IUserDefinition derived objects in a nested 
    dictionary whose primary key is the target name, secondary key is the category of the user definition and 
    tertiary key is the name of the user definition.  
    '''
    
    __metaclass__ = Singleton
        
    UD_PATH = os.path.join(PLATFORM.application_directory(),"user_definitions.ud")
    
    def __init__(self):
        
32
        dict.__init__(self)
eric pellegrini's avatar
eric pellegrini committed
33
34
35
36
37
38
39
40
41
42
        
        self.load()                                               
                       
    def load(self):
        '''
        Load the user definitions.
        '''

        if not os.path.exists(UserDefinitionsStore.UD_PATH):
            return
43

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

        ud = self[target][section][name]
eric pellegrini's avatar
eric pellegrini committed
85
            
86
        return ud
eric pellegrini's avatar
eric pellegrini committed
87
        
88
    def remove_definition(self,target,section,name):
89
        
90
91
        if self.has_definition(target, section, name):
            del self[target][section][name]
eric pellegrini's avatar
eric pellegrini committed
92
                                                   
93
    def set_definition(self, target, section, name, value):
94
                        
95
        if self.has_definition(target, section, name):
96
            raise UserDefinitionsStoreError('Item %s is already registered as an user definition. You must delete it before setting it.' % (target,section,name))
97
98

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

100
    def filter(self,target,section):
eric pellegrini's avatar
eric pellegrini committed
101
        
102
103
        return self.get(target,{}).get(section,{}).keys()
            
104
105
106
107
    def has_definition(self,target,section,name):
        
        if not self.has_key(target):
            return False
108
109
                
        if not self[target].has_key(section):
110
111
112
            return False
        
        return self[target][section].has_key(name)
eric pellegrini's avatar
eric pellegrini committed
113
114
        
UD_STORE = UserDefinitionsStore()