Skip to content

Commit b6cc9d3

Browse files
committed
implemented interface to save data to a hdf5 file
implemented interface to call callback functions when settings are changed/saved.
1 parent 8937c65 commit b6cc9d3

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

__init__.py

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,62 @@
11
import os
22
import gtk
3-
3+
import h5py
44

55
# Create a generic interface for displaying pages of settings
66

77
class Settings(object):
88

9-
def __init__(self,parent = None,page_classes = []):
9+
def __init__(self,storage='hdf5',file=None,parent = None,page_classes = []):
1010
self.pages = {}
11+
self.instantiated_pages = {}
1112
self.dialog_open = False
1213
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+
1321
for c in page_classes:
1422
self.add_settings_interface(c)
15-
23+
1624
# This function can be called to add a interface
1725
# Each one of these will display as a seperate page in the settings window
1826
# You can not add a class more than once!
1927
# Classes must have unique Class.name attributes! (This might change later...)
2028
def add_settings_interface(self,setting_class):
2129
if setting_class.name in self.pages:
2230
return False
23-
self.pages[setting_class.name] = setting_class()
31+
32+
self.pages[setting_class.name] = setting_class(self.load(setting_class.__name__))
2433
return True
2534

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+
2651
# A simple interface for accessing values in the settings interface
2752
def get_value(self,settings_class,value_name):
2853
return self.pages[settings_class.name].get_value(value_name)
2954

3055
# goto_page should be the CLASS which you wish to go to!
3156
def create_dialog(self,goto_page=None):
3257
if not self.dialog_open:
58+
self.instantiated_pages = {}
59+
3360
builder = gtk.Builder()
3461
builder.add_from_file(os.path.join(os.path.dirname(os.path.realpath(__file__)),'settings_interface.glade'))
3562
builder.connect_signals(self)
@@ -40,7 +67,21 @@ def create_dialog(self,goto_page=None):
4067
#sorted(a.items(),key=lambda x: x[1])
4168
set_page = None
4269
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)
4485

4586
if goto_page and isinstance(c,goto_page):
4687
# this is the page we want to go to!
@@ -57,12 +98,34 @@ def create_dialog(self,goto_page=None):
5798
self.window.show()
5899
self.dialog_open = True
59100
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+
60105
self.window.present()
106+
107+
def register_callback(self,callback):
108+
self.callback_list.append(callback)
61109

110+
def remove_callback(self,callback):
111+
self.callback_list.remove(callback)
112+
62113
def on_save(self,widget):
63114
# 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+
66129
self.close()
67130

68131
def on_cancel(self,widget):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy