Eu preciso executar este script Python como abaixo, porque ele precisa acessar o xdisplay do usuário:
DISPLAY=:0 XAUTHORITY=/home/<USER>/.Xauthority su <USER> -c "python-script"
O comando acima não tem problema em executar outros scripts Python ou comandos como notify-send
, mas quando se trata deste script nada é exibido, embora possa ser visto em execução na lista de processos.
Eu acho (e espero) que um especialista em Python possa dizer o que esse script Python precisa para acessar a exibição do usuário :
#!/usr/bin/python
import os
import re
import gtk
import gio
import signal
import subprocess
import appindicator
APP_NAME = 'indicator-chars'
APP_VERSION = '0.2'
class IndicatorChars:
CHARS_PATH = os.path.join(os.getenv('HOME'), '.indicator-chars')
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
submenu_title_pattern = re.compile(r'\[([^]]+)\] *')
description_pattern = re.compile(r' *(\([^)]+\)) *')
def __init__(self):
self.ind = appindicator.Indicator(
"Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.png'),
appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.update_menu()
def create_menu_item(self, label):
item = gtk.MenuItem()
item.set_label(label)
return item
def on_chars_changed(self, filemonitor, file, other_file, event_type):
if event_type == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
print 'Characters changed, updating menu...'
self.update_menu()
def update_menu(self, widget = None, data = None):
try:
charDef = open(self.CHARS_PATH).readlines()
except IOError:
charDef = []
# Create menu
menu = gtk.Menu()
for charLine in charDef:
charLine = unicode(charLine)
charLine = charLine.strip()
submenu_match = self.submenu_title_pattern.match(charLine)
if submenu_match:
submenu_title = submenu_match.group(1)
# remove title part from remainder:
charLine = charLine[submenu_match.end():]
else:
submenu_title = ''.join(
self.description_pattern.split(charLine)[::2])
parentItem = self.create_menu_item(submenu_title)
subMenu = gtk.Menu()
while charLine:
char = charLine[0]
charLine = charLine[1:]
description_match = self.description_pattern.match(charLine)
if description_match:
item_title = char + ' ' + description_match.group(1)
# remove description part from remainder:
charLine = charLine[description_match.end():]
else:
item_title = char
subItem = self.create_menu_item(item_title)
subItem.connect("activate", self.on_char_click, char)
subMenu.append(subItem)
parentItem.set_submenu(subMenu)
menu.append(parentItem)
menu.append(gtk.SeparatorMenuItem())
quit_item = self.create_menu_item('Quit')
quit_item.connect("activate", self.on_quit)
menu.append(quit_item)
# Show the menu
self.ind.set_menu(menu)
menu.show_all()
def on_char_click(self, widget, char):
cb = gtk.Clipboard(selection="PRIMARY")
cb.set_text(char)
def on_quit(self, widget):
gtk.main_quit()
if __name__ == "__main__":
# Catch CTRL-C
signal.signal(signal.SIGINT, lambda signal, frame: gtk.main_quit())
# Run the indicator
i = IndicatorChars()
# Monitor bookmarks changes
file = gio.File(i.CHARS_PATH)
monitor = file.monitor_file()
monitor.connect("changed", i.on_chars_changed)
# Main gtk loop
gtk.main()