# Sample Python code. # Goal # We want to create a website with small,medium, large and extra large versions of the same pictures # and HTML files to fit the medium,large,extra large. One picture per HTML file. # The Large or Extra large category maybe optional. This explains the Use_large and Use_extra_large globals. # There is a directory for each size : They are called "s","m","l","xl". Each directory contains both # jpeg files and HTML files, except for "s" which contains no HTML files. # There is a master HTML file (index.htm) which contains the thumbnails and links to the "m" files, # which in turn contain links to the "L" files # which in turn contain references to the "XL" files. # The script below generates the HTML files only. # You have to make sure the directories exist in the first place. These are relative to the current working directory. import sys # We may call on sys.exit import os # We call on os.stat, os.access #Mnemonics for message languages French=1 English=2 # Configuration section Use_large = 0 Use_extra_large = 1 Language = French # This should work on Windoze FileSep = "/" #File directory separator # End configuration section if Language == French: ClickMessage="Cliquez sur l'image pour voir une image plus grande." FileSizeMessage1="Taille de l'image agrandie est " FileSizeMessage2=" Octets" elif Language == English : ClickMessage="Click on the picture to see a larger version." FileSizeMessage1="Size of the next picture is " FileSizeMessage2=" Bytes" # Just for testing: sample_dictionary = {"pic1.jpg":"Tower of London","pic2.jpg":"Buckingham Palace","pic3.jpg":"Kew gardens"} # dict2htlml creates a whole lot of files given # a dictionary associating filenames with textual descriptions. # Each file produced # contains just one picture and the description from the dictionary. The page title is put into the title bar # An appropriate dictionary might be sample_dictionary def dict2html( picture_dict, page_title): keys = picture_dict.keys() for key in keys: key2html(key,picture_dict[key], page_title) def key2html(picture_name,caption,page_title): key2html2("m", picture_name, caption, page_title) if Use_large: key2html2("l", picture_name, caption, page_title) if Use_extra_large: key2html2("xl", picture_name, caption, page_title) def key2html2(size_string, picture_name, caption, page_title): filename = make_current_filename(size_string, picture_name)#MAKE A HTML FILENAME FROM PICTURE NAME next_filename = make_linked_filename(size_string, picture_name)#MAKE NAME OF FILE LINKED TO THE HYPERLINK INSIDE try: #STANDARD PYTHON ERROR HANDLING outfile_descr = open(filename, "w") except: #DISGRACEFUL EXIT print "Cannot create", filename sys.exit(1) #NOW FILL IN THE FILE CONTENTS outfile_descr.write("\n") outfile_descr.write("
\n") outfile_descr.write("')
if next_filename <> "":
outfile_descr.write('')
outfile_descr.write('')
if next_filename <> "":
outfile_descr.write('')
outfile_descr.write('