diff options
Diffstat (limited to 'PrestametuVoz.activity/manejar_datos.py')
-rwxr-xr-x | PrestametuVoz.activity/manejar_datos.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/PrestametuVoz.activity/manejar_datos.py b/PrestametuVoz.activity/manejar_datos.py new file mode 100755 index 0000000..e29a9b3 --- /dev/null +++ b/PrestametuVoz.activity/manejar_datos.py @@ -0,0 +1,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 |