diff options
author | Ariel Calzada <ariel@activitycentral.com> | 2012-02-10 18:27:31 (GMT) |
---|---|---|
committer | Ariel Calzada <ariel@activitycentral.com> | 2012-02-10 18:27:31 (GMT) |
commit | 670d501f53183d4774897194420714d830db7452 (patch) | |
tree | 1d0129ffc6affbddba2268bf0858dd08451954e1 /archivo.py |
Diffstat (limited to 'archivo.py')
-rw-r--r-- | archivo.py | 248 |
1 files changed, 248 insertions, 0 deletions
diff --git a/archivo.py b/archivo.py new file mode 100644 index 0000000..ee83156 --- /dev/null +++ b/archivo.py @@ -0,0 +1,248 @@ +# -*- coding: utf-8 -*- +# +#Copyright (C) 2010, Yader Velasquez +# +#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 gtk +import pickle +import gobject +import os +import logging +from gettext import gettext as _ +_log = logging.getLogger('Log Archivo') + +def separar_cadena(texto): + nuevo_texto = '' + for i in range(0, len(texto), 40): + texto_temporal = texto[i: i + 40] + '\n' + nuevo_texto = nuevo_texto + texto_temporal + + return nuevo_texto + +def indice_entero(cadena): + '''Convert a turple's index to an interger number, for + be used by others functions''' + cadena = cadena.replace('(','') + cadena = cadena.replace(',','') + cadena = cadena.replace(')','') + return int(cadena) + +def crear_modelo(indice, path): + '''create the liststore model for the main block''' + path = path + '/data/actividades.pkl' + modelo = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING,\ + gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) + + if not os.path.exists(path): + _log.debug('THE FILE ACTIVIDADES DID NOT EXISTS') + archivo = open(path, 'wb') + dia = {} + pickle.dump(dia, archivo) + modelo.append(['', '', '', None, None, None, None]) + + else: + archivo = open(path, 'rb') + dia = pickle.load(archivo) + contar_int = 1 + + if indice in dia: + if len(dia[indice]): + for activ in dia[indice]: + contar = str(contar_int) + modelo.append([contar, activ[0], activ[1][1], activ[2], activ[3], activ[4], activ[1][0]]) + contar_int += 1 + else: + modelo.append(['', '', '', None, None, None, None]) + else : + modelo.append(['', '', '', None, None, None, None]) + + archivo.close() + return modelo + +def tareas_pendientes(path): + '''Save/Update a file with the pending task, + activities marked as in progress''' + progreso = gtk.STOCK_YES + modelo = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING) + lista_temporal = list() + path_actividades = path + '/data/actividades.pkl' + path_tareas = path + if os.path.exists(path_actividades): + archivo = open(path_actividades, 'r') + dia = pickle.load(archivo) + _log.debug(dia) + for fechas_datos in dia: + for datos in dia[fechas_datos]: + if progreso in datos: + modelo.append([datos[0], '#FFFFFF']) + return modelo + +def comprobar_archivo(path, archivo): + '''Return true if the ephemeris database exists, + else return false''' + if archivo is 'efemeride': + path = path + '/data/efemerides.pkl' + elif archivo is 'frase': + path = path + '/data/frase.pkl' + + if not os.path.exists(path): + return False + else: + return True + +def abrir_archivo(path, archivo): + '''open the ephemeris file and return a data dictionary''' + if archivo is 'efemeride': + path = path + '/data/efemerides.pkl' + elif archivo is 'frase': + path = path + '/data/frase.pkl' + archivo = open(path, 'rb') + texto = pickle.load(archivo) + archivo.close() + return texto + +def guardar_archivo(path, file_name, tipo_archivo): + '''import, convert and save a new ephemeris/phrases database''' + if tipo_archivo is 1: + path = path + '/data/efemerides.pkl' + elif tipo_archivo is 2: + path = path + '/data/frase.pkl' + di = {} + archivo = open(file_name, 'r') + lines = archivo.readlines() + for i in range(len(lines)): + if tipo_archivo is 1: + current_text = lines[i][10 : len(lines[i]) - 2] + else: + current_text = lines[i][4 : len(lines[i]) - 2] + for j in range(30, len(current_text),30): + current_text = current_text[:j] + '-\n' + current_text[j:] + if tipo_archivo is 1: + current_id = lines[i][0 : 8] + else: + current_id = lines[i][0 : 2] + + di[current_id] = current_text + _log.debug(di[current_id]) + file_new = open(path, 'wb') + pickle.dump(di,file_new) + file_new.close() + archivo.close() + +def guardar_dato(indice, contenido, categoria, entero_cat, bolean, progre, path, indice_lista=False): + '''save the content in a file, add the content to a dictionary->list''' + color_categoria = ['#6E92FF', '#82FF5F', '#FFE251', '#FF7D7D', '#FFFFFF'] + + path = path + '/data/actividades.pkl' + indice = str(indice) + f = open(path, 'rb+wb') + dia = pickle.load(f) + + if bolean: + prioridad = gtk.STOCK_ABOUT + else: + prioridad = None + + if progre is 1: + progreso = gtk.STOCK_YES + elif progre is 2: + progreso = gtk.STOCK_APPLY + else: + progreso = None + + if not indice in dia: + dia[indice] = [] + if len(contenido) > 40: + contenido = separar_cadena(contenido) + if entero_cat > -1: + color = color_categoria[entero_cat] + else: + color = '#FFFFFF' + + if indice_lista: + indice_lista = str(indice_lista) + indice = str(indice) + indice_lista = indice_entero(indice_lista) + dia[indice][indice_lista] = [contenido, categoria, prioridad, \ + progreso, color] + else: + dia[indice].append([contenido, categoria, prioridad, \ + progreso, color]) + f.seek(0) + pickle.dump(dia, f) + f.close() + +def borrar_dato(indice, indice_lista, path): + '''delete the selected content''' + path = path + '/data/actividades.pkl' + indice_lista = str(indice_lista) + indice = str(indice) + indice_lista = indice_entero(indice_lista) + f = open(path, 'rb+wb') + dia = pickle.load(f) + del dia[indice][indice_lista] + f.seek(0) + pickle.dump(dia, f) + f.close() + + if len(dia[indice]): #return 1 if the date does not have data + return 0 + else: + return 1 + +def dict_meses(path, fecha, opcion=0): + '''Create if not exists and return a data + dictionary with the months index''' + path = path + '/data/lista_dias.pkl' + + indice = fecha[:4] + fecha = int(fecha[4:]) + + if os.path.exists(path): + f = open(path, 'rb+wb') + datos = pickle.load(f) + + if not indice in datos: + datos[indice] = [] + + if opcion: + if not fecha in datos[indice]: + datos[indice].append(fecha) + else: + _log.debug('THE FILE LISTA_DIAS DID NOT EXISTS') + f = open(path, 'wb') + datos = {} + datos[indice] = [] + + f.seek(0) + pickle.dump(datos, f) + f.close() + return datos + +def borrar_dict_meses(path, fecha): + '''delete from the dict the date when the + activity stack is empty''' + + path = path + '/data/lista_dias.pkl' + indice = fecha[:4] + fecha = int(fecha[4:]) + f = open(path, 'rb+wb') + datos = pickle.load(f) + borrar = datos[indice].index(fecha) + del datos[indice][borrar] + f.seek(0) + pickle.dump(datos, f) + f.close() + |