summaryrefslogtreecommitdiff
path: root/kiwi/desktopparser.py
blob: f55edd20a6ab02bd2b01a6eff7411d1315a25320 (plain)
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
#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2006 Johan Dahlin <jdahlin@async.com.br>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): Johan Dahlin <jdahlin@async.com.br>
#
# Based on gkeyfile.c from glib, written by
#
#   Ray Strode <rstrode@redhat.com>
#   Matthias Clasen <mclasen@redhat.com>
#

from ConfigParser import ConfigParser

# Private

def _localize(option, locale):
    if locale:
        option = option + '[%s]' % locale
    return option

def _tobool(s):
    if s == 'true':
        return True
    return False

def _frombool(s):
    if s:
        return 'true'
    return 'false'

class DesktopParser(ConfigParser):
    """
    A DesktopParser for GNOME/KDE .desktop files.
    The API is similar to GKeyFile from glib.

    Example:

    >>> parser = DesktopParser()
    >>> parser.read('/usr/share/applications/gnome-terminal.desktop')
    >>> parser.get_locale('Desktop Entry', 'Comment', 'pt')
    """
    def __init__(self, defaults=None):
        ConfigParser.__init__(self, defaults)
        self._list_separator = ';'

    # ConfigParser overrides

    def optionxform(self, optionstr):
        # .desktop files are case sensitive
        # The default implementation makes it lowercase,
        # so override to just use it as it was read
        return optionstr

    # Public

    def set_list_separator(self, separator):
        """
        Sets the character which is used to separate
        values in lists. Typically ';' or ',' are used
        as separators. The default list separator is ';'.

        @param separator: the separator
        """
        self._list_separator = separator

    def set_locale(self, section, option, locale, value):
        """
        Set locale.

        @param section: section name
        @param option: an option
        @param locale: a locale
        @param value: value to set
        """
        self.set(section, _localize(option, locale), value)

    def get_locale(self, section, option, locale):
        """
        Get locale.

        @param section: section name
        @param option: an option
        @param locale: a locale
        """
        return self.get(section, _localize(option, locale))

    def get_string_list(self, section, option):
        """
        Get a list as string.

        @param section: section name
        @param option: an option
        """
        return self.get(section, option).split(self._list_separator)

    def set_string_list(self, section, option, values):
        """
        Set a list of string values.

        @param section: section name
        @param option: an option
        @param values: list of string values
        """
        value = self._list_separator.join(values)
        self.set(section, option, value)

    def get_integer_list(self, section, option):
        """
        Get a list of integers as string.

        @param section: section name
        @param option: an option
        """
        return map(int, self.get_string_list(section, option))

    def set_integer_list(self, section, option, values):
        """
        Set a list with integer values.

        @param section: section name
        @param option: an option
        @param values: list of integer values
        """
        self.set_string_list(section, option, map(str, values))

    def get_boolean_list(self, section, option):
        """
        Get a boolean list.

        @param section: section name
        @param option: an option
        """
        return map(_tobool, self.get_string_list(section, option))

    def set_boolean_list(self, section, option, values):
        """
        Set an list wiht boolena values.

        @param section: section name
        @param option: an option
        @param values: list of boolean values
        """
        self.set_string_list(section, option, map(_frombool, values))

    def set_string_list_locale(self, section, option, locale, values):
        """
        Set string list with locale values.

        @param section: section name
        @param option: an option
        @param locale: a locale
        @param values: list of string values
        """
        self.set_string_list(section, _localize(option, locale), values)

    def get_string_list_locale(self, section, option, locale):
        """
        Get list locale as an string.

        @param section: section name
        @param option: an option
        @param locale: a locale
        """
        return self.get_string_list(section, _localize(option, locale))