weslly / Nettuts-Fetch

Fetch the latest version of remote files and zip packages

Home Page:http://net.tutsplus.com/articles/news/introducing-nettuts-fetch/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Would it be possible to add support for a callback in fetch_get command

kblomqvist opened this issue · comments

I would like to use Fetch plugin in my own plugin that sets up a project by fetching the framework. However, it's a bit tricky to solve how to "wait" fetch to be completed. I thought that the best way would be with a callback (called when done) if only supported by fetch_get. Would it be possible to get this feature to the command?

I have tried it this way, but callback is always None.

class FetchGetCommand(sublime_plugin.TextCommand):
    result = None
    url = None
    location = None
    option = None

--> def run(self, edit, option, url, location=None, callback=None):
        self.url = url
        self.location = location
        self.option = option

        threads = []
        thread = FetchDownload(url, option, location, 5)
        threads.append(thread)
        thread.start()
-->     self.handle_threads(edit, threads, 0, 0, 1, callback)

--> def handle_threads(self, edit, threads, offset=0, i=0, dir=1, callback=None):
        status = None
        next_threads = []
        for thread in threads:
            status = thread.result
            txt = thread.txt
            if thread.is_alive():
                next_threads.append(thread)
                continue
            if thread.result == False:
                continue

        threads = next_threads

        if len(threads):
            # This animates a little activity indicator in the status area
            before = i % 8
            after = (7) - before

            if not after:
                dir = -1
            if not before:
                dir = 1

            i += dir
            sublime.status_message('Downloading file from %s [%s=%s] ' % \
                (self.url, ' ' * before, ' ' * after))

            sublime.set_timeout(lambda: self.handle_threads(edit, threads,
-->             offset, i, dir, callback), 100)
            return

        self.view.erase_status('fetch')
        if status and self.option == 'package':
            sublime.status_message(('The package from %s was successfully' +
                                   ' downloaded and extracted') % self.url)

        elif status and self.option == 'txt':
            for region in self.view.sel():
                self.view.replace(edit, region, txt)

            sublime.status_message(('The file was successfully downloaded' +
                                   ' from %s') % self.url)

-->     if not callback is None:
-->         callback()

Usage from an external plug-in:

self.window.run_command("fetch_get", {
  "option": "package",
  "url": self.url,
  "location": self.location,
  "callback": self.configure
})