1
1
import os
2
2
import gtk
3
-
3
+ import h5py
4
4
5
5
# Create a generic interface for displaying pages of settings
6
6
7
7
class Settings (object ):
8
8
9
- def __init__ (self ,parent = None ,page_classes = []):
9
+ def __init__ (self ,storage = 'hdf5' , file = None , parent = None ,page_classes = []):
10
10
self .pages = {}
11
+ self .instantiated_pages = {}
11
12
self .dialog_open = False
12
13
self .parent = parent
14
+ self .storage = storage
15
+ self .file = file
16
+ self .callback_list = []
17
+
18
+ if not self .file :
19
+ raise Exception ('You must specify a file to load/save preferences from' )
20
+
13
21
for c in page_classes :
14
22
self .add_settings_interface (c )
15
-
23
+
16
24
# This function can be called to add a interface
17
25
# Each one of these will display as a seperate page in the settings window
18
26
# You can not add a class more than once!
19
27
# Classes must have unique Class.name attributes! (This might change later...)
20
28
def add_settings_interface (self ,setting_class ):
21
29
if setting_class .name in self .pages :
22
30
return False
23
- self .pages [setting_class .name ] = setting_class ()
31
+
32
+ self .pages [setting_class .name ] = setting_class (self .load (setting_class .__name__ ))
24
33
return True
25
34
35
+ def load (self ,name ):
36
+ if self .storage == 'hdf5' :
37
+ with h5py .File (self .file ,'r+' ) as h5file :
38
+ # does the settings group exist?
39
+ if 'preferences' not in h5file :
40
+ h5file ['/' ].create_group ('preferences' )
41
+
42
+ # is there an entry for this preference type?
43
+ group = h5file ['/preferences' ]
44
+ if name not in group .attrs :
45
+ group .attrs [name ] = repr ({})
46
+ data = eval (group .attrs [name ])
47
+ return data
48
+ else :
49
+ raise Exception ("the Settings module cannot handle the storage type: %s" % str (self .storage ))
50
+
26
51
# A simple interface for accessing values in the settings interface
27
52
def get_value (self ,settings_class ,value_name ):
28
53
return self .pages [settings_class .name ].get_value (value_name )
29
54
30
55
# goto_page should be the CLASS which you wish to go to!
31
56
def create_dialog (self ,goto_page = None ):
32
57
if not self .dialog_open :
58
+ self .instantiated_pages = {}
59
+
33
60
builder = gtk .Builder ()
34
61
builder .add_from_file (os .path .join (os .path .dirname (os .path .realpath (__file__ )),'settings_interface.glade' ))
35
62
builder .connect_signals (self )
@@ -40,7 +67,21 @@ def create_dialog(self,goto_page=None):
40
67
#sorted(a.items(),key=lambda x: x[1])
41
68
set_page = None
42
69
for name , c in sorted (self .pages .items ()):
43
- page = c .create_dialog (self .notebook )
70
+ page ,icon = c .create_dialog (self .notebook )
71
+
72
+ # save page
73
+ self .instantiated_pages [c .__class__ ] = page
74
+
75
+ # Create label
76
+ if isinstance (icon ,gtk .Image ):
77
+ # use their icon
78
+ pass
79
+ else :
80
+ # use default icon
81
+ pass
82
+
83
+ tab_label = gtk .Label (c .name )
84
+ self .notebook .append_page (page ,tab_label )
44
85
45
86
if goto_page and isinstance (c ,goto_page ):
46
87
# this is the page we want to go to!
@@ -57,12 +98,34 @@ def create_dialog(self,goto_page=None):
57
98
self .window .show ()
58
99
self .dialog_open = True
59
100
else :
101
+ if goto_page and goto_page in self .instantiated_pages :
102
+
103
+ self .notebook .set_current_page (self .notebook .page_num (self .instantiated_pages [goto_page ]))
104
+
60
105
self .window .present ()
106
+
107
+ def register_callback (self ,callback ):
108
+ self .callback_list .append (callback )
61
109
110
+ def remove_callback (self ,callback ):
111
+ self .callback_list .remove (callback )
112
+
62
113
def on_save (self ,widget ):
63
114
# Save the settings
64
- for page in self .pages .values ():
65
- page .save ()
115
+ if self .storage == 'hdf5' :
116
+ with h5py .File (self .file ,'r+' ) as h5file :
117
+ group = h5file ['/preferences' ]
118
+ for page in self .pages .values ():
119
+ group .attrs [page .__class__ .__name__ ] = repr (page .save ())
120
+ else :
121
+ # this should never happen as the exception will have been raised on load!
122
+ pass
123
+
124
+ # run callback functions!
125
+ # Notifies other areas of the program that settings have changed
126
+ for callback in self .callback_list :
127
+ callback ()
128
+
66
129
self .close ()
67
130
68
131
def on_cancel (self ,widget ):
0 commit comments