import web import os import Image import markdown import cStringIO from collections import namedtuple opj = os.path.join # some nice imports import itertools as it Version = namedtuple('Version', 'current last') APPDIR = os.path.dirname(os.path.abspath(__file__)) def appdir (*args): return os.path.join(APPDIR, *args) def url (path): return "\"%s\"" % web.url(path) def versions (): with open(appdir("versions")) as f: return Version._make(f.read().split()) def toJS (ls): ls = ("'%s':'%s'" % x for x in ls) return "{ %s }" % ", ".join(ls) def getImages (path, tmpPath, size): if path[0] != '/': path = '/' + path if tmpPath[0] != '/': tmpPath = '/' + tmpPath appPath = appdir(path[1:]) appTPath = appdir(tmpPath[1:]) for f in sorted(os.listdir(appPath)): if not f.endswith(".png"): continue thumbName = "thumb-" + f imgUrl = url(opj(path, f)) thumbUrl = url(opj(tmpPath, thumbName)) imgPath = opj(appPath, f) thumbPath = opj(appTPath, thumbName) if (not os.path.exists(thumbPath)) or \ os.stat(imgPath).st_mtime > os.stat(thumbPath).st_mtime: im = Image.open(imgPath) im.thumbnail(size, Image.ANTIALIAS) im.save(thumbPath) descName = f + ".desc" desc = None if os.path.exists(opj(appPath, descName)): with open(opj(appPath, descName)) as d: desc = d.read().strip() yield (imgUrl, thumbUrl, desc) def getChangelogs (path): if path[0] != '/': path = '/' + path appPath = appdir(path[1:]) md = markdown.Markdown(safe_mode = "escape", output_format = "html4") def ver_to_int (x): x = x.split(".")[:-1] pow = 3 res = 0 for i in x: res += int(i) ** pow pow -= 1 return res for f in sorted((f for f in os.listdir(appPath) if f.endswith(".txt") and not f[0] == "."), key = ver_to_int, reverse = True): io = cStringIO.StringIO() md.convertFile(input = opj(appPath, f), output = io, encoding="utf-8") html = unicode(io.getvalue(), "utf-8") io.close() yield (html, f[:-4])