~mvo/software-center/fix-index-update-terms-bug

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
# Copyright (C) 20011 Canonical
#
# Authors:
#  Andrew Higginson
#
# 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; version 3.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# py3 compat
try:
    from configparser import SafeConfigParser
    SafeConfigParser  # pyflakes
except ImportError:
    from ConfigParser import SafeConfigParser
import os
import logging

from paths import SOFTWARE_CENTER_CONFIG_FILE

LOG = logging.getLogger(__name__)


class SoftwareCenterConfig(object, SafeConfigParser):

    SECTIONS = ("general", "reviews")

    def __init__(self, config):
        object.__init__(self)
        SafeConfigParser.__init__(self)
        # imported here to avoid cycle
        from utils import safe_makedirs
        safe_makedirs(os.path.dirname(config))
        # we always want this sections, even on fresh installs
        for section in self.SECTIONS:
            self.add_section(section)

        # read the config
        self.configfile = config
        try:
            self.read(self.configfile)
        except Exception as e:
            # don't crash on a corrupted config file
            LOG.warn("Could not read the config file '%s': %s",
                     self.configfile, e)
            pass

    def write(self):
        tmpname = self.configfile + ".new"
        # see LP: #996333, its ok to remove the old configfile as
        # its rewritten anyway
        from utils import ensure_file_writable_and_delete_if_not
        ensure_file_writable_and_delete_if_not(tmpname)
        ensure_file_writable_and_delete_if_not(self.configfile)
        try:
            f = open(tmpname, "w")
            SafeConfigParser.write(self, f)
            f.close()
            os.rename(tmpname, self.configfile)
        except Exception as e:
            # don't crash if there's an error when writing to the config file
            # (LP: #996333)
            LOG.warn("Could not write the config file '%s': %s",
                     self.configfile, e)
            pass

    # generic property helpers
    def _generic_get(self, option, section="general", default=""):
        if not self.has_option(section, option):
            self.set(section, option, default)
        return self.get(section, option)

    def _generic_set(self, option, value, section="general"):
        self.set(section, option, value)

    def _generic_getbool(self, option, section="general", default=False):
        if not self.has_option(section, option):
            self.set(section, option, str(default))
        return self.getboolean(section, option)

    def _generic_setbool(self, option, value, section="general"):
        if value:
            self.set(section, option, "True")
        else:
            self.set(section, option, "False")

    # our properties that will automatically sync with the configfile
    add_to_unity_launcher = property(
        lambda self: self._generic_getbool("add_to_launcher", default=True),
        lambda self, value: self._generic_setbool("add_to_launcher", value),
        None,
        "Defines if apps should get added to the unity launcher")
    app_window_size = property(
        lambda self: self._generic_get("size", default="-1, -1"),
        lambda self, value: self._generic_set("size", value),
        None,
        "Defines the size of the application window")
    app_window_maximized = property(
        lambda self: self._generic_getbool("maximized", default=False),
        lambda self, value: self._generic_setbool("maximized", value),
        None,
        "Defines if apps should be started maximized")
    recommender_uuid = property(
        lambda self: self._generic_get("recommender_uuid"),
        lambda self, value: self._generic_set("recommender_uuid", value),
        None,
        "The UUID generated for the recommendations")
    recommender_profile_id = property(
        lambda self: self._generic_get("recommender_profile_id"),
        lambda self, value: self._generic_set("recommender_profile_id", value),
        None,
        "The recommendation profile id of the user")
    recommender_opt_in_requested = property(
        lambda self: self._generic_getbool(
            "recommender_opt_in_requested", default=False),
        lambda self, value: self._generic_setbool(
            "recommender_opt_in_requested", value),
        None,
        "The user has requested a opt-in and its pending")
    user_accepted_tos = property(
        lambda self: self._generic_getbool("accepted_tos", default=False),
        lambda self, value: self._generic_setbool("accepted_tos", value),
        None,
        "The user has accepted the terms-of-service")
    # the review section
    reviews_post_via_gwibber = property(
        lambda self: self._generic_getbool(
            "gwibber_send", section="reviews", default=False),
        lambda self, value: self._generic_setbool(
            "gwibber_send", value, section="reviews"),
        None,
        "Also post reviews via gwibber")
    reviews_gwibber_account_id = property(
        lambda self: self._generic_get(
            "account_id", section="reviews", default=""),
        lambda self, value: self._generic_setbool(
            "account_id", value, section="reviews"),
        None,
        "The account id to use when sending via gwibber")


# one global instance of the config
_software_center_config = None


def get_config(filename=SOFTWARE_CENTER_CONFIG_FILE):
    """ get the global config class """
    global _software_center_config
    if not _software_center_config:
        _software_center_config = SoftwareCenterConfig(filename)
    return _software_center_config