From 289b56c730894f47b0f765e7063b27462817bb7a Mon Sep 17 00:00:00 2001 From: Victoria Drake Date: Sun, 13 Feb 2022 07:03:48 -0600 Subject: [PATCH] Update to Python 3.9 - Works on Ubuntu 21.10 - Lint and address some deprecations Close #47 Close #35 Close #30 --- debian/control | 5 +- emojione-picker | 494 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 361 insertions(+), 138 deletions(-) diff --git a/debian/control b/debian/control index f1430338..5a2c76b9 100644 --- a/debian/control +++ b/debian/control @@ -4,13 +4,12 @@ Priority: optional Homepage: https://github.com/gentakojima/emojione-picker-ubuntu Standards-Version: 3.9.5 X-Python-Version: << 2.8 -Build-Depends: debhelper (>= 7), python (<<2.8) +Build-Depends: debhelper (>= 9), python (<<2.8) Maintainer: Jorge Suárez de Lis Package: emojione-picker -Section: python -Depends: python-gi (>=3.2), python2.7, python-notify (>=0.1), gir1.2-notify-0.7, gir1.2-appindicator3-0.1, gir1.2-rsvg-2.0, xdotool +Depends: python-gi (>=3.2), python3.9, python-notify (>=0.1), gir1.2-notify-0.7, gir1.2-appindicator3-0.1, gir1.2-rsvg-2.0, xdotool Architecture: all Description: Emoji Picker for the indicator area using Emoji One Emoji Picker featuring several menus and a search panel. diff --git a/emojione-picker b/emojione-picker index 7c56a1db..510be47d 100755 --- a/emojione-picker +++ b/emojione-picker @@ -1,4 +1,4 @@ -#!/usr/bin/python2.7 +#!/usr/bin/python3.9 # -*- coding: UTF-8 -*- # @@ -12,53 +12,73 @@ from os.path import expanduser import signal from collections import OrderedDict import re +from xml.dom.minidom import TypeInfo -# Fix enconding in older Python versions -reload(sys) -sys.setdefaultencoding('utf8') +# Fix encoding in older Python versions +# reload(sys) +# sys.setdefaultencoding("utf8") # Import GTK from gi import require_version -require_version('AppIndicator3', '0.1') -require_version('Gtk', '3.0') -require_version('Notify', '0.7') + +require_version("AppIndicator3", "0.1") +require_version("Gtk", "3.0") +require_version("Notify", "0.7") from gi.repository import GLib, Gtk, GObject, Gdk, Notify, GdkPixbuf from gi.repository import AppIndicator3 as appindicator # Where is the data? -directories = [expanduser("~") + "/.local/share/emojione-picker", "/usr/local/share/emojione-picker", "/usr/share/emojione-picker", os.path.dirname(os.path.realpath(__file__))] +directories = [ + expanduser("~") + "/.local/share/emojione-picker", + "/usr/local/share/emojione-picker", + "/usr/share/emojione-picker", + os.path.dirname(os.path.realpath(__file__)), +] for d in directories: if os.path.isdir(d): - directory = d; + directory = d break # Categories definitions -categories = ["recent", "people", "food", "nature", "objects", "activity", "travel", "flags", "symbols"] +categories = [ + "recent", + "people", + "food", + "nature", + "objects", + "activity", + "travel", + "flags", + "symbols", +] # Settings handling -default_settings = settings = { +default_settings = settings = { "toned": -1, "notifications": True, "lowend": False, "recent": 20, - "paste": False + "paste": False, } configpath = expanduser("~") + "/.config/emojione-picker" os.path.isdir(configpath) or os.mkdir(configpath) -sockname = '/tmp/emojisocket.' + str(os.getuid()) + os.environ['DISPLAY'] +sockname = "/tmp/emojisocket." + str(os.getuid()) + os.environ["DISPLAY"] builder = Gtk.Builder() + def handle_sock_signal(sock, *args): GLib.io_add_watch(sock, GLib.IO_IN, handle_sock_signal) conn, addr = sock.accept() conn.close() - open_search_window('','') + open_search_window("", "") + def exit(self, w): sys.exit() + def open_settings_window(self, w): global settings builder.add_from_file(directory + "/assets/settings.glade") @@ -68,31 +88,34 @@ def open_settings_window(self, w): settings_window.present() settings_window.grab_focus() # Apply settings - builder.get_object("combo_toned").set_active(settings["toned"]+1) + builder.get_object("combo_toned").set_active(settings["toned"] + 1) builder.get_object("check_notifications").set_active(settings["notifications"]) builder.get_object("check_lowend").set_active(settings["lowend"]) builder.get_object("recent").set_value(settings["recent"]) builder.get_object("check_paste").set_active(settings["paste"]) + class SettingsButtonHandler: def onButtonPressed(self, button): apply_settings() button.get_parent_window().destroy() - def onToggled(self,button): + + def onToggled(self, button): if builder.get_object("check_paste").get_active() == True: builder.get_object("check_notifications").set_sensitive(False) else: builder.get_object("check_notifications").set_sensitive(True) + def apply_settings(): global settings # Get settings from dialog - settings = { - "toned": builder.get_object("combo_toned").get_active()-1, + settings = { + "toned": builder.get_object("combo_toned").get_active() - 1, "notifications": builder.get_object("check_notifications").get_active(), "lowend": builder.get_object("check_lowend").get_active(), - "recent": builder.get_object("recent").get_value_as_int(), - "paste": builder.get_object("check_paste").get_active() + "recent": builder.get_object("recent").get_value_as_int(), + "paste": builder.get_object("check_paste").get_active(), } # Save settings to file save_settings() @@ -100,14 +123,18 @@ def apply_settings(): os.unlink(sockname) os.execv(__file__, sys.argv) + def save_settings(): global settings - with open(configfile, 'w') as outfile: + with open(configfile, "w") as outfile: json.dump(settings, outfile) + # Search feature searchbuilder = None search_window = None + + def open_search_window(self, w): global searchbuilder global search_window @@ -131,7 +158,11 @@ def open_search_window(self, w): global sorted_recent, searchresults for i in sorted_recent: emoji_name = sorted_recent[i]["name"] - emoji_image = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_recent[i]["unicode"] + ".svg", 24, 24) + emoji_image = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + "/assets/svg/" + sorted_recent[i]["unicode"] + ".svg", + 24, + 24, + ) emoji_code = sorted_recent[i]["unicode"] iconstore.append([emoji_image, emoji_code, emoji_name]) searchresults = [] @@ -140,8 +171,11 @@ def open_search_window(self, w): return + searchresults = None selectionChanged = False + + class SearchHandler: def onSearchChanged(self, search): global searchresults @@ -153,21 +187,25 @@ class SearchHandler: global sorted_recent for i in sorted_recent: emoji_name = sorted_recent[i]["name"] - emoji_image = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_recent[i]["unicode"] + ".svg", 24, 24) + emoji_image = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + "/assets/svg/" + sorted_recent[i]["unicode"] + ".svg", + 24, + 24, + ) emoji_code = sorted_recent[i]["unicode"] iconstore.append([emoji_image, emoji_code, emoji_name]) searchresults = [] for i in sorted_recent.keys(): searchresults.append(sorted_recent[i]) return - numfound=0 + numfound = 0 searchresults = [] for i in sorted_data: match = False for keyword in sorted_data[i]["keywords"]: - if keyword.find(search)>-1: + if keyword.find(search) > -1: match = True - if sorted_data[i]["name"].find(search)>-1: + if sorted_data[i]["name"].find(search) > -1: match = True if match == True: numfound += 1 @@ -175,16 +213,23 @@ class SearchHandler: return searchresults.append(sorted_data[i]) emoji_name = sorted_data[i]["name"] - emoji_image = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_data[i]["unicode"] + ".svg", 24, 24) + emoji_image = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + "/assets/svg/" + sorted_data[i]["unicode"] + ".svg", + 24, + 24, + ) emoji_code = sorted_data[i]["unicode"] iconstore.append([emoji_image, emoji_code, emoji_name]) + def onIconActivated(self, icon, index): global searchresults item_response(self, searchresults[int(index.to_string())]) icon.get_parent_window().hide() + def onSelectionChanged(self, data): global selectionChanged selectionChanged = True + def onKeyReleased(self, window, event): global selectionChanged if event.keyval == Gdk.KEY_Escape: @@ -194,25 +239,27 @@ class SearchHandler: elif event.keyval == Gdk.KEY_Up and selectionChanged == False: searchbuilder.get_object("search").grab_focus() selectionChanged = False + def onOutFocus(self, window, data): window.hide() + # Load settings at startup configfile = configpath + "/settings.json" if os.path.isfile(configfile): - with open(configfile) as settings_json_file: - try: - settings = json.load(settings_json_file) - except: - save_settings() - + with open(configfile) as settings_json_file: + try: + settings = json.load(settings_json_file) + except: + save_settings() + else: save_settings() for k in default_settings.keys(): if not k in settings: settings[k] = default_settings[k] - + # Load recent emojis at startup recentfile = configpath + "/recent.json" if os.path.isfile(recentfile): @@ -220,7 +267,7 @@ if os.path.isfile(recentfile): recent = json.load(recent_json_file) recentindex = 0 for k in recent.keys(): - if(int(recent[k]["recent_order"])>recentindex): + if int(recent[k]["recent_order"]) > recentindex: recentindex = int(recent[k]["recent_order"]) recentindex += 1 else: @@ -236,6 +283,8 @@ if settings["lowend"] == True: # Refresh recent icons submenu sorted_recent = None + + def refresh_recent_submenu(): global recent, recent_items, sorted_recent @@ -243,6 +292,7 @@ def refresh_recent_submenu(): def orderfunc(tup): key, d = tup return -int(d["recent_order"]) + sorted_recent = sorted(recent.items(), key=orderfunc) sorted_recent = OrderedDict(sorted_recent) @@ -251,21 +301,29 @@ def refresh_recent_submenu(): for key in sorted_recent: if i >= settings["recent"]: break - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_recent[key]["unicode"] + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + "/assets/svg/" + sorted_recent[key]["unicode"] + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) recent_items[i].set_image(img) recent_items[i].set_label(sorted_recent[key]["name"].title()) recent_items[i].show() if ("recent_" + str(i)) in signals.keys(): recent_items[i].disconnect(signals["recent_" + str(i)]) - signals["recent_" + str(i)] = recent_items[i].connect("activate", item_response, sorted_recent[key]) + signals["recent_" + str(i)] = recent_items[i].connect( + "activate", item_response, sorted_recent[key] + ) i = i + 1 + # Type character using xdootool [Experimental] def type_xdotool(text): time.sleep(0.1) - subprocess.Popen(["xdotool", "type", text.decode('unicode-escape')]) - return False # Glib.idle_add needs this + subprocess.Popen(["xdotool", "type", text.decode("unicode-escape")]) + return False # Glib.idle_add needs this + # Click response def item_response(self, w): @@ -282,17 +340,23 @@ def item_response(self, w): chars = w["unicode"].split("-") output = "" for char in chars: - output = output + '\\U' + (char.zfill(8)) - if settings["paste"]==True: - GLib.idle_add(type_xdotool,output) + output = output + "\\U" + (char.zfill(8)) + if settings["paste"] == True: + GLib.idle_add(type_xdotool, output) else: clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) - GLib.idle_add(clipboard.set_text,output.decode('unicode-escape'), -1) + GLib.idle_add( + clipboard.set_text, output.encode("Latin1").decode("unicode-escape"), -1 + ) # Show notification - if settings["notifications"]==True: - if settings["paste"]==False: - n = Notify.Notification.new(w["name"].title(), "Emoji is now in the clipboard. Paste it wherever you want!", directory + "/assets/svg/" + w["unicode"] + ".svg") + if settings["notifications"] == True: + if settings["paste"] == False: + n = Notify.Notification.new( + w["name"].title(), + "Emoji copied to clipboard.", + directory + "/assets/svg/" + w["unicode"] + ".svg", + ) n.show() # Remove item from recent if already present @@ -306,32 +370,37 @@ def item_response(self, w): # Store item on recent w["recent_order"] = recentindex recentindex = recentindex + 1 - recent.update({w["emoji_order"]:w}) + recent.update({w["emoji_order"]: w}) # Remove older items if recent is too big + biggestindex = len(recent) if len(recent) > settings["recent"]: biggestindex = 0 for k in recent.keys(): if recent[k]["recent_order"] > biggestindex: biggestindex = recent[k]["recent_order"] - nextbiggest = biggestindex - for i in range (1, settings["recent"]): - nextbiggest_candidate = 0 - for k in recent.keys(): - if recent[k]["recent_order"] < nextbiggest and recent[k]["recent_order"] > nextbiggest_candidate: - nextbiggest_candidate = recent[k]["recent_order"] - nextbiggest = nextbiggest_candidate - minindex = nextbiggest + nextbiggest = biggestindex + for i in range(1, settings["recent"]): + nextbiggest_candidate = 0 for k in recent.keys(): - if recent[k]["recent_order"] < minindex: - del recent[k] + if ( + recent[k]["recent_order"] < nextbiggest + and recent[k]["recent_order"] > nextbiggest_candidate + ): + nextbiggest_candidate = recent[k]["recent_order"] + nextbiggest = nextbiggest_candidate + minindex = nextbiggest + for k in recent.keys(): + if recent[k]["recent_order"] < minindex: + del recent[k] # Save recentfile - with open(recentfile, 'w') as outfile: + with open(recentfile, "w") as outfile: json.dump(recent, outfile) # Refresh recent icons submenu - GLib.idle_add(refresh_recent_submenu) + GLib.idle_add(refresh_recent_submenu) + def get_emoji_group(code): for key in groups_data: @@ -339,6 +408,7 @@ def get_emoji_group(code): return key return False + if __name__ == "__main__": try: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -346,7 +416,7 @@ if __name__ == "__main__": except: # Calling GObject.threads_init() is not needed for PyGObject 3.10.2+ - GObject.threads_init() + # GObject.threads_init() # Initialize notifications Notify.init("emojione-picker") @@ -355,46 +425,56 @@ if __name__ == "__main__": menu = Gtk.Menu() # Create indicator - ind = appindicator.Indicator.new("emojione-picker", directory + "/assets/icon-default.svg", appindicator.IndicatorCategory.APPLICATION_STATUS) + ind = appindicator.Indicator.new( + "emojione-picker", + directory + "/assets/icon-default.svg", + appindicator.IndicatorCategory.APPLICATION_STATUS, + ) ind.set_status(appindicator.IndicatorStatus.ACTIVE) # If there is a icon present in the theme, use it! try: - icons = Gtk.IconTheme.get_default() - icon = icons.load_icon("emojione-picker", Gtk.IconSize.MENU, 0) - ind.set_icon("emojione-picker") + icons = Gtk.IconTheme.get_default() + icon = icons.load_icon("emojione-picker", Gtk.IconSize.MENU, 0) + ind.set_icon("emojione-picker") except: - pass + pass # Get proper icon sizes iconsizes = Gtk.IconSize.lookup(Gtk.IconSize.MENU) - + # Create categories items and submenus category_item = {} category_menu = {} for category in categories: - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/categories/" + category + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + "/assets/categories/" + category + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) - category_item[category] = Gtk.ImageMenuItem(category.title()) + category_item[category] = Gtk.ImageMenuItem(label=category.title()) item_settings = category_item[category].get_settings() - item_settings.set_property('gtk-menu-images', True) + item_settings.set_property("gtk-menu-images", True) GLib.idle_add(category_item[category].set_image, img) category_menu[category] = Gtk.Menu() - category_item[category].set_submenu(category_menu[category]); + category_item[category].set_submenu(category_menu[category]) # Load groups and icons definition and rearrange them in order with open(directory + "/assets/groups.json") as groups_file: groups_data = json.load(groups_file) with open(directory + "/assets/emoji.json") as json_file: json_data = json.load(json_file) + def orderfunc(tup): key, d = tup return int(d["emoji_order"]) - sorted_data = sorted(json_data.items(), key=orderfunc) - sorted_data = OrderedDict(sorted_data) + + sorted_d = sorted(json_data.items(), key=orderfunc) + sorted_data = OrderedDict(sorted_d) # Load icons into menu items - tones_re = re.compile('(.*) tone[ ]?\d', re.IGNORECASE) + tones_re = re.compile("(.*) tone[ ]?\d", re.IGNORECASE) item_groups = {} submenu_groups = {} items = {} @@ -403,117 +483,261 @@ if __name__ == "__main__": signals = {} for category in categories: for key in sorted_data: - if settings["lowend"] == False or not sorted_data[key]["unicode"] in lowend: + if ( + settings["lowend"] == False + or not sorted_data[key]["unicode"] in lowend + ): if sorted_data[key]["category"] == category: emoji_group = get_emoji_group(sorted_data[key]["unicode"]) if emoji_group != False: # Grouped emoji if not emoji_group in item_groups: # Create group item - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + groups_data[emoji_group]["icon"] + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + + "/assets/svg/" + + groups_data[emoji_group]["icon"] + + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) - item_groups[emoji_group] = Gtk.ImageMenuItem(groups_data[emoji_group]["name"].title()) + item_groups[emoji_group] = Gtk.ImageMenuItem( + label=groups_data[emoji_group]["name"].title() + ) item_settings = item_groups[emoji_group].get_settings() - item_settings.set_property('gtk-menu-images', True) + item_settings.set_property("gtk-menu-images", True) GLib.idle_add(item_groups[emoji_group].set_image, img) - GLib.idle_add(category_menu[category].append, item_groups[emoji_group]) + GLib.idle_add( + category_menu[category].append, + item_groups[emoji_group], + ) GLib.idle_add(item_groups[emoji_group].show) # Create group submenu submenu_groups[emoji_group] = Gtk.Menu() - GLib.idle_add(item_groups[emoji_group].set_submenu, submenu_groups[emoji_group]) + GLib.idle_add( + item_groups[emoji_group].set_submenu, + submenu_groups[emoji_group], + ) if tones_re.match(sorted_data[key]["name"]) == None: - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_data[key]["unicode"] + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + + "/assets/svg/" + + sorted_data[key]["unicode"] + + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) - items[sorted_data[key]["unicode"]] = Gtk.ImageMenuItem(sorted_data[key]["name"].title()) - item_settings = items[sorted_data[key]["unicode"]].get_settings() - item_settings.set_property('gtk-menu-images', True) - GLib.idle_add(items[sorted_data[key]["unicode"]].set_image, img) - GLib.idle_add(submenu_groups[emoji_group].append, items[sorted_data[key]["unicode"]]) + items[sorted_data[key]["unicode"]] = Gtk.ImageMenuItem( + label=sorted_data[key]["name"].title() + ) + item_settings = items[ + sorted_data[key]["unicode"] + ].get_settings() + item_settings.set_property("gtk-menu-images", True) + GLib.idle_add( + items[sorted_data[key]["unicode"]].set_image, img + ) + GLib.idle_add( + submenu_groups[emoji_group].append, + items[sorted_data[key]["unicode"]], + ) GLib.idle_add(items[sorted_data[key]["unicode"]].show) - signals[key] = items[sorted_data[key]["unicode"]].connect("activate", item_response, sorted_data[key]) + signals[key] = items[ + sorted_data[key]["unicode"] + ].connect("activate", item_response, sorted_data[key]) else: # This won't happen, we are not grouping toned icons pass else: - if tones_re.match(sorted_data[key]["name"]) == None: + if tones_re.match(sorted_data[key]["name"]) == None: # Not toned emoji (aka simplest case), just add to menu - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_data[key]["unicode"] + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + + "/assets/svg/" + + sorted_data[key]["unicode"] + + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) - items[sorted_data[key]["unicode"]] = Gtk.ImageMenuItem(sorted_data[key]["name"].title()) - item_settings = items[sorted_data[key]["unicode"]].get_settings() - item_settings.set_property('gtk-menu-images', True) - GLib.idle_add(items[sorted_data[key]["unicode"]].set_image, img) - GLib.idle_add(category_menu[category].append, items[sorted_data[key]["unicode"]]) + items[sorted_data[key]["unicode"]] = Gtk.ImageMenuItem( + label=sorted_data[key]["name"].title() + ) + item_settings = items[ + sorted_data[key]["unicode"] + ].get_settings() + item_settings.set_property("gtk-menu-images", True) + GLib.idle_add( + items[sorted_data[key]["unicode"]].set_image, img + ) + GLib.idle_add( + category_menu[category].append, + items[sorted_data[key]["unicode"]], + ) GLib.idle_add(items[sorted_data[key]["unicode"]].show) - signals[key] = items[sorted_data[key]["unicode"]].connect("activate", item_response, sorted_data[key]) + signals[key] = items[ + sorted_data[key]["unicode"] + ].connect("activate", item_response, sorted_data[key]) else: # Toned emoji - if settings["toned"] == -1 and settings["lowend"] == False: + if ( + settings["toned"] == -1 + and settings["lowend"] == False + ): # Show all toned emojis in a submenu original_key = key chars = sorted_data[key]["unicode"].split("-") - tone_group = chars[0] # FIXME this can be multibyte so taking the first byt isn't right + tone_group = chars[ + 0 + ] # FIXME this can be multibyte so taking the first byt isn't right if not tone_group in tone_groups: # Create tone submenu submenu_tones[tone_group] = Gtk.Menu() - GLib.idle_add(items[tone_group].set_submenu, submenu_tones[tone_group]) + GLib.idle_add( + items[tone_group].set_submenu, + submenu_tones[tone_group], + ) tone_groups[tone_group] = items[tone_group] # Get key from original icon for k in sorted_data: if sorted_data[k]["unicode"] == tone_group: key = k # Append untoned icon to submenu - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_data[key]["unicode"] + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + + "/assets/svg/" + + sorted_data[key]["unicode"] + + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) - items[sorted_data[key]["unicode"]] = Gtk.ImageMenuItem(sorted_data[key]["name"].title()) + items[ + sorted_data[key]["unicode"] + ] = Gtk.ImageMenuItem( + label=sorted_data[key]["name"].title() + ) item_settings = items[tone_group].get_settings() - item_settings.set_property('gtk-menu-images', True) - GLib.idle_add(items[sorted_data[key]["unicode"]].set_image, img) - GLib.idle_add(submenu_tones[tone_group].append, items[sorted_data[key]["unicode"]]) - GLib.idle_add(items[sorted_data[key]["unicode"]].show) - signals[key] = items[sorted_data[key]["unicode"]].connect("activate", item_response, sorted_data[key]) + item_settings.set_property( + "gtk-menu-images", True + ) + GLib.idle_add( + items[ + sorted_data[key]["unicode"] + ].set_image, + img, + ) + GLib.idle_add( + submenu_tones[tone_group].append, + items[sorted_data[key]["unicode"]], + ) + GLib.idle_add( + items[sorted_data[key]["unicode"]].show + ) + signals[key] = items[ + sorted_data[key]["unicode"] + ].connect( + "activate", item_response, sorted_data[key] + ) key = original_key # Append toned emoji to toned submenu - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_data[key]["unicode"] + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + + "/assets/svg/" + + sorted_data[key]["unicode"] + + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) - items[sorted_data[key]["unicode"]] = Gtk.ImageMenuItem(sorted_data[key]["name"].title()) - item_settings = items[sorted_data[key]["unicode"]].get_settings() - item_settings.set_property('gtk-menu-images', True) - GLib.idle_add(items[sorted_data[key]["unicode"]].set_image, img) - GLib.idle_add(submenu_tones[tone_group].append, items[sorted_data[key]["unicode"]]) - GLib.idle_add(items[sorted_data[key]["unicode"]].show) - signals[key] = items[sorted_data[key]["unicode"]].connect("activate", item_response, sorted_data[key]) + items[ + sorted_data[key]["unicode"] + ] = Gtk.ImageMenuItem( + label=sorted_data[key]["name"].title() + ) + item_settings = items[ + sorted_data[key]["unicode"] + ].get_settings() + item_settings.set_property("gtk-menu-images", True) + GLib.idle_add( + items[sorted_data[key]["unicode"]].set_image, + img, + ) + GLib.idle_add( + submenu_tones[tone_group].append, + items[sorted_data[key]["unicode"]], + ) + GLib.idle_add( + items[sorted_data[key]["unicode"]].show + ) + signals[key] = items[ + sorted_data[key]["unicode"] + ].connect( + "activate", item_response, sorted_data[key] + ) else: # Show only one tone for toned emojis - if settings["toned"] == 0 or settings["lowend"] == True: + if ( + settings["toned"] == 0 + or settings["lowend"] == True + ): # Untoned emojis are already shown by default, so do nothing # When using lowend mode, untoned emojis are enforced pass else: desired_tone = settings["toned"] - current_tone = re.search("\d",sorted_data[key]["name"]).group(0) + current_tone = re.search( + "\d", sorted_data[key]["name"] + ).group(0) if desired_tone == int(current_tone): # Replace item with desired tone toned_key = key - chars = sorted_data[key]["unicode"].split("-") # FIXME this can be multibyte so taking the first byt isn't right + chars = sorted_data[key]["unicode"].split( + "-" + ) # FIXME this can be multibyte so taking the first byt isn't right untoned_code = chars[0] for k in sorted_data: - if sorted_data[k]["unicode"] == untoned_code: + if ( + sorted_data[k]["unicode"] + == untoned_code + ): key = k # Replace image in item - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(directory + "/assets/svg/" + sorted_data[toned_key]["unicode"] + ".svg", iconsizes[1], iconsizes[2]) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( + directory + + "/assets/svg/" + + sorted_data[toned_key]["unicode"] + + ".svg", + iconsizes[1], + iconsizes[2], + ) img = Gtk.Image.new_from_pixbuf(pixbuf) - GLib.idle_add(items[sorted_data[key]["unicode"]].set_image, img) + GLib.idle_add( + items[ + sorted_data[key]["unicode"] + ].set_image, + img, + ) # Replace action - items[sorted_data[key]["unicode"]].disconnect(signals[key]) - signals[key] = items[sorted_data[key]["unicode"]].connect("activate", item_response, sorted_data[toned_key]) - + items[ + sorted_data[key]["unicode"] + ].disconnect(signals[key]) + signals[key] = items[ + sorted_data[key]["unicode"] + ].connect( + "activate", + item_response, + sorted_data[toned_key], + ) + # Load icons into recent category recent_items = [] - for i in range (0, settings["recent"]): - recent_items.append( Gtk.ImageMenuItem()) + for i in range(0, settings["recent"]): + recent_items.append(Gtk.ImageMenuItem()) item_settings = recent_items[i].get_settings() - item_settings.set_property('gtk-menu-images', True) + item_settings.set_property("gtk-menu-images", True) category_menu["recent"].append(recent_items[i]) refresh_recent_submenu() @@ -524,29 +748,29 @@ if __name__ == "__main__": # Create separator separator = Gtk.SeparatorMenuItem() - GLib.idle_add(menu.append,separator) + GLib.idle_add(menu.append, separator) GLib.idle_add(separator.show) # Create search item - search_item = Gtk.MenuItem("Search Emoji") - GLib.idle_add(menu.append,search_item) + search_item = Gtk.MenuItem(label="Search Emoji") + GLib.idle_add(menu.append, search_item) GLib.idle_add(search_item.show) search_item.connect("activate", open_search_window, "Search") # Create settings item - settings_item = Gtk.MenuItem("Settings...") - GLib.idle_add(menu.append,settings_item) - GLib.idle_add(settings_item.show) + settings_item = Gtk.MenuItem(label="Settings...") + GLib.idle_add(menu.append, settings_item) + GLib.idle_add(settings_item.show) settings_item.connect("activate", open_settings_window, "Settings") - + # Create another separator separator = Gtk.SeparatorMenuItem() - GLib.idle_add(menu.append,separator) + GLib.idle_add(menu.append, separator) GLib.idle_add(separator.show) # Create exit item - exit_item = Gtk.MenuItem("Exit") - GLib.idle_add(menu.append,exit_item) + exit_item = Gtk.MenuItem(label="Exit") + GLib.idle_add(menu.append, exit_item) GLib.idle_add(exit_item.show) exit_item.connect("activate", exit, "Exit") @@ -564,7 +788,7 @@ if __name__ == "__main__": sock.listen(1) GLib.io_add_watch(sock, GLib.IO_IN, handle_sock_signal) except: - print 'Could not listen to socket!' + print("Could not listen to socket!") # Run server signal.signal(signal.SIGINT, signal.SIG_DFL)