-
-
#!/usr/bin/python
-
#Copyright (c) 2011 BASeCamp Corporation
-
# All rights reserved.
-
#
-
# Redistribution and use in source and binary forms, with or without
-
# modification, are permitted provided that the following conditions
-
# are met:
-
#
-
# Redistributions of source code must retain the above copyright notice,
-
# this list of conditions and the following disclaimer.
-
#
-
# Redistributions in binary form must reproduce the above copyright
-
# notice, this list of conditions and the following disclaimer in the
-
# documentation and/or other materials provided with the distribution.
-
#
-
# Neither the name of the project’s author nor the names of its
-
# contributors may be used to endorse or promote products derived from
-
# this software without specific prior written permission.
-
#
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
-
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-
-
-
import sys,os,random,ConfigParser,string
-
from optparse import OptionParser
-
#Wallpaper cycling script
-
#written Sunday March 18th 2012
-
#designed to be added to crontab with a delay
-
#example line in crontab:
-
#*/4 * * * * /home/bc_programming/bin/cyclebg.py
-
#when run, it will change the wallpaper to the next one in the cycle and exit.
-
#Author: BC_Programming
-
-
-
-
#location of data path. we keep track of the wallpapers we have already cycled there.
-
datapath = os.path.expanduser("~/.cyclebg/")
-
datafile = datapath + "cycled_data.dat"
-
wallpaperfile = datapath + "wallpapers.dat"
-
disablefile = datapath + "disable.dat" #if this file exists we are "disabled" and don’t change the wallpaper.
-
#command to run to change the bg.
-
-
#following is for Gnome 2
-
#bgchangecmd = ‘dconf gsettings set org.gnome.desktop.background picture-uri "file://%s"’
-
#gnome 3 command…
-
-
bgchangecmd = ‘gsettings set org.gnome.desktop.background picture-uri "file:///%s"’
-
#we need two lists: the list of possible wallpapers, and the wallpapers we’ve already listed.
-
-
def isdisabled():
-
return os.path.exists(disablefile)
-
#returns the list of wallpapers we’ve already cycled through.
-
def getusedlist():
-
if not os.path.exists(datafile):
-
#if the file doesn’t exist, then return an empty list.
-
return []
-
else:
-
return list(open(datafile))
-
-
#clears the used wallpapers list.
-
def clearused():
-
if os.path.exists(datafile):
-
os.remove(datafile)
-
-
def markused(wallpaper):
-
#mark a wallpaper as viewed
-
writelist = open(datafile,‘a’)
-
writelist.write(wallpaper)
-
-
#returns all the wallpapers we are to cycle through.
-
def getwallpapers():
-
if not os.path.exists(wallpaperfile):
-
return []
-
else:
-
return list(open(wallpaperfile))
-
-
-
-
#routine that changes the background.
-
def changebackground(picture):
-
#changes the background to the specified image.
-
cmdrun =bgchangecmd
-
cmdrun = cmdrun.replace("%s",picture)
-
print " running ",cmdrun
-
os.system(cmdrun)
-
-
def main():
-
if isdisabled(): sys.exit(2)
-
sys.exit(changewallpaper())
-
-
def changewallpaper():
-
#retrieve the wallpapers and used wallpapers…
-
used = getusedlist()
-
papers = getwallpapers()
-
#exit if no wallpapers in main list…
-
if len(papers)==0:
-
print "No wallpapers! add wallpaper file paths to ", wallpaperfile, "!"
-
return 1
-
-
#now, remove all entries in papers that exist in used.
-
removethese= [] #list of items to remove
-
for iterateused in papers:
-
if iterateused in used:
-
removethese = removethese + [iterateused]
-
-
-
#now, remove the items…
-
for iterateremove in removethese:
-
papers.remove(iterateremove)
-
-
-
if len(papers)==0:
-
#if papers list is empty, egads! clear the used list, since we’ve cycled through them all evidently.
-
clearused()
-
#and… well balls to it we’ll just call ourself.
-
changewallpaper()
-
-
-
-
#now, the main logic. Choose a wallpaper…
-
choosefrom = papers
-
#shuffle the choosefrom listing…
-
random.shuffle(choosefrom)
-
#take one of the items from the resulting shuffled list.
-
chosen = choosefrom.pop()
-
#add it to the list of seen wallpapers…
-
markused(chosen)
-
#and attempt to change to it…
-
changebackground(chosen)
-
return 0
-
-
-
-
#parse our options/arguments…
-
parser = OptionParser(usage="usage: %prog [options] ",
-
version="%prog 1.0")
-
parser.add_option("-g","–gui",
-
action="store_true", dest="gui", default=False,
-
help="run GUI")
-
-
global options,args
-
(options,args) = parser.parse_args();
-
-
-
-
-
main()
-
-
-
So this is the background changer script you’ve been talking about earlier? Interesting.