2 # Copyright 2016 RIFT.IO Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
23 from gi
.repository
import (RwDts
as rwdts
)
24 import rift
.mano
.dts
as mano_dts
26 import rift
.downloader
as url_downloader
29 class DownloadStatusPublisher(mano_dts
.DtsHandler
, url_downloader
.DownloaderProtocol
):
31 def __init__(self
, log
, dts
, loop
):
32 super().__init
__(log
, dts
, loop
)
35 def xpath(self
, download_id
=None):
36 return ("D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job" +
37 ("[download-id='{}']".format(download_id
) if download_id
else ""))
41 self
.reg
= yield from self
.dts
.register(xpath
=self
.xpath(),
42 flags
=rwdts
.Flag
.PUBLISHER|rwdts
.Flag
.CACHE|rwdts
.Flag
.NO_PREP_READ
)
44 assert self
.reg
is not None
47 def on_download_progress(self
, download_job_msg
):
48 """callback that triggers update.
50 key
= download_job_msg
.download_id
51 # Trigger progess update
52 self
.reg
.update_element(
53 self
.xpath(download_id
=key
),
56 def on_download_finished(self
, download_job_msg
):
57 """callback that triggers update.
60 # clean up the local cache
61 key
= download_job_msg
.download_id
65 # Publish the final state
66 self
.reg
.update_element(
67 self
.xpath(download_id
=key
),
71 def register_downloader(self
, downloader
):
72 downloader
.delegate
= self
73 future
= self
.loop
.run_in_executor(None, downloader
.download
)
74 self
.tasks
[downloader
.download_id
] = (downloader
, future
)
76 return downloader
.download_id
79 def cancel_download(self
, key
):
80 task
, future
= self
.tasks
[key
]
83 task
.cancel_download()
88 for task
, future
in self
.tasks
:
93 """ de-register with dts """
94 if self
.reg
is not None: