You are not logged in.

#1 2014-11-15 06:25:30

bhante
Member
Registered: 2008-05-20
Posts: 22

openbox pipemenu - need help : convert script from python2 to python3

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

#2 2014-11-15 07:10:07

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: openbox pipemenu - need help : convert script from python2 to python3

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

#3 2014-11-15 10:37:12

bhante
Member
Registered: 2008-05-20
Posts: 22

Re: openbox pipemenu - need help : convert script from python2 to python3

2to3 can convert python2 to pyhton3 syntax.

But the modules ?!.

Offline

#4 2014-11-15 10:48:00

whitie
Member
Registered: 2011-03-13
Posts: 24

Re: openbox pipemenu - need help : convert script from python2 to python3

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

#5 2014-11-15 11:33:58

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: openbox pipemenu - need help : convert script from python2 to python3

bhante wrote:

This script works well with python2.

Is there some reason not to use python2 then?

Offline

#6 2014-11-16 13:15:09

bhante
Member
Registered: 2008-05-20
Posts: 22

Re: openbox pipemenu - need help : convert script from python2 to python3

No particular reason. Just I would like to have the script in python3 ( if not, in bash).

Offline

Board footer

Powered by FluxBB