~kevang/mnemosyne-proj/grade-shortcuts-improvements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
##############################################################################
#
# component_manager.py <Peter.Bienstman@UGent.be>
#
##############################################################################



##############################################################################
#
# ComponentManager
#
#   Manages the different components. Each component belongs to a type
#   (database, scheduler, card_type, card_type_widget, ...).
#
#   The component manager can also store relationships between components,
#   e.g. a card_type_widget is used for a certain card_type.
#
#   For certain components, many can be active at the same time (card types,
#   filters, function hooks, ...). For others, there can be only on active
#   at the same time, like schedule, database ... The idea is that the last
#   one registered takes preference. This means that e.g. the default
#   scheduler needs to be registered first.
#
##############################################################################

class ComponentManager():

    ##########################################################################
    #
    # __init__
    #
    ##########################################################################
    
    def __init__(self):
        
        self.components = {} # { used_for : {type : [component]} }



    ##########################################################################
    #
    # register
    #
    ##########################################################################

    def register(self, type, component, used_for=None):

        if not self.components.has_key(used_for):
            self.components[used_for] = {}

        if not self.components[used_for].has_key(type):
            self.components[used_for][type] = [component]
        else:
            if component not in self.components[type]:
                self.components[used_for][type].append(component)


            
    ##########################################################################
    #
    # unregister
    #
    ##########################################################################

    def unregister(self, type, component, used_for=None):

        self.components[used_for][type].remove(component)



    ##########################################################################
    #
    # get_all
    #
    #   For components for which there can be many active at once.
    #
    ##########################################################################
    
    def get_all(self, type, used_for=None):

        if type in self.components[used_for]:
            return self.components[used_for][type]
        else:
            return []


    
    ##########################################################################
    #
    # get_current
    #
    #   For component for which there can be only one active at one time.
    #
    ##########################################################################

    def get_current(self, type, used_for=None):

        if type in self.components[used_for]:
            return self.components[used_for][type][-1]
        else:
            return None




##############################################################################
#
# The component manager needs to be accessed by many different parts of the
# library, so we hold it in a global variable.
#
##############################################################################

print "Registering component manager"

component_manager = ComponentManager()



##############################################################################
#
# Convenience functions, for easier access from the outside world.
#
# Keep these?
#
##############################################################################

def get_database():
    return component_manager.get_current("database")

def get_scheduler():
    return component_manager.get_current("scheduler")

def get_ui_controller_main():
    return component_manager.get_current("ui_controller_main")

def get_ui_controller_review():
    return component_manager.get_current("ui_controller_review")

def get_card_types():
    return component_manager.get_all("card_type")

def get_card_type_by_id(id): # TODO: speed up with dict!
    for t in get_card_types():
        if t.id == id:
            return t
    
def get_fact_filters():
    return component_manager.get_all("fact_filter")