Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/PrestametuVoz.activity/manejar_datos.py
blob: e29a9b33e3de274183d5c87af0f5075fd315c7f1 (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
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
''' Lee y carga los archivos '''

#Copyright (C) 2009  Federico Moreira - <federico@piensalibre.info>
#                    Alejandro Esperón - <ratman26@gmail.com>
#                    Esteban Arias - <earias@plan.ceibal.edu.uy>
#
#    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 3 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, see <http://www.gnu.org/licenses/>

import os
from os import environ, makedirs, chmod
from os.path import join, basename, isdir, split, normpath
import zipfile
import tempfile
import simplejson as json

from sugar.activity import activity
from tarjeta import Tarjeta


def guardar_datos(archivo, lista_completa):
    ''' Leo los datos que me pasan y los guardo en la base '''
    lista =[]
    for item in lista_completa:
        i = {}
        i['titulo'] = item.titulo
        i['imagen'] = item.imagen
        i['audio'] = item.audio
        lista.append(i)
    try:
       a = open(os.path.join('data', archivo), 'w' )    
       json.dump(lista, a)
    except IOError, error:
       print error
    finally:
       a.close()

def cargar_datos(archivo):
    ''' Leo los datos de la base y los devuelvo en una lista '''
    try:
	#carga demo luego modificar para multiples planillas...		
        game_file = os.path.join(os.path.dirname(__file__), 'demos', archivo+'.zip')

        tmp_root = os.path.join(environ['SUGAR_ACTIVITY_ROOT'], 'instance')
        temp_folder = tempfile.mkdtemp(dir=tmp_root)
        chmod(temp_folder, 0777)

        ''' extracts files in the zip file '''
        game_name = basename(game_file)[:-4]
        zipFile = zipfile.ZipFile(game_file, "r")
        for each in zipFile.namelist():
            if not each.endswith('/'):
                root, name = split(each)
                directory = normpath(os.path.join(temp_folder, root))
                if not isdir(directory):
                    makedirs(directory)
                file(os.path.join(directory, name), 'wb').write(zipFile.read(each))

	print 'RUTA: ' + os.path.join(temp_folder, 'data', archivo)
        a = open(os.path.join(temp_folder, 'data', archivo)) 

        l = json.load(a)
        lista = []
        for item in l:
            card = Tarjeta()
            card.titulo = item['titulo']
            card.imagen = os.path.join(temp_folder, item['imagen'])
            card.audio = os.path.join(temp_folder, item['audio'])
            lista.append(card) 
        a.close()
    except IOError, error:
        print error
    #finally:
    #    a.close()
    return lista

def cargar_planillas(archivo):
    ''' Carga las plantillas '''
    try:
        archivo = open(archivo)
        lista = archivo.readlines()
    except IOError, error:
        print error
    finally:
        archivo.close()
    return lista