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('

') print('') # If we have a name that looks like it might mean something, use it as # a legend: name=os.path.basename(filename)[:-4] if re.match("\D\D\D",name): print "" + re.sub("_"," ",name) + "" else: print '' 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]' for file in filelist: sourcefile=sourcepath+"/"+dirname+"/"+file targetfile=targetpath+"/"+dirname+"/"+file if cmptimestamps(sourcefile,targetfile): targetdir=os.path.dirname(targetfile) if not os.path.exists(targetdir): os.makedirs(targetdir) im = Image.open(sourcefile) im.thumbnail([2000,2000]) im.save(targetdir+"/"+file) if not os.path.exists(targetpath+"/thumbnails/"+dirname): os.makedirs(targetpath+"/thumbnails/"+dirname) #im = Image.open(sourcepath+"/"+dirname+"/"+file) im.thumbnail([150,150]) im.save(targetpath+"/thumbnails/"+dirname+"/"+file) printPicCode(sourcepath,targetpath,dirname+"/"+file) print '' if recursive : if sortorder=='forward': subdirlist.sort() else: cmp=lambda a,b : 2*(a"+re.sub("_"," ",subdir)+"") scandir(sourcepath,targetpath,dirname+"/"+subdir) def page_depth(page): """ Returns the depth of the page in the website tree """ link = page['target'] depth = 0 if link[:2]=='./': link=link[2:] while link[:3]=='../': depth=depth-1 link=link[3:] for char in link: if char=='/': depth+=1 return depth def cmp_pages(page1, page2): """ This functions take 2 pages and compares them. This is useful for sorting for the tree and other similar places. """ if not page1['uservalues']['sorttag'] == page2['uservalues']['sorttag']: return 2*(page1['uservalues']['sorttag'] < page2['uservalues']['sorttag'] ) - 1 return 2*(page1['crumb'] < page2['crumb'] ) - 1 def _print_sphinx(sourcepath, targetpath): sourcepath = os.path.join(sourcepath, 'sphinx/build/html/') for filename in glob.glob(os.path.join(sourcepath, '*')): targetfile = os.path.join(targetpath, os.path.basename(filename)) if os.path.exists(targetfile): try: shutil.rmtree(targetfile) except OSError: pass if os.path.isdir(filename): shutil.copytree(filename, targetfile) else: shutil.copy(filename, targetpath) from BeautifulSoup import BeautifulSoup soup = BeautifulSoup(file(os.path.join(sourcepath, 'index.html')).read()) body = soup.findAll('div', attrs={'class':'body'})[0] body.attrs = [] print """ """ print body.renderContents().replace('\n', '\n ') print "\n\n"