Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
pygtk code help: systray script to unmount fs
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
yayo
n00b
n00b


Joined: 19 May 2014
Posts: 34

PostPosted: Wed Jan 28, 2015 1:23 am    Post subject: pygtk code help: systray script to unmount fs Reply with quote

Hello all! :)
Hope this is the correct section for this. otherwise sorry, feel free to yell at me whatever and then move it somewhere else. : P

I was trying to write down a little script for making a systray icon to manage removable media, like listing attached devices and unmounting them with a click. sorta.
I found that pygtk is probably the best solution for such task, but since this is the 1st time I deal with python I have no clue what I'm doing... haem... (coff!) : P
I just mixed up code found here and there on the internet and finally managed to get it work, but it's still a little mess.

I was wondering if someone with better skill than me with pyton could provide hints or suggestions. ??: /

This is the code so far:
Code:
#! /usr/bin/env python
#! -*- coding : utf8 -*-

import os
import sys
import pygtk
pygtk.require("2.0")
import gtk
import gobject

class Pymail:
   def __init__(self):
      self.icon = gtk.StatusIcon()
      self.icon.set_from_icon_name('gnome-dev-removable-usb')
      self.icon.set_visible(True)
      self.icon.connect('popup-menu', self.on_right_click)
      self.constructMenu()

   def constructMenu(self):
      separator = gtk.SeparatorMenuItem()
      self.menu = gtk.Menu()

      if self.get_sdb():
         sdbvendor = os.popen("udevadm info -q property --name=sdb|grep ID_VENDOR=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdbmodel = os.popen("udevadm info -q property --name=sdb|grep ID_MODEL=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdbbus = os.popen("udevadm info -q property --name=sdb|grep ID_BUS=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdbtype = os.popen("udevadm info -q property --name=sdb|grep ID_TYPE=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdblabel  = 'device: /dev/sdb1, type: '+sdbbus+' '+sdbtype+', vendor:'+sdbvendor+', model:'+sdbmodel+', mountpoint: /mnt/usb1'
         sdbItem = gtk.MenuItem(sdblabel);
         sdbItem.connect('activate', self.on_sdb)
         self.menu.add(sdbItem)
      if self.get_sdc():
         sdcvendor = os.popen("udevadm info -q property --name=sdc|grep ID_VENDOR=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdcmodel = os.popen("udevadm info -q property --name=sdc|grep ID_MODEL=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdcbus = os.popen("udevadm info -q property --name=sdc|grep ID_BUS=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdctype = os.popen("udevadm info -q property --name=sdc|grep ID_TYPE=|grep -Po '(?<==).*'|tr -d '\n'").read()
         sdclabel  = 'device: /dev/sdc1, type: '+sdcbus+' '+sdctype+', vendor: '+sdcvendor+', model: '+sdcmodel+', mountpoint: /mnt/usb1'
         sdcItem = gtk.MenuItem(sdclabel);
         sdcItem.connect('activate', self.on_sdc)
         self.menu.add(sdcItem)

      """self.menu.add(separator)"""

      """quitItem = gtk.ImageMenuItem('gtk-quit')"""
      """quitItem.connect('activate', self.on_quit)"""
      """self.menu.add(quitItem)"""

      self.menu.show_all()

   def get_sdb(self):
      return os.popen('if [ -e /dev/sdb ] ; then echo 1; fi').read()
   def get_sdc(self):
      return os.popen('if [ -e /dev/sdc ] ; then echo 1; fi').read()

   def on_right_click(self, widget, button, time):
      self.menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.icon)
      
   def on_sdb(self, widget):
      return os.popen('sudo umount /mnt/usb1 && notify-send -i usb "/mnt/usb1 unmounted" "you can now safely unplug device"').read()
   def on_sdc(self, widget):
      return os.popen('sudo umount /mnt/usb2 && notify-send -i usb "/mnt/usb2 unmounted" "you can now safely unplug device"').read()
   
   """def on_quit(self, widget):"""
   """gtk.mainquit()"""

if __name__=="__main__":
   mail = Pymail()
   gtk.main()


It searches for /dev/sdb or /dev/sdc and creates related menu entry. A click and the device is umounted.

(Well, not that bad, considering I know absolutely nothing of python. : P )

Some things I'd like to fix, but II have no clue how to do:
- the script should stay running and silent with no icon, check every xx seconds for new attached devices, add corresponding entry to the menu for each device found, then show the icon on systray
- menu entries should disappear once the device have been removed (or maybe marked as plugged but not mounted, thus not unmountable (,thus remountable?))
- Rely on actually mounted partitions rather than existing /dev files
- automatic menu entries generation for every possible removable media, rather than having a list of cases (better result with shorter and smarter code)
- I have no idea how to deal with cddvd, since /dev/sr0 ir always present

comments? hints? suggestions? : /

Thanks in advance and sorry for my poor English. : )
Back to top
View user's profile Send private message
Roman_Gruber
Advocate
Advocate


Joined: 03 Oct 2006
Posts: 3806
Location: Austro Bavaria

PostPosted: Mon Feb 02, 2015 8:34 pm    Post subject: Reply with quote

Quote:
- the script should stay running and silent with no icon, check every xx seconds for new attached devices, add corresponding entry to the menu for each device found, then show the icon on systray


Such behaviour is usally called deamon. e.g. lxdm -d starts lxdm in daemon mode. you may check man lxdm for an explanation about the d flag.

You may have put it in poratage and progarmming section of the fourm

The next thing is to parse dmesg or /var/log/messages when you have a syslogger running and create those menuentries or just write a udev-rule to mount /umount stuff. I just talk in a bigger point of view. I have never tried / programmed this and therefore see it as possible suggestion which should work afaik.

I saw some ohter guy here recently who wrote a kit free mounter. you may see if that is appropriate for yourself
Back to top
View user's profile Send private message
yayo
n00b
n00b


Joined: 19 May 2014
Posts: 34

PostPosted: Fri Feb 06, 2015 11:41 pm    Post subject: Reply with quote

hey th04l124! Thanks for the reply. :)

hmm, yeah, maybe that the programming area was more appropriate for such thread, but when I posted it I was a bit in a hurry and didn't rly notice that section... : P

I already wrote some udev rules for mounting removable devices
Code:
# automounter per chiavette usb / cd-dvd / schede sd

# usb
ACTION=="add", KERNEL=="sd[b-z][1-9]", RUN+="/bin/mkdir -p /mnt/$parent/%n", RUN+="/bin/mount /dev/%k /mnt/$parent/%n"
ACTION=="remove", KERNEL=="sd[b-z]", RUN+="/bin/rm -rf /mnt/%k"

# sd
ACTION=="add", KERNEL=="mmcblk0p[1-9]", RUN+="/bin/mkdir -p /mnt/sd/%n", RUN+="/bin/mount /dev/%k /mnt/sd/%n"
ACTION=="remove", KERNEL=="mmcblk0", RUN+="/bin/rm -rf /mnt/sd"


but as you can see the remove part just get rid of empty folders, since it runs only when the device gets physically removed, being the FS unmounted or not. That's why I was trying to make something nice for monitoring removable devs and allow me to unmounting them easily via mouse. : P
In fact a computer is made just for that purpose, to automagically do things for you! ^_^

Now the point is to verify if I can write (I should say assemble and fix existing pieces of code found on the web) something easy and working without too much effort, because otherwise it doesn't make sense, of course. : P

At the present time I'm someway busy with other stuff (healt, writing a book, healt, (some kind of) job, healt...), but I'll get back to this soon too.
Dubtful I'll manage to write a fully functional piece of software to get it merged to the portage tree, but perhaps a tiny script to share with the community... :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum