Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/persistencia.py
blob: 092e9137ba49969f6d158ad30c7abfba0f3fc404 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os
import sys
import simplejson
import globos
from sugar.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
                #globoData.ancho_text, globoData.alto_text =
                #globo.ancho_text, globo.alto_text

                globoData['text_width'] = globo.texto.ancho
                globoData['text_height'] = globo.texto.alto
                globoData['text_x'], globoData['text_y'] = \
                    globo.texto.x, globo.texto.y
                globoData['text_text'] = globo.texto.texto
                globoData['text_rows'] = globo.texto.renglones
                globoData['text_sp_rows'] = globo.texto.esp_reg

                globoData['text_row_height'] = globo.texto.alto_renglon
                globoData['text_font_size'] = globo.texto.font_size
                globoData['text_font_type'] = globo.texto.font_type
                globoData['text_bold'] = globo.texto.bold
                globoData['text_italic'] = globo.texto.italic

                globoData['text_color_r'] = globo.texto.color_r
                globoData['text_color_g'] = globo.texto.color_g
                globoData['text_color_b'] = globo.texto.color_b

                globoData['text_show_border'] = globo.texto.mostrar_borde
                globoData['text_show_cursor'] = globo.texto.mostrar_cursor

                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:
            simplejson.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 = simplejson.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(None)
            primero = False
            box = page.get_active_box()
            box.image_name = boxData['image_name']
            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(x=globo_x, y=globo_y,
                        modo=globo_modo, direccion=globo_direccion)
                elif (tipo_globo == 'CLOUD'):
                    globo = globos.Nube(x=globo_x, y=globo_y,
                        direccion=globo_direccion)
                elif (tipo_globo == 'EXCLAMATION'):
                    globo = globos.Grito(x=globo_x, y=globo_y,
                        direccion=globo_direccion)
                elif (tipo_globo == 'RECTANGLE'):
                    globo = globos.Rectangulo(x=globo_x, y=globo_y)
                elif (tipo_globo == 'IMAGE'):
                    image_name = globoData['image_name']
                    globo = globos.Imagen(image_name, x=globo_x, y=globo_y)
                    globo.direccion = globo_direccion

                if globo != 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']

                    globo.texto.ancho = globoData['text_width']
                    globo.texto.alto = globoData['text_height']
                    globo.texto.x, globo.texto.y = globoData['text_x'], \
                        globoData['text_y']
                    globo.texto.texto = globoData['text_text']
                    globo.texto.renglones = globoData['text_rows']
                    globo.texto.esp_reg = globoData['text_sp_rows']

                    globo.texto.alto_renglon = globoData['text_row_height']
                    globo.texto.font_size = globoData['text_font_size']
                    globo.texto.font_type = globoData['text_font_type']
                    globo.texto.bold = globoData['text_bold']
                    globo.texto.italic = globoData['text_italic']

                    globo.texto.color_r = globoData['text_color_r']
                    globo.texto.color_g = globoData['text_color_g']
                    globo.texto.color_b = globoData['text_color_b']

                    globo.texto.mostrar_borde = globoData['text_show_border']
                    globo.texto.mostrar_cursor = globoData['text_show_cursor']
                    box.globos.append(globo)

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

            box.queue_draw()