📜 ⬆️ ⬇️

Writing an applet for GNOME in Python

I publish this post at the request of the respected VladX , who has a temporary misunderstanding with karma.

An applet is a small application built directly into the GNOME panel. Usually, an applet performs some kind of narrow functionality (changing the volume, mounting devices), which distinguishes it from the cumbersome window application. In fact, it is not difficult for a coder knowing a Python to write your own applet, but there is a solution for those who do not know: a large number of useful (and not very) applets are on gnomefiles.org , in addition, there are examples of writing applets on C on the official site .

Go!


So, we write an applet. The applet should do something. Our applet, for example, will show habrakarma and habrasila. For all this information we will use the Habr API .

#! / usr / bin / env python
# coding = utf-8
import sys , os , gtk, gtk. gdk , pygtk, gnomeapplet, gnome
')
pygtk. require ( '2.0' )

__USER__ = 'VladX'
__URL__ = 'http://habrahabr.ru/api/profile/' + __USER__


This is how our applet begins. Everything is clear here - we import the necessary libraries, put the necessary parameters into the variables. In order to avoid any problems with Russian characters, we will use UTF-8 encoding (and by the way, I advise you to always do the same).

class MyApplet ( gnomeapplet. Applet ) :

def __init__ ( self , applet, iid ) :

self . applet = applet
self . applet . set_name ( 'MyApplet' )
self . hbox = gtk. HBox ( )
self . applet . add ( self . hbox )
self . event = gtk. EventBox ( )
self . hbox add ( self . event )
self . info = gtk. Label ( )
self . event . add ( self . info ) # In order for an object to react to various events, it must be placed in the Event Box
self . event . set_tooltip_text ( 'Habrayuzer' + __USER__ )
self . event . connect ( 'button-press-event' , self . callback_button )
self .__ init_popupmenu ( )
self . applet . connect ( 'destroy' , self . callback_destroy )
self . applet . show_all ( ) # Show all this on the panel
self . info . set_text ( self . get_info ( ) )

def __init_popupmenu ( self ) :

self . applet . setup_menu ( '' '
<popup name = ' button3 '>
<menuitem name = ' Open Item ' verb = ' Open ' stockid = ' gtk- open ' />
<menuitem name = ' About Item ' verb = ' About ' stockid = ' gtk-about ' />
</ popup> ' ' ' , [
( 'Open' , self . Callback_open ) ,
( 'About' , self . Callback_about )
] , None )

def get_info ( self ) :
'' 'Making a request, for example, using Pycurl' ''
import pycurl, StringIO
content = StringIO . StringIO ( )
c = pycurl. Curl ( )
c. setopt ( pycurl. URL , __URL__ )
c. setopt ( pycurl. FOLLOWLOCATION , 1 )
c. setopt ( pycurl. WRITEFUNCTION , content. write ) # Set the method that writes the answer to a variable
c. perform ( )
c. close ( )
content = content. getvalue ( )
'' 'Parsing code' ''
try :
karma = content [ content. index ( '<karma>' ) + 7 : content. index ( '</ karma>' ) ]

except :
karma = '*'

try :
rating = content [ content. index ( '<rating>' ) + 8 : content. index ( '</ rating>' ) ]

except :
rating = '*'


return karma + '/' + rating

def callback_button ( self , widget, event ) :

self . callback_open ( self )

def callback_open ( self , event, data = None ) :
'' 'Open in the browser by clicking' ''
gnome url_show (
'http: //' +
__USER__. replace ( '_' , '-' ) + # Replace underscore characters with hyphens
'.habrahabr.ru /'
)

def callback_about ( self , event, data = None ) :
'' 'Show the standard GNOME window' ''
os . system ( 'gnome-about' )

def callback_destroy ( self , applet ) :
'' 'Destroying an object' ''
del self . applet


This is actually the most interesting part of the applet. The constructor __init__ () method initializes the object, the __init_popupmenu () method adds “Open” and “About” commands to the context menu and sets the corresponding callback method. The get_info () method receives a response from the server and processes it for the cherished numbers of karma and power. It would be foolish to use a special xml-parser in such a trivial task, so we will do it in a simple bydlokodersky way.

def applet_factory ( applet, iid ) :

MyApplet ( applet, iid )
return true

def main ( args ) :

if len ( sys . argv ) == 2 and sys . argv [ 1 ] == 'run-in-window' :

window = gtk. Window ( gtk. WINDOW_TOPLEVEL )
window. set_title ( 'It works!' )
window. connect ( 'destroy' , gtk. main_quit )
applet = gnomeapplet. Applet ( )
applet_factory ( applet, None )
applet. reparent ( window )
window. show_all ( )
gtk main ( )
sys . exit ( )

elif len ( sys . argv ) == 2 and sys . argv [ 1 ] == 'help' :
'' 'We deduce a help by parameters' ''
print '' '
--run-in-window - run applet independent of gnome-panel
--help - show this message ' ' '

else :

gnomeapplet. bonobo_factory ( 'OAFIID: GNOME_MyApplet_Factory' ,
MyApplet .__ gtype__,
'My Applet' ,
'1.0' ,
applet_factory )



if __name__ == '__main__' :
main ( sys . argv )


These functions are the same in most Python applets; you only need to change the arguments passed to the bonobo_factory method to your own.
There is also one useful point: when passing the --run-in-window parameter to the script, the applet runs in a separate window, which helps a lot when debugging.

Well, it would seem that all. But no - the applet is written, but it is not on the list of the gnome applets. To fix this, you need to put the file (in this case, you can use the name gnomeMyAppletFactory.server ) in the / usr / lib64 / bonobo / servers directory with something like this:

< oaf_info >
< oaf_server iid = 'OAFIID: GNOME_MyApplet_Factory' type = 'exe' location = '/home/vlad/applets/src/habrapplet.py' >
< oaf_attribute name = 'repo_ids' type = 'stringv' >
< item value = 'IDL: Bonobo / GenericFactory: 1.0' / >
< item value = 'IDL: Bonobo / Unknown: 1.0' / >
< / oaf_attribute >
< oaf_attribute name = 'name' type = 'string' value = 'My Applet' / >
< oaf_attribute name = 'description' type = 'string' value = 'Show your karma and ratio' / >
< / oaf_server >

< oaf_server iid = 'OAFIID: GNOME_MyApplet' type = 'factory' location = 'OAFIID: GNOME_MyApplet_Factory' >
< oaf_attribute name = 'repo_ids' type = 'stringv' >
< item value = 'IDL: GNOME / Vertigo / PanelAppletShell: 1.0' / >
< item value = 'IDL: Bonobo / Control: 1.0' / >
< item value = 'IDL: Bonobo / Unknown: 1.0' / >
< / oaf_attribute >
< oaf_attribute name = 'name' type = 'string' value = 'My First Applet' / >
< oaf_attribute name = 'name-ru' type = 'string' value = 'Habrapplet' / >
< oaf_attribute name = 'description' type = 'string' value = 'Show your karma and ratio' / >
< oaf_attribute name = 'description-en' type = 'string' value = 'Displays your karma and habrasil' / >
< oaf_attribute name = 'panel: category' type = 'string' value = 'Utility' / >
< oaf_attribute name = 'panel: icon' type = 'string' value = 'computer.png' / >
< / oaf_server >
< / oaf_info >


Do not forget to change the path to the script on your own and allow its execution using chmod + x .

Successful codding,% username%!

ps In preparing the material used resource highlight.hohli.com .

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


All Articles