#! / 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__
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
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 )
< 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 >
Source: https://habr.com/ru/post/64317/
All Articles