X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=indicator-chars.py;h=3edfa5a25a92f9ab495d9ac206fcbec838a1c705;hb=8706f2303d461e3fe2ce1abc5d852a1ffabc8ea1;hp=7111568a42b29ba73c5fde9edee0d93e2ea11198;hpb=9119151680c58f984c8bb688706cafed9e4b0573;p=indicator-chars.git diff --git a/indicator-chars.py b/indicator-chars.py index 7111568..3edfa5a 100755 --- a/indicator-chars.py +++ b/indicator-chars.py @@ -1,9 +1,13 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # Very simple chars indicator. # Author: Tobias Schlitt # +# Hacked by Cyrille37 on 2016-02-03 +# +# Ported to Python 3 by Adrian I Lam +# # Copyright (c) 2011, Tobias Schlitt # All rights reserved. # @@ -31,33 +35,37 @@ # DAMAGE. import os -import gtk -import gio +import re +from gi.repository import Gtk, Gio, AppIndicator3 import signal import subprocess -import appindicator APP_NAME = 'indicator-chars' -APP_VERSION = '0.1' +APP_VERSION = '0.2_python3' class IndicatorChars: - CHARS_PATH = os.getenv('HOME') + '/.indicator-chars' + 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", self.SCRIPT_DIR + '/light16x16.png', appindicator.CATEGORY_APPLICATION_STATUS) - self.ind.set_status(appindicator.STATUS_ACTIVE) + self.ind = AppIndicator3.Indicator.new( + "Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.svg'), + AppIndicator3.IndicatorCategory.APPLICATION_STATUS) + self.ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE) self.update_menu() def create_menu_item(self, label): - item = gtk.MenuItem() + 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...' + if event_type == Gio.FileMonitorEvent.CHANGES_DONE_HINT: + print('Characters changed, updating menu...') self.update_menu() def update_menu(self, widget = None, data = None): @@ -67,39 +75,67 @@ class IndicatorChars: charDef = [] # Create menu - menu = gtk.Menu() - self.ind.set_menu(menu) + menu = Gtk.Menu() for charLine in charDef: - charLine = unicode(charLine) + charLine = str(charLine) charLine = charLine.strip() - parentItem = self.create_menu_item(charLine) - subMenu = gtk.Menu() - for char in charLine: - subItem = self.create_menu_item(char) + 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 = Gtk.Clipboard(selection="PRIMARY") + cb.set_text(char) + cb = Gtk.Clipboard(selection="CLIPBOARD") 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()) + 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() + file = Gio.File.new_for_path(i.CHARS_PATH) + monitor = file.monitor_file(Gio.FileMonitorFlags.NONE, None) monitor.connect("changed", i.on_chars_changed) # Main gtk loop - gtk.main() + Gtk.main()