~ubuntu-branches/ubuntu/trusty/screenlets/trusty

« back to all changes in this revision

Viewing changes to src/share/screenlets/Netmonitor/NetmonitorScreenlet.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2011-02-24 00:51:35 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110224005135-p65cxwmpfwwy3woi
Tags: 0.1.2+bzr616-0ubuntu1
* New upstream snapshot.
 - Move individual screenlets into another source package.
 - Many bug fixes since 0.1.2.
* Convert to source format 3.0 (quilt).
* debian/patches
 - Removed all patches, merged upstream.
* debian/rules:
 - Convert to dh7.
 - Don't remove copies of feedparser, not in this package.
 - Don't remove empty directory for the individual screenlets.
* debian/screenlets.install:
 - Update installed files.
* debian/compat
 - Bump to level 7.
* debian/control:
 - Rewrite Depends and build-depends.
* README.Debian :
 - Mention mutter as a composite manager.
* debian/README.source:
 - Remove the README.source.
* debian/screenlets.manpages:
 - Add the manpages.
* debian/copyright:
 - Rewrite with dep5 style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
#
4
 
#Copyright (C) 2007 Helder Fraga
5
 
#
6
 
#This program is free software; you can redistribute it and/or
7
 
#modify it under the terms of the GNU General Public License
8
 
#as published by the Free Software Foundation; either version 2
9
 
#of the License, or (at your option) any later version.
10
 
#
11
 
#This program is distributed in the hope that it will be useful,
12
 
#but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#GNU General Public License for more details.
15
 
#
16
 
#You should have received a copy of the GNU General Public License
17
 
#along with this program; if not, write to the Free Software
18
 
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
 
#
20
 
import screenlets
21
 
from screenlets import Screenlet
22
 
from screenlets.options import FloatOption, BoolOption, StringOption, FontOption, ColorOption, IntOption
23
 
import cairo
24
 
import pango
25
 
import gobject
26
 
import commands
27
 
import gtk
28
 
import datetime
29
 
import os
30
 
import sys
31
 
from sys import argv
32
 
import gconf
33
 
 
34
 
class NetmonitorScreenlet(screenlets.Screenlet):
35
 
        """A Screenlet that displays net traffic , and month totals"""
36
 
        
37
 
        # default meta-info for Screenlets
38
 
        __name__ = 'NetmonitorScreenlet'
39
 
        __version__ = '0.7'
40
 
        __author__ = 'Helder Fraga aka Whise'
41
 
        __desc__ =__doc__
42
 
 
43
 
        __timeout = None
44
 
        p_layout = None
45
 
        update_interval = 1
46
 
        __buffer = None
47
 
        dev = 'eth0'
48
 
        unit_d = 'KB'
49
 
        unit_u = 'KB'
50
 
        u1 = 0
51
 
        n1 = 0
52
 
        u2 = 0
53
 
        n2 = 0
54
 
        xo = 0
55
 
        mini = False
56
 
        download_total = 0
57
 
        upload_total = 0
58
 
        dlt = 0
59
 
        dev_choices = []
60
 
        ult = 0
61
 
        dlt = 0
62
 
        frame_color = (1, 1, 1, 1)
63
 
        color_text = (0, 0, 0, 0.6)
64
 
        font = 'FreeSans'
65
 
        mypath = argv[0][:argv[0].find('NetmonitorScreenlet.py')].strip()
66
 
        month = 0
67
 
        show_frame = True
68
 
        show_txt = True
69
 
        
70
 
        def __init__(self, **keyword_args):
71
 
                screenlets.Screenlet.__init__(self, width=180, height=110, uses_theme=True,ask_on_option_override=False, **keyword_args) 
72
 
                
73
 
                self.theme_name = "default"
74
 
                self.add_menuitem("toggle", "Toggle mini view") 
75
 
                self.add_default_menuitems()
76
 
                self.update_interval = self.update_interval
77
 
                #self.disable_updates = True
78
 
                self.get_devices()
79
 
                self.add_options_group('Options', '')
80
 
                self.add_option(BoolOption('Options', 'show_frame',bool(self.show_frame), 'Show frame','Show frame'))
81
 
                self.add_option(BoolOption('Options', 'show_txt',bool(self.show_txt), 'Show In Out text','Show Text'))
82
 
                self.add_option(ColorOption('Options','frame_color', 
83
 
                        self.frame_color, 'Frame color', 
84
 
                        'Frame color'))
85
 
                self.add_option(ColorOption('Options','color_text', 
86
 
                        self.color_text, 'Text color', ''))
87
 
                self.add_option(FontOption('Options','font', 
88
 
                        self.font, 'Text Font', 
89
 
                        'Text font'))
90
 
                self.add_option(StringOption('Options', 'dev', self.dev,'Select Device', '',choices = self.dev_choices))
91
 
                self.add_option(StringOption('Options', 'download_total', self.download_total,'Download total this month', '',))                
