~ubuntu-branches/ubuntu/lucid/pida/lucid

« back to all changes in this revision

Viewing changes to pida/core/options.py

  • Committer: Bazaar Package Importer
  • Author(s): Jan Luebbe
  • Date: 2007-09-05 17:54:09 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070905175409-ty9f6qpuctyjv1sd
Tags: 0.5.1-2
* Depend on librsvg2-common, which is not pulled in by the other depends
  (closes: #394860)
* gvim is no alternative for python-gnome2 and python-gnome2-extras
  (closes: #436431)
* Pida now uses ~/.pida2, so it can no longer be confused by old
  configurations (closes: #421378)
* Culebra is no longer supported by upstream (closes: #349009)
* Update manpage (closes: #440375)
* Update watchfile (pida is now called PIDA)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import gconf
 
2
 
 
3
from pida.core.base import BaseConfig
 
4
 
 
5
class OptionsManager(object):
 
6
 
 
7
    def __init__(self, boss=None):
 
8
        self._client = gconf.client_get_default()
 
9
        self.initialize_gconf()
 
10
 
 
11
    def initialize_gconf(self):
 
12
        self.add_directory('pida')
 
13
        self.add_directory('pida', 'keyboard_shortcuts')
 
14
 
 
15
    def add_directory(self, *parts):
 
16
        self._client.add_dir(
 
17
            '/'.join(['/apps'] + list(parts)),
 
18
            gconf.CLIENT_PRELOAD_NONE
 
19
        )
 
20
 
 
21
    def add_service_directory(self, service):
 
22
        self.add_directory('pida', service.get_name())
 
23
        
 
24
    def register_option(self, option):
 
25
        val = self._client.get(option.key)
 
26
        if val is None:
 
27
            option.set(self._client, option.default)
 
28
        if option.callback is not None:
 
29
            self.add_notify(option, option.callback)
 
30
 
 
31
    def add_notify(self, option, callback, *args):
 
32
        args = tuple([option] + list(args))
 
33
        if len(args) == 1:
 
34
            args = args[0]
 
35
        self._client.notify_add(option.key, callback, args)
 
36
 
 
37
    def get_value(self, option):
 
38
        return option.get(self._client)
 
39
 
 
40
    def set_value(self, option, value):
 
41
        return option.set(self._client, value)
 
42
 
 
43
 
 
44
class OTypeBase(object):
 
45
 
 
46
    def _getter(self, client):
 
47
        return getattr(client, 'get_%s' % self.gconf_name)
 
48
    
 
49
    def _setter(self, client):
 
50
        return getattr(client, 'set_%s' % self.gconf_name)
 
51
 
 
52
    def get(self, client, key):
 
53
        return self._getter(client)(key)
 
54
        
 
55
    def set(self, client, key, value):
 
56
        return self._setter(client)(key, value)
 
57
        
 
58
    
 
59
 
 
60
class OTypeString(OTypeBase):
 
61
    """A string configuration type"""
 
62
 
 
63
    gconf_name = 'string'
 
64
 
 
65
 
 
66
class OTypeBoolean(OTypeBase):
 
67
    """A Boolean configuration type"""
 
68
 
 
69
    gconf_name = 'bool'
 
70
 
 
71
 
 
72
class OTypeInteger(OTypeBase):
 
73
    """An integer configuration type"""
 
74
 
 
75
    gconf_name = 'int'
 
76
 
 
77
 
 
78
class OTypeStringList(OTypeBase):
 
79
    """A list of strings configuration type"""
 
80
 
 
81
    gconf_name = 'list'
 
82
 
 
83
    def get(self, client, key):
 
84
        return self._getter(client)(key, gconf.VALUE_STRING)
 
85
        
 
86
    def set(self, client, key, value):
 
87
        return self._setter(client)(key, gconf.VALUE_STRING, value)
 
88
 
 
89
class OTypeFile(OTypeString):
 
90
 
 
91
    """For files"""
 
92
 
 
93
class OTypeFont(OTypeString):
 
94
    
 
95
    """Fonts"""
 
96
 
 
97
class OTypeStringOption(OTypeString):
 
98
    
 
99
    """String from a list of options"""
 
100
 
 
101
# Awful
 
102
def otype_string_options_factory(options):
 
103
    return type('', (OTypeStringOption,), {'options': options})
 
104
 
 
105
 
 
106
class OptionItem(object):
 
107
 
 
108
    def __init__(self, group, name, label, rtype, default, doc, callback):
 
109
        self.group = group
 
110
        self.name = name
 
111
        self.label = label
 
112
        self.rtype = rtype()
 
113
        self.doc = doc
 
114
        self.default = default
 
115
        self.key = self._create_key()
 
116
        self.callback = callback
 
117
 
 
118
    def _create_key(self):
 
119
        return '/apps/pida/%s/%s' % (self.group, self.name)
 
120
 
 
121
    def get(self, client):
 
122
        return self.rtype.get(client, self.key)
 
123
        
 
124
    def set(self, client, value):
 
125
        return self.rtype.set(client, self.key, value)
 
126
 
 
127
    def get_value(self):
 
128
        return manager.get_value(self)
 
129
 
 
130
    def set_value(self, value):
 
131
        return manager.set_value(self, value)
 
132
 
 
133
    value = property(get_value, set_value)
 
134
 
 
135
    def add_notify(self, callback, *args):
 
136
        manager.add_notify(self, callback, *args)
 
137
 
 
138
 
 
139
manager = OptionsManager()
 
140
 
 
141
class OptionsConfig(BaseConfig):
 
142
 
 
143
    manager = manager
 
144
 
 
145
    def create(self):
 
146
        self._options = {}
 
147
        self.create_options()
 
148
        self.register_options()
 
149
 
 
150
    def create_options(self):
 
151
        """Create the options here"""
 
152
 
 
153
    def register_options(self):
 
154
        self.manager.add_service_directory(self.svc)
 
155
        for option in self._options.values():
 
156
            self.manager.register_option(option)
 
157
 
 
158
    def create_option(self, name, label, rtype, default, doc, callback=None):
 
159
        opt = OptionItem(self.svc.get_name(), name, label, rtype, default, doc,
 
160
                         callback)
 
161
        self.add_option(opt)
 
162
        return opt
 
163
 
 
164
    def add_option(self, option):
 
165
        self._options[option.name] = option
 
166
 
 
167
    def get_option(self, optname):
 
168
        return self._options[optname]
 
169
 
 
170
    def get_value(self, optname):
 
171
        return self.manager.get_value(self.get_option(optname))
 
172
 
 
173
    def set_value(self, optname, value):
 
174
        return self.manager.set_value(self.get_option(optname), value)
 
175
 
 
176
    def __len__(self):
 
177
        return len(self._options)
 
178
 
 
179
    def iter_options(self):
 
180
        return self._options.values()
 
181
 
 
182