git » galala » flex » tree

[flex] / common.py

#!/usr/bin/env python
# encoding: utf8

import os
import pickle
import json
import codecs
import ConfigParser


class Image (object):
    def __init__(self, album):
        self.album = album
        self.realpath = ''
        self.title = ''
        self.description = ''
        self.link = ''
        # TODO: date

    @property
    def imagepath(self):
        return "images/" + self.albumpath

    @property
    def normalpath(self):
        return "normal/" + self.albumpath

    @property
    def thumbpath(self):
        return "thumbs/" + self.albumpath

    @property
    def albumpath(self):
        return self.album.shortname + "/" \
                + os.path.basename(self.realpath)

    @property
    def anchor(self):
        return os.path.basename(self.realpath)

    def __cmp__(self, other):
        return cmp(self.albumpath, other.albumpath)

    def to_dict(self):
        link = self.link
        if not self.link:
            link = self.normalpath

        # This must match the gallery parameters, as it is given to it
        # as a json object
        return dict(
            anchor = self.anchor,
            image = self.normalpath,
            thumb = self.thumbpath,
            title = self.title,
            description = self.description,
            url = link)

class Album (object):
    def __init__(self, shortname):
        self.shortname = shortname
        self.title = ''
        self.text = ''
        self.images = []

    def images_to_json(self):
        return json.dumps([ i.to_dict() for i in self.images ],
                    indent = 4)

class Page (object):
    def __init__(self):
        self.title = ''
        self.introduction = ''
        self.albums = []
        self.resize_to = 1200


def page_save(page, path):
    c = ConfigParser.SafeConfigParser()

    c.add_section("page")
    c.set("page", "title", page.title.encode('utf8'))
    c.set("page", "introduction", page.introduction.encode('utf8'))
    c.set("page", "resize_to", str(page.resize_to))

    # a list with the album names in order, so we preserve it since the
    # order of the sections could be lost
    c.set("page", "album_names",
            " ".join(a.shortname for a in page.albums))

    for a in page.albums:
        section = "$album " + a.shortname
        c.add_section(section)
        c.set(section, "shortname", a.shortname)
        c.set(section, "title", a.title.encode('utf8'))
        c.set(section, "text", a.text.encode('utf8'))

        for i in a.images:
            section = "$img " + i.albumpath
            c.add_section(section)
            c.set(section, "albumshortname", i.album.shortname)
            c.set(section, "realpath", i.realpath)
            c.set(section, "title", i.title.encode('utf8'))
            c.set(section, "description", i.description.encode('utf8'))
            c.set(section, "link", i.link)

    c.write(open(path + '.tmp', 'w'))
    os.rename(path + '.tmp', path)



def page_load(path):
    c = ConfigParser.SafeConfigParser()
    c.readfp(codecs.open(path, encoding = "utf8"))

    p = Page()
    p.title = c.get("page", "title")
    p.introduction = c.get("page", "introduction")
    if c.has_option("page", "resize_to"):
        p.resize_to = c.getint("page", "resize_to")
    album_names = c.get("page", "album_names").split()

    albums = {}
    for section in c.sections():
        if section.startswith("$album"):
            shortname = c.get(section, "shortname")
            a = albums.get(shortname, Album(shortname))
            a.shortname = shortname
            a.title = c.get(section, "title")
            a.text = c.get(section, "text")
            albums[a.shortname] = a

        elif section.startswith("$img"):
            # get the album, use a default if it not exists (note
            # we don't store it in p, that will be done when the
            # album section is processed)
            albumshortname = c.get(section, "albumshortname")
            a = albums.get(albumshortname, Album(albumshortname))
            albums[a.shortname] = a

            i = Image(a)
            i.realpath = c.get(section, "realpath")
            i.title = c.get(section, "title")
            i.description = c.get(section, "description")
            i.link = c.get(section, "link")
            a.images.append(i)

    for name in album_names:
        if name in albums:
            p.albums.append(albums[name])
            #albums[name].images.sort()

    return p