Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/archivo.py
blob: 467b52b13119d763ee2224de1227582363343742 (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
# -*- 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 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)
    
    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])

    else:
        #_log.debug('THE FILE EXISTS')
        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], activ[2]]) 
                    contar_int += 1
            else:
                modelo.append(['', '', '', None])
        else :
            modelo.append(['', '', '', None])

    archivo.close()
    return modelo

def comprobar_efemeride(path):
    '''Return true if the ephemeris database exists,
    else return false'''
    path = path + '/data/efemerides.pkl'
    if not os.path.exists(path):
        return False
    else:
        return True

def abrir_efemeride(path):
    '''open the ephemeris file and return a data dictionary'''
    path = path + '/data/efemerides.pkl'
    _log.debug('THE FILE EPHEMERIS EXISTS')        
    archivo = open(path, 'rb')
    texto = pickle.load(archivo)
    archivo.close()
    return texto

def guardar_efemeride(path, file_name):
    '''import, convert and save a new ephemeris database'''
    path = path + '/data/efemerides.pkl'
    di = {}
    file = open(file_name, 'r')
    lines = file.readlines()
    for i in range(len(lines)):
        current_text = lines[i][10 : len(lines[i]) - 2]
        
        for j in range(30, len(current_text),30):
            current_text = current_text[:j] + '\n' + current_text[j + 1 :]
        current_id = lines[i][0 : 8]
        di[current_id] = current_text        
    file_new = open(path, 'wb') 
    pickle.dump(di,file_new)
    file_new.close()
    file.close()

def guardar_dato(indice, contenido, categoria, bolean, path):
    '''save the content in a file, add the content to a dictionary->list'''
    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 not indice in dia: 
        dia[indice] = []
    
    dia[indice].append([contenido, categoria, prioridad])
    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()

def dict_meses(path, fecha=None):
    '''Create if not exists and return a data
    dictionary with the months index'''
    path = path + '/data/lista_dias.pkl'
    if fecha is not None:
        indice = fecha[:4]
        fecha = int(fecha[4:])
    
    if not os.path.exists(path):
        _log.debug('THE FILE LISTA_DIAS DID NOT EXISTS')
        f = open(path, 'wb')
        datos = {}
       
    else:
        f = open(path, 'rb+wb')
        datos = pickle.load(f)
        
        if fecha is not None:
            if indice in datos:
                datos[indice].append(fecha)
            else:
                datos[indice] = [] 
       
       
    f.seek(0)
    pickle.dump(datos, f)
    f.close()
    return datos