Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/document.py
blob: fe84abb62364156a03b656e22992bd8546a9e8b4 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import gtk
import cjson
from zipfile import ZipFile

import theme
from sound import Sound
from ground import Ground
from utils import *

from char import Frame

class Document:
    tape = []
    ground = None
    sound = None


    for i in range(theme.TAPE_COUNT):
        tape.append(Frame(None, theme.EMPTY))

def clean(index):
    from char import Frame
    Document.tape[index] = Frame(None, theme.EMPTY)

def save(filepath):
    zip = ZipFile(filepath, 'w')

    cfg = { 'ground': {},
            'sound' : {},
            'tape'  : [] }

    def _save(node, arcname, value):
        if value.custom():
            node['custom'] = True
            node['filename'] = arcname
            zip.writestr(arcname, value.read())
        else:
            node['custom'] = False
            node['filename'] = value.filename()
        node['name'] = value.name

    _save(cfg['ground'], 'ground.png', Document.ground)
    _save(cfg['sound'], 'sound', Document.sound)

    arcfiles = {}
    for i, frame in enumerate(
            [i for i in set(Document.tape) if not i.empty() and i.custom()]):
        arcfiles[frame] = 'frame%03d.png' % i
        zip.writestr(arcfiles[frame], frame.read())

    for i, frame in enumerate(Document.tape):
        if frame.empty():
            continue
        node = {}
        if frame.custom():
            node['custom'] = True
            node['filename'] = arcfiles[frame]
        else:
            node['custom'] = False
            node['filename'] = frame.filename()
        node['index'] = i
        cfg['tape'].append(node)

    zip.writestr('MANIFEST', cjson.encode(cfg))
    zip.close()

    import shutil
    shutil.copy(filepath, '/tmp/foo.zip')

def load(filepath):
    zip = ZipFile(filepath, 'r')
    cfg = cjson.decode(zip.read('MANIFEST'))

    def _load(node, klass):
        if node['custom']:
            out = klass(node['name'], zip.read(node['filename']),
                    theme.RESTORED)
        else:
            out = klass(node['name'], node['filename'], theme.PREINSTALLED)
        return out

    Document.ground = _load(cfg['ground'], Ground)
    Document.sound = _load(cfg['sound'], Sound)

    loaded = {}
    for node in cfg['tape']:
        i = node['index']
        if i >= theme.TAPE_COUNT:
            continue
        filename = node['filename']
        if node['custom']:
            frame = loaded.get(filename)
            if not frame:
                frame = loaded[filename] = Frame(
                        zip.read(filename), theme.RESTORED)
            Document.tape[i] = frame
        else:
            Document.tape[i] = Frame(filename, theme.PREINSTALLED)

    zip.close()