"""Reads auction data from text files (get_auction_data), generates an HTML file from auction data (run from the command line), and resizes pictures (resize_imgs)""" import Image from genshi.template import TemplateLoader from genshi import XML import os, shutil FIELDS = ['auction_title', 'auction_subtitle', 'auction_content', 'header_img', 'imgs'] FULLSIZE = 0.65 #percentage of original image size to resize to MIDSIZE = 0.16 THUMBSIZE = 0.06 name_func = lambda prefix: (lambda s: (lambda t: "/".join(t[:-1]+[prefix+"_"+t[-1]]))(s.split("/"))) thumbnail_name = name_func("thumb") mid_name = name_func("mid") def resize_imgs(name): """Searches for .jpg files in ./name/, resizes and compresses them into thumbnail, mid-size, and full-size images, and finally backs up the original images to their own folder.""" imgs = [x for x in os.listdir("./%s" % name) if x.lower().endswith(".jpg")] os.mkdir("./%s/%s" % (name, 'x')) os.mkdir("./%s/%s" % (name, 'original')) for filename in imgs: print filename, img = Image.open("./%s/%s" % (name, filename)) x,y = img.size img.resize((x*FULLSIZE, y*FULLSIZE),resample=Image.ANTIALIAS).save("./%s/%s/%s" % (name, 'x', filename), quality=86) img.resize((x*MIDSIZE, y*MIDSIZE),resample=Image.ANTIALIAS).save("./%s/mid_%s" % (name, filename), quality=86) img.resize((x*THUMBSIZE, y*THUMBSIZE),resample=Image.ANTIALIAS).save("./%s/thumb_%s" % (name, filename), quality=86) shutil.move("./%s/%s" % (name, filename), "./%s/%s/%s" % (name, 'original', filename)) shutil.move("./%s/%s/%s" % (name, 'x', filename), "./%s/%s" % (name, filename)) print "!" os.rmdir("./%s/%s/" % (name, 'x')) def get_auction_data(name): """Opens the text file ./name.txt, reads and processes the fields, then returns a dict of the auction data.""" txt = open("%s.txt" % name).read() fields = [s.strip() for s in txt.split("\n#\n")] fields = dict(zip(FIELDS, fields)) fields["auction_content"] = XML(fields["auction_content"]) fields["imgs"] = [(img, thumbnail_name(img)) for img in fields["imgs"].split("\n")] fields["header_img"] = (fields["header_img"], mid_name(fields["header_img"])) return fields if __name__ == "__main__": name = raw_input("Name? ") fields = get_auction_data(name) loader = TemplateLoader(".", auto_reload=True) template = loader.load("template.gen.html") stream = template.generate(**fields) open(name+".html", 'w').write(stream.render('xhtml'))