Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/archivo.py
blob: 3651db19c1340e1c405d6fd4f5520cda312bb0f2 (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
# -*- 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):
    '''return an int from a string with tuple sintax'''
    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_INT, gobject.TYPE_STRING) 
    vacio = _('no hay actividades para el día de hoy')
    
    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([0, vacio])

    else:
        _log.debug('THE FILE EXISTS')
        archivo = open(path, 'rb')
        dia = pickle.load(archivo)
        contar = 1
    
        if dia.has_key(indice): 
            if len(dia[indice]):
                for activ in dia[indice]:
                    modelo.append([contar, activ]) 
                    contar += 1
            else:
                modelo.append([0, vacio])
        else :
            modelo.append([0, vacio])

    archivo.close() 
    return modelo

def abrir_efemeride(path):
    '''open the ephemeris file and return a data dictionary'''
    path = path + '/data/efemerides.pkl'

    if not os.path.exists(path):
        _log.debug('THE FILE EFEMERIDES DID NOT EXISTS')
        archivo = open(path, 'wb')
        texto = {}
        pickle.dump(texto, archivo)
    
    else:
        _log.debug('THE FILE EXISTS')        
        archivo = open(path, 'rb')
        texto = pickle.load(archivo)

    return texto

def guardar_dato(indice, contenido, 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 dia.has_key(indice) is False:
        dia[indice] = []
    dia[indice].append(contenido)
    f.seek(0)
    pickle.dump(dia, f)
    f.flush()
    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.flush()
    f.close()