~larryprice/libertine/deb-filepicker

« back to all changes in this revision

Viewing changes to python/libertine/service/download.py

  • Committer: Larry Price
  • Date: 2017-03-09 17:26:36 UTC
  • mfrom: (388.2.46 devel)
  • Revision ID: larry.price@canonical.com-20170309172636-jketrvc7sj0wnzp0
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2016-2017 Canonical Ltd.
 
1
# Copyright 2017 Canonical Ltd.
2
2
#
3
3
# This program is free software: you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
# You should have received a copy of the GNU General Public License
13
13
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
14
 
 
15
 
15
16
import dbus.service
16
17
import threading
 
18
 
 
19
from . import constants
17
20
from libertine import utils
18
21
from time import time
19
22
 
20
 
DOWNLOAD_INTERFACE = "com.canonical.applications.Download"
21
 
PROGRESS_INTERFACE = "com.canonical.libertine.Service.Progress"
22
23
 
23
 
class Progress(dbus.service.Object):
24
 
    def __init__(self, connection):
25
 
        utils.get_logger().debug("creating a Progress object")
 
24
class Download(dbus.service.Object):
 
25
    def __init__(self, connection, id):
26
26
        self._finished = False
27
 
        self._result = []
 
27
        self._result = ''
28
28
        self._error = ''
29
 
        dbus.service.Object.__init__(self, conn=connection, object_path=("/Progress/%s" % hex(int(time()*10000000))[2:]))
30
 
 
31
 
        # self.emit_processing() # Disabled until something requires the Download interface
32
 
 
33
 
    @property
34
 
    def id(self):
35
 
        return self._object_path
36
 
 
 
29
        dbus.service.Object.__init__(self, conn=connection, object_path=(constants.DOWNLOAD_OBJECT % id))
 
30
 
 
31
        # Disabled until something requires the Download interface
 
32
        # self.emit_processing()
 
33
 
 
34
    # This is currently how services using the Ubuntu SDK to show progress
 
35
    # determine whether or not an operation is running.
37
36
    def emit_processing(self):
38
37
        if not self.done:
39
38
            self.processing(self.id)
40
39
            threading.Timer(0.5, self.emit_processing).start()
41
40
 
42
41
    @property
 
42
    def id(self):
 
43
        return self._object_path
 
44
 
 
45
    @property
43
46
    def done(self):
44
47
        return self._finished
45
48
 
46
 
    @dbus.service.signal(DOWNLOAD_INTERFACE)
 
49
    @property
 
50
    def result(self):
 
51
        return self._result.strip()
 
52
 
 
53
    @property
 
54
    def last_error(self):
 
55
        return self._error
 
56
 
 
57
    def data(self, message):
 
58
        self._result += message + '\n'
 
59
 
 
60
    # Signals to satisfy the download interface
 
61
 
 
62
    @dbus.service.signal(constants.DOWNLOAD_INTERFACE, signature='o')
47
63
    def processing(self, path):
48
64
        utils.get_logger().debug("emit processing('%s')" % path)
49
65
 
50
 
    @dbus.service.signal(DOWNLOAD_INTERFACE)
 
66
    @dbus.service.signal(constants.DOWNLOAD_INTERFACE, signature='o')
51
67
    def finished(self, path):
52
68
        utils.get_logger().debug("emit finished('%s')" % path)
53
69
        self._finished = True
54
70
 
55
 
    @dbus.service.signal(DOWNLOAD_INTERFACE)
 
71
    @dbus.service.signal(constants.DOWNLOAD_INTERFACE)
56
72
    def progress(self, received, total):
57
73
        utils.get_logger().debug("emit progress(%d, %d)" % (received, total))
58
74
 
59
 
    @dbus.service.signal(DOWNLOAD_INTERFACE)
 
75
    @dbus.service.signal(constants.DOWNLOAD_INTERFACE, signature='s')
60
76
    def error(self, message):
61
77
        utils.get_logger().error("emit error(%s)" % message)
62
78
        self._error = message
63
79
        self._finished = True
64
 
 
65
 
    @dbus.service.signal(PROGRESS_INTERFACE)
66
 
    def data(self, message):
67
 
        utils.get_logger().debug("emit data(%s)" % message)
68
 
        self._result.append(message)
69
 
 
70
 
    @dbus.service.method(PROGRESS_INTERFACE, out_signature='b')
71
 
    def running(self):
72
 
        utils.get_logger().debug(not self.done)
73
 
        return not self.done
74
 
 
75
 
    @dbus.service.method(PROGRESS_INTERFACE, out_signature='s')
76
 
    def result(self):
77
 
        full_result = "\n".join(self._result)
78
 
        utils.get_logger().debug(full_result)
79
 
        return full_result
80
 
 
81
 
    @dbus.service.method(PROGRESS_INTERFACE, out_signature='s')
82
 
    def last_error(self):
83
 
        utils.get_logger().debug(self._error)
84
 
        return self._error