~mvo/software-center/tos-dialog

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
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2012 Canonical
#
# Authors:
#  Michael Vogt
#
# 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

from gi.repository import GObject
import logging

import softwarecenter.paths
from spawn_helper import SpawnHelper

from softwarecenter.config import get_config
from softwarecenter.db.utils import get_installed_apps_list
from softwarecenter.utils import get_uuid

LOG = logging.getLogger(__name__)

class RecommenderAgent(GObject.GObject):

    __gsignals__ = {
        "server-status" : (GObject.SIGNAL_RUN_LAST,
                           GObject.TYPE_NONE, 
                           (GObject.TYPE_PYOBJECT,),
                          ),
        "profile" : (GObject.SIGNAL_RUN_LAST,
                     GObject.TYPE_NONE, 
                     (GObject.TYPE_PYOBJECT,),
                    ),
        "submit-profile-finished" : (GObject.SIGNAL_RUN_LAST,
                            GObject.TYPE_NONE, 
                            (GObject.TYPE_PYOBJECT, str),
                           ),
        "submit-anon-profile-finished" : (GObject.SIGNAL_RUN_LAST,
                                 GObject.TYPE_NONE, 
                                 (GObject.TYPE_PYOBJECT,),
                                ),
        "recommend-me" : (GObject.SIGNAL_RUN_LAST,
                              GObject.TYPE_NONE, 
                              (GObject.TYPE_PYOBJECT,),
                             ),
        "recommend-app" : (GObject.SIGNAL_RUN_LAST,
                           GObject.TYPE_NONE, 
                           (GObject.TYPE_PYOBJECT,),
                          ),
        "recommend-all-apps" : (GObject.SIGNAL_RUN_LAST,
                                GObject.TYPE_NONE, 
                                (GObject.TYPE_PYOBJECT,),
                               ),
        "recommend-top" : (GObject.SIGNAL_RUN_LAST,
                           GObject.TYPE_NONE, 
                           (GObject.TYPE_PYOBJECT,),
                          ),
        "error" : (GObject.SIGNAL_RUN_LAST,
                   GObject.TYPE_NONE, 
                   (str,),
                  ),
        }
    
    def __init__(self, xid=None):
        GObject.GObject.__init__(self)
        self.xid = xid
        self.recommender_uuid = self._get_recommender_uuid()

    def query_server_status(self):
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.needs_auth = True
        spawner.connect("data-available", self._on_server_status_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI", "server_status")
            
    def post_submit_profile(self, db):
        """ This will post the users profile to the recommender server
            and also generate the UUID for the user if that is not 
            there yet
        """
        # if we have not already set a recommender UUID, now is the time
        # to do it
        if not self.recommender_uuid:
            self.recommender_uuid = get_uuid()
        installed_pkglist = [app.pkgname 
                             for app in get_installed_apps_list(db)]
        data = self._generate_submit_profile_data(self.recommender_uuid,
                                                  installed_pkglist)
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.needs_auth = True
        spawner.connect("data-available", self._on_submit_profile_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI",
            "submit_profile",
            data=data)
            
    def post_submit_anon_profile(self, uuid, installed_packages, extra):
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.needs_auth = True
        spawner.connect("data-available", self._on_submit_anon_profile_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI",
            "submit_anon_profile",
            uuid=uuid,
            installed_packages=installed_packages,
            extra=extra)
            
    def query_profile(self, pkgnames):
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.needs_auth = True
        spawner.connect("data-available", self._on_profile_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI",
            "profile",
            pkgnames=pkgnames)

    def query_recommend_me(self):
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.needs_auth = True
        spawner.connect("data-available", self._on_recommend_me_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI", "recommend_me")
            
    def query_recommend_app(self, pkgname):
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.connect("data-available", self._on_recommend_app_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI",
            "recommend_app",
            pkgname=pkgname)
            
    def query_recommend_all_apps(self):
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.connect("data-available", self._on_recommend_all_apps_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI", "recommend_all_apps")
            
    def query_recommend_top(self):
        # build the command
        spawner = SpawnHelper()
        spawner.parent_xid = self.xid
        spawner.connect("data-available", self._on_recommend_top_data)
        spawner.connect("error", lambda spawner, err: self.emit("error", err))
        spawner.run_generic_piston_helper(
            "SoftwareCenterRecommenderAPI", "recommend_top")
            
    def is_opted_in(self):
        """
        Return True is the user is currently opted-in to the recommender
        service
        """
        if self.recommender_uuid:
            return True
        else:
            return False
            
    def _on_server_status_data(self, spawner, piston_server_status):
        self.emit("server-status", piston_server_status)
        
    def _on_profile_data(self, spawner, piston_profile):
        self.emit("profile", piston_profile)
        
    def _on_submit_profile_data(self, spawner, piston_submit_profile):
        self.emit("submit-profile-finished", 
                  piston_submit_profile, 
                  self.recommender_uuid)
        
    def _on_submit_anon_profile_data(self, spawner, piston_submit_anon_profile):
        self.emit("submit-anon_profile", piston_submit_anon_profile)

    def _on_recommend_me_data(self, spawner, piston_me_apps):
        self.emit("recommend-me", piston_me_apps)
        
    def _on_recommend_app_data(self, spawner, piston_app):
        self.emit("recommend-app", piston_app)
        
    def _on_recommend_all_apps_data(self, spawner, piston_all_apps):
        self.emit("recommend-all-apps", piston_all_apps)
        
    def _on_recommend_top_data(self, spawner, piston_top_apps):
        self.emit("recommend-top", piston_top_apps)
        
    def _get_recommender_uuid(self):
        """ returns the recommender UUID value, which can be empty if it
            has not yet been set (indicating that the user has not yet
            opted-in to the recommender service)
        """
        config = get_config()
        if config.has_option("general", "recommender_uuid"):
            recommender_uuid = config.get("general", "recommender_uuid")
            if recommender_uuid:
                return recommender_uuid
        return ""
        
    def _generate_submit_profile_data(self, recommender_uuid, package_list):
        submit_profile_data = [
            {
                'uuid': recommender_uuid, 
                'package_list': package_list
            }
        ]
        return submit_profile_data

   
if __name__ == "__main__":
    from gi.repository import Gtk

    def _recommend_top(agent, top_apps):
        print ("_recommend_top: %s" % top_apps)
    def _recommend_me(agent, top_apps):
        print ("_recommend_me: %s" % top_apps)
    def _error(agent, msg):
        print ("got a error: %s" % msg)
        Gtk.main_quit()

    # test specific stuff
    logging.basicConfig()
    softwarecenter.paths.datadir = "./data"

    agent = RecommenderAgent()
    agent.connect("recommend-top", _recommend_top)
    agent.connect("recommend-me", _recommend_me)
    agent.connect("error", _error)
    agent.query_recommend_top()
    agent.query_recommend_me()


    Gtk.main()