You are not logged in.

#1 2008-01-24 04:00:22

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

python command line arguments

I am trying to write a python script that will stop gnome-screensaver, run mplayer, then start gnome-screensaver again.  The idea is that this script is called "mplayer" and comes earlier in my PATH than /usr/bin/mplayer so I just type the mplayer command as usual.  Anyway, here is my first attempt at a python script....

#!/usr/bin/python 

import dbus
import os
import sys

bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
devobj = bus.get_object('org.gnome.ScreenSaver', '/org/gnome/ScreenSaver')
dev = dbus.Interface(devobj, "org.gnome.ScreenSaver")
cookie = dev.Inhibit('MPlayer', 'Watching video')

os.system("/usr/bin/mplayer TODO_insert_parameters_from_command_line")

dev.UnInhibit(cookie)

You might have spotted my problem in the region of the "TODO_insert_parameters_from_command_line".  big_smile  I know the parameters are stored in sys.argv but how do I join them together into one string?

Offline

#2 2008-01-24 04:41:50

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: python command line arguments

if you want the arguments ala python parsing, you want optparse.
If you want to just pass them through, you want argv (which returns a list), which you can join to become a string. the first arg is the name of the file itself, so slice that off.

example

#!/usr/bin/python 
import sys
print ' '.join(sys.argv[1:])

"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2008-01-24 05:11:08

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: python command line arguments

Thanks.  I had just found join but didn't know the "sys.argv[1:]" syntax.  I have now got

command = '/usr/bin/mplayer ' + ' '.join(sys.argv[1:])
os.system(command)

Edit:  Crap that does not work... 

mplayer -alang jp -slang en video/Ergo\ Proxy/01.mkv

then the escaping "\" in the directory name disappears. 

However, I have found the "replace" command which should allow me to fix this....  I'll post back if I fail!

Last edited by Allan (2008-01-24 05:30:44)

Offline

#4 2008-01-24 05:48:08

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: python command line arguments

Well, I finally got this to work!

file = len(sys.argv) - 1
command = '/usr/bin/mplayer ' + ' '.join(sys.argv[1:file]) + ' ' + sys.argv[file].replace(" ", "\ ")
os.system(command)

No more pesky gnome-screensaver in the middle of my anime.

Offline

#5 2008-01-24 06:46:23

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: python command line arguments

you don't need to find the length manually, you can use negative indexing in slicing.

import sys
import os
import os.path
command = '/usr/bin/mplayer %s "%s"' % (' '.join(sys.argv[1:-1], os.path.normpath(sys.argv[-1:]))
os.system(command)

by quoting the path, you shouldn't have problems with spaces, and using python's sprintf for string building is a better way to go than string addition.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#6 2008-01-24 07:08:09

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: python command line arguments

cactus wrote:

you don't need to find the length manually, you can use negative indexing in slicing.

Wow, python is cool!  I am going to have to learn it properly now...

Anyway, just fixing one of the lines in case anyone else is following this.  Well, it think it is fixed because it works for me!

command = '/usr/bin/mplayer %s "%s"' % (' '.join(sys.argv[1:-1]), os.path.normpath(sys.argv[-1]))

The slice "[-1:]" cause a crash for me.

Last edited by Allan (2008-01-24 07:12:22)

Offline

#7 2008-01-24 07:30:45

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: python command line arguments

ahh. [-1:] returns a list, of one element (but a python list of strings, not a single string). so you would have had to join that list too.. which is not really needed.
Nice extrapolation to using just the -1 array index. Looks good.
smile


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#8 2008-01-24 18:04:12

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python command line arguments

PS Ergo Proxy is good

Offline

#9 2008-01-24 18:35:27

X/ax
Member
From: Oost vlaanderen, Belgium
Registered: 2008-01-13
Posts: 275
Website

Re: python command line arguments

phrakture wrote:

PS Ergo Proxy is good

So is elfen lied ^^


My coding blog (or an attempt at it)
Archer start page (or an attempt at it)

Offline

#10 2008-01-24 18:41:04

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python command line arguments

Allan wrote:

No more pesky gnome-screensaver in the middle of my anime.

Erm, doesn't mplayer have an option for this? I thought there was some --disable-screensaver type flag. I know my screen doesn't blank via DPMS when mplayer is running

Offline

#11 2008-01-24 21:14:00

diamondmind
Member
From: Lexington
Registered: 2007-12-19
Posts: 31

Re: python command line arguments

phrakture wrote:
Allan wrote:

No more pesky gnome-screensaver in the middle of my anime.

Erm, doesn't mplayer have an option for this? I thought there was some --disable-screensaver type flag. I know my screen doesn't blank via DPMS when mplayer is running

From man page:

       -stop-xscreensaver (X11 only)
              Turns off xscreensaver at startup and turns it on again on exit.

But I find reinventing the wheel for something like this especially helpful .. especially when trying to learn a new language. tongue

Offline

#12 2008-01-24 22:32:11

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: python command line arguments

That only stops xscreensaver not gnome-screensaver.  There used to be a patch applied to stop gnome-screensaver but it was removed and I can no longer get it to work...

Turn out that the dbus stuff there doens't work for me anyway...

About 1/2 way through Ergo Proxy and it is good.  Same with Elfen Lied.  I think I will open an anime thread...

Offline

#13 2008-01-24 22:42:09

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python command line arguments

Allan wrote:

That only stops xscreensaver not gnome-screensaver.  There used to be a patch applied to stop gnome-screensaver but it was removed and I can no longer get it to work...

Turn out that the dbus stuff there doens't work for me anyway...

About 1/2 way through Ergo Proxy and it is good.  Same with Elfen Lied.  I think I will open an anime thread...

The old-standby for this is to write something that moves the mouse to 0,0 and then moves it to like 0,5 then back every minute or so.

Offline

#14 2008-01-24 22:52:19

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: python command line arguments

phrakture wrote:

The old-standby for this is to write something that moves the mouse to 0,0 and then moves it to like 0,5 then back every minute or so.

Or change the dbus stuff to os.system("killall gnome-screensaver") & os.system("gnome-screensaver").  Simple but thuggish big_smile

Offline

#15 2008-01-25 00:53:38

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: python command line arguments

Allan wrote:
phrakture wrote:

The old-standby for this is to write something that moves the mouse to 0,0 and then moves it to like 0,5 then back every minute or so.

Or change the dbus stuff to os.system("killall gnome-screensaver") & os.system("gnome-screensaver").  Simple but thuggish big_smile

https://bugs.launchpad.net/ubuntu/+sour … +bug/53947

You might be able to set and unset the gconf key value specified there.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

Board footer

Powered by FluxBB