summaryrefslogtreecommitdiff
path: root/helper.py
blob: 7f9782493a29d5689d0171488bbf2cf79a6f9f9d (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
import web
import os
import Image

opj = os.path.join

# some nice imports
import itertools as it


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 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)