92
 
                self.add_option(StringOption('Options', 'upload_total', self.upload_total,'Upload total this month', '',))
93
 
                self.add_option(BoolOption('Options', 'mini', self.mini,'Use mini mode', '',))
94
 
 
95
 
                self.__timeout = gobject.timeout_add(1000, self.update)
96
 
                self.gconf_client = gconf.client_get_default() 
97
 
                self.gconf_path = "/apps/screenlets/" + self.__name__
98
 
                self.gconf_client.notify_add(self.gconf_path, self.config_event)
99
 
                self.gconf_key = "/apps/screenlets/" + self.__name__ + "/month"#
100
 
                print self.gconf_client.get_int(self.gconf_key)
101
 
                #self.gconf_client.notify_add(self.gconf_key, self.update) 
102
 
 
103
 
        def config_event(self):
104
 
                pass
105
 
 
106
 
        def get_devices(self):
107
 
                if self.dev_choices != []: del self.dev_choices
108
 
                self.dev_choices = []
109
 
                data = commands.getoutput("cat /proc/net/dev |grep :")
110
 
                for f in data.split(' '):
111
 
                        if f != None and f.find(':') != -1 and not f.startswith('0'):  
112
 
                                self.dev_choices.append(str(f).split(':')[0])
113
 
        
114
 
 
115
 
 
116
 
        def __setattr__(self, name, value):
117
 
                # call Screenlet.__setattr__ in baseclass (ESSENTIAL!!!!)
118
 
        #       if name == 'download_total' or name == 'upload_total' :
119
 
        #               value = str(value)
120
 
                if name != 'xo' or  name != 'download' or  name != 'upload' or  name != 'unit1' or name != 'unit2' or name != 'u2' or name != 'u1' or name != 'n2' or name != 'n1'  or  name != 'p_layout' or name != 'download_total' or name != 'upload_total':       
121
 
                
122
 
                        screenlets.Screenlet.__setattr__(self, name, value)     
123
 
                if name == 'mini':
124
 
                        if value == True:
125
 
                                self.height = 60
126
 
                        else:
127
 
                                self.height = 110
128
 
 
129
 
 
130
 
                return True
131
 
 
132
 
        
133
 
        def menuitem_callback(self, widget, id):
134
 
                screenlets.Screenlet.menuitem_callback(self, widget, id)
135
 
 
136
 
                if id == "toggle":
137
 
                        self.mini = not self.mini
138
 
 
139
 
        def on_mouse_down(self,event):
140
 
                if event.button == 3:
141
 
                        self.get_devices()
142
 
                        
143
 
        def get_load(self):
144
 
                try:
145
 
                        data = commands.getoutput("cat /proc/net/dev")
146
 
                        data = data[data.find(self.dev + ":")+5:]
147
 
                        self.u1 = float( data.split()[0] )
148
 
                        self.n1 = float( data.split()[8] )
149
 
                        self.month = self.gconf_client.get_int(self.gconf_key)
150
 
                        if self.month == 0:
151
 
                                print 'no month saving new month'
152
 
                                self.month = datetime.datetime.now().month
153
 
                                self.gconf_client.set_int(self.gconf_key, int(self.month))              
154
 
 
155
 
                        self.download = str(int(self.u1 -self.u2)/1024)
156
 
                        self.upload = str(int(self.n1 -self.n2)/1024)
157
 
 
158
 
                        if self.u2 == 0 : self.download = '0'
159
 
                        if self.n2 == 0 : self.upload = '0'
160
 
 
161
 
 
162
 
                
163
 
                        if str(self.month) ==  str(datetime.datetime.now().month):
164
 
                                print 'same month'
165
 
                                self.download_total = int(self.download_total) + (int(self.download))
166
 
                                self.upload_total = int(self.upload_total) + (int(self.upload))
167
 
                        else:
168
 
                                print 'diferent month'
169
 
 
170
 
                                self.download_total = 0
171
 
                                self.upload_total = 0
172
 
                                self.month =  datetime.datetime.now().month
173
 
                                self.gconf_client.set_int(self.gconf_key, int(self.month))      
174
 
 
175
 
                        if int(self.download_total) >= 1024: 
176
 
 
177
 
                                self.dlt = float(int(self.download_total)) / 1024
178
 
                                self.unit_d = 'MB'
179
 
 
180
 
                        elif  int(self.download_total) >= (1024*1024): 
181
 
        
182
 
                                self.dlt = (float(int(self.download_total)) / 1024)/1024
183
 
                                self.unit_d = 'GB'
184
 
 
185
 
                        elif  int(self.download_total) >= (1024*1024*1024): 
186
 
        
187
 
                                self.dlt = ((float(int(self.download_total)) / 1024)/1024)/1024
188
 
                                self.unit_d = 'TB'
189
 
 
190
 
                        else:
191
 
 
192
 
                                self.dlt = float(int(self.download_total))
193
 
 
194
 
                        if int(self.upload_total) >= 1024: 
