This file is indexed.

/usr/lib/meld/meld/util/prefs.py is in meld 1.5.3-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
### Copyright (C) 2002-2006 Stephen Kennedy <stevek@gnome.org>
### Copyright (C) 2011-2012 Kai Willadsen <kai.willadsen@gmail.com>

### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.

### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.

### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Module to help implement 'instant-apply' preferences.

Usage:

import prefs
defaults = {
    "colour" : prefs.Value(prefs.STRING, "red")
    "size" : prefs.Value(prefs.INT, 10)
}

p = prefs.Preferences("/apps/myapp", defaults)
# use variables as if they were normal attributes.
draw(p.colour, p.size)
# settings are persistent
p.color = "blue"

"""

import os
import sys

import glib


class Value(object):
    """Represents a settable preference.
    """

    __slots__ = ["type", "default", "current"]

    def __init__(self, t, d):
        """Create a value.

        t : a string : one of ("bool", "int", "string")
        d : the default value, also the initial value
        """
        self.type = t
        self.default = d
        self.current = d

# types of values allowed
BOOL = "bool"
INT = "int"
STRING = "string"
FLOAT = "float"
LIST = "list"


class GConfPreferences(object):
    """Persistent preferences object that handles preferences via gconf.

    Example:
    import prefs
    defaults = {"spacing": prefs.Value(prefs.INT, 4),
                "font": prefs.Value(prefs.STRING, "monospace") }
    p = prefs.Prefs("myapp", defaults)
    print p.font
    p.font = "sans" # written to gconf too
    p2 = prefs.Prefs("myapp", defaults)
    print p.font # prints "sans"
    """

    def __init__(self, rootkey, initial):
        """Create a preferences object.

        Settings are initialised with 'initial' and then overriden
        from values in the gconf database if available.

        rootkey : the root gconf key where the values will be stored
        initial : a dictionary of string to Value objects.
        """
        self.__dict__["_gconf"] = gconf.client_get_default()
        self.__dict__["_listeners"] = []
        self.__dict__["_rootkey"] = rootkey
        self.__dict__["_prefs"] = initial
        self._gconf.add_dir(rootkey, gconf.CLIENT_PRELOAD_NONE)
        self._gconf.notify_add(rootkey, self._on_preference_changed)
        for key, value in self._prefs.items():
            gval = self._gconf.get_without_default("%s/%s" % (rootkey, key))
            if gval is not None:
                if value.type == LIST:
                    # We only use/support str lists at the moment
                    val_tuple = getattr(gval, "get_%s" % value.type)()
                    value.current = [v.get_string() for v in val_tuple]
                else:
                    value.current = getattr(gval, "get_%s" % value.type)()

    def __getattr__(self, attr):
        return self._prefs[attr].current

    def get_default(self, attr):
        return self._prefs[attr].default

    def __setattr__(self, attr, val):
        value = self._prefs[attr]
        if value.current != val:
            value.current = val
            setfunc = getattr(self._gconf, "set_%s" % value.type)
            if value.type == LIST:
                # We only use/support str lists at the moment
                setfunc("%s/%s" % (self._rootkey, attr), gconf.VALUE_STRING,
                        val)
            else:
                setfunc("%s/%s" % (self._rootkey, attr), val)
            try:
                for l in self._listeners:
                    l(attr, val)
            except StopIteration:
                pass

    def _on_preference_changed(self, client, timestamp, entry, extra):
        attr = entry.key[entry.key.rfind("/") + 1:]
        try:
            value = self._prefs[attr]
        # Changes for unknown keys are ignored
        except KeyError:
            pass
        else:
            if entry.value is not None:
                val = getattr(entry.value, "get_%s" % value.type)()
                if value.type == LIST:
                    # We only use/support str lists at the moment
                    val = [v.get_string() for v in val]
                setattr(self, attr, val)
            # Setting a value to None deletes it and uses the default value
            else:
                setattr(self, attr, value.default)

    def notify_add(self, callback):
        """Register a callback to be called when a preference changes.

        callback : a callable object which take two parameters, 'attr' the
                   name of the attribute changed and 'val' the new value.
        """
        self._listeners.append(callback)

    def dump(self):
        """Print all preferences.
        """
        for k, v in self._prefs.items():
            print k, v.type, v.current


class ConfigParserPreferences(object):
    """Persistent preferences object that handles preferences via ConfigParser.

    This preferences implementation is provided as a fallback for gconf-less
    platforms. The ConfigParser library is included in Python and should be
    available everywhere. The biggest drawbacks to this backend are lack of
    access to desktop-wide settings, and lack of external change notification.
    """

    def __init__(self, rootkey, initial):
        """Create a preferences object.

        Settings are initialised with 'initial' and then overriden
        from values in the ConfigParser database if available.

        rootkey : unused (retained for compatibility with existing gconf API)
        initial : a dictionary of string to Value objects.
        """
        self.__dict__["_parser"] = ConfigParser.SafeConfigParser()
        self.__dict__["_listeners"] = []
        self.__dict__["_prefs"] = initial
        self.__dict__["_type_mappings"] = {
            BOOL: self._parser.getboolean,
            INT: self._parser.getint,
            STRING: self._parser.get,
            FLOAT: self._parser.getfloat,
            LIST: self._parser.get,
        }

        if sys.platform == "win32":
            pref_dir = os.path.join(os.getenv("APPDATA"), "Meld")
        else:
            pref_dir = os.path.join(glib.get_user_config_dir(), "meld")

        if not os.path.exists(pref_dir):
            os.makedirs(pref_dir)

        self.__dict__["_file_path"] = os.path.join(pref_dir, "meldrc.ini")

        try:
            config_file = open(self._file_path, "r")
            try:
                self._parser.readfp(config_file)
            finally:
                config_file.close()
        except IOError:
            # One-way move of old preferences
            old_path = os.path.join(os.path.expanduser("~"), ".meld")
            old_file_path = os.path.join(old_path, "meldrc.ini")
            if os.path.exists(old_file_path):
                try:
                    config_file = open(old_file_path, "r")
                    try:
                        self._parser.readfp(config_file)
                    finally:
                        config_file.close()
                    new_config_file = open(self._file_path, "w")
                    try:
                        self._parser.write(new_config_file)
                    finally:
                        new_config_file.close()
                except IOError:
                    pass

        for key, value in self._prefs.items():
            if self._parser.has_option("DEFAULT", key):
                value.current = self._type_mappings[value.type]("DEFAULT", key)

    def __getattr__(self, attr):
        return self._prefs[attr].current

    def get_default(self, attr):
        return self._prefs[attr].default

    def __setattr__(self, attr, val):
        value = self._prefs[attr]
        if value.current != val:
            value.current = val
            self._parser.set(None, attr, str(val))

            try:
                fp = open(self._file_path, "w")
                try:
                    self._parser.write(fp)
                finally:
                    fp.close()
            except IOError:
                pass

            try:
                for l in self._listeners:
                    l(attr, val)
            except StopIteration:
                pass

    def notify_add(self, callback):
        """Register a callback to be called when a preference changes.

        callback : a callable object which take two parameters, 'attr' the
                   name of the attribute changed and 'val' the new value.
        """
        self._listeners.append(callback)

    def dump(self):
        """Print all preferences.
        """
        for k, v in self._prefs.items():
            print k, v.type, v.current

# Prefer gconf, falling back to ConfigParser
try:
    import gconf
    Preferences = GConfPreferences
except ImportError:
    import ConfigParser
    Preferences = ConfigParserPreferences