# 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("") outfile_descr.write(page_title) outfile_descr.write("\n") outfile_descr.write("\n") outfile_descr.write("\n") outfile_descr.write("

") outfile_descr.write(picture_name) 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('

\n') if next_filename <> "": next_size_string = next_size(size_string) outfile_descr.write(ClickMessage) output_file_size(next_size_string, outfile_descr,picture_name) outfile_descr.write('

Home

\n') outfile_descr.write("\n") outfile_descr.write("\n") outfile_descr.close() def make_current_filename(size,picture_name): pn_wo_extension = picture_name[:-4] html_file_rel_path = size+FileSep+pn_wo_extension+".htm" return html_file_rel_path def make_linked_filename(size_string, picture_name): size_string2 = next_size(size_string) if size_string2 == "": return "" pn_wo_extension = picture_name[:-4] html_file_rel_path = ".."+FileSep+size_string+FileSep+pn_wo_extension+".htm" return html_file_rel_path def output_file_size(size_string, outfile_descr,picture_name): next_picture_filename = size_string + FileSep + picture_name if not os.access(next_picture_filename,os.F_OK): #TEST EXISTENCE OF FILE print "Warning: Image file",next_picture_filename,"does not exist" return stat_result = os.stat(next_picture_filename) #RETURNS MANY THINGS BUT WE ARE USING SIZE outfile_descr.write(FileSizeMessage1) outfile_descr.write(str(stat_result.st_size))#FILE SIZE CONVERTED TO STRING outfile_descr.write(FileSizeMessage2) #returns "" when there is no larger size def next_size(size): if size == "s": return "m" elif size == "m": if Use_large: return "l" if Use_extra_large: return "xl" else: return "" elif size == "l": if Use_extra_large: return "xl" else: return "" else: return "" # Now the definitions are done: Execute thiS (TEST CODE) dict2html(sample_dictionary, "Visit of Britain")