195
 
 
196
 
                                self.ult = float(int(self.upload_total)) / 1024
197
 
                                self.unit_u = 'MB'
198
 
 
199
 
                        elif  int(self.upload_total) >= (1024*1024): 
200
 
 
201
 
                                self.ult = (float(int(self.upload_total)) / 1024)/1024
202
 
                                self.unit_u = 'GB'
203
 
 
204
 
                        elif  int(self.upload_total) >= (1024*1024*1024): 
205
 
 
206
 
                                self.ult = ((float(int(self.upload_total)) / 1024)/1024)/1024
207
 
                                self.unit_u = 'TB'
208
 
 
209
 
                        else:
210
 
 
211
 
                                self.ult = float(int(self.upload_total))
212
 
 
213
 
                        try:
214
 
                                self.ult = str(self.ult)
215
 
                                self.dlt = str(self.dlt)
216
 
 
217
 
                                if self.ult.find('.') != 0 :
218
 
                                        self.ult = self.ult[:self.ult.find('.')+2 ].strip()
219
 
                                if self.dlt.find('.')  != 0:
220
 
                                        self.dlt = self.dlt[:self.dlt.find('.')+2 ].strip()
221
 
                                print 'download total - '+ self.dlt
222
 
                                print 'upload total - ' +self.ult
223
 
                        except:
224
 
                                pass
225
 
                        self.u2 = self.u1
226
 
                        self.n2 = self.n1
227
 
                        screenlets.Screenlet.__setattr__(self, 'download_total', str(self.download_total))
228
 
        
229
 
                        screenlets.Screenlet.__setattr__(self, 'upload_total', str(self.upload_total))
230
 
                        self.session.backend.flush()
231
 
                except:
232
 
                        self.download = 0
233
 
                        self.upload = 0
234
 
 
235
 
        
236
 
 
237
 
 
238
 
        def on_draw(self, ctx):
239
 
 
240
 
 
241
 
                ctx.scale(self.scale, self.scale)
242
 
                ctx.set_operator(cairo.OPERATOR_OVER)
243
 
 
244
 
 
245
 
                ctx.set_source_rgba(1, 1, 1, 1)
246
 
 
247
 
                
248
 
                if len(self.session.instances) != 0:
249
 
                        self.get_load()
250
 
 
251
 
                        if self.show_frame:
252
 
                                gradient = cairo.LinearGradient(0, self.height*2,0, 0)
253
 
                                gradient.add_color_stop_rgba(1,*self.frame_color)
254
 
                                gradient.add_color_stop_rgba(0.7,self.frame_color[0],self.frame_color[1],self.frame_color[2],1-self.frame_color[3]+0.5)
255
 
 
256
 
                                ctx.set_source(gradient)
257
 
                        
258
 
                                self.draw_rectangle_advanced (ctx, 0, 0, self.width-12, self.height-12, rounded_angles=(5,5,5,5), fill=True, border_size=2, border_color=(0,0,0,0.5), shadow_size=6, shadow_color=(0,0,0,0.5))
259
 
                        self.draw_icon(ctx, 10, 12,gtk.STOCK_GO_DOWN,16,16)
260
 
                        self.draw_icon(ctx, 10, 32,gtk.STOCK_GO_UP,16,16)
261
 
                        ctx.set_source_rgba(*self.color_text)
262
 
                        self.draw_text(ctx,  str(self.download) + ' KB', -10, 12,  self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_RIGHT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
263
 
                        self.draw_text(ctx, str(self.upload) + ' KB', -10, 30,  self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_RIGHT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
264
 
                        if self.show_txt:self.draw_text(ctx, 'In -\nOut -', 30, 12,  self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_LEFT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
265
 
                        
266
 
                        if self.mini == False:
267
 
                                self.draw_icon(ctx, 10, 64,gtk.STOCK_GO_DOWN,16,16)
268
 
                                self.draw_icon(ctx, 10, 84,gtk.STOCK_GO_UP,16,16)
269
 
                                self.draw_text(ctx, '<small> <small><small>Totals this Month</small></small></small>', 0,45,  self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_CENTER,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
270
 
                                if self.show_txt:self.draw_text(ctx, 'In -\nOut -' , 30, 64,  self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_LEFT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
271
 
                        
272
 
                                self.draw_text(ctx, str(self.dlt) + ' '+ self.unit_d + '\n' + str(self.ult)+ ' '+ self.unit_u, -10,64,  self.font.split(' ')[0], 14, self.width,allignment=pango.ALIGN_RIGHT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
273
 
        
274
 
 
275
 
        def update (self):
276
 
        
277
 
                self.redraw_canvas()
278
 
                return True
279
 
 
280
 
        def on_draw_shape(self,ctx):
281
 
                self.on_draw(ctx)
282
 
 
283
 
if __name__ == "__main__":
284
 
        import screenlets.session
285
 
        screenlets.session.create_session(NetmonitorScreenlet)