🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.python
Article #236

skrypt do zmiany tapety i prośba o komentarz

#236
From: Marcin Wojtczuk
Date: Thu, 05 Dec 2002 21:58
78 lines
3263 bytes
Jak wiadomo przykłady są istotą nauki, napisałem zatem przykładowy
skrypt do zmiany tapety pulpitu. Proszę Was o komentarz, wytknięcie
błędów stylistycznych, poprawki, opis co można zrobić lepiej, jak
lepiej wykorzystać pythona itp.

#v+
from os 	import access,getenv,listdir,execv,spawnv,P_WAIT
from random 	import Random
from math 	import floor
import sys

# --- customize this section --- #
images	 	= getenv('HOME') + '/obrazy/tapety/'
imgchanger	= '/usr/local/bin/icewmbg'
imgchanger_flags = ' -s '
ending		= '.chosen'
# --- DONT'T TOUCH below --- #

wallpapers 	= listdir(images)
recordfile 	= images + ending

# delete our record file from filelist, so that it isn't chosen as
# wallpaper :) (unless it's absent). Touch it if it doesn't exists
if ending in wallpapers:	
    wallpapers.remove(ending)
else:
    if spawnv( P_WAIT, "/bin/touch", ("WTF/bezsensu", recordfile) ):
	print "wanted to create %s, but failed" % (recordfile)
	sys.exit(1)

# check which wallpaper is currently displayed
if access(recordfile, 6):		#WTF? Gdzie są opisane te tryby ?
    f = open(recordfile, "r")
    current = f.readline()
    if "\n" in current:			#get rid of fsckin \n
	current = current[:-1] 	
    f.close()
    # remove current wallpaper from list of wallpapers
    if current in wallpapers:		#needed in case, file was deleted
	wallpapers.remove(current)
else:
    print "need +r access to %s! touch it at least" % (recordfile)
    sys.exit(1)


seed = Random()
chosen = wallpapers[int(floor(len(wallpapers) * seed.random()))]

# record what was chosen, so that we have true change allways
# TODO: add swich - user might don't care
if access(recordfile, 6):
    f = open(recordfile, "w")
    f.write(chosen)
    f.close()
else:
    print "need +w access to %s!" % (recordfile)
    sys.exit(1)


#change wallpaper
cmd = ( imgchanger, 
	imgchanger_flags, 
	images + chosen
	)
execv(cmd[0], cmd[1:])
#v-

Tam gdzie jest 'WTF' kończą się moje zdolności pojmowania tego co python
robi z argv dla komend. Nie dałem import * przy os, dlatego, że nadpisałoby
mi to funkcję open().

Skrypt działa (zmienia mi tapetę w gnome ;). Uprasza się o odpalanie z &.
Nie wiem czemu icewmbg, chodź umiera, to jednak nie oddaje klawiatury.

Dziękuję za uwagę i proszę o pomoc.
-- 
    [ http://www.stringi.com/viper/ ] [ GG 1513591 ]
	All good things come to those who wait
Technical Details
Message-ID: <h5iosa-e8c.ln@fastviper.viper.lan>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!news.k.pl!news.tkmedia.pl!news-1.feed.trzepak.pl!news.telbank.pl!fastviper.viper.lan!news

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!news.k.pl!news.tkmedia.pl!news-1.feed.trzepak.pl!news.telbank.pl!fastviper.viper.lan!news
From: Marcin Wojtczuk <fastviper@telkab.pl>
Newsgroups: pl.comp.lang.python
Subject: skrypt do zmiany tapety i prośba o komentarz
Date: Thu, 5 Dec 2002 22:58:09 +0100
Organization: PLD
Lines: 78
Message-ID: <h5iosa-e8c.ln@fastviper.viper.lan>
NNTP-Posting-Host: net2-3.telkab.pl
Mime-Version: 1.0
X-Trace: news.telbank.pl 1039125650 11708 195.136.7.130 (5 Dec 2002 22:00:50 GMT)
X-Complaints-To: usenet@news.telbank.pl
NNTP-Posting-Date: 5 Dec 2002 22:00:50 GMT
X-Licznik: 1564
User-Agent: slrn/0.9.7.4 (pl-1.4.3) (Linux/2.4.19pk-ll (i686))
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit