summaryrefslogtreecommitdiff
path: root/helper.py
blob: 5c7425ab6812147ab1ec091db17cab51fcf95a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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])