You are not logged in.
https://github.com/freddix/openbox/blob … rives-menu
#!/usr/bin/python
import gio
sub_id = 0
drives = gio.VolumeMonitor().get_connected_drives()
print '<openbox_pipe_menu>'
for drive in drives:
# get volumes on a drive
volumes = drive.get_volumes()
if volumes:
# main menu entry
drive_name = drive.get_name()
print '<menu id="drives-sub-menu' + str(sub_id) + '\" label=\"' + drive_name + '\">'
for volume in volumes:
volume_str = volume.get_name()
volume_ident = volume.get_identifier("unix-device")
mount = volume.get_mount()
icon = volume.get_icon()
if isinstance(icon, gio.ThemedIcon):
icon_name = icon.get_names()[0]
else:
icon_name = ""
# volume icon & label
if mount:
print '<item label=\"[ ' + volume_str + ' ]\" icon=\"' + icon_name + '\">'
else:
print '<item label=\"' + volume_str + '\" icon=\"' + icon_name + '\">'
print '<action name="Execute">'
if mount:
print '<command>udisksctl unmount -b ' + volume_ident + '</command>'
else:
print '<command>udisksctl mount -b ' + volume_ident + '</command>'
print '</action>'
print '</item>'
sub_id = sub_id + 1
print '</menu>'
print '</openbox_pipe_menu>'This script works well with python2.
Can any one help me to convert this script into python3 or bash.
Offline
I don't know what module 'gio' does, so I can't help you if that module is incompatible. It looks to me that all you have to do is convert all the print statements to function calls.
I other words, each time your script does a
print ...
change it so that the argument(s) is surrounded by parens. e.g.
print ( ... )
Thus
print '<menu id="drives-sub-menu' + str(sub_id) + '\" label=\"' + drive_name + '\">'would become
print ('<menu id="drives-sub-menu' + str(sub_id) + '\" label=\"' + drive_name + '\">')Also it looks like the script you posted has a mixture of spaces and tabs which will fail in either python2 or python3. You would have to make sure you are consistant.
Offline
2to3 can convert python2 to pyhton3 syntax.
But the modules ?!.
Offline
Hello,
I have ported it to Python 3 and to not generate XML by hand. But it seems that Gio has problems on Python 3. On my machine it Segfaults (see comment in the code).
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as ET
from gi.repository import Gio
def main():
sub_id = 0
_volmon = Gio.VolumeMonitor()
# The next line gives a Segfault on my up-to-date ArchLinux system
drives = _volmon.get_connected_drives()
root = ET.Element('openbox_pipe_menu')
for drive in drives:
volumes = drive.get_volumes()
if volumes:
drive_name = drive.get_name()
_id = 'drives-sub-menu-{}'.format(sub_id)
menu = ET.SubElement(root, 'menu',
attrib={'id': _id, 'label': drive_name})
for volume in volumes:
volume_name = volume.get_name()
volume_id = volume.get_identifier('unix-device')
mount = volume.get_mount()
icon = volume.get_icon()
if isinstance(icon, Gio.ThemedIcon):
icon_name = icon.get_names()[0]
else:
icon_name = ''
if mount:
volume_name = '[{}]'.format(volume_name)
item = ET.SubElement(
menu, 'item', attrib={'label': volume_name,
'icon': icon_name}
)
action = ET.SubElement(item, 'action',
attrib={'name': 'Execute'})
command = ET.SubElement(action, 'command')
_mnt = 'un' if mount else ''
command.text = 'udisksctl {}mount -b {}'.format(_mnt, volume_id)
sub_id += 1
return root
if __name__ == '__main__':
root = main()
print(ET.dump(root))Maybe I am missing a package.
Whitie
Offline
This script works well with python2.
Is there some reason not to use python2 then?
Offline
No particular reason. Just I would like to have the script in python3 ( if not, in bash).
Offline