Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/persistencia.py
blob: b6d6c0f22bcd53852cfb60f807417e93cc270540 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import json

import globos
from sugar3.activity import activity
import zipfile


class PageData:

    def __init__(self):
        self.boxs = []


class BoxData:

    def __init__(self):
        self.globos = []
        self.image_name = None


class Persistence:

    def write(self, file_name, page):

        """
        Persitencia:
        Cuadro_titulo, globos[]
        Lista de cuadros, imagen de fondo, globos[]
        por cada globo: tipo, posicion (x,y), ancho, alto,
        direccion, pos_flecha (x,y), texto,font, tamanio, color
        """
        instance_path = os.path.join(activity.get_activity_root(), 'instance')

        # Copio los datos de Page en PageData

        pageData = {}
        pageData['version'] = '1'
        pageData['boxs'] = []
        for box in page.boxs:
            boxData = {}
            boxData['image_name'] = box.image_name
            boxData['globes'] = []
            for globo in box.globos:
                globoData = {}
                globoData['title_globe'] = (globo == box.title_globe)
                print 'Grabando', globo.globe_type
                globoData['globe_type'] = globo.globe_type
                globoData['radio'] = globo.radio
                globoData['width'], globoData['height'] = \
                    globo.ancho, globo.alto
                if (globo.__class__ != globos.Rectangulo):
                    globoData['point_0'] = globo.punto[0]
                    globoData['point_1'] = globo.punto[1]
                globoData['direction'] = globo.direccion
                if (globo.__class__ == globos.Globo):
                    globoData['mode'] = globo.modo
                if (globo.__class__ == globos.Imagen):
                    globoData['image_name'] = globo.image_name
                globoData['x'], globoData['y'] = globo.x, globo.y

                if globo.texto is not None:
                    globoData['text_width'] = globo.texto.ancho
                    globoData['text_height'] = globo.texto.alto
                    globoData['text_text'] = globo.texto.text
                    globoData['text_color'] = globo.texto.color

                    globoData['text_font_description'] = \
                        globo.texto.font_description

                boxData['globes'].append(globoData)
            pageData['boxs'].append(boxData)

        # hago picle de pageData
        print pageData

        data_file_name = 'data.json'
        f = open(os.path.join(instance_path, data_file_name), 'w')
        try:
            json.dump(pageData, f)
        finally:
            f.close()

        print 'file_name', file_name

        z = zipfile.ZipFile(file_name, 'w')
        z.write(os.path.join(instance_path, data_file_name).encode(
            'ascii', 'ignore'), data_file_name.encode('ascii', 'ignore'))
        for box in page.boxs:
            if (box.image_name != ''):
                z.write(os.path.join(
                    instance_path,
                    box.image_name).encode('ascii', 'ignore'),
                    box.image_name.encode('ascii', 'ignore'))
        z.close()

    def read(self, file_name, page):

        instance_path = os.path.join(activity.get_activity_root(), 'instance')
        z = zipfile.ZipFile(file_name, 'r')
        for file_name in z.namelist():
            if (file_name != './'):
                try:
                    print 'extrayendo', file_name
                    # la version de python en las xo no permite hacer
                    # extract :(
                    # z.extract(file_name,instance_path)
                    data = z.read(file_name)
                    fout = open(os.path.join(instance_path, file_name), 'w')
                    fout.write(data)
                    fout.close()
                except:
                    print 'Error extrayendo', file_name
        z.close()
        data_file_name = 'data.json'

        pageData = PageData()
        f = open(os.path.join(instance_path, data_file_name), 'r')
        try:
            pageData = json.load(f)
        finally:
            f.close()

        primero = True
        for boxData in pageData['boxs']:
            if not primero:
                # el primero ya esta creado
                page.add_box_from_journal_image(boxData['image_name'])
            primero = False
            box = page.get_active_box()
            for globoData in boxData['globes']:
                globo_x, globo_y = globoData['x'], globoData['y']
                globo_modo = None
                if ('mode' in globoData):
                    globo_modo = globoData['mode']
                globo_direccion = globoData['direction']

                tipo_globo = globoData['globe_type']
                print 'tipo_globo', tipo_globo
                globo = None
                if (tipo_globo == 'GLOBE'):
                    globo = globos.Globo(box, x=globo_x, y=globo_y,
                                         modo=globo_modo,
                                         direccion=globo_direccion)
                elif (tipo_globo == 'CLOUD'):
                    globo = globos.Nube(box, x=globo_x, y=globo_y,
                                        direccion=globo_direccion)
                elif (tipo_globo == 'EXCLAMATION'):
                    globo = globos.Grito(box, x=globo_x, y=globo_y,
                                         direccion=globo_direccion)
                elif (tipo_globo == 'RECTANGLE'):
                    globo = globos.Rectangulo(box, x=globo_x, y=globo_y)
                elif (tipo_globo == 'IMAGE'):
                    image_name = globoData['image_name']
                    globo = globos.Imagen(box, image_name,
                                          x=globo_x, y=globo_y)
                    globo.direccion = globo_direccion

                if globo is not None:
                    globo.radio = globoData['radio']
                    globo.ancho, globo.alto = globoData['width'], \
                        globoData['height']

                    if (tipo_globo != 'RECTANGLE'):
                        globo.punto = [globoData['point_0'],
                                       globoData['point_1']]

                    globo.x, globo.y = globoData['x'], globoData['y']

                    if (tipo_globo != 'IMAGE'):
                        globo.texto.ancho = globoData['text_width']
                        globo.texto.alto = globoData['text_height']
                        globo.texto.color = globoData['text_color']
                        globo.texto.set_font_description(
                            globoData['text_font_description'])
                        globo.texto.set_text(globoData['text_text'])
                    box.globos.append(globo)

                    if globoData['title_globe']:
                        box.title_globe = globo

            #box.redraw()