"""
Show a molecule every hour.
"""
__author__ = "$Author: jurgen $"
___date__ = "$Date: 2006/10/19 17:35:50 $"
import sys, time, os, csv, urllib, random, string
try:
import ctypes
except ImportError:
raise ImportError, "The ctypes module is required. See http://starship.python.net/crew/theller/ctypes/"
try:
import Image
except ImportError:
raise ImportError, "The Image module of the library PIL (Python Imaging Library). See http://www.pythonware.com/products/pil/"
class BackgroundSetter:
## Define settings only in this subroutine!
def __init__(self):
self.pdb_code_list = []
self.tmp_dir = "C:\\temp"
self.wallpaper_filename = os.path.join( self.tmp_dir, "default.bmp")
self.wallpaper_filenameT= os.path.join( self.tmp_dir, "default.gif")
self.info_page = os.path.join( self.tmp_dir, "pdb_images_info.html")
#self.wallpaper_urlname = "P:\molgrap"
self.wallpaper_urlname = "http://www.bmrb.wisc.edu/servlet_data/molgrap"
self.pdb_code_list_url = "http://www.bmrb.wisc.edu/servlet_data/viavia/bmrb_pdb_match/lists/pdb_main.csv"
self.pdb_url = "http://www.rcsb.org/pdb/cgi/explore.cgi?pdbId="
self.sleep_time = 60*60
self.org_file_content = """
PDB Entry %s
PDB Entry %s
"""
if not os.path.isdir(self.tmp_dir):
msg = "ERROR: couldn't find temporary directory; specify a read/writable one in the __init__ function"
raise msg
############################################################################
## No changes after this line.
############################################################################
def getPDB_code_list(self):
try:
resource = urllib.urlopen(self.pdb_code_list_url)
reader = csv.reader(resource)
except IOError:
print "ERROR: couldn't open url for reader: ", self.pdb_code_list_url
return 0
try:
header_read = reader.next()
print "DEBUG: read header: ", header_read
for row in reader:
pdb_code = row[0]
self.pdb_code_list.append( pdb_code )
# Never know when the connection is finally empty.
except IOError:
pass
print "DEBUG: Found number of PDB entries: ", len(self.pdb_code_list)
return 1
def loopDisplay(self):
while 1:
while 1:
entry_code = random.choice( self.pdb_code_list )
sub_dir = entry_code[1:3]
# Let's not repeat the same image
del(self.pdb_code_list[self.pdb_code_list.index(entry_code)])
infile_url = b.wallpaper_urlname + "/pic/" + sub_dir + "/" + entry_code + "_xl.gif"
try:
print "DEBUG: Getting url: " + infile_url
(filename, headers) = urllib.urlretrieve(infile_url, self.wallpaper_filenameT)
except:
print "WARNING: couldn't retrieve url: " + infile_url
time.sleep(1) # Make sure we don't eat too much cpu when things fail.
continue
try:
im = Image.open(filename)
except:
print "WARNING: couldn't open image file: " + filename + " perhaps because of theoretical models that don't have an associated image"
continue
break
im.save(b.wallpaper_filename)
ctypes.windll.user32.SystemParametersInfoA( 20, 0, b.wallpaper_filename, 3 )
old_string = "%u"
new_string = self.pdb_url;
new_file_content = string.replace( self.org_file_content, old_string, new_string)
old_string = "%s"
new_string = entry_code;
new_file_content = string.replace( new_file_content, old_string, new_string)
open( self.info_page, 'w' ).write( new_file_content )
time.sleep(self.sleep_time)
if __name__ == "__main__":
b = BackgroundSetter()
if not b.getPDB_code_list():
print "ERROR: couldn't get pdb code list: "
sys._exit(1)
b.loopDisplay()