【问题标题】:Get progress by Esky tasks通过 Esky 任务获取进度
【发布时间】:2013-09-05 09:11:23
【问题描述】:

我在冻结的应用中使用Esky。它具有以下属性和方法 在 Esky 课程中可用:

app.version:                the current best available version.

app.active_version:         the currently-executing version, or None
                            if the esky isn't for the current app.

app.find_update():          find the best available update, or None
                            if no updates are available.

app.fetch_version(v):       fetch the specified version into local storage.

app.install_version(v):     install and activate the specified version.

现在,这很好,但我想在我的 Gui 中显示下载任务的进度。

我怎样才能做到这一点?

【问题讨论】:

    标签: python py2exe esky


    【解决方案1】:

    wxPython 已将 Esky 封装在他们自己的 SoftwareUpdate 方法中:

    https://github.com/wxWidgets/wxPython/blob/master/wx/lib/softwareupdate.py

    在他们的实现中,应用程序检查新版本,并询问用户是否愿意更新(使用 wx GUI 进行交互)。如果用户选择更新,代码只需调用 esky 的 auto_update() 方法来处理其余部分,但他们为它提供了一个 _updateProgress 方法,该方法更新进度条并提供指示 Esky 进度的消息:

    self._esky.auto_update(self._updateProgress)
    

    ...

    def _updateProgress(self, status):
        # Show progress of the download and install. This function is passed to Esky
        # functions to use as a callback.
        if self._pd is None and status.get('status') != 'done':
            self._pd = wx.ProgressDialog('Software Update', ' '*40, 
                                          style=wx.PD_CAN_ABORT|wx.PD_APP_MODAL,
                                          parent=self._parentWindow)
            self._pd.Update(0, '')
    
            if self._parentWindow:
                self._pd.CenterOnParent()
    
        simpleMsgMap = { 'searching'   : 'Searching...',
                         'retrying'    : 'Retrying...',
                         'ready'       : 'Download complete...',
                         'installing'  : 'Installing...',
                         'cleaning up' : 'Cleaning up...',}
    
        if status.get('status') in simpleMsgMap:
            self._doUpdateProgress(True, simpleMsgMap[status.get('status')])
    
        elif status.get('status') == 'found':
            self._doUpdateProgress(True, 'Found version %s...' % status.get('new_version'))
    
        elif status.get('status') == 'downloading':
            received = status.get('received')
            size = status.get('size')
            currentPercentage = 1.0 * received / size * 100
            if currentPercentage > 99.5:
                self._doUpdateProgress(False, "Unzipping...", int(currentPercentage))
            else:
                self._doUpdateProgress(False, "Downloading...", int(currentPercentage))
    
        elif status.get('status') == 'done': 
            if self._pd:
                self._pd.Destroy()
            self._pd = None
    
        wx.Yield()
    
    
    def _doUpdateProgress(self, pulse, message, value=0):
        if pulse:
            keepGoing, skip = self._pd.Pulse(message)
        else:
            keepGoing, skip = self._pd.Update(value, message)
        if not keepGoing: # user pressed the cancel button
            self._pd.Destroy()
            self._pd = None
            raise UpdateAbortedError()
    

    上面的代码直接取自https://github.com/wxWidgets/wxPython/blob/master/wx/lib/softwareupdate.py

    此功能记录在 Esky 源代码中,file: init.pyline: 689

    代码本身显示了您的回调在整个更新过程中可以看到的内容。以下是调用回调的一些摘录:

    callback({"status":"searching"})
    callback({"status":"found", "new_version":version})
    callback({"status":"installing", "new_version":version})
    for status in self.sudo_proxy.fetch_version_iter(version):
        if callback is not None:
            callback(status)
    callback({"status":"cleaning up"})
    callback({"status":"cleaning up"})
    callback({"status":"error","exception":e})
    callback({"status":"done"})
    

    【讨论】:

      【解决方案2】:

      没有正确记录,有fetch_version_iter生成器函数:

      fetch_version_iter:类似于 fetch_version 但产生进度更新 在执行期间

      它产生以下值:

      yield {“状态”:“正在下载”, “大小”:infile_size, “收到”:partfile.tell(), }

      yield {"status":"retrying","size":None}

      yield {"status":"ready","path":name}

      另外,你可以得到这样的文件名:

      app.version_finder.version_graph.get_best_path(app.version,v)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-06
        • 2021-01-02
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 2016-11-14
        相关资源
        最近更新 更多