~ubuntu-branches/ubuntu/raring/pybik/raring

« back to all changes in this revision

Viewing changes to pybiklib/confstore.py

  • Committer: Package Import Robot
  • Author(s): B. Clausius
  • Date: 2013-02-03 17:35:32 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130203173532-a71ulf5b07fcul37
Tags: 1.0.1-1
* New upstream release
  + Improved user interface.
  + Added Towers and Bricks (non cubic puzzles).
  + Added an option to show the back faces.
  + The cube can be manipulated with the keyboard.
  + Animation is faster and rendering more beautiful.
  + Added more pretty patterns.
  + Added a new solver.
  + Added new translations.
* More generic watch file based on the proposal by Bart Martens
* Updated debhelper dependency and compat to 9
* Updated Standards-Version to 3.9.4, no changes needed
* debian/copyright:
  + Updated Format URL for final copyright format 1.0
  + Added paragraphs for image files
* Updated Build-Depends: new: python-numpy, python-qt4, help2man
* Updated Depends for transitions:
  + GTK2/GConf -> Qt4 (PySide or PyQt4)
  + GtkGlExt -> QtOpenGL (PySide or PyQt4)
* Suggests python-opengl (unusual usage) and gconf2 (config transition)
* Splittet into an arch dependent and an arch independent package
  (increased size and build time)
* Enabled parallel build for the architecture independent part
* Install autogenerated README file without install paragraph
* Replace the license file (displayed in the about box) by a link
  to usr/share/common-licenses/GPL-3 and add lintian override

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#-*- coding:utf-8 -*-
2
 
 
3
 
#  Pybik -- A 3 dimensional magic cube game.
4
 
#  Copyright © 2009, 2011-2012  B. Clausius <barcc@gmx.de>
5
 
#
6
 
#  This program is free software: you can redistribute it and/or modify
7
 
#  it under the terms of the GNU General Public License as published by
8
 
#  the Free Software Foundation, either version 3 of the License, or
9
 
#  (at your option) any later version.
10
 
#
11
 
#  This program is distributed in the hope that it will be useful,
12
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#  GNU General Public License for more details.
15
 
#
16
 
#  You should have received a copy of the GNU General Public License
17
 
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 
19
 
 
20
 
import gconf
21
 
import glib
22
 
import warnings
23
 
 
24
 
UNDEFINED = 0
25
 
COLORED = 1
26
 
IMAGED = 2
27
 
 
28
 
TILED = 0
29
 
MOSAIC = 1
30
 
 
31
 
SELECTIONMODE_QUAD = 0
32
 
SELECTIONMODE_EXT = 1
33
 
 
34
 
 
35
 
gconf_root = '/apps/pybik'
36
 
 
37
 
defaults = {
38
 
            'dimension'     : 3,
39
 
            'lighting'      : False,
40
 
            'selection_mode': SELECTIONMODE_EXT,
41
 
            'frameQty'      : 8,
42
 
            'dynamic_script_selection' : False,
43
 
            'saved_state'   : 'Cube 3 identity:',
44
 
            'saved_moves'   : '',
45
 
            'saved_pos'     : 0,
46
 
            'colors/0/color': '#a81407',
47
 
            'colors/1/color': '#d94b1c',
48
 
            'colors/2/color': '#f0c829',
49
 
            'colors/3/color': '#e3e3e3',
50
 
            'colors/4/color': '#1d6311',
51
 
            'colors/5/color': '#00275e',
52
 
            'colors/0/facetype': COLORED,
53
 
            'colors/1/facetype': COLORED,
54
 
            'colors/2/facetype': COLORED,
55
 
            'colors/3/facetype': COLORED,
56
 
            'colors/4/facetype': COLORED,
57
 
            'colors/5/facetype': COLORED,
58
 
            'colors/0/pattern': -1,
59
 
            'colors/1/pattern': -1,
60
 
            'colors/2/pattern': -1,
61
 
            'colors/3/pattern': -1,
62
 
            'colors/4/pattern': -1,
63
 
            'colors/5/pattern': -1,
64
 
            'colors/0/imagefile': '',
65
 
            'colors/1/imagefile': '',
66
 
            'colors/2/imagefile': '',
67
 
            'colors/3/imagefile': '',
68
 
            'colors/4/imagefile': '',
69
 
            'colors/5/imagefile': '',
70
 
            'colors/0/imagemode': TILED,
71
 
            'colors/1/imagemode': TILED,
72
 
            'colors/2/imagemode': TILED,
73
 
            'colors/3/imagemode': TILED,
74
 
            'colors/4/imagemode': TILED,
75
 
            'colors/5/imagemode': TILED,
76
 
            'background_color': '#2B0011',
77
 
           }
78
 
 
79
 
 
80
 
class ConfstoreDefault (object):
81
 
    root = None
82
 
    defaults = {}
83
 
    
84
 
    def __init__(self, parent, defaults_):
85
 
        self.callbacks = []
86
 
        self.finalize(parent, defaults_)
87
 
        
88
 
    def init_notify(self):
89
 
        pass
90
 
        
91
 
    def finalize(self, parent, defaults_):
92
 
        if parent == None:
93
 
            assert ConfstoreDefault.root == None, ConfstoreDefault.root
94
 
            ConfstoreDefault.root = self
95
 
        if not self.defaults:
96
 
            self.defaults = {}
97
 
        for key in defaults_:
98
 
            if '/' not in key:
99
 
                #debug('%s: self.defaults[%s] = %s' % (self.root_key, key, defaults_[key]))
100
 
                self.defaults[key] = defaults_[key]
101
 
            else:
102
 
                key1, key2 = key.split('/', 1)
103
 
                if key1 in self.defaults:
104
 
                    self.defaults[key1].finalize(self, {key2:defaults_[key]})
105
 
                else:
106
 
                    self.defaults[key1] = self.__class__(self, {key2:defaults_[key]}, key1)
107
 
    
108
 
    def __getitem__(self, key):
109
 
        return self.__getattr__(str(key))
110
 
        
111
 
    def __getattr__(self, keyname):
112
 
        #debug('Default getattr', self.root_key, keyname)
113
 
        return self.defaults[keyname]
114
 
        
115
 
    def set_color(self, i, color):
116
 
        self.colors[i].color = str(color)
117
 
        self.colors[i].facetype = COLORED
118
 
        
119
 
    def set_image_filename(self, i, image_filename):
120
 
        self.colors[i].imagefile = image_filename
121
 
        self.colors[i].facetype = IMAGED
122
 
        
123
 
        
124
 
class ConfstoreGConf (ConfstoreDefault):
125
 
    client = None
126
 
    
127
 
    def __init__(self, parent, defaults_, key):
128
 
        self.values = {}
129
 
        
130
 
        if parent is None:
131
 
            self.root_key = key
132
 
            assert ConfstoreGConf.client is None
133
 
            ConfstoreGConf.client = gconf.client_get_default()
134
 
        else:
135
 
            self.root_key = '/'.join((parent.root_key, key))
136
 
        
137
 
        ConfstoreDefault.__init__(self, parent, defaults_)
138
 
    
139
 
    def init_notify(self):
140
 
        try:
141
 
            self.client.add_dir(self.root_key, gconf.CLIENT_PRELOAD_RECURSIVE)
142
 
            self.client.notify_add(self.root_key, self.on_gconf_notify)
143
 
        except glib.GError as e:
144
 
            warnings.warn(str(e))
145
 
        
146
 
    def clear_cache(self):
147
 
        self.values.clear()
148
 
    
149
 
    def on_gconf_notify(self, unused_client, unused_cnxn_id, entry, unused_args):
150
 
        keys = entry.key[len(self.root_key)+1:].split('/')
151
 
        #debug('on_gconf_notify', cnxn_id, entry.key, keys)
152
 
        c = self
153
 
        for key in keys[:-1]:
154
 
            c = getattr(c, key)
155
 
        try:
156
 
            del c.values[keys[-1]]
157
 
        except KeyError:
158
 
            pass
159
 
        for callback in self.callbacks:
160
 
            callback(*keys)
161
 
    
162
 
    @staticmethod
163
 
    def _gconf_typename(value):
164
 
        typename = value.__class__.__name__
165
 
        if typename == "str":
166
 
            typename = "string"
167
 
        return typename
168
 
    
169
 
    def __getattr__(self, key):
170
 
        # try cache
171
 
        if key in self.values:
172
 
            return self.values[key]
173
 
        
174
 
        # try gconf
175
 
        try:
176
 
            value = self.client.get('/'.join((self.root_key, key)))
177
 
        except glib.GError as e:
178
 
            warnings.warn(str(e))
179
 
            value = None
180
 
        if value != None:
181
 
            ret = getattr(value, "get_" + self._gconf_typename(self.defaults[key]))()
182
 
            self.values[key] = ret
183
 
            return ret
184
 
        
185
 
        # fallback default
186
 
        return ConfstoreDefault.__getattr__(self, key)
187
 
    
188
 
    def __setattr__(self, key, value):
189
 
        if key in self.defaults:
190
 
            typename = self._gconf_typename(self.defaults[key])
191
 
            gconf_value = gconf.Value(typename)
192
 
            getattr(gconf_value, 'set_'+typename)(value)
193
 
            
194
 
            #debug('Cache setattr', self.root_key, key, value)
195
 
            self.values[key] = value
196
 
            #debug('GConf setattr', self.root_key, key, gconf_value)
197
 
            self.client.set('/'.join((self.root_key, key)), gconf_value)
198
 
        else:
199
 
            object.__setattr__(self, key, value)
200
 
    
201
 
    def __delattr__(self, key):
202
 
        if key in self.defaults:
203
 
            #self.__setattr__(key, self.defaults[key])
204
 
            if key in self.values:
205
 
                #debug('Cache delattr', self.root_key, key)
206
 
                del self.values[key]
207
 
            #debug('GConf delattr', self.root_key, key)
208
 
            self.client.unset('/'.join((self.root_key, key)))
209
 
        else:
210
 
            #debug('del', key)
211
 
            object.__delattr__(self, key)
212
 
            
213
 
    @property
214
 
    def mouse_left_handed(self):
215
 
        try:
216
 
            value = self.client.get('/desktop/gnome/peripherals/mouse/left_handed')
217
 
        except glib.GError as e:
218
 
            warnings.warn(str(e))
219
 
            value = None
220
 
        if value != None:
221
 
            ret = value.get_bool()
222
 
            return ret
223
 
        return False
224
 
 
225
 
 
226
 
# the Confstore root
227
 
confstore = ConfstoreGConf(None, defaults, gconf_root)
228