在其他过程完成后更改appindicator的图标ocurrs

时间:2012-11-28 01:32:11

标签: python appindicator

我正在为Grive编写一个appindicator,这是一个用于同步Google云端硬盘文件的守护程序。由于我的编程经验很少,所以我决定编写一个Python脚本,将Grive作为子进程调用,而不是将其集成到C ++源代码中。

我已经调整了Stefaan Lippens'code for asynchronously reading subprocess pipes以显示通知并在发生重要事件时更改指示符的图标(例如添加了新文件或网络错误)。通知效果很好;但是,指标的图标只有在整个过程完成后才会改变,这是没用的,因为我需要在完成后多次更改它。

以下是我正在使用的代码:

async.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
import time
import threading
import Queue

class AsynchronousFileReader(threading.Thread):
    '''
    Helper class to implement asynchronous reading of a file
    in a separate thread. Pushes read lines on a queue to
    be consumed in another thread.
    '''

    def __init__(self, fd, queue):
        assert isinstance(queue, Queue.Queue)
        assert callable(fd.readline)
        threading.Thread.__init__(self)
        self._fd = fd
        self._queue = queue

    def run(self):
        '''The body of the tread: read lines and put them on the queue.'''
        for line in iter(self._fd.readline, ''):
            self._queue.put(line)

    def eof(self):
        '''Check whether there is no more content to expect.'''
        return not self.is_alive() and self._queue.empty()

def run(command, show):
    '''
    Main function to consume the output of a command.
    command = The command to be run
    show = Function that will process output
    '''
    # Launch the command as subprocess.
    process = subprocess.Popen(command, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)

    # Launch the asynchronous readers of the process' stdout and stderr.
    stdout_queue = Queue.Queue()
    stdout_reader = AsynchronousFileReader(process.stdout, stdout_queue)
    stdout_reader.start()
    stderr_queue = Queue.Queue()
    stderr_reader = AsynchronousFileReader(process.stderr, stderr_queue)
    stderr_reader.start()

    # Check the queues if we received some output (until there is nothing more to get).
    while not stdout_reader.eof() or not stderr_reader.eof():
        # Show what we received from standard output.
        while not stdout_queue.empty():
            line = stdout_queue.get()
            show(line)

        # Show what we received from standard error.
        while not stderr_queue.empty():
            line = stderr_queue.get()
            show(line)

        # Sleep a bit before asking the readers again.
        time.sleep(.1)

    # Let's be tidy and join the threads we've started.
    stdout_reader.join()
    stderr_reader.join()

    # Close subprocess' file descriptors.
    process.stdout.close()
    process.stderr.close()

    return True

grive.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
import time
import async
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import GObject
from gi.repository import Notify
from gi.repository import AppIndicator3 as AppIndicator

GRIVE_FOLDER = "/home/ilhuitemoc/Publike/Google Drive"

def show(line):
    line = line.replace("\n", "")
    print line
    if line.startswith("[gr::expt::MsgTag*] = "):
        line = line.replace("[gr::expt::MsgTag*] = ","",1)
        n = Notify.Notification.new("Error", line, icon)
        n.show()
        indicator.set_icon("ubuntuone-client-offline")
    if line.startswith("sync "):
        line = line.replace("sync ","",1)
        line = line.replace('"','<b>',1)
        line = line.replace('"','</b>',1)
        n = Notify.Notification.new("Sync in progress", line, icon)
        n.show()
        indicator.set_icon("ubuntuone-client-updating")
    if "Finished!" in line:
        #n = Notify.Notification.new(line, line, icon)
        #n.show()
        indicator.set_icon("ubuntuone-client-idle")

def openinfolder(obj):
    subprocess.call(["xdg-open",GRIVE_FOLDER])

def openinbrowser(obj):
    subprocess.call(["xdg-open","http://drive.google.com/"])

if __name__ == '__main__':
    subprocess.call(["killall","pantheon-notify"])
    time.sleep(1)

    indicator = AppIndicator.Indicator.new('grive', 'ubuntuone-client-offline', AppIndicator.IndicatorCategory.APPLICATION_STATUS)
    indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
    menu = Gtk.Menu()

    status = Gtk.MenuItem("Connecting...") #Not finished yet
    status.set_sensitive(False)
    menu.append(status)

    sp = Gtk.SeparatorMenuItem()
    menu.append(sp)

    mi = Gtk.MenuItem("Open Google Drive folder")
    mi.connect('activate',openinfolder)
    menu.append(mi)

    mi = Gtk.MenuItem("Go to Google Drive webpage")
    mi.connect('activate',openinbrowser)
    menu.append(mi)

    sp = Gtk.SeparatorMenuItem()
    menu.append(sp)

    mi = Gtk.ImageMenuItem("Quit")
    img = Gtk.Image.new_from_stock(Gtk.STOCK_QUIT, Gtk.IconSize.MENU)
    mi.set_image(img)
    mi.connect('activate',Gtk.main_quit)
    menu.append(mi)

    menu.show_all()

    indicator.set_menu(menu)

    Notify.init('grive')
    icon = 'google-drive'

    #async.run('cd "%s" && grive' % GRIVE_FOLDER, show)
    GObject.timeout_add(5*60000, async.run, 'cd "%s" && grive' % GRIVE_FOLDER, show)
    #GObject.timeout_add(5000, async.run, "test.sh", show)
    Gtk.main()

我认为我做错了什么,但这对我来说并不明显。使用子流程修改指标是否正确?或者我可以正确地做其他任何方式吗?

0 个答案:

没有答案