📜 ⬆️ ⬇️

We write addition to Gnome-deskbar-applet

Gnome deskbar-applet is an applet to the Gnome panel that allows you to search for files, execute programs by typing the first letters in their name (like Quicksilver or a spotlight from a poppy)
In this article I will tell you how easy it is to write Python add-ons for this applet. We will write a search in Habré.

Create a habr-suggestions.py file somewhere, open it in your favorite editor.
For starters, let's make a "skeleton":
 from gettext import gettext as _
 import deskbar.interfaces.Module
 import deskbar.interfaces.Match
 from deskbar.handlers.actions.ShowUrlAction import ShowUrlAction
 from deskbar.core.Utils import load_icon
    
 HANDLERS = ["HabrSuggestHandler"]
    
 class HabrSuggestHandler (deskbar.interfaces.Module):
    
     INFOS = {'icon': load_icon ("habr.png"),
              'name': _ ("Habrahabr.ru Suggest"),
              'description': _ ("As you type, Habr will offer suggestions."),
              'version': '1.0.0.0'}
    
     def __init __ (self):
         deskbar.interfaces.Module .__ init __ (self)
        
     def query (self, qstring):
         pass



Go ahead. The search itself will be carried out through the page habrahabr.ru/search/?q= WHAT_WE WE LOOK FOR , we add the functionality to the so-called “suggestions”, suggestions, which we implement through Google. We will need the simplejson module to parse Google responses.

 from gettext import gettext as _
 import deskbar.interfaces.Module
 import deskbar.interfaces.Match
 from deskbar.handlers.actions.ShowUrlAction import ShowUrlAction
 from deskbar.core.Utils import load_icon
 import urllib
 try:
     import simplejson
 except:
     pass
    
 HANDLERS = ["HabrSuggestHandler"]
    
 GOOGLE_SUGGEST_URL = "http://suggestqueries.google.com/complete/search"
 HABR_SEARCH_URL = "http://habrahabr.ru/search/"

 class HabrSuggestAction (ShowUrlAction):
     def __init __ (self, query):
         ShowUrlAction .__ init __ (self, query, GOOGLE_SEARCH_URL + '?' + Urllib.urlencode ({'q': query}))
        
     def get_verb (self):
         return _ ("Search <b> Habrahabr.ru </ b> for <i>% (name) s </ i>")

 class GoogleSuggestMatch (deskbar.interfaces.Match):
     def __init __ (self, query, ** args):
         deskbar.interfaces.Match .__ init__ (self, name = query, category = "web", icon = "habr.png", ** args)
         self._query = query
         self.add_action (HabrSuggestAction (query)) 
        
     def get_hash (self, text = None):
         return self._query
        
 class HabrSuggestHandler (deskbar.interfaces.Module):
    
     INFOS = {'icon': load_icon ("habr.png"),
              'name': _ ("Habrahabr.ru Suggest"),
              'description': _ ("As you type, Habr will offer suggestions."),
              'version': '1.0.0.0'}
    
     def __init __ (self):
         deskbar.interfaces.Module .__ init __ (self)
        
     def query (self, qstring):        
         args = {'output': 'firefox', 'qu': qstring, 'as_sitesearch': 'http://habrahabr.ru'}
         url = GOOGLE_SUGGEST_URL + '?'  + urllib.urlencode (args)
         result = simplejson.load (urllib.urlopen (url))
         if not 'Error' in result:
             matches = []
             # result looks like ["qstring", ["suggestion 1", "suggestion 2",]]
             for suggest in result [1]:
                 matches.append (GoogleSuggestMatch (suggest))
             self._emit_query_ready (qstring, matches)
        
     @staticmethod
     def has_requirements ():
         try:
             import simplejson
             return true
         except:
             HabrSuggestHandler.INSTRUCTIONS = _ ("Python module simplejson is not available")
             return false

')
Installing add-ons is easy - just drag the tar.bz2 file with the .py and .png file inside into the Deskbar-applet settings window, like this:


Tick ​​it and it will work:

image

Download .tar.bz2 with addition
// tell me where to find the coloring of the python code for the habr?

Source: https://habr.com/ru/post/74413/


All Articles