Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/archivo.py
blob: b088ad4279ba2cca23835a82e121371ba833e34c (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# -*- 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()