import os, glob, sys import Image import re # EXIF.py from http://home.cfl.rr.com/genecash/digital_camera.html import EXIF from shutil import copyfile, rmtree import shutil from docutils import core from docutils import io as docIO import urllib LaTeXpreamble = file('preamble.tex').read() def cmptimestamps(file1, file2): """ Compare time stamps of two files and return True if file1 (source) is more recent than file2 (target). """ try: stats1 = os.stat(file1) stats2 = os.stat(file2) except os.error: return 1 return (stats1.st_mtime > stats2.st_mtime) def printPicCode(sourcepath, targetpath, filename): """ Inserts the HTML code for an image. """ print('
") def rest2html(RestString): """ Processes a Rest string to an html fragment """ parts = core.publish_parts( source=RestString, writer_name='html') return parts['html_body'] class NullStream(object): """ Class to implement a dev/null like object """ def write(self, text): pass publisher = core.Publisher( source_class = docIO.StringInput, destination_class = docIO.StringOutput ) publisher.set_components('standalone', 'restructuredtext', 'pseudoxml') publisher.process_programmatic_settings(None, None, None) def check_rst(string): """ Check if every textBlock can be compiled as Rest. """ publisher.set_source(string, None) compiled_rst = publisher.reader.read(publisher.source, publisher.parser, publisher.settings) if compiled_rst.parse_messages: return False return True def rest2pdf(rest_file_name, out_file_name): """ Uses pdflatex to compile the given file to a pdf. """ rest_string = file(rest_file_name).read() # The rest compiler spits out all its errors on std error. We don't want # that, it is ugly: old_stderr = sys.stderr sys.stderr = NullStream() if not check_rst(rest_string): raise Exception, "Not a valid rst file" latex_string = core.publish_string(rest_string, source_path=rest_file_name, writer_name='newlatex2e') sys.stderr = old_stderr latex_base_name = os.path.dirname(rest_file_name)+os.sep+"rst_to_latex" latex_string = latex_string.replace('\\DECvisitdocument', '%s\n\\DECvisitdocument' % LaTeXpreamble) latex_string = latex_string.replace( "\input{../docutils/docutils/docutils/writers/newlatex2e/base.tex}", "\input{/home/varoquau/www/docutils/docutils/docutils/writers/newlatex2e/base.tex}") print >>file(latex_base_name+'.tex', 'w'), latex_string old_dir = os.getcwd() os.chdir(os.path.dirname(rest_file_name)) os.system('pdflatex --interaction scrollmode ' + latex_base_name) copyfile(latex_base_name+".pdf", out_file_name) map(os.unlink,glob.glob('rst_to_latex*')) os.chdir(old_dir) class ListBySubject(object): def __init__(self, section): self.section = section self.listed_subjects = [] def list(self, *subject_tags): pages = [] subsections= [] if len(subject_tags)>0: self.listed_subjects += subject_tags for page in self.section['pages']: if not ( page['uservalues']['subject_tag'] in subject_tags): continue entry = (urllib.quote(page['target']), page['link-title'], page['page-description']) if page['subdir']: subsections.append(entry) else: pages.append(entry) else: for page in self.section['pages']: if ( 'subject_tag' in page['uservalues'] and page['uservalues']['subject_tag'] in self.listed_subjects): continue entry = (urllib.quote(page['target']), page['link-title'], page['page-description']) if page['subdir']: subsections.append(entry) else: pages.append(entry) return (pages, subsections) def scandir(sourcepath, targetpath, dirname, sortorder='forward', recursive=1): """ Scans a dir for images and builds a gallery of these. """ global os filelist = [] subdirlist=[] datedic={} # If no image has and exif date, then we will sort by name: datesAvailable=0 if sortorder=='forward': compfunc=lambda a,b : 2*(datedic[a]>datedic[b])-1 else : compfunc=lambda a,b : 2*(datedic[a]